My Project
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
Classes | Macros | Functions
mapio.h File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <netcdf.h>

Go to the source code of this file.

Classes

struct  mapdata
 Stores a submap of the netcdf GRD data. More...
 
struct  mapsrc
 Stores info needed for accessing GMT GRD file. More...
 
struct  mapbounds
 Stores info about the boundaries of a GMT GRD file. This information can be used to determine if a location is within the bounds of an existing NetCDF GRD file. More...
 

Macros

#define NAN   0
 
#define MAPIO_DEBUG   0
 If not equal to zero then debug output will be printed. More...
 
#define MAPIO_OK   1
 
#define MAPIO_READERROR   2
 
#define MAPIO_OUTOFMEMORY   4
 
#define MAPSRC_IS_EMPTY   0
 
#define MAPSRC_IS_FILLED   1
 
#define MAPSRC_FILL_FAILURE   2
 
#define MAPDATA_IS_EMPTY   0
 
#define MAPDATA_IS_FILLED   1
 
#define MAPDATA_FILL_FAILURE   2
 
#define MAPBOUNDS_OK   0
 
#define MAPBOUNDS_OUT_OF_BOUNDS   1
 
#define MAPBOUNDS_NEAR_EDGE   2
 

Functions

int check_error (int status, struct mapsrc *src)
 
void mapsrc_fill (const char *file, struct mapsrc *src)
 Opens a GMT GRD file and read coordinate variables. More...
 
void mapsrc_free (struct mapsrc *src)
 Frees the memory used by a mapsrc structure. More...
 
float mapsrc_find (struct mapsrc *src, double x, double y)
 Find a z value at a given point. More...
 
struct mapsrcmapsrc_init (void)
 Initializes a mapsrc structure. More...
 
char * mapsrc_tostring (struct mapsrc *src)
 create a string of a mapsrc structure More...
 
int mapdata_fill (struct mapsrc *src, struct mapdata *data, double x, double y, double xwidth, double ywidth)
 Read a submap from a netcdf UTM GRD file into a mapdata structure. More...
 
void mapdata_free (struct mapdata *data, int free_all)
 Free's the memory held by a mapdata structure. More...
 
char * mapdata_tostring (struct mapdata *data)
 create a string of a mapdata structure More...
 
int nearest (double key, const double *base, size_t nmemb)
 Find the index of the nearest value. More...
 
void z_print (const float *z, int rows, int columns)
 
float getZ (const float *z, int row, int column, int columns)
 
int mapdata_check (struct mapdata *data, struct mapsrc *src, double xcenter, double ycenter, double xwidth, double ywidth)
 Returns a code indicating the status of the mapdata. More...
 
struct mapboundsmapbounds_init ()
 Create a mapbounds struct. More...
 
int mapbounds_fill1 (struct mapsrc *src, struct mapbounds *bounds)
 Fills a mapbounds structure from a mapsrc object. More...
 
int mapbounds_fill2 (const char *file, struct mapbounds *bounds)
 Fill in a mapbounds structure from a netcdf file. More...
 
char * mapbounds_tostring (struct mapbounds *bounds)
 Return a string representation of a mapbounds struct. More...
 
int mapbounds_contains (struct mapbounds *bounds, const double x, const double y)
 
int mapdata_checksize (struct mapbounds *bounds, struct mapdata *data, const double xwidth, const double ywidth)
 

Macro Definition Documentation

#define MAPBOUNDS_NEAR_EDGE   2
#define MAPBOUNDS_OK   0
#define MAPBOUNDS_OUT_OF_BOUNDS   1
#define MAPDATA_FILL_FAILURE   2
#define MAPDATA_IS_EMPTY   0
#define MAPDATA_IS_FILLED   1
#define MAPIO_DEBUG   0

If not equal to zero then debug output will be printed.

Author
Copyright 2007 MBARI. All rights reserved.
Date
2007-09-11

MAPIO is currently written to read a netCDF file (GMT version 2 GRD file) in UTM coordinates. (Generated with the -G3 flag in MBSsystem).

Here's an example CDL for the GMT version 2 netcdf grids we'll be working with:

 netcdf canyonDataUTMG3 {
 dimensions:
 x = 1022 ;
 y = 879 ;
 variables:
 double x(x) ;
 x:long_name = "Easting (meters)" ;
 x:actual_range = 597413.876506377, 598435.621004263 ;
 double y(y) ;
 y:long_name = "Northing (meters)" ;
 y:actual_range = 4071749.4760972, 4072627.48862688 ;
 float z(y, x) ;
 z:long_name = "Depth (m)" ;
 z:_FillValue = nanf ;
 z:actual_range = 374.4248f, 536.1101f ;
global attributes:
 :Conventions = "COARDS" ;
 :title = "Bathymetry Grid" ;
 :description = "\n",
 "\tProjection: UTM10N\n",
 "\tGrid created by mbgrid\n",
 "\tMB-system Version 5.1.0\n",
 "\tRun by <root> on <JacksonHole.stanford.edu> at <Thu Aug 30 00:44:23 2007>" ;
 :node_offset = 0 ;
 }
 

For this code, the projection is assumed to be UTM. We are ignoring the zone; you'll have to be smart enough to make sure all coordinates are within the same zone as the GRD file you are using.

#define MAPIO_OK   1
#define MAPIO_OUTOFMEMORY   4
#define MAPIO_READERROR   2
#define MAPSRC_FILL_FAILURE   2
#define MAPSRC_IS_EMPTY   0
#define MAPSRC_IS_FILLED   1
#define NAN   0

Function Documentation

int check_error ( int  status,
struct mapsrc src 
)
float getZ ( const float *  z,
int  row,
int  column,
int  columns 
)
int mapbounds_contains ( struct mapbounds bounds,
const double  x,
const double  y 
)
int mapbounds_fill1 ( struct mapsrc src,
struct mapbounds bounds 
)

Fills a mapbounds structure from a mapsrc object.

function: mapbounds_fill1

Fills in a mapbounds structure based on info in the src stucture

Parameters
srcThe mapsrc data structure
boundsThe mapbounds data structure to populate
Returns
TODO...return a meaninful error code
int mapbounds_fill2 ( const char *  file,
struct mapbounds bounds 
)

Fill in a mapbounds structure from a netcdf file.

function: mapbounds_fill2

Fills in a maounds structure based on data in the netcdf file

struct mapbounds* mapbounds_init ( )

Create a mapbounds struct.

function: mapbounds_init

Allocates memory for a mapbounds struct as sets all values to 0. The structure's memory will need to be freed when done with the structure.

Returns
A mapbounds structure
char* mapbounds_tostring ( struct mapbounds bounds)

Return a string representation of a mapbounds struct.

function: mapbounds_tostring

String rep of a mapbounds struct

Parameters
boundsA mapbounds struct
Returns
A char*. Don't forget to free it when you're done with it
int mapdata_check ( struct mapdata data,
struct mapsrc src,
double  xcenter,
double  ycenter,
double  xwidth,
double  ywidth 
)

Returns a code indicating the status of the mapdata.

function: mapdata_check

TODO

Parameters
dataTHe mapdata structure
Returns
An integer indicating the status of the data structure
int mapdata_checksize ( struct mapbounds bounds,
struct mapdata data,
const double  xwidth,
const double  ywidth 
)
int mapdata_fill ( struct mapsrc src,
struct mapdata data,
double  x,
double  y,
double  xwidth,
double  ywidth 
)

Read a submap from a netcdf UTM GRD file into a mapdata structure.

function: mapdata_fill

Reads in a rectangular block of bathymetry data from a file (defined by src). The size of the rectangle is based on the params but will NOT be exaclty as specified by xwidth and ywidth.

Parameters
srcThe mapsrc structure definining where we are reading data from
dataA mapdata structure for storing the submap
xThe easting position of the vehicle. In the same UTM coordinate system as the src map.
yThe northing position of the vehicle. In the same UTM coordinate system as the src map.
xwidthThe width of the submap in meters to retrive.
ywidthThe width of the submap in meters to retrieve
Returns
An integer code of MAPBOUNDS_OK if all is OK, MAPBOUNDS_OUT_OF_BOUNDS if the x or y coordinate is outside the boundaries fo the map reference by src, or MAPBOUNDS_NEAR_EDGE if you x,y position is near the edge (resulting in a truncated submap)
void mapdata_free ( struct mapdata data,
int  free_all 
)

Free's the memory held by a mapdata structure.

function: mapdata_free

Parameters
dataThe mapdata structure who's memory we want to free
free_allIf equal to 0 then the structure itself will not be freed, only the internal pointers. All structure values will be set to 0. if not equal to 1 then the data structure's memory will be release.
char* mapdata_tostring ( struct mapdata data)

create a string of a mapdata structure

function: mapdata_tostring

Parameters
dataThe mapdata structure
Returns
A string. Don't forget to free it when you're done.
void mapsrc_fill ( const char *  file,
struct mapsrc src 
)

Opens a GMT GRD file and read coordinate variables.

function: mapsrc_fill

Opens a GMT version 2 GRD file (netcdf). It stores the needed information, such as ids to the netcdf file, variables, and the data for the x and y coordinate variables. Note that 'struct mapsrc' contains references to arrays which must be released. If an error occurs on fill a flag will be set in the mapsrc.status field.

Parameters
fileThe path of the file to read.
srcThe mapsrc structure for storing the information about the netcdf data file.
float mapsrc_find ( struct mapsrc src,
double  x,
double  y 
)

Find a z value at a given point.

function: mapsrc_find

Find a z-value in a map at a given x, y location

Parameters
srcThe mapsrc structure pointing to the map you want to read
xThe x coordinate in meters UTM (use same zone as netCDF file).
yThe y coordinate in meters UTM (use same zone as netCDF file).
Returns
The z value in meters of the point nearest to the x, y location NAN is returned if the x,y coordinate falls outside the boundaries of the map.
void mapsrc_free ( struct mapsrc src)

Frees the memory used by a mapsrc structure.

function: mapsrc_free

A mapsrc structure references to arrays. This function will free those arrays as well as the parent structure.

Parameters
srcIs the mapsrc structure to be freed
struct mapsrc* mapsrc_init ( void  )

Initializes a mapsrc structure.

function: mapsrc_init

Allocates memory for a mapsrc structure and sets the status flags. You will need to call 'mapsrc_free' on the structure to release it's memory when you are finished with it.

Returns
A mapsrc structure.
char* mapsrc_tostring ( struct mapsrc src)

create a string of a mapsrc structure

function: mapsrc_tostring

Parameters
srcThe mapsrc structure
Returns
A string. Don't forget to free it when you're done.
int nearest ( double  key,
const double *  base,
size_t  nmemb 
)

Find the index of the nearest value.

function: nearest

Finds the index of the nearest value in a double array to a key value. this is a naive implementation but should get us going.

Parameters
keyThe key value
baseThe double array that you're searcng to find the nearest value in. This implementation of nearest assumes the arrays are sorted ascending.
nmembThe number of elements in the base array
void z_print ( const float *  z,
int  rows,
int  columns 
)