Commit 6676d7fe authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Added cdf_util.c and cdf_util.h.

parent aee00288
......@@ -24,6 +24,8 @@ libcdi_la_SOURCES = \
cdf.h \
cdf_int.c \
cdf_int.h \
cdf_util.c \
cdf_util.h \
cdi.h \
cdi_error.c \
cdi_limits.h \
......
......@@ -168,14 +168,15 @@ LTLIBRARIES = $(lib_LTLIBRARIES) $(noinst_LTLIBRARIES)
am__DEPENDENCIES_1 =
libcdi_la_DEPENDENCIES = $(am__DEPENDENCIES_1)
am__libcdi_la_SOURCES_DIST = basetime.c basetime.h binary.c binary.h \
calendar.c calendar.h cdf.c cdf.h cdf_int.c cdf_int.h cdi.h \
cdi_error.c cdi_limits.h cdi_util.c cgribex.h cgribexlib.c \
datetime.h dmemory.c dmemory.h cksum.c cksum.h cdi_cksum.c \
cdi_cksum.h cdi_uuid.h dtypes.h error.c error.h exse.h extra.h \
extralib.c file.c file.h gaussgrid.c gaussgrid.h gribapi.c \
gribapi.h gribapi_utilities.h grid.c grid.h ieg.h ieglib.c \
input_file.c input_file.h institution.c institution.h model.c \
model.h namespace.c namespace.h serialize.h serialize.c \
calendar.c calendar.h cdf.c cdf.h cdf_int.c cdf_int.h \
cdf_util.c cdf_util.h cdi.h cdi_error.c cdi_limits.h \
cdi_util.c cgribex.h cgribexlib.c datetime.h dmemory.c \
dmemory.h cksum.c cksum.h cdi_cksum.c cdi_cksum.h cdi_uuid.h \
dtypes.h error.c error.h exse.h extra.h extralib.c file.c \
file.h gaussgrid.c gaussgrid.h gribapi.c gribapi.h \
gribapi_utilities.h grid.c grid.h ieg.h ieglib.c input_file.c \
input_file.h institution.c institution.h model.c model.h \
namespace.c namespace.h serialize.h serialize.c \
referenceCounting.c referenceCounting.h resource_handle.c \
resource_handle.h service.h servicelib.c stream_cdf_i.c \
stream_cdf_o.c stream_cdf.h stream_cgribex.c stream_cgribex.h \
......@@ -196,8 +197,8 @@ am__objects_1 = cdiFortran.lo
am__objects_3 = gribapi_utilities.lo stream_gribapi.lo
@HAVE_LIBGRIB_API_TRUE@am__objects_4 = $(am__objects_3)
am_libcdi_la_OBJECTS = basetime.lo binary.lo calendar.lo cdf.lo \
cdf_int.lo cdi_error.lo cdi_util.lo cgribexlib.lo dmemory.lo \
cksum.lo cdi_cksum.lo error.lo extralib.lo file.lo \
cdf_int.lo cdf_util.lo cdi_error.lo cdi_util.lo cgribexlib.lo \
dmemory.lo cksum.lo cdi_cksum.lo error.lo extralib.lo file.lo \
gaussgrid.lo gribapi.lo grid.lo ieglib.lo input_file.lo \
institution.lo model.lo namespace.lo serialize.lo \
referenceCounting.lo resource_handle.lo servicelib.lo \
......@@ -507,29 +508,30 @@ noinst_LTLIBRARIES = libcdiresunpack.la $(am__append_1) \
@ENABLE_CDI_LIB_TRUE@include_HEADERS = cdi.h cdi.inc $(am__append_5)
AM_CFLAGS = $(PPM_CORE_CFLAGS) $(YAXT_CFLAGS) $(MPI_C_INCLUDE)
libcdi_la_SOURCES = basetime.c basetime.h binary.c binary.h calendar.c \
calendar.h cdf.c cdf.h cdf_int.c cdf_int.h cdi.h cdi_error.c \
cdi_limits.h cdi_util.c cgribex.h cgribexlib.c datetime.h \
dmemory.c dmemory.h cksum.c cksum.h cdi_cksum.c cdi_cksum.h \
cdi_uuid.h dtypes.h error.c error.h exse.h extra.h extralib.c \
file.c file.h gaussgrid.c gaussgrid.h gribapi.c gribapi.h \
gribapi_utilities.h grid.c grid.h ieg.h ieglib.c input_file.c \
input_file.h institution.c institution.h model.c model.h \
namespace.c namespace.h serialize.h serialize.c \
referenceCounting.c referenceCounting.h resource_handle.c \
resource_handle.h service.h servicelib.c stream_cdf_i.c \
stream_cdf_o.c stream_cdf.h stream_cgribex.c stream_cgribex.h \
stream_ext.c stream_ext.h stream_grb.c stream_grb.h \
stream_gribapi.h stream_history.c stream_ieg.c stream_ieg.h \
cdi_int.c cdi_int.h stream_record.c stream_srv.c stream_srv.h \
stream_var.c grb_write.c grb_read.c cdf_write.c cdf_read.c \
cdf_lazy_grid.c cdf_lazy_grid.h subtype.c subtype.h swap.h \
table.c table.h tablepar.h taxis.c taxis.h timebase.c \
timebase.h tsteps.c util.c varscan.c varscan.h version.c \
vlist.c vlist.h vlist_att.c vlist_att.h vlist_var.c \
vlist_var.h zaxis.c zaxis.h stream.c stream_write.c \
stream_read.c swap.c iterator.c iterator.h iterator_fallback.c \
iterator_fallback.h iterator_grib.c iterator_grib.h \
$(am__append_2) $(am__append_3)
calendar.h cdf.c cdf.h cdf_int.c cdf_int.h cdf_util.c \
cdf_util.h cdi.h cdi_error.c cdi_limits.h cdi_util.c cgribex.h \
cgribexlib.c datetime.h dmemory.c dmemory.h cksum.c cksum.h \
cdi_cksum.c cdi_cksum.h cdi_uuid.h dtypes.h error.c error.h \
exse.h extra.h extralib.c file.c file.h gaussgrid.c \
gaussgrid.h gribapi.c gribapi.h gribapi_utilities.h grid.c \
grid.h ieg.h ieglib.c input_file.c input_file.h institution.c \
institution.h model.c model.h namespace.c namespace.h \
serialize.h serialize.c referenceCounting.c \
referenceCounting.h resource_handle.c resource_handle.h \
service.h servicelib.c stream_cdf_i.c stream_cdf_o.c \
stream_cdf.h stream_cgribex.c stream_cgribex.h stream_ext.c \
stream_ext.h stream_grb.c stream_grb.h stream_gribapi.h \
stream_history.c stream_ieg.c stream_ieg.h cdi_int.c cdi_int.h \
stream_record.c stream_srv.c stream_srv.h stream_var.c \
grb_write.c grb_read.c cdf_write.c cdf_read.c cdf_lazy_grid.c \
cdf_lazy_grid.h subtype.c subtype.h swap.h table.c table.h \
tablepar.h taxis.c taxis.h timebase.c timebase.h tsteps.c \
util.c varscan.c varscan.h version.c vlist.c vlist.h \
vlist_att.c vlist_att.h vlist_var.c vlist_var.h zaxis.c \
zaxis.h stream.c stream_write.c stream_read.c swap.c \
iterator.c iterator.h iterator_fallback.c iterator_fallback.h \
iterator_grib.c iterator_grib.h $(am__append_2) \
$(am__append_3)
# cfortran.h is an optional part of libcdi
libcdi_la_USE_FC_extra_sources = \
......@@ -690,6 +692,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cdf_int.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cdf_lazy_grid.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cdf_read.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cdf_util.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cdf_write.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cdiFortran.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cdi_cksum.Plo@am__quote@
......
#include <string.h>
#include <ctype.h>
#include "dmemory.h"
#include "cdi.h"
#include "cdf_util.h"
void strtolower(char *str)
{
if ( str )
for ( size_t i = 0; str[i]; ++i )
str[i] = (char)tolower((int)str[i]);
}
int get_timeunit(size_t len, const char *ptu)
{
int timeunit = -1;
if ( len > 2 )
{
if ( memcmp(ptu, "sec", 3) == 0 ) timeunit = TUNIT_SECOND;
else if ( memcmp(ptu, "minute", 6) == 0 ) timeunit = TUNIT_MINUTE;
else if ( memcmp(ptu, "hour", 4) == 0 ) timeunit = TUNIT_HOUR;
else if ( memcmp(ptu, "day", 3) == 0 ) timeunit = TUNIT_DAY;
else if ( memcmp(ptu, "month", 5) == 0 ) timeunit = TUNIT_MONTH;
else if ( memcmp(ptu, "calendar_month", 14) == 0 ) timeunit = TUNIT_MONTH;
else if ( memcmp(ptu, "year", 4) == 0 ) timeunit = TUNIT_YEAR;
}
else if ( len == 1 )
{
if ( ptu[0] == 's' ) timeunit = TUNIT_SECOND;
}
return timeunit;
}
bool is_time_units(const char *timeunits)
{
bool status = strncmp(timeunits, "sec", 3) == 0
|| strncmp(timeunits, "minute", 6) == 0
|| strncmp(timeunits, "hour", 4) == 0
|| strncmp(timeunits, "day", 3) == 0
|| strncmp(timeunits, "month", 5) == 0;
return status;
}
bool is_timeaxis_units(const char *timeunits)
{
bool status = false;
size_t len = strlen(timeunits);
char *tu = (char *) Malloc((len+1)*sizeof(char));
memcpy(tu, timeunits, (len+1) * sizeof(char));
char *ptu = tu;
for ( size_t i = 0; i < len; i++ ) ptu[i] = (char)tolower((int)ptu[i]);
int timeunit = get_timeunit(len, ptu);
if ( timeunit != -1 )
{
while ( ! isspace(*ptu) && *ptu != 0 ) ptu++;
if ( *ptu )
{
while ( isspace(*ptu) ) ptu++;
int timetype = memcmp(ptu, "as", 2) == 0 ? TAXIS_ABSOLUTE :
memcmp(ptu, "since", 5) == 0 ? TAXIS_RELATIVE : -1;
status = timetype != -1;
}
}
Free(tu);
return status;
}
bool is_height_units(const char *units)
{
bool status = false;
int u0 = units[0];
if ( (u0=='m' && (!units[1] || strncmp(units, "meter", 5) == 0)) ||
(!units[2] && units[1]=='m' && (u0=='c' || u0=='d' || u0=='k')) )
{
status = true;
}
return status;
}
bool is_pressure_units(const char *units)
{
bool status = false;
if ( strncmp(units, "millibar", 8) == 0 ||
strncmp(units, "mb", 2) == 0 ||
strncmp(units, "hectopas", 8) == 0 ||
strncmp(units, "hPa", 3) == 0 ||
strncmp(units, "Pa", 2) == 0 )
{
status = true;
}
return status;
}
bool is_DBL_axis(/*const char *units,*/ const char *longname)
{
bool status = false;
if ( strcmp(longname, "depth below land") == 0 ||
strcmp(longname, "depth_below_land") == 0 ||
strcmp(longname, "levels below the surface") == 0 )
{
/*
if ( strcmp(ncvars[ncvarid].units, "cm") == 0 ||
strcmp(ncvars[ncvarid].units, "dm") == 0 ||
strcmp(ncvars[ncvarid].units, "m") == 0 )
*/
status = true;
}
return status;
}
bool is_depth_axis(const char *stdname, const char *longname)
{
bool status = false;
if ( strcmp(stdname, "depth") == 0 )
status = true;
else
if ( strcmp(longname, "depth_below_sea") == 0 ||
strcmp(longname, "depth below sea") == 0 )
{
status = true;
}
return status;
}
bool is_height_axis(const char *stdname, const char *longname)
{
bool status = false;
if ( strcmp(stdname, "height") == 0 )
status = true;
else
if ( strcmp(longname, "height") == 0 ||
strcmp(longname, "height above the surface") == 0 )
{
status = true;
}
return status;
}
bool is_lon_axis(const char *units, const char *stdname)
{
bool status = false;
char lc_units[16];
memcpy(lc_units, units, 15);
lc_units[15] = 0;
strtolower(lc_units);
if ( ((memcmp(lc_units, "degree", 6) == 0 || memcmp(lc_units, "radian", 6) == 0) &&
(memcmp(stdname, "grid_longitude", 14) == 0 || memcmp(stdname, "longitude", 9) == 0)) )
{
status = true;
}
if ( status == false &&
memcmp(stdname, "grid_latitude", 13) && memcmp(stdname, "latitude", 8) &&
memcmp(lc_units, "degree", 6) == 0 )
{
int ioff = 6;
if ( lc_units[ioff] == 's' ) ioff++;
if ( lc_units[ioff] == '_' ) ioff++;
if ( lc_units[ioff] == 'e' ) status = true;
}
return status;
}
bool is_lat_axis(const char *units, const char *stdname)
{
bool status = false;
char lc_units[16];
memcpy(lc_units, units, 15);
lc_units[15] = 0;
strtolower(lc_units);
if ( ((memcmp(lc_units, "degree", 6) == 0 || memcmp(lc_units, "radian", 6) == 0) &&
(memcmp(stdname, "grid_latitude", 13) == 0 || memcmp(stdname, "latitude", 8) == 0)) )
{
status = true;
}
if ( status == false &&
memcmp(stdname, "grid_longitude", 14) && memcmp(stdname, "longitude", 9) &&
memcmp(lc_units, "degree", 6) == 0 )
{
int ioff = 6;
if ( lc_units[ioff] == 's' ) ioff++;
if ( lc_units[ioff] == '_' ) ioff++;
if ( lc_units[ioff] == 'n' || lc_units[ioff] == 's' ) status = true;
}
return status;
}
#ifndef CDF_UTIL_H_
#define CDF_UTIL_H_
#include <stdbool.h>
void strtolower(char *str);
int get_timeunit(size_t len, const char *ptu);
bool is_time_units(const char *timeunits);
bool is_timeaxis_units(const char *timeunits);
bool is_height_units(const char *units);
bool is_pressure_units(const char *units);
bool is_DBL_axis(/*const char *units,*/ const char *longname);
bool is_depth_axis(const char *stdname, const char *longname);
bool is_height_axis(const char *stdname, const char *longname);
bool is_lon_axis(const char *units, const char *stdname);
bool is_lat_axis(const char *units, const char *stdname);
#endif
......@@ -94,6 +94,7 @@ files="basetime.c \
calendar.c \
cdf.c \
cdf_int.c \
cdf_util.c \
cdf_lazy_grid.c \
cdi_cksum.c \
cdi_error.c \
......
......@@ -16,6 +16,7 @@
#include "cdf_int.h"
#include "varscan.h"
#include "vlist.h"
#include "cdf_util.h"
#include "cdf_lazy_grid.h"
//#define PROJECTION_TEST
......@@ -112,79 +113,6 @@ typedef struct {
}
ncvar_t;
static
void strtolower(char *str)
{
if ( str )
for ( size_t i = 0; str[i]; ++i )
str[i] = (char)tolower((int)str[i]);
}
static
int get_timeunit(size_t len, const char *ptu)
{
int timeunit = -1;
if ( len > 2 )
{
if ( memcmp(ptu, "sec", 3) == 0 ) timeunit = TUNIT_SECOND;
else if ( memcmp(ptu, "minute", 6) == 0 ) timeunit = TUNIT_MINUTE;
else if ( memcmp(ptu, "hour", 4) == 0 ) timeunit = TUNIT_HOUR;
else if ( memcmp(ptu, "day", 3) == 0 ) timeunit = TUNIT_DAY;
else if ( memcmp(ptu, "month", 5) == 0 ) timeunit = TUNIT_MONTH;
else if ( memcmp(ptu, "calendar_month", 14) == 0 ) timeunit = TUNIT_MONTH;
else if ( memcmp(ptu, "year", 4) == 0 ) timeunit = TUNIT_YEAR;
}
else if ( len == 1 )
{
if ( ptu[0] == 's' ) timeunit = TUNIT_SECOND;
}
return timeunit;
}
static
bool isTimeUnits(const char *timeunits)
{
bool status = strncmp(timeunits, "sec", 3) == 0
|| strncmp(timeunits, "minute", 6) == 0
|| strncmp(timeunits, "hour", 4) == 0
|| strncmp(timeunits, "day", 3) == 0
|| strncmp(timeunits, "month", 5) == 0;
return status;
}
static
bool isTimeAxisUnits(const char *timeunits)
{
bool status = false;
size_t len = strlen(timeunits);
char *tu = (char *) Malloc((len+1)*sizeof(char));
memcpy(tu, timeunits, (len+1) * sizeof(char));
char *ptu = tu;
for ( size_t i = 0; i < len; i++ ) ptu[i] = (char)tolower((int)ptu[i]);
int timeunit = get_timeunit(len, ptu);
if ( timeunit != -1 )
{
while ( ! isspace(*ptu) && *ptu != 0 ) ptu++;
if ( *ptu )
{
while ( isspace(*ptu) ) ptu++;
int timetype = memcmp(ptu, "as", 2) == 0 ? TAXIS_ABSOLUTE :
memcmp(ptu, "since", 5) == 0 ? TAXIS_RELATIVE : -1;
status = timetype != -1;
}
}
Free(tu);
return status;
}
static
void scanTimeString(const char *ptu, int *rdate, int *rtime)
......@@ -684,7 +612,7 @@ int cdfTimeDimID(int fileID, int ndims, int nvars)
cdfGetAttText(fileID, varid, "units", sizeof(sbuf), sbuf);
strtolower(sbuf);
if ( isTimeUnits(sbuf) ) return dimids[0];
if ( is_time_units(sbuf) ) return dimids[0];
}
}
}
......@@ -810,150 +738,6 @@ void cdfSetDim(ncvar_t *ncvars, int ncvarid, int dimid, int dimtype)
ncvars[ncvarid].dimtype[dimid] = dimtype;
}
static
bool isLonAxis(const char *units, const char *stdname)
{
bool status = false;
char lc_units[16];
memcpy(lc_units, units, 15);
lc_units[15] = 0;
strtolower(lc_units);
if ( ((memcmp(lc_units, "degree", 6) == 0 || memcmp(lc_units, "radian", 6) == 0) &&
(memcmp(stdname, "grid_longitude", 14) == 0 || memcmp(stdname, "longitude", 9) == 0)) )
{
status = true;
}
if ( status == false &&
memcmp(stdname, "grid_latitude", 13) && memcmp(stdname, "latitude", 8) &&
memcmp(lc_units, "degree", 6) == 0 )
{
int ioff = 6;
if ( lc_units[ioff] == 's' ) ioff++;
if ( lc_units[ioff] == '_' ) ioff++;
if ( lc_units[ioff] == 'e' ) status = true;
}
return status;
}
static
bool isLatAxis(const char *units, const char *stdname)
{
bool status = false;
char lc_units[16];
memcpy(lc_units, units, 15);
lc_units[15] = 0;
strtolower(lc_units);
if ( ((memcmp(lc_units, "degree", 6) == 0 || memcmp(lc_units, "radian", 6) == 0) &&
(memcmp(stdname, "grid_latitude", 13) == 0 || memcmp(stdname, "latitude", 8) == 0)) )
{
status = true;
}
if ( status == false &&
memcmp(stdname, "grid_longitude", 14) && memcmp(stdname, "longitude", 9) &&
memcmp(lc_units, "degree", 6) == 0 )
{
int ioff = 6;
if ( lc_units[ioff] == 's' ) ioff++;
if ( lc_units[ioff] == '_' ) ioff++;
if ( lc_units[ioff] == 'n' || lc_units[ioff] == 's' ) status = true;
}
return status;
}
static
bool isDBLAxis(/*const char *units,*/ const char *longname)
{
bool status = false;
if ( strcmp(longname, "depth below land") == 0 ||
strcmp(longname, "depth_below_land") == 0 ||
strcmp(longname, "levels below the surface") == 0 )
{
/*
if ( strcmp(ncvars[ncvarid].units, "cm") == 0 ||
strcmp(ncvars[ncvarid].units, "dm") == 0 ||
strcmp(ncvars[ncvarid].units, "m") == 0 )
*/
status = true;
}
return status;
}
static
bool unitsIsHeight(const char *units)
{
bool status = false;
int u0 = units[0];
if ( (u0=='m' && (!units[1] || strncmp(units, "meter", 5) == 0)) ||
(!units[2] && units[1]=='m' && (u0=='c' || u0=='d' || u0=='k')) )
{
status = true;
}
return status;
}
static
bool isDepthAxis(const char *stdname, const char *longname)
{
bool status = false;
if ( strcmp(stdname, "depth") == 0 )
status = true;
else
if ( strcmp(longname, "depth_below_sea") == 0 ||
strcmp(longname, "depth below sea") == 0 )
{
status = true;
}
return status;
}
static
bool isHeightAxis(const char *stdname, const char *longname)
{
bool status = false;
if ( strcmp(stdname, "height") == 0 )
status = true;
else
if ( strcmp(longname, "height") == 0 ||
strcmp(longname, "height above the surface") == 0 )
{
status = true;
}
return status;
}
static
bool unitsIsPressure(const char *units)
{
bool status = false;
if ( strncmp(units, "millibar", 8) == 0 ||
strncmp(units, "mb", 2) == 0 ||
strncmp(units, "hectopas", 8) == 0 ||
strncmp(units, "hPa", 3) == 0 ||
strncmp(units, "Pa", 2) == 0 )
{
status = true;
}
return status;
}
static
int scan_hybrid_formula(int ncid, int ncfvarid, int *apvarid, int *bvarid, int *psvarid, int *avarid, int *p0varid)
{
......@@ -1961,21 +1745,21 @@ void verify_coordinate_vars_1(int ncid, int ndims, ncdim_t *ncdims, ncvar_t *ncv
if ( ncvars[ncvarid].units[0] != 0 )
{
if ( isLonAxis(ncvars[ncvarid].units, ncvars[ncvarid].stdname) )
if ( is_lon_axis(ncvars[ncvarid].units, ncvars[ncvarid].stdname) )
{
ncvars[ncvarid].islon = true;
cdfSetVar(ncvars, ncvarid, FALSE);
cdfSetDim(ncvars, ncvarid, 0, X_AXIS);
ncdims[ncdimid].dimtype = X_AXIS;
}
else if ( isLatAxis(ncvars[ncvarid].units, ncvars[ncvarid].stdname) )