eccodes/tests/grib_multi_from_message.c

88 lines
2.0 KiB
C
Raw Normal View History

2013-03-25 12:04:10 +00:00
/*
2019-04-15 13:44:45 +00:00
* Copyright 2005-2019 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.
*/
/*
2019-12-16 12:47:29 +00:00
* test: reading GRIB2 multi fields messages from memory
2013-03-25 12:04:10 +00:00
*/
#include "grib_api.h"
#include <sys/stat.h>
2019-11-05 14:23:23 +00:00
#include <assert.h>
#ifndef ECCODES_ON_WINDOWS
2013-03-25 12:04:10 +00:00
#include <unistd.h>
#endif
2013-03-25 12:04:10 +00:00
2019-12-16 12:47:29 +00:00
static void usage(const char* prog)
{
fprintf(stderr, "usage: %s [-m] file.grib\n", prog);
2017-09-01 16:36:16 +00:00
exit(1);
}
2013-03-25 12:04:10 +00:00
2019-12-16 12:47:29 +00:00
int main(int argc,char* argv[])
{
struct stat finfo;
char shortName[20]={0,};
2019-12-16 12:47:29 +00:00
size_t len;
grib_handle* h=NULL;
size_t fsize;
unsigned char* data=NULL;
unsigned char* p=NULL;
void* pdata=NULL;
int error=0;
int count=0;
char* filename=NULL;
FILE* f=NULL;
long level=0;
grib_context* c=grib_context_get_default();
2013-03-25 12:04:10 +00:00
if (argc==3 && !strcmp(argv[1],"-m")) {
grib_multi_support_on(0);
filename=argv[2];
}
else if (argc==2) filename=argv[1];
else usage(argv[0]);
2013-03-25 12:04:10 +00:00
2019-11-05 14:23:23 +00:00
assert(filename);
f=fopen(filename,"rb");
if (!f) {perror(filename);exit(1);}
2013-03-25 12:04:10 +00:00
2019-12-16 12:47:29 +00:00
stat(filename, &finfo);
fsize=finfo.st_size;
2013-03-25 12:04:10 +00:00
data=(unsigned char*)malloc(fsize);
p=data;
2013-03-25 12:04:10 +00:00
if (!data) {
fprintf(stderr,"unable to allocate %ld bytes\n",(long)fsize);
exit(1);
}
2013-03-25 12:04:10 +00:00
if( fread(data, 1, fsize, f) != fsize) {
perror(filename);
exit(1);
}
fclose(f);
pdata=&data;
2013-03-25 12:04:10 +00:00
while ((h=grib_handle_new_from_multi_message(c,(void**)pdata,&fsize,&error))!=NULL) {
count++;
len=20;
GRIB_CHECK(grib_get_string(h,"shortName",shortName,&len),"shortName");
GRIB_CHECK(grib_get_long(h,"level",&level),"level");
printf("%d %s %ld\n",count,shortName,level);
grib_handle_delete(h);
}
2013-03-25 12:04:10 +00:00
free(p);
2013-03-25 12:04:10 +00:00
return 0;
2013-03-25 12:04:10 +00:00
}