OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
nc_gridutils.c File Reference
#include "nc_gridutils.h"
Include dependency graph for nc_gridutils.c:

Go to the source code of this file.

Macros

#define NEW_CACHE_SIZE   240000000
 
#define NEW_CACHE_NELEMS   1001
 
#define NEW_CACHE_PREEMPTION   .75
 

Functions

void handle_nc_error (int status, char *file, int line)
 
var_info_t * allocate_varinfo ()
 
var_info_t * load_varinfo (int ncid, int varid)
 
int find_varid (int ncid, const char *varnames[], int *varid)
 
grid_info_t * allocate_gridinfo ()
 
int init_gridinfo (char *filename, const char *varnames[], grid_info_t *grid)
 
int fix_latlon (float *lat, float *lon)
 
int latlon_findex (grid_info_t *grid, float lat, float lon, double *findex)
 
int latlon_index (grid_info_t *grid, float lat, float lon, size_t *index)
 
int get_bylatlon (grid_info_t *grid, float lat, float lon, double *value)
 
int has_extra_column (grid_info_t *grid)
 
int get_grid_area (grid_info_t *grid, float north, float south, float east, float west, grid_area_t *area)
 
int interp_gridvar (grid_info_t *grid, float lat, float lon, double *result)
 
void print_gridinfo (grid_info_t grid)
 

Detailed Description

Convenience utilities for handling NetCDF4 gridded variables.

Author
Gwyn Fireman
Ocean Ecology Laboratory, NASA Goddard Space Flight Center

Definition in file nc_gridutils.c.

Macro Definition Documentation

◆ NEW_CACHE_NELEMS

#define NEW_CACHE_NELEMS   1001

Definition at line 10 of file nc_gridutils.c.

◆ NEW_CACHE_PREEMPTION

#define NEW_CACHE_PREEMPTION   .75

Definition at line 11 of file nc_gridutils.c.

◆ NEW_CACHE_SIZE

#define NEW_CACHE_SIZE   240000000

Definition at line 9 of file nc_gridutils.c.

Function Documentation

◆ allocate_gridinfo()

grid_info_t* allocate_gridinfo ( )

Allocate memory for structure describing grid.

Return values
gridEmpty structure for describing gridded variable.

Definition at line 107 of file nc_gridutils.c.

◆ allocate_varinfo()

var_info_t* allocate_varinfo ( )

Allocate memory for structure describing NetCDF variable.

Return values
varEmpty structure for describing NetCDF variable.

Definition at line 34 of file nc_gridutils.c.

◆ find_varid()

int find_varid ( int  ncid,
const char *  varnames[],
int *  varid 
)

Find ID of first variable found in input list.

Parameters
[in]ncidNetCDF ID, from a previous call to nc_open(),
[in]varnamesNull-terminated list of possible variable names.
[out]varidNetCDF variable ID
Returns
Error if no variable found.

Definition at line 83 of file nc_gridutils.c.

◆ fix_latlon()

int fix_latlon ( float *  lat,
float *  lon 
)

Force coordinates into range: {-90 <= lat < 90} and {-180 <= lon < 190}

Parameters
[in,out]lat,lonDesired geographic coordinates.
Returns
Error if either coordinate is NaN.

Definition at line 281 of file nc_gridutils.c.

◆ get_bylatlon()

int get_bylatlon ( grid_info_t *  grid,
float  lat,
float  lon,
double *  value 
)

Read gridded variable value nearest to specified coordinates.

Parameters
[in]gridStructure describing grid.
[in]lat,lonDesired geographic coordinates.
[out]valuePointer to value, or FillValue if not found.
Returns
Error if coordinates outside of grid range. (0=good)

Definition at line 368 of file nc_gridutils.c.

◆ get_grid_area()

int get_grid_area ( grid_info_t *  grid,
float  north,
float  south,
float  east,
float  west,
grid_area_t area 
)

Read gridded variable values covering specified boundaries.

Parameters
[in]gridStructure describing grid.
[in]north,south,east,westGeographic boundaries of desired area.
[out]areaStructure contains and describes extracted area.
Returns
Error if any boundary set outside of grid range.

Definition at line 406 of file nc_gridutils.c.

◆ handle_nc_error()

void handle_nc_error ( int  status,
char *  file,
int  line 
)

Print NetCDF error message and exit.

Parameters
[in]statusNetCDF error status.
[in]fileOriginating FILE.
[in]lineOriginating LINE.

Definition at line 21 of file nc_gridutils.c.

◆ has_extra_column()

int has_extra_column ( grid_info_t *  grid)

Determine whether grid has extra longitude column. Needed when extracting area crossing +/- 180 degrees.

Parameters
[in]gridStructure describing grid.
Returns
TRUE: has extra longitude column.
FALSE: no extra longitude column.

Definition at line 394 of file nc_gridutils.c.

◆ init_gridinfo()

int init_gridinfo ( char *  filename,
const char *  varnames[],
grid_info_t *  grid 
)

Load information about gridded variable from a file.

Parameters
[in]filename
[in]varnamesNull-terminated list of possible variable names.
[out]gridStructure describing grid.
Returns
Error status.

Definition at line 132 of file nc_gridutils.c.

◆ interp_gridvar()

int interp_gridvar ( grid_info_t *  grid,
float  lat,
float  lon,
double *  result 
)

Get bilinear interpolation of any gridded variable.

Parameters
[in]gridStructure describing grid.
[in]lat,lonDesired geographic coordinates.
[out]resultPointer to interpolated value, or FillValue if not found.
Returns
Error if coordinates outside of grid range.
Remarks
nc_get_var1_double will automatically convert native type to double - see http://www.unidata.ucar.edu/software/netcdf/docs/netcdf/Type-Conversion.html

Definition at line 529 of file nc_gridutils.c.

◆ latlon_findex()

int latlon_findex ( grid_info_t *  grid,
float  lat,
float  lon,
double *  findex 
)

Find floating point index of specified coordinates within gridded variable.

Parameters
[in]gridStructure describing grid.
[in]lat,lonDesired geographic coordinates.
[out]findexIndices of [y,x] corresponding to [lat,lon].
Returns
Error if coordinates outside of grid range.

Definition at line 312 of file nc_gridutils.c.

◆ latlon_index()

int latlon_index ( grid_info_t *  grid,
float  lat,
float  lon,
size_t *  index 
)

Find integer index of specified coordinates within gridded variable.

Parameters
[in]gridStructure describing grid.
[in]lat,lonDesired geographic coordinates.
[out]indexIndices of [y,x] corresponding to [lat,lon].
Returns
Error if coordinates outside of grid range. (0=good)

Definition at line 342 of file nc_gridutils.c.

◆ load_varinfo()

var_info_t* load_varinfo ( int  ncid,
int  varid 
)

Read NetCDF variable attributes and load into structure.

Parameters
[in]ncidNetCDF ID, from a previous call to nc_open(),
[in]varidNetCDF variable ID
Return values
varStructure describing NetCDF variable.

Definition at line 52 of file nc_gridutils.c.

◆ print_gridinfo()

void print_gridinfo ( grid_info_t  grid)

Print grid description structure.

Parameters
[in]gridStructure describing grid.

Definition at line 592 of file nc_gridutils.c.