mirror of https://github.com/ecmwf/eccodes.git
GRIB-459: Provide implementation for C API call: grib_get_length
This commit is contained in:
parent
b228421510
commit
b0c7a7a6d7
142
examples/C/get.c
142
examples/C/get.c
|
@ -16,97 +16,109 @@
|
|||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include "grib_api.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int err = 0;
|
||||
double *values = NULL;
|
||||
size_t values_len= 0;
|
||||
int err = 0;
|
||||
double *values = NULL;
|
||||
size_t values_len= 0;
|
||||
size_t i = 0, len = 0;
|
||||
|
||||
size_t i = 0;
|
||||
double latitudeOfFirstGridPointInDegrees;
|
||||
double longitudeOfFirstGridPointInDegrees;
|
||||
double latitudeOfLastGridPointInDegrees;
|
||||
double longitudeOfLastGridPointInDegrees;
|
||||
|
||||
double latitudeOfFirstGridPointInDegrees;
|
||||
double longitudeOfFirstGridPointInDegrees;
|
||||
double latitudeOfLastGridPointInDegrees;
|
||||
double longitudeOfLastGridPointInDegrees;
|
||||
double jDirectionIncrementInDegrees;
|
||||
double iDirectionIncrementInDegrees;
|
||||
|
||||
double jDirectionIncrementInDegrees;
|
||||
double iDirectionIncrementInDegrees;
|
||||
long numberOfPointsAlongAParallel;
|
||||
long numberOfPointsAlongAMeridian;
|
||||
|
||||
long numberOfPointsAlongAParallel;
|
||||
long numberOfPointsAlongAMeridian;
|
||||
double average = 0;
|
||||
|
||||
double average = 0;
|
||||
FILE* in = NULL;
|
||||
char* filename = "../../data/regular_latlon_surface.grib1";
|
||||
grib_handle *h = NULL;
|
||||
|
||||
FILE* in = NULL;
|
||||
char* filename = "../../data/regular_latlon_surface.grib1";
|
||||
grib_handle *h = NULL;
|
||||
in = fopen(filename,"r");
|
||||
if(!in) {
|
||||
printf("ERROR: unable to open file %s\n",filename);
|
||||
return 1;
|
||||
}
|
||||
|
||||
in = fopen(filename,"r");
|
||||
if(!in) {
|
||||
printf("ERROR: unable to open file %s\n",filename);
|
||||
return 1;
|
||||
}
|
||||
/* create new handle from a message in a file*/
|
||||
h = grib_handle_new_from_file(0,in,&err);
|
||||
if (h == NULL) {
|
||||
printf("Error: unable to create handle from file %s\n",filename);
|
||||
}
|
||||
|
||||
/* create new handle from a message in a file*/
|
||||
h = grib_handle_new_from_file(0,in,&err);
|
||||
if (h == NULL) {
|
||||
printf("Error: unable to create handle from file %s\n",filename);
|
||||
}
|
||||
/* Store the filename in the key "file" for this handle */
|
||||
len = strlen(filename);
|
||||
GRIB_CHECK(grib_set_string(h, "file", filename, &len), 0);
|
||||
|
||||
/* get as a long*/
|
||||
GRIB_CHECK(grib_get_long(h,"Ni",&numberOfPointsAlongAParallel),0);
|
||||
printf("numberOfPointsAlongAParallel=%ld\n",numberOfPointsAlongAParallel);
|
||||
/* get as a long*/
|
||||
GRIB_CHECK(grib_get_long(h,"Ni",&numberOfPointsAlongAParallel),0);
|
||||
printf("numberOfPointsAlongAParallel=%ld\n",numberOfPointsAlongAParallel);
|
||||
|
||||
/* get as a long*/
|
||||
GRIB_CHECK(grib_get_long(h,"Nj",&numberOfPointsAlongAMeridian),0);
|
||||
printf("numberOfPointsAlongAMeridian=%ld\n",numberOfPointsAlongAMeridian);
|
||||
/* get as a long*/
|
||||
GRIB_CHECK(grib_get_long(h,"Nj",&numberOfPointsAlongAMeridian),0);
|
||||
printf("numberOfPointsAlongAMeridian=%ld\n",numberOfPointsAlongAMeridian);
|
||||
|
||||
/* get as a double*/
|
||||
GRIB_CHECK(grib_get_double(h,"latitudeOfFirstGridPointInDegrees",&latitudeOfFirstGridPointInDegrees),0);
|
||||
printf("latitudeOfFirstGridPointInDegrees=%g\n",latitudeOfFirstGridPointInDegrees);
|
||||
/* get as a double*/
|
||||
GRIB_CHECK(grib_get_double(h,"latitudeOfFirstGridPointInDegrees",&latitudeOfFirstGridPointInDegrees),0);
|
||||
printf("latitudeOfFirstGridPointInDegrees=%g\n",latitudeOfFirstGridPointInDegrees);
|
||||
|
||||
/* get as a double*/
|
||||
GRIB_CHECK(grib_get_double(h,"longitudeOfFirstGridPointInDegrees",&longitudeOfFirstGridPointInDegrees),0);
|
||||
printf("longitudeOfFirstGridPointInDegrees=%g\n",longitudeOfFirstGridPointInDegrees);
|
||||
/* get as a double*/
|
||||
GRIB_CHECK(grib_get_double(h,"longitudeOfFirstGridPointInDegrees",&longitudeOfFirstGridPointInDegrees),0);
|
||||
printf("longitudeOfFirstGridPointInDegrees=%g\n",longitudeOfFirstGridPointInDegrees);
|
||||
|
||||
/* get as a double*/
|
||||
GRIB_CHECK(grib_get_double(h,"latitudeOfLastGridPointInDegrees",&latitudeOfLastGridPointInDegrees),0);
|
||||
printf("latitudeOfLastGridPointInDegrees=%g\n",latitudeOfLastGridPointInDegrees);
|
||||
/* get as a double*/
|
||||
GRIB_CHECK(grib_get_double(h,"latitudeOfLastGridPointInDegrees",&latitudeOfLastGridPointInDegrees),0);
|
||||
printf("latitudeOfLastGridPointInDegrees=%g\n",latitudeOfLastGridPointInDegrees);
|
||||
|
||||
/* get as a double*/
|
||||
GRIB_CHECK(grib_get_double(h,"longitudeOfLastGridPointInDegrees",&longitudeOfLastGridPointInDegrees),0);
|
||||
printf("longitudeOfLastGridPointInDegrees=%g\n",longitudeOfLastGridPointInDegrees);
|
||||
/* get as a double*/
|
||||
GRIB_CHECK(grib_get_double(h,"longitudeOfLastGridPointInDegrees",&longitudeOfLastGridPointInDegrees),0);
|
||||
printf("longitudeOfLastGridPointInDegrees=%g\n",longitudeOfLastGridPointInDegrees);
|
||||
|
||||
/* get as a double*/
|
||||
GRIB_CHECK(grib_get_double(h,"jDirectionIncrementInDegrees",&jDirectionIncrementInDegrees),0);
|
||||
printf("jDirectionIncrementInDegrees=%g\n",jDirectionIncrementInDegrees);
|
||||
/* get as a double*/
|
||||
GRIB_CHECK(grib_get_double(h,"jDirectionIncrementInDegrees",&jDirectionIncrementInDegrees),0);
|
||||
printf("jDirectionIncrementInDegrees=%g\n",jDirectionIncrementInDegrees);
|
||||
|
||||
/* get as a double*/
|
||||
GRIB_CHECK(grib_get_double(h,"iDirectionIncrementInDegrees",&iDirectionIncrementInDegrees),0);
|
||||
printf("iDirectionIncrementInDegrees=%g\n",iDirectionIncrementInDegrees);
|
||||
/* get as a double*/
|
||||
GRIB_CHECK(grib_get_double(h,"iDirectionIncrementInDegrees",&iDirectionIncrementInDegrees),0);
|
||||
printf("iDirectionIncrementInDegrees=%g\n",iDirectionIncrementInDegrees);
|
||||
|
||||
/* get the size of the values array*/
|
||||
GRIB_CHECK(grib_get_size(h,"values",&values_len),0);
|
||||
/* get the size of the values array*/
|
||||
GRIB_CHECK(grib_get_size(h,"values",&values_len),0);
|
||||
|
||||
values = malloc(values_len*sizeof(double));
|
||||
values = malloc(values_len*sizeof(double));
|
||||
|
||||
/* get data values*/
|
||||
GRIB_CHECK(grib_get_double_array(h,"values",values,&values_len),0);
|
||||
/* get data values*/
|
||||
GRIB_CHECK(grib_get_double_array(h,"values",values,&values_len),0);
|
||||
|
||||
average = 0;
|
||||
for(i = 0; i < values_len; i++)
|
||||
average += values[i];
|
||||
average = 0;
|
||||
for(i = 0; i < values_len; i++)
|
||||
average += values[i];
|
||||
|
||||
average /=(double)values_len;
|
||||
average /=(double)values_len;
|
||||
|
||||
free(values);
|
||||
free(values);
|
||||
|
||||
printf("There are %d values, average is %g\n",(int)values_len,average);
|
||||
printf("There are %d values, average is %g\n",(int)values_len,average);
|
||||
|
||||
grib_handle_delete(h);
|
||||
{
|
||||
/* Now retrieve the value of the key "file" */
|
||||
char file[256]={0,};
|
||||
GRIB_CHECK(grib_get_length(h, "file", &len),0);
|
||||
assert(len == 1+strlen(filename));
|
||||
grib_get_string(h, "file", file, &len);
|
||||
assert( strcmp(file, filename) == 0 );
|
||||
}
|
||||
|
||||
fclose(in);
|
||||
return 0;
|
||||
grib_handle_delete(h);
|
||||
|
||||
fclose(in);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -20,105 +20,105 @@
|
|||
#include "grib_api.h"
|
||||
|
||||
void usage(char* prog) {
|
||||
printf("Usage: %s latlon_file grib_orography grib_file grib_file ...\n",prog);
|
||||
exit(1);
|
||||
printf("Usage: %s latlon_file grib_orography grib_file grib_file ...\n",prog);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
FILE* fin=0;
|
||||
int ret=0;
|
||||
char* fname=0;
|
||||
float lat,lon;
|
||||
double *vlat,*vlon;
|
||||
int npoints=0,i=0,n=0;
|
||||
grib_handle* h;
|
||||
double *outlats,*outlons,*values,*lsm_values,*distances;
|
||||
int* indexes;
|
||||
long step=0;
|
||||
char time[10]={0,};
|
||||
char date[10]={0,};
|
||||
long parameter=0;
|
||||
size_t len=0;
|
||||
long iid=0;
|
||||
long *id=NULL;
|
||||
FILE* fin=0;
|
||||
int ret=0;
|
||||
char* fname=0;
|
||||
float lat,lon;
|
||||
double *vlat,*vlon;
|
||||
int npoints=0,i=0,n=0;
|
||||
grib_handle* h;
|
||||
double *outlats,*outlons,*values,*lsm_values,*distances;
|
||||
int* indexes;
|
||||
long step=0;
|
||||
char time[10]={0,};
|
||||
char date[10]={0,};
|
||||
long parameter=0;
|
||||
size_t len=0;
|
||||
long iid=0;
|
||||
long *id=NULL;
|
||||
|
||||
if (argc < 2) usage(argv[0]);
|
||||
if (argc < 2) usage(argv[0]);
|
||||
|
||||
fname=argv[1];
|
||||
fin=fopen(fname,"r");
|
||||
if(!fin) { perror(fname); exit(1); }
|
||||
npoints=0;
|
||||
while (fscanf(fin,"%ld %g %g",&iid,&lat,&lon) != EOF) npoints++;
|
||||
fclose(fin);
|
||||
|
||||
id=(long*)malloc(npoints*sizeof(long));
|
||||
if (!id) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(long)));exit(1);}
|
||||
vlat=(double*)malloc(npoints*sizeof(double));
|
||||
if (!vlat) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
|
||||
vlon=(double*)malloc(npoints*sizeof(double));
|
||||
if (!vlon) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
|
||||
outlats=(double*)malloc(npoints*sizeof(double));
|
||||
if (!outlats) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
|
||||
outlons=(double*)malloc(npoints*sizeof(double));
|
||||
if (!outlons) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
|
||||
values=(double*)malloc(npoints*sizeof(double));
|
||||
if (!values) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
|
||||
lsm_values=(double*)malloc(npoints*sizeof(double));
|
||||
if (!lsm_values) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
|
||||
distances=(double*)malloc(npoints*sizeof(double));
|
||||
if (!distances) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
|
||||
indexes=(int*)malloc(npoints*sizeof(int));
|
||||
if (!indexes) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
|
||||
|
||||
fname=argv[1];
|
||||
fin=fopen(fname,"r");
|
||||
if(!fin) { perror(fname); exit(1); }
|
||||
i=0;
|
||||
while (fscanf(fin,"%ld %g %g",&iid,&lat,&lon) != EOF) {
|
||||
id[i]=iid;vlat[i]=lat;
|
||||
while(lon < 0) lon+=360;
|
||||
vlon[i]=lon;
|
||||
i++;
|
||||
}
|
||||
fclose(fin);
|
||||
|
||||
fname=argv[2];
|
||||
fin=fopen(fname,"r");
|
||||
if(!fin) { perror(fname); exit(1); }
|
||||
h=grib_handle_new_from_file(0,fin,&ret);
|
||||
if (!h || ret!=GRIB_SUCCESS) {printf(" unable to create handle\n");exit(1);}
|
||||
|
||||
grib_nearest_find_multiple(h,1,vlat,vlon,npoints,
|
||||
outlats,outlons,lsm_values,distances,indexes);
|
||||
|
||||
grib_handle_delete(h);
|
||||
|
||||
fclose(fin);
|
||||
|
||||
for (n=3;n<=argc-1;n++) {
|
||||
fname=argv[n];
|
||||
fname=argv[1];
|
||||
fin=fopen(fname,"r");
|
||||
if(!fin) { perror(fname); exit(1); }
|
||||
while ((h=grib_handle_new_from_file(0,fin,&ret))!=NULL) {
|
||||
grib_get_double_elements(h,"values",indexes,npoints,values);
|
||||
npoints=0;
|
||||
while (fscanf(fin,"%ld %g %g",&iid,&lat,&lon) != EOF) npoints++;
|
||||
fclose(fin);
|
||||
|
||||
len=10;
|
||||
grib_get_string(h,"date",date,&len);
|
||||
len=10;
|
||||
grib_get_string(h,"time",time,&len);
|
||||
grib_get_long(h,"step",&step);
|
||||
grib_get_long(h,"parameter",¶meter);
|
||||
id=(long*)malloc(npoints*sizeof(long));
|
||||
if (!id) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(long)));exit(1);}
|
||||
vlat=(double*)malloc(npoints*sizeof(double));
|
||||
if (!vlat) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
|
||||
vlon=(double*)malloc(npoints*sizeof(double));
|
||||
if (!vlon) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
|
||||
outlats=(double*)malloc(npoints*sizeof(double));
|
||||
if (!outlats) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
|
||||
outlons=(double*)malloc(npoints*sizeof(double));
|
||||
if (!outlons) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
|
||||
values=(double*)malloc(npoints*sizeof(double));
|
||||
if (!values) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
|
||||
lsm_values=(double*)malloc(npoints*sizeof(double));
|
||||
if (!lsm_values) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
|
||||
distances=(double*)malloc(npoints*sizeof(double));
|
||||
if (!distances) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
|
||||
indexes=(int*)malloc(npoints*sizeof(int));
|
||||
if (!indexes) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
|
||||
|
||||
printf("# %s %s %ld %ld\n",date,time,step,parameter);
|
||||
grib_handle_delete(h);
|
||||
for (i=0;i<npoints;i++)
|
||||
printf("%ld %g %g %g %g\n",
|
||||
id[i],outlats[i],outlons[i],
|
||||
lsm_values[i],values[i]);
|
||||
fname=argv[1];
|
||||
fin=fopen(fname,"r");
|
||||
if(!fin) { perror(fname); exit(1); }
|
||||
i=0;
|
||||
while (fscanf(fin,"%ld %g %g",&iid,&lat,&lon) != EOF) {
|
||||
id[i]=iid;vlat[i]=lat;
|
||||
while(lon < 0) lon+=360;
|
||||
vlon[i]=lon;
|
||||
i++;
|
||||
}
|
||||
fclose(fin);
|
||||
|
||||
fname=argv[2];
|
||||
fin=fopen(fname,"r");
|
||||
if(!fin) { perror(fname); exit(1); }
|
||||
h=grib_handle_new_from_file(0,fin,&ret);
|
||||
if (!h || ret!=GRIB_SUCCESS) {printf(" unable to create handle\n");exit(1);}
|
||||
|
||||
grib_nearest_find_multiple(h,1,vlat,vlon,npoints,
|
||||
outlats,outlons,lsm_values,distances,indexes);
|
||||
|
||||
grib_handle_delete(h);
|
||||
|
||||
fclose(fin);
|
||||
}
|
||||
|
||||
return ret;
|
||||
for (n=3;n<=argc-1;n++) {
|
||||
fname=argv[n];
|
||||
fin=fopen(fname,"r");
|
||||
if(!fin) { perror(fname); exit(1); }
|
||||
while ((h=grib_handle_new_from_file(0,fin,&ret))!=NULL) {
|
||||
grib_get_double_elements(h,"values",indexes,npoints,values);
|
||||
|
||||
GRIB_CHECK(grib_get_length(h, "date", &len),0);
|
||||
grib_get_string(h,"date",date,&len);
|
||||
GRIB_CHECK(grib_get_length(h, "time", &len),0);
|
||||
grib_get_string(h,"time",time,&len);
|
||||
grib_get_long(h,"step",&step);
|
||||
grib_get_long(h,"parameter",¶meter);
|
||||
|
||||
printf("# %s %s %ld %ld\n",date,time,step,parameter);
|
||||
grib_handle_delete(h);
|
||||
for (i=0;i<npoints;i++)
|
||||
printf("%ld %g %g %g %g\n",
|
||||
id[i],outlats[i],outlons[i],
|
||||
lsm_values[i],values[i]);
|
||||
}
|
||||
|
||||
fclose(fin);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
1815
src/grib_value.c
1815
src/grib_value.c
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue