int get_box(grib_handle *h,double *east,double *north,double *west,double *south) { int ret=0; long iScansNegatively,jScansPositively; ret=grib_get_long(h,"iScansNegatively",&iScansNegatively); if (ret) return ret; ret=grib_get_long(h,"jScansPositively",&jScansPositively); if (ret) return ret; if (iScansNegatively) { grib_get_double(h,"longitudeOfFirstGridPointInDegrees",west); grib_get_double(h,"longitudeOfLastGridPointInDegrees",east); } else { grib_get_double(h,"longitudeOfFirstGridPointInDegrees",east); grib_get_double(h,"longitudeOfLastGridPointInDegrees",west); } if (jScansPositively) { grib_get_double(h,"latitudeOfFirstGridPointInDegrees",south); grib_get_double(h,"latitudeOfLastGridPointInDegrees",north); } else { grib_get_double(h,"latitudeOfFirstGridPointInDegrees",north); grib_get_double(h,"latitudeOfLastGridPointInDegrees",south); } if (*east>*west) *east-=360; return ret; }
int main(int argc,char* argv[]) { grib_index* index=NULL; grib_handle* h=NULL; char* infile=NULL; int i,j,k,l; long ostep,olevel,onumber; char oshortName[200]; size_t lenshortName=200; int ret=0,count=0; if (argc != 2) usage(argv[0]); infile=argv[1]; outfile=argv[2]; printf("indexing...\n"); /* Create an index from a grib file with a given set of keys*/ index=grib_index_new_from_file(0,infile,"shortName,level,number,step",&ret); if (ret) {printf("error: %s\n",grib_get_error_message(ret)); exit(ret);} printf("end indexing...\n"); /* ask the index to order the fields thorough some keys*/ ret=grib_index_orderby(index,"level,shortName,number:desc,step:asc"); if (ret!=GRIB_SUCCESS) exit(1); count=0; /* loop through all the fields in the asked order*/ while ((h=grib_handle_new_from_index(index,&ret))!=NULL){ count++; if (ret) {printf("error: %d\n",ret); exit(ret);} lenshortName=200; grib_get_string(h,"shortName",oshortName,&lenshortName); grib_get_long(h,"level",&olevel); grib_get_long(h,"number",&onumber); grib_get_long(h,"step",&ostep); printf("shortName=%s ",oshortName); printf("level=%ld ",olevel); printf("number=%ld ",onumber); printf("step=%ld \n",ostep); grib_handle_delete(h); } if (ret!=GRIB_END_OF_INDEX) { printf("error: %s\n",grib_get_error_message(ret)); exit(ret); } printf(" %d messages selected\n",count); return 0; }
static int value_count(grib_accessor* a,long* numberOfElements) { grib_accessor_signed_bits* self = (grib_accessor_signed_bits*)a; *numberOfElements=0; return grib_get_long(a->parent->h,self->numberOfElements,numberOfElements); }
static int pack_string(grib_accessor* a, const char* val, size_t *len) { grib_accessor_g2_mars_labeling* self = (grib_accessor_g2_mars_labeling*)a; char* key=NULL; int ret=0; long lval=0; switch (self->index) { case 0: key=(char*)self->the_class; break; case 1: key=(char*)self->type; break; case 2: key=(char*)self->stream; break; default : grib_context_log(a->parent->h->context,GRIB_LOG_ERROR, "invalid first argument of g2_mars_labeling in %s",a->name); return GRIB_INTERNAL_ERROR; break; } ret=grib_set_string(a->parent->h, key,val,len); if (ret) return ret; /* failed */ ret=grib_get_long(a->parent->h, key,&lval); if (ret) return ret; /* failed */ return extra_set(a,lval); }
static int is_rotated_grid(grib_handle* h) { long is_rotated = 0; int err = grib_get_long(h, "is_rotated_grid", &is_rotated); if (!err && is_rotated ) return 1; return 0; }
int grib_fieldset_column_copy_from_handle(grib_handle* h,grib_fieldset* set,int i) { int err=0; long lval=0; double dval=0; char sval[1024]; size_t slen=1024; if (!set || !h || set->columns[i].type == 0) return GRIB_INVALID_ARGUMENT; if (set->columns[i].size >= set->columns[i].values_array_size) grib_fieldset_columns_resize(set,set->columns[i].values_array_size+GRIB_ARRAY_INCREMENT); switch (set->columns[i].type) { case GRIB_TYPE_LONG: err=grib_get_long(h,set->columns[i].name,&lval); set->columns[i].long_values[set->columns[i].size]=lval; break; case GRIB_TYPE_DOUBLE: err=grib_get_double(h,set->columns[i].name,&dval); set->columns[i].double_values[set->columns[i].size]=dval; break; case GRIB_TYPE_STRING: err=grib_get_string(h,set->columns[i].name,sval,&slen); set->columns[i].string_values[set->columns[i].size]=grib_context_strdup(h->context,sval); break; } set->columns[i].errors[set->columns[i].size]=err; set->columns[i].size++; return err; }
static int pack_long (grib_accessor* a, const long* val, size_t *len) { grib_accessor_signed_bits* self = (grib_accessor_signed_bits*)a; int ret = 0; long off = 0; long numberOfBits=0; size_t buflen = 0; unsigned char *buf = NULL; unsigned long i = 0; unsigned long rlen = value_count(a); if(*len != rlen) { ret=grib_set_long(a->parent->h,self->numberOfElements,rlen); } ret=grib_get_long(a->parent->h,self->numberOfBits,&numberOfBits); if (ret) return ret; buflen = compute_byte_count(a); buf = grib_context_malloc_clear(a->parent->h->context,buflen+sizeof(long)); for(i=0; i < rlen;i++) grib_encode_signed_longb(buf, val[i] , &off, numberOfBits); grib_buffer_replace(a, buf, buflen,1,1); grib_context_free(a->parent->h->context,buf); return ret; }
static int value_count(grib_accessor* a,long* numberOfValues) { grib_accessor_data_g2complex_packing* self = (grib_accessor_data_g2complex_packing*)a; *numberOfValues = 0; return grib_get_long(a->parent->h,self->numberOfValues,numberOfValues); }
static int unpack_long (grib_accessor* a, long* val, size_t *len) { grib_accessor_signed_bits* self = (grib_accessor_signed_bits*)a; int i; int ret=0; long pos = a->offset*8; long rlen = value_count(a); long numberOfBits = 0; if(*len < rlen) { grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, " wrong size (%ld) for %s it contains %d values ",*len, a->name , rlen); *len = 0; return GRIB_ARRAY_TOO_SMALL; } ret=grib_get_long(a->parent->h,self->numberOfBits,&numberOfBits); if (ret) return ret; if (numberOfBits==0) { int i; for (i=0;i<rlen;i++) val[i]=0; return GRIB_SUCCESS; } for (i=0;i<rlen;i++) val[i] = grib_decode_signed_longb(a->parent->h->buffer->data, &pos, numberOfBits); *len = rlen; return GRIB_SUCCESS; }
static int find(grib_nearest* nearest, grib_handle* h, double inlat, double inlon,unsigned long flags, double* outlats,double* outlons, double *outvalues,double *distances,int* indexes, size_t *len) { grib_nearest_sh* self = (grib_nearest_sh*) nearest; long J,K,M; double *values; int size,i,ret; size_t vsize=0; double val; if( (ret = grib_get_long(h,self->J,&J))!= GRIB_SUCCESS) return ret; if( (ret = grib_get_long(h,self->K,&K))!= GRIB_SUCCESS) return ret; if( (ret = grib_get_long(h,self->M,&M))!= GRIB_SUCCESS) return ret; size=2*LEGENDRE_SIZE(J); vsize=size; values=(double*)grib_context_malloc_clear(h->context,sizeof(double)*size); if (!values) { grib_context_log(h->context,GRIB_LOG_ERROR, "nearest_sh: unable to allocate %d bytes", sizeof(double)*size); return GRIB_OUT_OF_MEMORY; } if( (ret = grib_get_double_array(h,self->values_key,values,&vsize)) != GRIB_SUCCESS) return ret; Assert(vsize==size); val=grib_invtrans(h->context,J,inlat,inlon,values); grib_context_free(h->context,values); for (i=0;i<4;i++) { outlats[i]=inlat; outlons[i]=inlon; outvalues[i]=val; indexes[i]=-1; } return GRIB_SUCCESS; }
int main(int argc,char* argv[]) { struct stat finfo; char shortName[20]={0,}; size_t len=20; 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(); 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]); f=fopen(filename,"r"); if (!f) {perror(filename);exit(1);} fstat(fileno((FILE*)f),&finfo); fsize=finfo.st_size; data=(unsigned char*)malloc(fsize); p=data; if (!data) { fprintf(stderr,"unable to allocate %ld bytes\n",(long)fsize); exit(1); } if( fread(data, 1, fsize, f) != fsize) { perror(filename); exit(1); } fclose(f); pdata=&data; 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); } free(p); return 0; }
static int unpack_long(grib_accessor* a, long* val, size_t *len) { grib_accessor_g2_chemical* self = (grib_accessor_g2_chemical*)a; long productDefinitionTemplateNumber=0; grib_get_long(a->parent->h, self->productDefinitionTemplateNumber,&productDefinitionTemplateNumber); *val=is_productDefinitionTemplateNumber_Chemical(productDefinitionTemplateNumber); return GRIB_SUCCESS; }
int main(int argc, char** argv) { int err = 0; long parameterCategory=0,parameterNumber=0,discipline=0; FILE* in = NULL; char* filename = "../../data/multi.grib2"; grib_handle *h = NULL; /* turn on support for multi fields messages */ grib_multi_support_on(0); /* turn off support for multi fields messages */ /* grib_multi_support_off(0); */ in = fopen(filename,"r"); if(!in) { printf("ERROR: unable to open file %s\n",filename); return 1; } while ((h = grib_handle_new_from_file(0,in,&err)) != NULL ) { GRIB_CHECK(err,0); GRIB_CHECK(grib_get_long(h,"discipline",&discipline),0); printf("discipline=%ld\n",discipline); GRIB_CHECK(grib_get_long(h,"parameterCategory",¶meterCategory),0); printf("parameterCategory=%ld\n",parameterCategory); GRIB_CHECK(grib_get_long(h,"parameterNumber",¶meterNumber),0); printf("parameterNumber=%ld\n",parameterNumber); if ( discipline == 0 && parameterCategory==2) { if (parameterNumber == 2) printf("-------- u -------\n"); if (parameterNumber == 3) printf("-------- v -------\n"); } grib_handle_delete(h); } fclose(in); return 0; }
long get(grib_handle *h,const char* what) { int e; long val; if((e = grib_get_long(h,what,&val)) != GRIB_SUCCESS) { fprintf(stderr,"%s, field %d [%s]: cannot get %s: %s\n",file,field,param,what,grib_get_error_message(e)); exit(1); val = -1; } return val; }
static int unpack_long(grib_accessor* a, long* val, size_t *len) { int ret=GRIB_SUCCESS; size_t size=0; size_t i; double max,min,d,b; double *values=0; long binaryScaleFactor,decimalScaleFactor; grib_accessor_second_order_bits_per_value* self = (grib_accessor_second_order_bits_per_value*)a; if (self->bitsPerValue) { *val=self->bitsPerValue; return GRIB_SUCCESS; } if((ret = grib_get_size(a->parent->h, self->values,&size)) != GRIB_SUCCESS) { *val=self->bitsPerValue; return GRIB_SUCCESS; } if((ret = grib_get_long(a->parent->h, self->binaryScaleFactor,&binaryScaleFactor)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h, self->decimalScaleFactor,&decimalScaleFactor)) != GRIB_SUCCESS) return ret; values=grib_context_malloc_clear(a->parent->h->context,sizeof(double)*size); if (!values) { grib_context_log(a->parent->h->context,GRIB_LOG_FATAL,"%s unable to allocate %ld bytes", a->name,(long)size); return GRIB_OUT_OF_MEMORY; } if((ret = grib_get_double_array_internal(a->parent->h, self->values,values,&size)) != GRIB_SUCCESS) return ret; max=values[0]; min=max; for (i=1;i<size;i++) { if (max<values[i]) max=values[i]; if (min>values[i]) min=values[i]; } d=grib_power(-decimalScaleFactor,10); b=grib_power(-binaryScaleFactor,2); /* self->bitsPerValue=(long)ceil(log((double)((max-min)*d+1))/log(2.0))-binaryScaleFactor; */ self->bitsPerValue=number_of_bits((unsigned long)(fabs(max-min)*b*d)); *val=self->bitsPerValue; grib_context_free(a->parent->h->context,values); return ret; }
static grib_values* get_key_values(grib_runtime_options* options,grib_handle* h) { int i=0; int ret=0; char value[MAX_STRING_LEN]={0,}; char* notfound="not found"; for (i=0;i<options->print_keys_count;i++) { size_t len=MAX_STRING_LEN; ret=GRIB_SUCCESS; if (grib_is_missing(h,options->print_keys[i].name,&ret) && ret==GRIB_SUCCESS) { options->print_keys[i].type=GRIB_TYPE_MISSING; sprintf(value,"MISSING"); } else if ( ret != GRIB_NOT_FOUND ){ if (options->print_keys[i].type == GRIB_TYPE_UNDEFINED) { grib_get_native_type(h,options->print_keys[i].name,&(options->print_keys[i].type)); } switch (options->print_keys[i].type) { case GRIB_TYPE_STRING: ret=grib_get_string( h,options->print_keys[i].name,value,&len); break; case GRIB_TYPE_DOUBLE: ret=grib_get_double( h,options->print_keys[i].name, &(options->print_keys[i].double_value)); sprintf(value,"%g",options->print_keys[i].double_value); break; case GRIB_TYPE_LONG: ret=grib_get_long( h,options->print_keys[i].name, &(options->print_keys[i].long_value)); sprintf(value,"%ld",(long)options->print_keys[i].long_value); break; default: fprintf(dump_file,"invalid type for %s\n",options->print_keys[i].name); exit(1); } } if (ret != GRIB_SUCCESS) { if (options->fail) GRIB_CHECK_NOLINE(ret,options->print_keys[i].name); if (ret == GRIB_NOT_FOUND) strcpy(value,notfound); else { fprintf(dump_file,"%s %s\n",grib_get_error_message(ret),options->print_keys[i].name); exit(ret); } } options->print_keys[i].string_value=strdup(value); } return options->print_keys; }
static void print(grib_context* c,grib_expression* g,grib_handle* f) { grib_expression_accessor* e = (grib_expression_accessor*)g; printf("access('%s",e->name); if(f) { long s = 0; grib_get_long(f,e->name,&s); printf("=%ld",s); } printf("')"); }
static long compute_byte_count(grib_accessor* a){ grib_accessor_signed_bits* self = (grib_accessor_signed_bits*)a; long numberOfBits; long numberOfElements; int ret=0; ret=grib_get_long(a->parent->h,self->numberOfBits,&numberOfBits); if (ret) { grib_context_log(a->parent->h->context,GRIB_LOG_ERROR, "%s unable to get %s to compute size",a->name,self->numberOfBits); return 0; } ret=grib_get_long(a->parent->h,self->numberOfElements,&numberOfElements); if (ret) { grib_context_log(a->parent->h->context,GRIB_LOG_ERROR, "%s unable to get %s to compute size",a->name,self->numberOfElements); return 0; } return (numberOfBits*numberOfElements+7)/8; }
static int unpack_long (grib_accessor* a, long* val, size_t *len) { grib_accessor_count_missing* self = (grib_accessor_count_missing*)a; unsigned char* p; int i; long size=0; long offset=0; long unusedBitsInBitmap=0; long numberOfDataPoints=0; grib_handle* h=a->parent->h; grib_accessor* bitmap=grib_find_accessor(a->parent->h,self->bitmap); *val=0; *len=1; if (!bitmap) return GRIB_SUCCESS; size=grib_byte_count(bitmap); offset=grib_byte_offset(bitmap); if (grib_get_long(h,self->unusedBitsInBitmap,&unusedBitsInBitmap) != GRIB_SUCCESS) { if (grib_get_long(h,self->numberOfDataPoints,&numberOfDataPoints) != GRIB_SUCCESS) { grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,"unable to count missing values"); return GRIB_INTERNAL_ERROR; } unusedBitsInBitmap=size*8-numberOfDataPoints; } p=a->parent->h->buffer->data+offset; size-=unusedBitsInBitmap/8; unusedBitsInBitmap= unusedBitsInBitmap % 8; for (i=0;i<size-1;i++) *val += bitsoff[*(p++)]; *val += bitsoff[(*p) | used[unusedBitsInBitmap]]; return GRIB_SUCCESS; }
static int pack_long(grib_accessor* a, const long* val, size_t *len) { grib_accessor_g2_chemical* self = (grib_accessor_g2_chemical*)a; long productDefinitionTemplateNumber=-1; long productDefinitionTemplateNumberNew=-1; /*long type=-1; long stream=-1;*/ long eps=-1; char stepType[15]={0,}; size_t slen=15; /*int chemical = *val;*/ int isInstant=0; /*long derivedForecast=-1;*/ int ret = 0; if (grib_get_long(a->parent->h, self->productDefinitionTemplateNumber,&productDefinitionTemplateNumber)!=GRIB_SUCCESS) return GRIB_SUCCESS; /* grib_get_long(a->parent->h, self->type,&type); grib_get_long(a->parent->h, self->stream,&stream); */ ret = grib_get_string(a->parent->h, self->stepType, stepType, &slen); Assert(ret == GRIB_SUCCESS); eps = is_productDefinitionTemplateNumber_EPS(productDefinitionTemplateNumber); if (!strcmp(stepType,"instant")) isInstant=1; if ( eps == 1 ) { if (isInstant) { productDefinitionTemplateNumberNew=41; } else { productDefinitionTemplateNumberNew=43; } } else { if (isInstant) { productDefinitionTemplateNumberNew=40; } else { productDefinitionTemplateNumberNew=42; } } if (productDefinitionTemplateNumber != productDefinitionTemplateNumberNew) { grib_set_long(a->parent->h, self->productDefinitionTemplateNumber,productDefinitionTemplateNumberNew); /*if (derivedForecast>=0) grib_set_long(a->parent->h, self->derivedForecast,derivedForecast);*/ } return 0; }
static int value_count(grib_accessor* a,long* number_of_points) { int ret=0; grib_accessor_data_apply_gdsnotpresent *self =(grib_accessor_data_apply_gdsnotpresent*)a; *number_of_points=0; if((ret = grib_get_long(a->parent->h,self->number_of_points,number_of_points)) != GRIB_SUCCESS) { grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, "grib_accessor_data_apply_gdsnotpresent: value_count: unable to get number of points"); } return ret; }
static int pack_long (grib_accessor* a, const long* val, size_t *len) { grib_accessor_unsigned_bits* self = (grib_accessor_unsigned_bits*)a; int ret = 0; long off = 0; long numberOfBits=0; size_t buflen = 0; unsigned char *buf = NULL; unsigned long i = 0; unsigned long rlen = value_count(a); /* if(*len < rlen) { grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, "Wrong size for %s it contains %d values ", a->name , rlen ); return GRIB_ARRAY_TOO_SMALL; } */ if (*len!=rlen) ret=grib_set_long(a->parent->h,self->numberOfElements,*len); ret=grib_get_long(a->parent->h,self->numberOfBits,&numberOfBits); if (ret) return ret; if (numberOfBits==0) { grib_buffer_replace(a, NULL, 0,1,1); return GRIB_SUCCESS; } buflen = compute_byte_count(a); buf = grib_context_malloc_clear(a->parent->h->context,buflen+sizeof(long)); for(i=0; i < *len; i++) grib_encode_unsigned_longb(buf, val[i] , &off, numberOfBits); grib_buffer_replace(a, buf, buflen,1,1); grib_context_free(a->parent->h->context,buf); return ret; }
static void print_key_values(grib_runtime_options* options,grib_handle* h) { int i; int ret=0; char* s="\"keys\" : {"; double dvalue=0; long lvalue=0; char value[MAX_STRING_LEN]; size_t len=MAX_STRING_LEN; for (i=0;i<options->print_keys_count;i++) { ret=GRIB_SUCCESS; printf("%s",s); len=MAX_STRING_LEN; printf("\"%s\" : ",options->print_keys[i].name); if (grib_is_missing(h,options->print_keys[i].name,&ret) && ret==GRIB_SUCCESS) printf("\"missing\""); else if ( ret == GRIB_SUCCESS ) { if (options->print_keys[i].type == GRIB_TYPE_UNDEFINED) grib_get_native_type(h,options->print_keys[i].name,&(options->print_keys[i].type)); switch (options->print_keys[i].type) { case GRIB_TYPE_STRING: ret=grib_get_string( h,options->print_keys[i].name,value,&len); printf("\"%s\"",value); break; case GRIB_TYPE_DOUBLE: ret=grib_get_double( h,options->print_keys[i].name,&dvalue); printf("%g",dvalue); break; case GRIB_TYPE_LONG: ret=grib_get_long( h,options->print_keys[i].name,&lvalue); printf("%ld",lvalue); break; default: printf("invalid_type"); break; } } if (ret == GRIB_NOT_FOUND) printf("null"); s=", "; } printf("}"); }
static void header(grib_dumper* d,grib_handle* h) { long edition=0; int ret=0; grib_dumper_c_code *self = (grib_dumper_c_code*)d; ret=grib_get_long(h,"editionNumber",&edition); if (ret != GRIB_SUCCESS) { grib_context_log(h->context,GRIB_LOG_ERROR,"Unable to get edition number."); Assert(0); } fprintf(self->dumper.out, "#include <grib_api.h>\n" "\n" "/* This code was generated automatically */\n" "\n"); fprintf(self->dumper.out, "\n" "int main(int argc,const char** argv)\n" "{\n" " grib_handle *h = NULL;\n" " size_t size = 0;\n" " double* vdouble = NULL;\n" " long* vlong = NULL;\n" " FILE* f = NULL;\n" " const char* p = NULL;\n" " const void* buffer = NULL;\n" "\n" " if(argc != 2) {\n" " fprintf(stderr,\"usage: %%s out\\n\",argv[0]);\n" " exit(1);\n" " }\n" "\n" " h = grib_handle_new_from_samples(NULL,\"GRIB%ld\");\n" " if(!h) {\n" " fprintf(stderr,\"Cannot create grib handle\\n\");\n" " exit(1);\n" " }\n" "\n",(long)edition ); }
static int unpack_long(grib_accessor* a, long* val, size_t *len) { grib_accessor_g2_mars_labeling* self = (grib_accessor_g2_mars_labeling*)a; char* key=NULL; switch (self->index) { case 0: key=(char*)self->the_class; break; case 1: key=(char*)self->type; break; case 2: key=(char*)self->stream; break; default : grib_context_log(a->parent->h->context,GRIB_LOG_ERROR, "invalid first argument of g2_mars_labeling in %s",a->name); return GRIB_INTERNAL_ERROR; break; } return grib_get_long(a->parent->h, key,val); }
static int extra_set(grib_accessor* a,long val) { /*TODO chemicals*/ int ret=0; grib_accessor_g2_mars_labeling* self = (grib_accessor_g2_mars_labeling*)a; char stepType[30]={0,}; size_t stepTypelen=30; long derivedForecast=-1; long productDefinitionTemplateNumberNew=-1; long productDefinitionTemplateNumber; long typeOfProcessedData=-1; long typeOfGeneratingProcess=-1; switch (self->index) { case 0: /* class */ return ret; break; case 1: /* type */ switch (val) { case 0: /* Unknown (0) */ typeOfProcessedData=255; typeOfGeneratingProcess=255; break; case 1: /* First guess (fg) */ case 3: /* Initialised analysis (ia) */ typeOfProcessedData=0; typeOfGeneratingProcess=1; break; case 2: /* Analysis (an) */ case 4: /* Oi analysis (oi) */ case 5: /* 3d variational analysis (3v) */ case 6: /* 4d variational analysis (4v) */ case 7: /* 3d variational gradients (3g) */ case 8: /* 4d variational gradients (4g) */ typeOfProcessedData=0; typeOfGeneratingProcess=0; break; case 9: /* Forecast (fc) */ typeOfProcessedData=1; typeOfGeneratingProcess=2; break; case 10: /* Control forecast (cf) */ typeOfProcessedData=3; typeOfGeneratingProcess=4; break; case 11: /* Perturbed forecast (pf) */ typeOfProcessedData=4; typeOfGeneratingProcess=4; break; case 12: /* Errors in first guess (ef) */ case 13: /* Errors in analysis (ea) */ typeOfProcessedData=255; typeOfGeneratingProcess=7; break; case 14: /* Cluster means (cm) */ case 15: /* Cluster std deviations (cs) */ typeOfProcessedData=255; typeOfGeneratingProcess=4; break; case 16: /* Forecast probability (fp) */ typeOfProcessedData=8; typeOfGeneratingProcess=5; break; case 17: /* Ensemble mean (em) */ derivedForecast=0; grib_get_string(a->parent->h,self->stepType,stepType,&stepTypelen); if (!strcmp(stepType,"instant")) { productDefinitionTemplateNumberNew=2; } else { productDefinitionTemplateNumberNew=12; } typeOfProcessedData=255; typeOfGeneratingProcess=4; break; case 18: /* Ensemble standard deviation (es) */ derivedForecast=4; grib_get_string(a->parent->h,self->stepType,stepType,&stepTypelen); if (!strcmp(stepType,"instant")) { productDefinitionTemplateNumberNew=2; } else { productDefinitionTemplateNumberNew=12; } typeOfProcessedData=255; typeOfGeneratingProcess=4; break; case 19: /* Forecast accumulation (fa) */ case 20: /* Climatology (cl) */ case 21: /* Climate simulation (si) */ case 22: /* Climate 30 days simulation (s3) */ case 23: /* Empirical distribution (ed) */ case 24: /* Tubes (tu) */ case 25: /* Flux forcing realtime (ff) */ case 26: /* Ocean forward (of) */ case 27: /* Extreme forecast index (efi) */ case 28: /* Extreme forecast index control (efic)*/ case 29: /* Probability boundaries (pb) */ typeOfProcessedData=255; typeOfGeneratingProcess=255; break; case 30: /* Event probability (ep) */ typeOfProcessedData=8; typeOfGeneratingProcess=5; break; case 31: /* Bias-corrected Forecast (bf) */ typeOfProcessedData=1; typeOfGeneratingProcess=3; break; case 32: /* Climate distribution (cd) */ case 33: /* 4D analysis increments (4i) */ case 34: /* Gridded observations (go) */ case 35: /* Model errors (me) */ case 36: /* Probability distribution (pd) */ case 37: /* Cluster information (ci) */ case 38: /* Shift of Tail (sot) */ case 40: /* Images (im) */ case 42: /* Simulated images (sim) */ typeOfProcessedData=255; typeOfGeneratingProcess=255; break; case 43: /* Weighted ensemble mean (wem) */ case 44: /* Weighted ensemble standard deviation (wes) */ case 45: /* Cluster representative (cr) */ case 46: /* Scaled ensemble standard deviation (ses) */ case 47: /* Time average ensemble mean (taem) */ case 48: /* Time average ensemble standard deviation (taes) */ typeOfProcessedData=255; typeOfGeneratingProcess=4; break; case 50: /* Sensitivity gradient (sg) */ case 52: /* Sensitivity forecast (sf) */ case 60: /* Perturbed analysis (pa) */ case 61: /* Initial condition perturbation (icp) */ case 62: /* Singular vector (sv) */ case 63: /* Adjoint singular vector (as) */ case 64: /* Signal variance (svar) */ typeOfProcessedData=255; typeOfGeneratingProcess=255; break; case 65: /* Calibration/Validation forecast (cv) */ typeOfProcessedData=5; typeOfGeneratingProcess=4; break; case 70: /* Ocean reanalysis (or) */ case 71: /* Flux forcing (fx) */ case 80: /* Forecast mean (fcmean) */ case 81: /* Forecast maximum (fcmax) */ case 82: /* Forecast minimum (fcmin) */ case 83: /* Forecast standard deviation (fcstdev) */ case 88: /* Gridded satellite data */ case 89: /* GFAS analysis */ typeOfProcessedData=255; typeOfGeneratingProcess=255; break; case 84: /* Ensemble mean of temporal mean (emtm) */ case 85: /* Ensemble standard deviation of temporal mean (estdtm) */ typeOfProcessedData=255; typeOfGeneratingProcess=4; break; default : grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,"unknown mars.type %d",(int)val); return GRIB_ENCODING_ERROR; } case 2: /* stream */ switch (val) { case 1030: /* enda */ case 1249: /* elda */ case 1250: /* ewla */ grib_get_string(a->parent->h,self->stepType,stepType,&stepTypelen); if (!strcmp(stepType,"instant")) { productDefinitionTemplateNumberNew=1; } else { productDefinitionTemplateNumberNew=11; } break; } break; default : grib_context_log(a->parent->h->context,GRIB_LOG_ERROR, "invalid first argument of g2_mars_labeling in %s",a->name); return GRIB_INTERNAL_ERROR; break; } if (productDefinitionTemplateNumberNew>=0) { grib_get_long(a->parent->h,self->productDefinitionTemplateNumber,&productDefinitionTemplateNumber); if (productDefinitionTemplateNumber!=productDefinitionTemplateNumberNew) grib_set_long(a->parent->h,self->productDefinitionTemplateNumber,productDefinitionTemplateNumberNew); } if (derivedForecast>=0) { grib_set_long(a->parent->h,self->derivedForecast,derivedForecast); } if (typeOfProcessedData>0) grib_set_long(a->parent->h,self->typeOfProcessedData,typeOfProcessedData); if (typeOfGeneratingProcess>0) grib_set_long(a->parent->h,self->typeOfGeneratingProcess,typeOfGeneratingProcess); return ret; }
static int pack_double(grib_accessor* a, const double* cval, size_t *len) { grib_accessor_data_g1simple_packing* self = (grib_accessor_data_g1simple_packing*)a; grib_accessor_class* super = *(a->cclass->super); size_t n_vals = *len; long half_byte = 0; int ret = 0; long offsetdata = 0; long offsetsection = 0; double reference_value = 0; long binary_scale_factor = 0; long bits_per_value = 0; long decimal_scale_factor = 0; double decimal = 1; size_t buflen = 0; unsigned char* buf = NULL; unsigned char* encoded = NULL; double divisor = 1; int i; long off = 0; grib_context* c=a->parent->h->context; grib_handle* h=a->parent->h; char* ieee_packing_s=NULL; char* packingType_s=NULL; char* precision_s=NULL; double units_factor=1.0; double units_bias=0.0; double* val=(double*)cval; double missingValue=9999.0; long constantFieldHalfByte=0; int err=0; if(*len != 0) { if(self->units_factor && (grib_get_double_internal(a->parent->h,self->units_factor,&units_factor)== GRIB_SUCCESS)) { grib_set_double_internal(a->parent->h,self->units_factor,1.0); } if(self->units_bias && (grib_get_double_internal(a->parent->h,self->units_bias,&units_bias)== GRIB_SUCCESS)) { grib_set_double_internal(a->parent->h,self->units_bias,0.0); } if (units_factor != 1.0) { if (units_bias != 0.0) for (i=0;i<n_vals;i++) val[i]=val[i]*units_factor+units_bias; else for (i=0;i<n_vals;i++) val[i]*=units_factor; } else if (units_bias != 0.0) for (i=0;i<n_vals;i++) val[i]+=units_bias; if (c->ieee_packing && self->ieee_packing) { long precision=c->ieee_packing==32 ? 1 : 2; size_t lenstr=strlen(self->ieee_packing); packingType_s=grib_context_strdup(c,self->packingType); ieee_packing_s=grib_context_strdup(c,self->ieee_packing); precision_s=grib_context_strdup(c,self->precision); grib_set_string(h,packingType_s,ieee_packing_s,&lenstr); grib_set_long(h,precision_s,precision); grib_context_free(c,packingType_s); grib_context_free(c,ieee_packing_s); grib_context_free(c,precision_s); return grib_set_double_array(h,"values",val,*len); } } ret = super->pack_double(a,val,len); switch (ret) { case GRIB_CONSTANT_FIELD: ret=grib_get_long(a->parent->h,"constantFieldHalfByte",&constantFieldHalfByte); if (ret) constantFieldHalfByte=0; if((ret = grib_set_long_internal(a->parent->h,self->half_byte, constantFieldHalfByte)) != GRIB_SUCCESS) return ret; grib_buffer_replace(a, NULL, 0,1,1); return GRIB_SUCCESS; break; case GRIB_NO_VALUES: ret=grib_get_long(a->parent->h,"constantFieldHalfByte",&constantFieldHalfByte); if (ret) constantFieldHalfByte=0; /* TODO move to def file */ grib_get_double(a->parent->h,"missingValue", &missingValue); if((err = grib_set_double_internal(a->parent->h,self->reference_value, missingValue)) != GRIB_SUCCESS) return err; if((ret = grib_set_long_internal(a->parent->h,self->binary_scale_factor, binary_scale_factor)) != GRIB_SUCCESS) return ret; if((ret = grib_set_long_internal(a->parent->h,self->half_byte, constantFieldHalfByte)) != GRIB_SUCCESS) return ret; grib_buffer_replace(a, NULL, 0,1,1); return GRIB_SUCCESS; break; case GRIB_INVALID_BPV: grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,"unable to compute packing parameters\n"); return ret; case GRIB_SUCCESS: break; default: grib_context_log(a->parent->h->context,GRIB_LOG_FATAL,"unable to compute packing parameters\n"); return ret; } if((ret = grib_get_double_internal(a->parent->h,self->reference_value, &reference_value)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->binary_scale_factor, &binary_scale_factor)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->bits_per_value,&bits_per_value)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->decimal_scale_factor, &decimal_scale_factor)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->offsetdata,&offsetdata)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->offsetsection,&offsetsection)) != GRIB_SUCCESS) return ret; decimal = grib_power(decimal_scale_factor,10) ; divisor = grib_power(-binary_scale_factor,2); buflen = (((bits_per_value*n_vals)+7)/8)*sizeof(unsigned char); if((buflen + (offsetdata-offsetsection)) %2) { buflen++; /* a->length++; a->parent->h->buffer->ulength++; */ } half_byte = (buflen*8)-((*len)*bits_per_value); grib_context_log(a->parent->h->context,GRIB_LOG_DEBUG, "HALF byte: buflen=%d bits_per_value=%ld len=%d half_byte=%ld\n", buflen,bits_per_value,*len,half_byte); Assert(half_byte <= 0x0f); if((ret = grib_set_long_internal(a->parent->h,self->half_byte, half_byte)) != GRIB_SUCCESS) return ret; buf = (unsigned char*)grib_context_buffer_malloc_clear(a->parent->h->context,buflen); encoded = buf; grib_encode_double_array(n_vals,val,bits_per_value,reference_value,decimal,divisor,encoded,&off); grib_context_log(a->parent->h->context, GRIB_LOG_DEBUG, "grib_accessor_data_g1simple_packing : pack_double : packing %s, %d values", a->name, n_vals); grib_buffer_replace(a, buf, buflen,1,1); grib_context_buffer_free(a->parent->h->context,buf); return GRIB_SUCCESS; }
int codes_get_long(grib_handle* h, const char* key, long* value) { return grib_get_long(h,key,value); }
static int find(grib_nearest* nearest, grib_handle* h, double inlat, double inlon,unsigned long flags, double* outlats,double* outlons, double *values, double *distances,int *indexes, size_t *len) { grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*) nearest; int ret=0,kk=0,ii=0,jj=0; int j=0; long* pla=NULL; long* pl=NULL; size_t nvalues=0; grib_iterator* iter=NULL; double lat=0,lon=0; long iradius; double radius; int ilat=0,ilon=0; if( (ret = grib_get_size(h,self->values_key,&nvalues))!= GRIB_SUCCESS) return ret; nearest->values_count = nvalues; if (grib_is_missing(h,self->radius,&ret)) { grib_context_log(h->context, GRIB_LOG_DEBUG,"Key '%s' is missing", self->radius); return ret ? ret : GRIB_GEOCALCULUS_PROBLEM; } if( (ret = grib_get_long(h,self->radius,&iradius))!= GRIB_SUCCESS) return ret; radius=((double)iradius)/1000.0; if (!nearest->h || (flags & GRIB_NEAREST_SAME_GRID)==0) { double dummy=0; double olat=1.e10; long n=0; ilat=0,ilon=0; if (grib_is_missing(h,self->Nj,&ret)) { grib_context_log(h->context, GRIB_LOG_DEBUG,"Key '%s' is missing", self->Nj); return ret ? ret : GRIB_GEOCALCULUS_PROBLEM; } if( (ret = grib_get_long(h,self->Nj,&n))!= GRIB_SUCCESS) return ret; self->lats_count=n; if (self->lats) grib_context_free(nearest->context,self->lats); self->lats=(double*)grib_context_malloc( nearest->context, self->lats_count* sizeof(double)); if (!self->lats) return GRIB_OUT_OF_MEMORY; if (self->lons) grib_context_free(nearest->context,self->lons); self->lons=(double*)grib_context_malloc( nearest->context, nearest->values_count*sizeof(double)); if (!self->lons) return GRIB_OUT_OF_MEMORY; iter=grib_iterator_new(h,0,&ret); if (ret) { grib_context_log(h->context,GRIB_LOG_ERROR,"unable to create iterator"); return ret; } while(grib_iterator_next(iter,&lat,&lon,&dummy)) { if (olat!=lat) { self->lats[ilat++]=lat; olat=lat; } self->lons[ilon++]=lon; } self->lats_count=ilat; grib_iterator_delete(iter); } nearest->h=h; if (!self->distances || (flags & GRIB_NEAREST_SAME_POINT)==0 || (flags & GRIB_NEAREST_SAME_GRID)==0) { double* lons=NULL; int nlon=0; size_t plsize=0; long nplm1=0; int nearest_lons_found=0; double lon_first,lon_last; int islocal=0; long plmax; double dimin; if ((ret=grib_get_double(h,self->lonFirst,&lon_first))!=GRIB_SUCCESS) { grib_context_log(h->context,GRIB_LOG_ERROR, "grib_nearest_latlon_reduced.find(): unable to get %s %s\n",self->lonFirst, grib_get_error_message(ret)); return ret; } if ((ret=grib_get_double(h,self->lonLast,&lon_last))!=GRIB_SUCCESS) { grib_context_log(h->context,GRIB_LOG_ERROR, "grib_nearest_latlon_reduced.find(): unable to get %s %s\n",self->lonLast, grib_get_error_message(ret)); return ret; } plsize=self->lats_count; if( (ret=grib_get_size(h,self->pl,&plsize))!= GRIB_SUCCESS) return ret; pla=(long*)grib_context_malloc(h->context,plsize*sizeof(long)); if (!pla) return GRIB_OUT_OF_MEMORY; if( (ret=grib_get_long_array(h,self->pl,pla,&plsize))!= GRIB_SUCCESS) return ret; pl=pla; while ((*pl)==0) {pl++;} plmax=pla[0]; for (j=0;j<plsize;j++) if (plmax<pla[j]) plmax=pla[j]; dimin=360.0/plmax; if ( 360-fabs(lon_last-lon_first) < 2 * dimin ) {islocal=0;} else {islocal=1;} if (islocal) for (j=0;j<plsize;j++) pla[j]--; /* printf("XXXX islocal=%d\n",islocal); */ while (inlon<0) inlon+=360; while (inlon>360) inlon-=360; ilat=self->lats_count; if (self->lats[ilat-1] > self->lats[0]) { if (inlat < self->lats[0] || inlat > self->lats[ilat-1]) return GRIB_OUT_OF_AREA; } else { if (inlat > self->lats[0] || inlat < self->lats[ilat-1]) return GRIB_OUT_OF_AREA; } if (!self->distances) self->distances=(double*)grib_context_malloc( nearest->context,4*sizeof(double)); if (!self->distances) return GRIB_OUT_OF_MEMORY; grib_binary_search(self->lats,ilat-1,inlat, &(self->j[0]),&(self->j[1])); nlon=0; for (jj=0;jj<self->j[0];jj++) nlon+=pl[jj]; nplm1=pl[self->j[0]]-1; lons=self->lons+nlon; nearest_lons_found=0; if (lons[nplm1]>lons[0]) { if (inlon< lons[0] || inlon > lons[nplm1]) { if (lons[nplm1]-lons[0]-360 <= lons[nplm1]-lons[nplm1-1]) { self->k[0]=0; self->k[1]=nplm1; nearest_lons_found=1; } else return GRIB_OUT_OF_AREA; } } else { if (inlon >lons[0] || inlon< lons[nplm1]) { if (lons[0]-lons[nplm1]-360 <= lons[0]-lons[1]) { self->k[0]=0; self->k[1]=nplm1; nearest_lons_found=1; } else return GRIB_OUT_OF_AREA; } } if (!nearest_lons_found) { grib_binary_search(lons,pl[self->j[0]]-1,inlon, &(self->k[0]),&(self->k[1])); } self->k[0]+=nlon; self->k[1]+=nlon; nlon=0; for (jj=0;jj<self->j[1];jj++) nlon+=pl[jj]; nplm1=pl[self->j[1]]-1; lons=self->lons+nlon; nearest_lons_found=0; if (lons[nplm1]>lons[0]) { if (inlon<lons[0] || inlon>lons[nplm1]) { if (lons[nplm1]-lons[0]-360 <= lons[nplm1]-lons[nplm1-1]) { self->k[2]=0; self->k[3]=nplm1; nearest_lons_found=1; } else return GRIB_OUT_OF_AREA; } } else { if (inlon>lons[0] || inlon<lons[nplm1]) { if (lons[0]-lons[nplm1]-360 <= lons[0]-lons[1]) { self->k[2]=0; self->k[3]=nplm1; nearest_lons_found=1; } else return GRIB_OUT_OF_AREA; } } if (!nearest_lons_found) { grib_binary_search(lons,pl[self->j[1]]-1,inlon, &(self->k[2]),&(self->k[3])); } self->k[2]+=nlon; self->k[3]+=nlon; kk=0; for (jj=0;jj<2;jj++) { for (ii=0;ii<2;ii++) { self->distances[kk]=grib_nearest_distance(radius,inlon,inlat, self->lons[self->k[kk]],self->lats[self->j[jj]]); kk++; } } grib_context_free(h->context,pla); } kk=0; for (jj=0;jj<2;jj++) { for (ii=0;ii<2;ii++) { distances[kk]=self->distances[kk]; outlats[kk]=self->lats[self->j[jj]]; outlons[kk]=self->lons[self->k[kk]]; grib_get_double_element_internal(h,self->values_key,self->k[kk],&(values[kk])); indexes[kk]=self->k[kk]; kk++; } } return GRIB_SUCCESS; }
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h) { int err=0; double *lat=0,*lon=0,*val=0; double missing_value=9999.; int skip_missing=1; char *kmiss=NULL, *p=NULL; char *missing_string=NULL; int i=0; grib_values* values=NULL; grib_iterator* iter = NULL; char* format=NULL; char* default_format="%.10e"; int print_keys=grib_options_on("p:"); long numberOfPoints=0; double *data_values=0,*lats=0,*lons=0; int n = 0; size_t size=0; if (grib_options_on("F:")) format=grib_options_get_option("F:"); else format=default_format; if ((err=grib_get_long(h,"numberOfPoints",&numberOfPoints)) !=GRIB_SUCCESS) { fprintf(dump_file,"ERROR: unable to get number of points\n"); return err; } iter=grib_iterator_new(h,0,&err); data_values=(double*)calloc(numberOfPoints+1,sizeof(double)); if (iter) { lats=(double*)calloc(numberOfPoints+1,sizeof(double)); lons=(double*)calloc(numberOfPoints+1,sizeof(double)); lat=lats; lon=lons; val=data_values; while(grib_iterator_next(iter,lat++,lon++,val++)) {} } else if (err==GRIB_NOT_IMPLEMENTED || err==GRIB_SUCCESS){ size=numberOfPoints; grib_get_double_array(h,"values",data_values,&size); if (size!=numberOfPoints) { if (!grib_options_on("q")) fprintf(dump_file,"ERROR: wrong number of points %d\n",(int)numberOfPoints); if (grib_options_on("f")) exit(1); } } else { grib_context_log(h->context,GRIB_LOG_ERROR, "%s",grib_get_error_message(err)); exit(err); } skip_missing=1; if (grib_options_on("m:")) { char* end=0; double mval=0; skip_missing=0; kmiss=grib_options_get_option("m:"); p=kmiss; while (*p != ':' && *p != '\0') p++; if (*p == ':' && *(p+1) != '\0') { *p='\0'; missing_string=strdup(p+1); } else { missing_string=strdup(kmiss); } mval=strtod(kmiss,&end); if (end==NULL) missing_value=mval; grib_set_double(h,"missingValue",missing_value); } if (iter) fprintf(dump_file,"Latitude, Longitude, "); fprintf(dump_file,"Value"); if (print_keys) for (i=0;i<options->print_keys_count; i++) fprintf(dump_file,", %s",options->print_keys[i].name); fprintf(dump_file,"\n"); if (print_keys) values=get_key_values(options,h); if (skip_missing==0){ for (i=0;i<numberOfPoints;i++) { if (iter) fprintf(dump_file,"%9.3f%9.3f ",lats[i],lons[i]); if (data_values[i] == missing_value) fprintf(dump_file,"%s",missing_string); else fprintf(dump_file,format,data_values[i]); if (print_keys) print_key_values(values,options->print_keys_count); fprintf(dump_file,"\n"); n++; } } else if ( skip_missing==1 ){ for (i=0;i<numberOfPoints;i++) { if (data_values[i] != missing_value){ if (iter) fprintf(dump_file,"%9.3f%9.3f ",lats[i],lons[i]); fprintf(dump_file,format,data_values[i]); if (print_keys) print_key_values(values,options->print_keys_count); fprintf(dump_file,"\n"); n++; } } } if (iter) grib_iterator_delete(iter); free(data_values); if (iter) { free(lats); free(lons); } return 0; }