eccodes/tigge/tigge_accumulations.c

278 lines
7.4 KiB
C
Raw Normal View History

2013-03-25 12:04:10 +00:00
/*
2020-01-28 14:32:34 +00:00
* (C) Copyright 2005- ECMWF.
2013-03-25 12:04:10 +00:00
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
* virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
*/
2014-03-31 12:57:06 +00:00
/* cmake config header */
2015-02-18 18:11:38 +00:00
#ifdef HAVE_ECCODES_CONFIG_H
#include "eccodes_config.h"
2014-03-31 12:57:06 +00:00
#endif
/* autoconf config header */
#ifdef HAVE_CONFIG_H
2013-03-25 12:04:10 +00:00
#include "config.h"
2014-03-31 12:57:06 +00:00
#endif
2013-03-25 12:04:10 +00:00
#include <stdio.h>
#include <assert.h>
#include "grib_api.h"
#define NUMBER(x) (sizeof(x)/sizeof(x[0]))
const char* checks[] = {
2017-09-01 16:36:48 +00:00
/* Section 1 */
"identificationOfOriginatingGeneratingCentre",
"identificationOfOriginatingGeneratingSubCentre",
"gribMasterTablesVersionNumber",
"versionNumberOfGribLocalTables",
"significanceOfReferenceTime",
"year",
"month",
"day",
"hour",
"minute",
"second",
"productionStatusOfProcessedData",
"typeOfProcessedData",
/*Section3*/
"sourceOfGridDefinition",
"numberOfDataPoints",
"numberOfOctetsForOptionalListOfNumbersDefiningNumberOfPoints",
"interpretationOfListOfNumbersDefiningNumberOfPoints",
"gridDefinitionTemplateNumber",
"shapeOfTheEarth",
"scaleFactorOfRadiusOfSphericalEarth",
"scaledValueOfRadiusOfSphericalEarth",
"scaleFactorOfMajorAxisOfOblateSpheroidEarth",
"scaledValueOfMajorAxisOfOblateSpheroidEarth",
"scaleFactorOfMinorAxisOfOblateSpheroidEarth",
"scaledValueOfMinorAxisOfOblateSpheroidEarth",
"numberOfPointsAlongAParallel",
"numberOfPointsAlongAMeridian",
"basicAngleOfTheInitialProductionDomain",
"subdivisionsOfBasicAngle",
"latitudeOfFirstGridPoint",
"longitudeOfFirstGridPoint",
"resolutionAndComponentFlags",
"latitudeOfLastGridPoint",
"longitudeOfLastGridPoint",
"iDirectionIncrement",
"jDirectionIncrement",
"scanningMode",
/*Section4*/
"numberOfSection",
"numberOfCoordinatesValues",
"productDefinitionTemplateNumber",
"parameterCategory",
"parameterNumber",
"typeOfGeneratingProcess",
"backgroundGeneratingProcessIdentifier",
"generatingProcessIdentifier",
"hoursAfterReferenceTimeOfDataCutoff",
"minutesAfterReferenceTimeOfDataCutoff",
/* "indicatorOfUnitOfTimeRange",*/
/* "forecastTime", */
"typeOfFirstFixedSurface",
"scaleFactorOfFirstFixedSurface",
"scaledValueOfFirstFixedSurface",
"typeOfSecondFixedSurface",
"scaleFactorOfSecondFixedSurface",
"scaledValueOfSecondFixedSurface",
"typeOfEnsembleForecast",
"perturbationNumber",
"numberOfForecastsInEnsemble",
/*
"yearOfEndOfOverallTimeInterval",
"monthOfEndOfOverallTimeInterval",
"dayOfEndOfOverallTimeInterval",
"hourOfEndOfOverallTimeInterval",
"minuteOfEndOfOverallTimeInterval",
"secondOfEndOfOverallTimeInterval",
*/
"numberOfTimeRangeSpecificationsDescribingTheTimeIntervalsUsedToCalculateTheStatisticallyProcessedField",
"totalNumberOfDataValuesMissingInStatisticalProcess",
"typeOfStatisticalProcessing",
"typeOfTimeIncrementBetweenSuccessiveFieldsUsedInTheStatisticalProcessing",
/* "indicatorOfUnitOfTimeForTimeRangeOverWhichStatisticalProcessingIsDone", */
/*
"lengthOfTheTimeRangeOverWhichStatisticalProcessingIsDone",
*/
"indicatorOfUnitOfTimeForTheIncrementBetweenTheSuccessiveFieldsUsed",
"timeIncrementBetweenSuccessiveFields",
2013-03-25 12:04:10 +00:00
};
long check_values[NUMBER(checks)];
2017-09-01 16:36:48 +00:00
static void usage(const char *prog)
2013-03-25 12:04:10 +00:00
{
2017-09-01 16:36:48 +00:00
fprintf(stderr,"%s: in1 [in2 ... ] out\n",prog);
exit(1);
2013-03-25 12:04:10 +00:00
}
2017-09-01 16:36:48 +00:00
static void init_checks(grib_handle* h)
2013-03-25 12:04:10 +00:00
{
2017-09-01 16:36:48 +00:00
size_t i;
for(i = 0; i < NUMBER(checks); i++) {
GRIB_CHECK(grib_get_long(h,checks[i],&check_values[i]),checks[i]);
}
2013-03-25 12:04:10 +00:00
}
2017-09-01 16:36:48 +00:00
static void do_checks(grib_handle* h)
2013-03-25 12:04:10 +00:00
{
2017-09-01 16:36:48 +00:00
size_t i;
long val;
for(i = 0; i < NUMBER(checks); i++)
{
GRIB_CHECK(grib_get_long(h,checks[i],&val),checks[i]);
if(val != check_values[i])
{
printf("Value mismatch for %s: %ld and %ld\n",checks[i],check_values[i],val);
exit(1);
}
}
2013-03-25 12:04:10 +00:00
}
2017-09-01 16:36:48 +00:00
static void output_field(grib_handle* h,FILE* f,long bits,double* values,size_t size,const char* out)
2013-03-25 12:04:10 +00:00
{
2017-09-01 16:36:48 +00:00
const void *buffer;
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
GRIB_CHECK(grib_set_long(h,"numberOfBitsContainingEachPackedValue",bits),"numberOfBitsContainingEachPackedValue");
GRIB_CHECK(grib_set_double_array(h,"values",values,size),NULL);
GRIB_CHECK(grib_get_message(h,&buffer,&size),NULL);
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
if(fwrite(buffer,1,size,f) != size)
{
perror(out);
exit(1);
}
2013-03-25 12:04:10 +00:00
}
2017-09-01 16:36:48 +00:00
int main(int argc, char *argv[])
2013-03-25 12:04:10 +00:00
{
2017-09-01 16:36:48 +00:00
int i;
FILE *in,*out;
int e;
grib_handle *result = NULL,*h;
double* values = NULL;
double *tmp = NULL;
2018-01-19 16:20:59 +00:00
size_t size=0,count,j;
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
long step = 0;
long startStep = 0;
long endStep = 0;
long bits = 0;
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
if(argc < 3) usage(argv[0]);
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
out = fopen(argv[argc-1],"w");
if(!out) {
perror(argv[argc-1]);
exit(1);
}
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
for(i = 1; i < argc-1; i++)
{
in = fopen(argv[i],"rb");
2017-09-01 16:36:48 +00:00
if(!in) {
perror(argv[i]);
exit(1);
}
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
while( (h = grib_handle_new_from_file(NULL,in,&e)) != NULL )
{
long b = 0;
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
GRIB_CHECK(grib_get_long(h,"startStep",&startStep),"startStep");
GRIB_CHECK(grib_get_long(h,"endStep",&endStep),"endStep");
GRIB_CHECK(grib_get_long(h,"numberOfBitsContainingEachPackedValue",&b),"numberOfBitsContainingEachPackedValue");
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
if(b > bits) bits = b;
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
if(result == NULL)
{
grib_handle *g = grib_handle_clone(h);
result = h;
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
assert(g);
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
init_checks(g);
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
GRIB_CHECK(grib_get_size(g,"values",&size),argv[i]);
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
values = (double*)calloc(size,sizeof(double));
assert(values);
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
tmp = (double*)calloc(size,sizeof(double));
assert(tmp);
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
assert(startStep == 0);
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
GRIB_CHECK(grib_set_long(g,"startStep",0),"startStep");
GRIB_CHECK(grib_set_long(g,"endStep",0),"endStep");
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
output_field(g,out,0,values,size,argv[argc-1]);
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
grib_handle_delete(g);
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
}
else
do_checks(h);
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
GRIB_CHECK(grib_get_size(h,"values",&count),argv[i]);
assert(count == size);
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
GRIB_CHECK(grib_get_double_array(h,"values",tmp,&count),argv[i]);
assert(count == size);
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
for(j = 0; j < count; j++)
values[j] += tmp[j];
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
if(startStep != step)
{
printf("Fields not in step order: step=%ld previous=%ld\n",startStep,step);
exit(1);
}
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
assert(endStep - startStep == 6);
step = endStep;
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
GRIB_CHECK(grib_set_long(result,"startStep",0), "startStep");
GRIB_CHECK(grib_set_long(result,"endStep",step),"endStep");
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
output_field(result,out,bits,values,size,argv[argc-1]);
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
if(h != result)
grib_handle_delete(h);
}
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
GRIB_CHECK(e,argv[argc-2]);
}
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
if(result)
grib_handle_delete(result);
2013-03-25 12:04:10 +00:00
2020-02-27 13:12:49 +00:00
free(values);
2017-09-01 16:36:48 +00:00
if(fclose(out))
{
perror(argv[argc-1]);
exit(1);
}
2013-03-25 12:04:10 +00:00
2017-09-01 16:36:48 +00:00
return 0;
2013-03-25 12:04:10 +00:00
}