Commit 137ab788 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

Merge declaration and initialization.

parent 8a38c8b1
#if defined (HAVE_CONFIG_H)
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
......@@ -51,18 +51,7 @@ typedef struct {
static
int gribapiGetZaxisType(long editionNumber, int grib_ltype)
{
int zaxistype = ZAXIS_GENERIC;
if ( editionNumber <= 1 )
{
zaxistype = grib1ltypeToZaxisType(grib_ltype);
}
else
{
zaxistype = grib2ltypeToZaxisType(grib_ltype);
}
return zaxistype;
return (editionNumber <= 1) ? grib1ltypeToZaxisType(grib_ltype) : grib2ltypeToZaxisType(grib_ltype);
}
static
......@@ -110,14 +99,13 @@ double timeunit_factor(int tu1, int tu2)
static
int gribapiGetTimeUnits(grib_handle *gh)
{
int timeunits = -1;
long unitsOfTime = -1;
grib_get_long(gh, "indicatorOfUnitOfTimeRange", &unitsOfTime);
GRIB_CHECK(my_grib_set_long(gh, "stepUnits", unitsOfTime), 0);
timeunits = getTimeunits(unitsOfTime);
int timeunits = getTimeunits(unitsOfTime);
return timeunits;
}
......@@ -152,7 +140,6 @@ static
void gribapiGetDataDateTime(grib_handle *gh, int *datadate, int *datatime)
{
long lpar;
GRIB_CHECK(grib_get_long(gh, "dataDate", &lpar), 0);
*datadate = (int) lpar;
GRIB_CHECK(grib_get_long(gh, "dataTime", &lpar), 0); //FIXME: This looses the seconds in GRIB2 files.
......@@ -169,20 +156,13 @@ void gribapiSetDataDateTime(grib_handle *gh, int datadate, int datatime)
static
int gribapiGetValidityDateTime(grib_handle *gh, int *vdate, int *vtime)
{
int rdate, rtime;
int timeUnits, startStep = 0, endStep;
int tstepRange = 0;
int range;
long sigofrtime = 3;
long sigofrtime = 3;
if ( gribEditionNumber(gh) > 1 )
{
GRIB_CHECK(grib_get_long(gh, "significanceOfReferenceTime", &sigofrtime), 0);
}
GRIB_CHECK(grib_get_long(gh, "significanceOfReferenceTime", &sigofrtime), 0);
else
{
GRIB_CHECK(grib_get_long(gh, "timeRangeIndicator", &sigofrtime), 0);
}
GRIB_CHECK(grib_get_long(gh, "timeRangeIndicator", &sigofrtime), 0);
if ( sigofrtime == 3 ) //XXX: This looks like a bug to me, because timeRangeIndicator == 3 does not seem to have the same meaning as significanceOfReferenceTime == 3. I would recommend replacing this condition with `if(!gribapiTimeIsFC())`.
{
......@@ -190,13 +170,14 @@ int gribapiGetValidityDateTime(grib_handle *gh, int *vdate, int *vtime)
}
else
{
int rdate, rtime;
gribapiGetDataDateTime(gh, &rdate, &rtime);
timeUnits = gribapiGetTimeUnits(gh);
int timeUnits = gribapiGetTimeUnits(gh);
int startStep = 0, endStep = 0;
gribapiGetSteps(gh, timeUnits, &startStep, &endStep);
range = endStep - startStep;
int range = endStep - startStep;
if ( range > 0 )
{
if ( startStep == 0 ) tstepRange = -1;
......@@ -204,21 +185,19 @@ int gribapiGetValidityDateTime(grib_handle *gh, int *vdate, int *vtime)
}
{
static bool lprint = true;
extern int grib_calendar;
int ryear, rmonth, rday, rhour, rminute, rsecond;
int julday, secofday;
int64_t time_period = endStep;
int64_t addsec;
cdiDecodeDate(rdate, &ryear, &rmonth, &rday);
cdiDecodeTime(rtime, &rhour, &rminute, &rsecond);
if ( rday > 0 )
{
static bool lprint = true;
extern int grib_calendar;
int julday, secofday;
encode_caldaysec(grib_calendar, ryear, rmonth, rday, rhour, rminute, rsecond, &julday, &secofday);
addsec = 0;
int64_t time_period = endStep;
int64_t addsec = 0;
switch ( timeUnits )
{
case TUNIT_SECOND: addsec = time_period; break;
......@@ -316,8 +295,8 @@ static
int calcLevel(int level_sf, long factor, long level)
{
double result = 0;
if(level != GRIB_MISSING_LONG) result = (double)level*grib2ScaleFactor(factor);
if(level_sf) result *= level_sf;
if (level != GRIB_MISSING_LONG) result = (double)level*grib2ScaleFactor(factor);
if (level_sf) result *= level_sf;
return (int)result;
}
......@@ -325,10 +304,6 @@ static
void grib2GetLevel(grib_handle *gh, int *leveltype1, int *leveltype2, int *lbounds, int *level1,
int *level2, int *level_sf, int *level_unit)
{
int status;
long lpar;
long factor;
*leveltype1 = 0;
*leveltype2 = -1;
*lbounds = 0;
......@@ -337,7 +312,8 @@ void grib2GetLevel(grib_handle *gh, int *leveltype1, int *leveltype2, int *lboun
*level_sf = 0;
*level_unit = 0;
status = grib_get_long(gh, "typeOfFirstFixedSurface", &lpar); //1 byte
long lpar;
int status = grib_get_long(gh, "typeOfFirstFixedSurface", &lpar); //1 byte
if ( status == 0 )
{
long llevel;
......@@ -371,6 +347,7 @@ void grib2GetLevel(grib_handle *gh, int *leveltype1, int *leveltype2, int *lboun
break;
}
long factor;
GRIB_CHECK(grib_get_long(gh, "scaleFactorOfFirstFixedSurface", &factor), 0); //1 byte
GRIB_CHECK(grib_get_long(gh, "scaledValueOfFirstFixedSurface", &llevel), 0); //4 byte
*level1 = calcLevel(*level_sf, factor, llevel);
......@@ -1469,9 +1446,6 @@ int gribapiDecode(void *gribbuffer, size_t gribsize, double *data, size_t gridsi
int unreduced, size_t *nmiss, double missval, int vlistID, int varID)
{
int status = 0;
long lpar;
long numberOfPoints;
size_t datasize;
UNUSED(vlistID);
UNUSED(varID);
......@@ -1479,7 +1453,6 @@ int gribapiDecode(void *gribbuffer, size_t gribsize, double *data, size_t gridsi
if ( unreduced )
{
static bool lwarn = true;
if ( lwarn )
{
lwarn = false;
......@@ -1492,9 +1465,10 @@ int gribapiDecode(void *gribbuffer, size_t gribsize, double *data, size_t gridsi
GRIB_CHECK(my_grib_set_double(gh, "missingValue", missval), 0);
/* get the size of the values array*/
size_t datasize;
GRIB_CHECK(grib_get_size(gh, "values", &datasize), 0);
GRIB_CHECK(grib_get_long(gh, "numberOfPoints", &numberOfPoints), 0);
// long numberOfPoints;
// GRIB_CHECK(grib_get_long(gh, "numberOfPoints", &numberOfPoints), 0);
// printf("values_size = %d numberOfPoints = %ld\n", datasize, numberOfPoints);
if ( gridsize != datasize )
......@@ -1502,6 +1476,7 @@ int gribapiDecode(void *gribbuffer, size_t gribsize, double *data, size_t gridsi
size_t dummy = datasize;
GRIB_CHECK(grib_get_double_array(gh, "values", data, &dummy), 0);
long lpar;
GRIB_CHECK(grib_get_long(gh, "gridDefinitionTemplateNumber", &lpar), 0);
int gridtype = (int) lpar;
......@@ -1522,21 +1497,15 @@ int gribapiDecode(void *gribbuffer, size_t gribsize, double *data, size_t gridsi
static
void gribapiDefInstitut(grib_handle *gh, int vlistID, int varID)
{
int instID;
if ( vlistInqInstitut(vlistID) != CDI_UNDEFID )
instID = vlistInqInstitut(vlistID);
else
instID = vlistInqVarInstitut(vlistID, varID);
int instID = (vlistInqInstitut(vlistID) != CDI_UNDEFID) ?
vlistInqInstitut(vlistID) : vlistInqVarInstitut(vlistID, varID);
if ( instID != CDI_UNDEFID )
{
long center, subcenter;
long center0, subcenter0;
center = institutInqCenter(instID);
subcenter = institutInqSubcenter(instID);
long center = institutInqCenter(instID);
long subcenter = institutInqSubcenter(instID);
long center0, subcenter0;
GRIB_CHECK(grib_get_long(gh, "centre", &center0), 0);
GRIB_CHECK(grib_get_long(gh, "subCentre", &subcenter0), 0);
......@@ -1550,12 +1519,8 @@ void gribapiDefInstitut(grib_handle *gh, int vlistID, int varID)
static
void gribapiDefModel(grib_handle *gh, int vlistID, int varID)
{
int modelID;
if ( vlistInqModel(vlistID) != CDI_UNDEFID )
modelID = vlistInqModel(vlistID);
else
modelID = vlistInqVarModel(vlistID, varID);
int modelID = (vlistInqModel(vlistID) != CDI_UNDEFID) ?
vlistInqModel(vlistID) : vlistInqVarModel(vlistID, varID);
if ( modelID != CDI_UNDEFID )
GRIB_CHECK(my_grib_set_long(gh, "generatingProcessIdentifier", modelInqGribID(modelID)), 0);
......@@ -1571,8 +1536,7 @@ void gribapiDefParam(int editionNumber, grib_handle *gh, int param, const char *
if ( pnum < 0 )
{
size_t len;
len = strlen(stdname);
size_t len = strlen(stdname);
if ( len )
{
int status = my_grib_set_string(gh, "cfName", stdname, &len);
......@@ -1719,7 +1683,6 @@ static
int gribapiDefSteptype(int editionNumber, grib_handle *gh, int productDefinitionTemplate, int typeOfGeneratingProcess, int tsteptype, int gcinit)
{
long proDefTempNum = 0;
size_t len = 64;
const char *stepType;
if (tsteptype >= TSTEP_INSTANT && tsteptype <= TSTEP_RATIO)
......@@ -1744,7 +1707,7 @@ int gribapiDefSteptype(int editionNumber, grib_handle *gh, int productDefinition
if ( !gcinit )
{
if ( editionNumber > 1 ) GRIB_CHECK(my_grib_set_long(gh, "productDefinitionTemplateNumber", proDefTempNum), 0);
len = strlen(stepType);
size_t len = strlen(stepType);
GRIB_CHECK(my_grib_set_string(gh, "stepType", stepType, &len), 0);
}
......@@ -1770,7 +1733,6 @@ int gribapiDefDateTimeRel(int editionNumber, grib_handle *gh, int rdate, int rti
int status = -1;
int year, month, day, hour, minute, second;
int julday1, secofday1, julday2, secofday2, days, secs;
long startStep = 0, endStep;
cdiDecodeDate(rdate, &year, &month, &day);
cdiDecodeTime(rtime, &hour, &minute, &second);
......@@ -1792,7 +1754,8 @@ int gribapiDefDateTimeRel(int editionNumber, grib_handle *gh, int rdate, int rti
gribapiDefStepUnits(editionNumber, gh, timeunit, proDefTempNum, gcinit);
endStep = (int) ((days*86400.0 + secs)/factor);
long startStep = 0;
long endStep = (long) ((days*86400.0 + secs)/factor);
if ( editionNumber > 1 ) GRIB_CHECK(my_grib_set_long(gh, "significanceOfReferenceTime", 1), 0);
if ( editionNumber > 1 ) GRIB_CHECK(my_grib_set_long(gh, "stepRange", 0), 0);
......@@ -1821,10 +1784,9 @@ static
void gribapiDefTime(int editionNumber, int productDefinitionTemplate, int typeOfGeneratingProcess, grib_handle *gh,
int vdate, int vtime, int tsteptype, int numavg, int taxisID, int gcinit)
{
int taxistype = -1;
UNUSED(numavg);
int taxistype = -1;
if ( taxisID != -1 ) taxistype = taxisInqType(taxisID);
if ( typeOfGeneratingProcess == 196 )
......@@ -1841,15 +1803,13 @@ void gribapiDefTime(int editionNumber, int productDefinitionTemplate, int typeOf
if ( taxistype == TAXIS_RELATIVE )
{
int status;
int calendar = taxisInqCalendar(taxisID);
int rdate = taxisInqRdate(taxisID);
int rtime = taxisInqRtime(taxisID);
int timeunit = taxisInqTunit(taxisID);
status = gribapiDefDateTimeRel(editionNumber, gh, rdate, rtime, vdate, vtime,
int status = gribapiDefDateTimeRel(editionNumber, gh, rdate, rtime, vdate, vtime,
productDefinitionTemplate, typeOfGeneratingProcess, tsteptype, timeunit, calendar, gcinit);
if ( status != 0 ) taxistype = TAXIS_ABSOLUTE;
}
......@@ -2354,13 +2314,13 @@ void gribapiDefLevel(int editionNumber, grib_handle *gh, int zaxisID, int levelI
{
char units[CDI_MAX_NAME];
bool lbounds = false;
double dlevel1 = 0, dlevel2 = 0;
int zaxistype = zaxisInqType(zaxisID);
long ltype = zaxisInqLtype(zaxisID);
long ltype2 = zaxisInqLtype2(zaxisID);
double level = zaxisInqLevels(zaxisID, NULL) ? zaxisInqLevel(zaxisID, levelID) : levelID+1;
double dlevel1 = 0, dlevel2 = 0;
if ( zaxisInqLbounds(zaxisID, NULL) && zaxisInqUbounds(zaxisID, NULL) )
{
lbounds = true;
......@@ -2381,11 +2341,7 @@ void gribapiDefLevel(int editionNumber, grib_handle *gh, int zaxisID, int levelI
zaxisDefUnits(zaxisID, "Pa");
}
long grib_ltype = -1;
if ( editionNumber <= 1 )
grib_ltype = zaxisTypeToGrib1ltype(zaxistype);
else
grib_ltype = zaxisTypeToGrib2ltype(zaxistype);
long grib_ltype = (editionNumber <= 1) ? zaxisTypeToGrib1ltype(zaxistype) : zaxisTypeToGrib2ltype(zaxistype);
switch (zaxistype)
{
......@@ -3024,25 +2980,20 @@ size_t gribapiEncode(int varID, int levelID, int vlistID, int gridID, int zaxisI
size_t datasize, const double *data, size_t nmiss, void **gribbuffer, size_t *gribbuffersize,
int comptype, void *gribContainer)
{
size_t recsize = 0;
void *dummy = NULL;
bool lieee = false;
/* int ensID, ensCount, forecast_type; *//* Ensemble Data */
int typeOfGeneratingProcess;
int productDefinitionTemplate;
long bitsPerValue;
long editionNumber = 2;
char name[256];
char stdname[256];
// extern unsigned char _grib_template_GRIB2[];
cdi_check_gridsize_int_limit("GRIB", datasize);
int param = vlistInqVarParam(vlistID, varID);
int datatype = vlistInqVarDatatype(vlistID, varID);
typeOfGeneratingProcess = vlistInqVarTypeOfGeneratingProcess(vlistID, varID);
productDefinitionTemplate = vlistInqVarProductDefinitionTemplate(vlistID, varID);
int typeOfGeneratingProcess = vlistInqVarTypeOfGeneratingProcess(vlistID, varID);
int productDefinitionTemplate = vlistInqVarProductDefinitionTemplate(vlistID, varID);
char name[256];
char stdname[256];
vlistInqVarName(vlistID, varID, name);
vlistInqVarStdname(vlistID, varID, stdname);
......@@ -3079,12 +3030,12 @@ size_t gribapiEncode(int varID, int levelID, int vlistID, int gridID, int zaxisI
if ( ! gc->init ) gribapiDefParam((int)editionNumber, gh, param, name, stdname);
if ( editionNumber == 2 && (datatype == CDI_DATATYPE_FLT32 || datatype == CDI_DATATYPE_FLT64) ) lieee = true;
bool lieee = (editionNumber == 2 && (datatype == CDI_DATATYPE_FLT32 || datatype == CDI_DATATYPE_FLT64));
/* bitsPerValue have to be defined before call to DefGrid (complex packing) */
// if ( lieee == false )
{
bitsPerValue = grbBitsPerValue(datatype);
long bitsPerValue = grbBitsPerValue(datatype);
GRIB_CHECK(my_grib_set_long(gh, "bitsPerValue", bitsPerValue), 0);
}
......@@ -3143,6 +3094,7 @@ size_t gribapiEncode(int varID, int levelID, int vlistID, int gridID, int zaxisI
GRIB_CHECK(grib_set_double_array(gh, "values", data, datasize), 0);
/* get the size of coded message */
size_t recsize = 0;
GRIB_CHECK(grib_get_message(gh, (const void **)&dummy, &recsize), 0);
recsize += 512; /* add some space for possible filling */
*gribbuffersize = recsize;
......@@ -3163,10 +3115,10 @@ size_t gribapiEncode(int varID, int levelID, int vlistID, int gridID, int zaxisI
void gribapiChangeParameterIdentification(grib_handle *gh, int code, int ltype, int lev)
{
long indicatorOfParameter, indicatorOfTypeOfLevel, level; // timeRangeIndicator: could be included later
indicatorOfParameter = code;
indicatorOfTypeOfLevel = ltype;
level = lev;
// timeRangeIndicator: could be included later
long indicatorOfParameter = code;
long indicatorOfTypeOfLevel = ltype;
long level = lev;
if (indicatorOfParameter!=-1) GRIB_CHECK(my_grib_set_long(gh, "indicatorOfParameter", indicatorOfParameter), 0);
if (indicatorOfTypeOfLevel!=-1) GRIB_CHECK(my_grib_set_long(gh, "indicatorOfTypeOfLevel", indicatorOfTypeOfLevel), 0);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment