eccodes/examples/C/multi2.c

81 lines
2.3 KiB
C
Raw Permalink Normal View History

2013-03-25 14:23:07 +00:00
/*
2020-01-28 14:32:34 +00:00
* (C) Copyright 2005- ECMWF.
2013-03-25 14:23:07 +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.
*/
/*
* C Implementation: multi2.c
*
* Description: Repeatedly print data contained in a multi-field GRIB2 message
2013-03-25 14:23:07 +00:00
*/
#include "eccodes.h"
2013-03-25 14:23:07 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#ifndef HAVE_FSEEKO
#define fseeko fseek
#define ftello ftell
#endif
2020-01-22 14:57:43 +00:00
const int NUM_FIELDS = 4;
const int COUNT = 5;
2013-03-25 14:23:07 +00:00
const char* file_path = "../../data/multi_created.grib2";
static void read_data(FILE* fp, int num_msgs)
2013-03-25 14:23:07 +00:00
{
2020-01-22 14:57:43 +00:00
int err = 0, i;
long stepRange = 0;
codes_handle* h = NULL;
2014-06-18 16:14:01 +00:00
2021-11-06 21:46:06 +00:00
printf("File offset start = %ld\n", ftello(fp));
2020-01-22 14:57:43 +00:00
for (i = 0; i < num_msgs; ++i) {
2015-02-12 17:34:01 +00:00
h = codes_handle_new_from_file(0, fp, PRODUCT_GRIB, &err);
CODES_CHECK(err, 0);
2013-03-25 14:23:07 +00:00
2020-01-22 14:57:43 +00:00
CODES_CHECK(codes_get_long(h, "stepRange", &stepRange), 0);
2014-06-18 16:14:01 +00:00
printf("%d : stepRange=%ld\n", i, stepRange);
codes_handle_delete(h);
2014-06-18 16:14:01 +00:00
/* These tests make sure we always start from 1st field of the grib msg */
/* and not where we left off last time */
2020-01-22 14:57:43 +00:00
if (i == 0) assert(stepRange == 0); /* 1st field */
if (i == 1) assert(stepRange == 12); /* 2nd field */
if (i == 2) assert(stepRange == 24); /* 3rd field */
if (i == 3) assert(stepRange == 36); /* 4th field */
2014-06-18 16:14:01 +00:00
}
2013-03-25 14:23:07 +00:00
}
2017-09-01 16:35:30 +00:00
int main(int argc, char** argv)
{
int i;
FILE* fp = fopen(file_path, "rb");
if (!fp) {
fprintf(stderr, "ERROR: unable to open GRIB file %s)\n", file_path);
exit(1);
}
2017-09-01 16:35:30 +00:00
/* turn on support for multi-field messages */
2021-10-13 14:48:19 +00:00
codes_grib_multi_support_on(NULL);
2017-09-01 16:35:30 +00:00
2020-01-22 14:57:43 +00:00
for (i = 1; i < COUNT; ++i) {
printf("Pass %d: \n", i);
fseeko(fp, 0, SEEK_SET);
read_data(fp, NUM_FIELDS);
/* Reset the internal engine state using this file pointer for the next round */
codes_grib_multi_support_reset_file(codes_context_get_default(), fp);
2017-09-01 16:35:30 +00:00
}
fclose(fp);
2024-01-26 21:26:50 +00:00
codes_context_delete(NULL);
printf("All OK\n");
2017-09-01 16:35:30 +00:00
return 0;
}