int main(int argc, char* argv[]) { FILE* f; int err=0; char infile[]="../../data/reduced_gaussian_model_level.grib1"; grib_handle *h=NULL; grib_context* c=grib_context_get_default(); grib_values values[2]; int nvalues=2; int i; char* name = NULL; f=fopen(infile,"r"); if (!f) { perror(infile); exit(1); } h=grib_handle_new_from_file(c,f,&err); if (!h) { printf("unable to create handle from file %s\n",infile); exit(err); } fclose(f); values[0].type=GRIB_TYPE_LONG; values[0].name="centre"; values[0].long_value=98; values[1].type=GRIB_TYPE_LONG; values[1].name="level"; values[1].long_value=2; /*GRIB_VALUE_DIFFERENT -> value is different*/ err=grib_values_check(h,values,nvalues); if (err) { for (i=0;i<nvalues;i++) { if (values[i].error==err) name=(char*)values[i].name; } printf("ERROR: \"%s\" %s\n",name,grib_get_error_message(err)); } values[1].name="levelll"; err=grib_values_check(h,values,nvalues); if (err) { for (i=0;i<nvalues;i++) { if (values[i].error==err) name=(char*)values[i].name; } printf("ERROR: \"%s\" %s\n",name,grib_get_error_message(err)); } return 0; }
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 void dump_string(grib_dumper* d,grib_accessor* a,const char* comment) { grib_dumper_debug *self = (grib_dumper_debug*)d; char value[1024]; size_t size = sizeof(value); int err = grib_unpack_string(a,value,&size); int i; char *p = value; if(err) strcpy(value,"<error>"); if( a->length == 0 && (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0) return; set_begin_end(d,a); while(*p) { if(!isprint(*p)) *p = '.'; p++; } for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," "); fprintf(self->dumper.out,"%ld-%ld %s %s = %s",self->begin,self->end,a->creator->op, a->name,value); if(comment) fprintf(self->dumper.out," [%s]",comment); if(err) fprintf(self->dumper.out," *** ERR=%d (%s)",err,grib_get_error_message(err)); aliases(d,a); fprintf(self->dumper.out,"\n"); }
static void dump_double(grib_dumper* d,grib_accessor* a,const char* comment) { grib_dumper_wmo *self = (grib_dumper_wmo*)d; double value; size_t size = 1; int err = grib_unpack_double(a,&value,&size); if( a->length == 0 && (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0) return; set_begin_end(d,a); /*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/ print_offset(self->dumper.out,self->begin,self->theEnd); if ((d->option_flags & GRIB_DUMP_FLAG_TYPE) != 0) fprintf(self->dumper.out,"%s ",a->creator->op); if( ((a->flags & GRIB_ACCESSOR_FLAG_CAN_BE_MISSING) != 0) && grib_is_missing_internal(a)) fprintf(self->dumper.out,"%s = MISSING",a->name); else fprintf(self->dumper.out,"%s = %g",a->name,value); /*if(comment) fprintf(self->dumper.out," [%s]",comment);*/ if (err==0) print_hexadecimal(self->dumper.out,d->option_flags,a); if(err) fprintf(self->dumper.out," *** ERR=%d (%s) [grib_dumper_wmo::dump_double]",err,grib_get_error_message(err)); aliases(d,a); fprintf(self->dumper.out,"\n"); }
void validate(const char* path) { FILE *f = fopen(path,"r"); grib_handle *h = 0; int err; int count = 0; if(!f) { fprintf(stderr,"%s: %s\n",path,strerror(errno)); exit(1); } while( (h= grib_handle_new_from_file(0,f,&err)) != NULL) { ++field; split(h); grib_handle_delete(h); count++; param = "unknown"; } fclose(f); if(err) { fprintf(stderr,"%s: grib_handle_new_from_file: %s\n",path,grib_get_error_message(err)); exit(1); } if(count == 0) { fprintf(stderr,"%s does not contain any GRIBs\n",path); exit(1); } }
static void dump_bits(grib_dumper* d,grib_accessor* a,const char* comment) { grib_dumper_debug *self = (grib_dumper_debug*)d; long value; size_t size = 1; int err = grib_unpack_long(a,&value,&size); int i; if( a->length == 0 && (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0) return; set_begin_end(d,a); for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," "); fprintf(self->dumper.out,"%ld-%ld %s %s = %ld [",self->begin,self->end, a->creator->op,a->name,value); for(i=0;i<(a->length*8);i++) { if(test_bit(value,a->length*8-i-1)) fprintf(self->dumper.out,"1"); else fprintf(self->dumper.out,"0"); } if(comment) fprintf(self->dumper.out,":%s]",comment); else fprintf(self->dumper.out,"]"); if(err) fprintf(self->dumper.out," *** ERR=%d (%s)",err,grib_get_error_message(err)); aliases(d,a); fprintf(self->dumper.out,"\n"); }
static void dump_double(grib_dumper* d,grib_accessor* a,const char* comment) { grib_dumper_debug *self = (grib_dumper_debug*)d; double value; size_t size = 1; int err = grib_unpack_double(a,&value,&size); int i; if( a->length == 0 && (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0) return; set_begin_end(d,a); for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," "); if( ((a->flags & GRIB_ACCESSOR_FLAG_CAN_BE_MISSING) != 0) && grib_is_missing_internal(a)) fprintf(self->dumper.out,"%ld-%ld %s %s = MISSING",self->begin,self->end,a->creator->op, a->name); else fprintf(self->dumper.out,"%ld-%ld %s %s = %g",self->begin,self->end,a->creator->op, a->name,value); if(comment) fprintf(self->dumper.out," [%s]",comment); if(err) fprintf(self->dumper.out," *** ERR=%d (%s)",err,grib_get_error_message(err)); aliases(d,a); fprintf(self->dumper.out,"\n"); }
char* sget(grib_handle *h,const char* what,char* val,size_t size) { int e; if((e = grib_get_string(h,what,val,&size)) != GRIB_SUCCESS) { fprintf(stderr,"%s, field %d [%s]: cannot get %s: %s\n",file,field,param,what,grib_get_error_message(e)); exit(1); } return val; }
static int native_type(grib_expression* g,grib_handle *h) { grib_expression_accessor* e = (grib_expression_accessor*)g; int type = 0; int err; if((err=grib_get_native_type(h,e->name,&type)) != GRIB_SUCCESS) grib_context_log(h->context, GRIB_LOG_ERROR, "Error in native_type %s : %s", e->name,grib_get_error_message(err)); return type; }
long get(grib_handle *h,const char* what) { int e; long val; if((e = grib_get_long(h,what,&val)) != GRIB_SUCCESS) { printf("%s, field %d [%s]: cannot get %s: %s\n",file,field,param,what,grib_get_error_message(e)); error++; val = -1; } return val; }
double dget(grib_handle *h,const char* what) { int e; double val; if((e = grib_get_double(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 execute(grib_action* a, grib_handle *h) { int ret=0; grib_action_set* self = (grib_action_set*) a; ret=grib_set_expression(h,self->name,self->expression); if (self->nofail) return 0; if (ret != GRIB_SUCCESS) { grib_context_log(h->context,GRIB_LOG_ERROR,"Error while setting key %s (%s)", self->name,grib_get_error_message(ret)); } 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 int create_accessor(grib_section* p, grib_action* act, grib_loader *h ){ int ret = GRIB_SUCCESS; grib_action_template* a = ( grib_action_template*)act; grib_action* la = NULL; grib_action* next = NULL; grib_accessor* as = NULL; grib_section* gs = NULL; char fname[1024]={0,}; char *fpath=0; as = grib_accessor_factory(p, act,0,NULL); if(!as) return GRIB_INTERNAL_ERROR; if(a->arg){ ret = grib_recompose_name(p->h,as,a->arg,fname,1); if ((fpath=grib_context_full_defs_path(p->h->context,fname))==NULL) { if (!a->nofail) { grib_context_log(p->h->context,GRIB_LOG_ERROR, "Unable to find template %s from %s ",act->name,fname); return GRIB_FILE_NOT_FOUND; } la = get_empty_template(p->h->context,&ret); if (ret) return ret; } else la = grib_parse_file(p->h->context, fpath); } as->flags |= GRIB_ACCESSOR_FLAG_HIDDEN; gs = as->sub_section; gs->branch = la; /* Will be used to prevent unecessary reparse */ grib_push_accessor(as,p->block); if(la){ next = la; while(next){ ret = grib_create_accessor(gs, next,h); if(ret != GRIB_SUCCESS) { if(p->h->context->debug) { grib_context_log(p->h->context,GRIB_LOG_ERROR, "Error processing template %s: %s [%s] %04lx", fname,grib_get_error_message(ret),next->name,next->flags); } return ret; } next= next->next; } } return GRIB_SUCCESS; }
static void dump_bytes(grib_dumper* d,grib_accessor* a,const char* comment) { grib_dumper_c_code *self = (grib_dumper_c_code*)d; int err =0; size_t size = a->length; unsigned char* buf; if (a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) return; if(size == 0) return; buf = grib_context_malloc(d->handle->context,size); if(!buf) { fprintf(self->dumper.out,"/* %s: cannot malloc(%ld) */\n",a->name,(long)size); return; } err = grib_unpack_bytes(a,buf,&size); if(err){ grib_context_free(d->handle->context,buf); fprintf(self->dumper.out," *** ERR=%d (%s) \n}",err,grib_get_error_message(err)); return ; } #if 0 if(size > 100) { more = size - 100; size = 100; } k = 0; /* if(size > 100) size = 100; */ while(k < size) { int j; for(i = 0; i < d->depth + 3 ; i++) fprintf(self->dumper.out," "); for(j = 0; j < 16 && k < size; j++, k++) { fprintf(self->dumper.out,"%02x",buf[k]); if(k != size-1) fprintf(self->dumper.out,", "); } fprintf(self->dumper.out,"\n"); } #endif grib_context_free(d->handle->context,buf); }
void validate(const char* path) { FILE *f = fopen(path,"r"); grib_handle *h = 0; int err; int count = 0; const char* base = path; const char* p = path; file = path; field = 0; while(*p) { if(*p == '/') base = p+1; p++; } if(!f) { printf("%s: %s\n",path,strerror(errno)); error++; return; } while( (h= grib_handle_new_from_file(0,f,&err)) != NULL) { ++field; verify(h,path,base); grib_handle_delete(h); count++; param = "unknown"; } fclose(f); if(err) { printf("%s: grib_handle_new_from_file: %s\n",path,grib_get_error_message(err)); error++; return; } if(count == 0) { printf("%s does not contain any GRIBs\n",path); error++; return; } }
static grib_action* reparse(grib_action* a,grib_accessor* acc,int* doit) { int ret=0; long lres=0; grib_action_if* self = (grib_action_if*)a; /* printf("reparse %s %s\n",a->name,acc->name); */ if((ret=grib_expression_evaluate_long(acc->parent->h,self->expression,&lres)) != GRIB_SUCCESS) grib_context_log(acc->parent->h->context, GRIB_LOG_ERROR,"if reparse grib_expression_evaluate_long %s", grib_get_error_message(ret)); if(lres) return self->block_true; else return self->block_false; }
static void dump_string(grib_dumper* d,grib_accessor* a,const char* comment) { grib_dumper_wmo *self = (grib_dumper_wmo*)d; size_t size=0; char *value=NULL; char *p=NULL; int err = grib_get_string_length(a->parent->h,a->name,&size); value=(char *)grib_context_malloc_clear(a->parent->h->context,size); if (!value) { grib_context_log(a->parent->h->context,GRIB_LOG_FATAL,"unable to allocate %d bytes",(int)size); return; } err=grib_unpack_string(a,value,&size); p=value; if( a->length == 0 && (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0) { grib_context_free(a->parent->h->context,value); return; } set_begin_end(d,a); while(*p) { if(!isprint(*p)) *p = '.'; p++; } /*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/ print_offset(self->dumper.out,self->begin,self->theEnd); if ((d->option_flags & GRIB_DUMP_FLAG_TYPE) != 0) fprintf(self->dumper.out,"%s ",a->creator->op); fprintf(self->dumper.out,"%s = %s",a->name,value); if (err==0) print_hexadecimal(self->dumper.out,d->option_flags,a); /*if(comment) fprintf(self->dumper.out," [%s]",comment);*/ if(err) fprintf(self->dumper.out," *** ERR=%d (%s) [grib_dumper_wmo::dump_string]",err,grib_get_error_message(err)); aliases(d,a); fprintf(self->dumper.out,"\n"); if (value) grib_context_free(a->parent->h->context,value); }
static void dump_double(grib_dumper* d,grib_accessor* a,const char* comment) { grib_dumper_c_code *self = (grib_dumper_c_code*)d; double value; size_t size = 1; int err = grib_unpack_double(a,&value,&size); if(a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) return; if(a->length == 0) return; #if 0 if(comment) fprintf(self->dumper.out,"/* %s */\n",comment); #endif fprintf(self->dumper.out," GRIB_CHECK(grib_set_double(h,\"%s\",%g),%d);\n",a->name,value,0); if(err) fprintf(self->dumper.out," /* Error accessing %s (%s) */",a->name,grib_get_error_message(err)); }
static void dump_bits(grib_dumper* d,grib_accessor* a,const char* comment) { grib_dumper_wmo *self = (grib_dumper_wmo*)d; int i; long value = 0; size_t size = 1; int err = grib_unpack_long(a,&value,&size); if( a->length == 0 && (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0) return; set_begin_end(d,a); /*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/ print_offset(self->dumper.out,self->begin,self->theEnd); if ((d->option_flags & GRIB_DUMP_FLAG_TYPE) != 0) fprintf(self->dumper.out,"%s ",a->creator->op); fprintf(self->dumper.out,"%s = %ld [",a->name,value); for(i=0;i<(a->length*8);i++) { if(test_bit(value,a->length*8-i-1)) fprintf(self->dumper.out,"1"); else fprintf(self->dumper.out,"0"); } /* if(comment) fprintf(self->dumper.out,":%s]",comment); else */ fprintf(self->dumper.out,"]"); if (err==0) print_hexadecimal(self->dumper.out,d->option_flags,a); if(err) fprintf(self->dumper.out," *** ERR=%d (%s) [grib_dumper_wmo::dump_bits]",err,grib_get_error_message(err)); aliases(d,a); fprintf(self->dumper.out,"\n"); }
static void dump_bits(grib_dumper* d,grib_accessor* a,const char* comment) { grib_dumper_c_code *self = (grib_dumper_c_code*)d; long value; size_t size = 1; int err = grib_unpack_long(a,&value,&size); int i; char buf[1024]; if(a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) return; if(a->length == 0) return; buf[0] = 0; for(i=0;i<(a->length*8);i++) { if(test_bit(value,a->length*8-i-1)) strcat(buf,"1"); else strcat(buf,"0"); } if(comment) { strcat(buf,";"); strcat(buf,comment); } pcomment(self->dumper.out,value,buf); if(err) fprintf(self->dumper.out," /* Error accessing %s (%s) */",a->name,grib_get_error_message(err)); else fprintf(self->dumper.out," GRIB_CHECK(grib_set_long(h,\"%s\",%ld),%d);\n",a->name,value,0); fprintf(self->dumper.out,"\n"); }
static void dump_string(grib_dumper* d,grib_accessor* a,const char* comment) { grib_dumper_c_code *self = (grib_dumper_c_code*)d; char value[1024]; size_t size = sizeof(value); int err = grib_unpack_string(a,value,&size); if (a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) return; if(a->length == 0) return; #if 1 if(comment) fprintf(self->dumper.out,"/* %s */\n",comment); #endif fprintf(self->dumper.out," p = \"%s\";\n",value); fprintf(self->dumper.out," size = strlen(p)+1;\n"); fprintf(self->dumper.out," GRIB_CHECK(grib_set_string(h,\"%s\",p,&size),%d);\n",a->name,0); if(err) fprintf(self->dumper.out," /* Error accessing %s (%s) */",a->name,grib_get_error_message(err)); }
static void dump_bits(grib_dumper* d,grib_accessor* a,const char* comment) { grib_dumper_serialize *self = (grib_dumper_serialize*)d; long value; size_t size = 1; int err = grib_unpack_long(a,&value,&size); if( (a->flags & GRIB_ACCESSOR_FLAG_HIDDEN) != 0 ) return; if( (a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) != 0 && (d->option_flags & GRIB_DUMP_FLAG_READ_ONLY) == 0) return; fprintf(self->dumper.out,"%s = %ld ", a->name,value); #if 0 fprintf(self->dumper.out,"["); for(i=0;i<(a->length*8);i++) { if(test_bit(value,a->length*8-i-1)) fprintf(self->dumper.out,"1"); else fprintf(self->dumper.out,"0"); } if(comment) fprintf(self->dumper.out,":%s]",comment); else fprintf(self->dumper.out,"]"); #endif if(err) fprintf(self->dumper.out," *** ERR=%d (%s)",err,grib_get_error_message(err)); fprintf(self->dumper.out,"\n"); }
static void dump_long(grib_dumper* d,grib_accessor* a,const char* comment) { grib_dumper_c_code *self = (grib_dumper_c_code*)d; long value; size_t size = 1; int err = grib_unpack_long(a,&value,&size); if((a->flags & GRIB_ACCESSOR_FLAG_READ_ONLY)) return; #if 1 if(comment) pcomment(self->dumper.out,value,comment); #endif if( ((a->flags & GRIB_ACCESSOR_FLAG_CAN_BE_MISSING) != 0) && (value == GRIB_MISSING_LONG)) fprintf(self->dumper.out," GRIB_CHECK(grib_set_missing(h,\"%s\"),%d);\n",a->name,0); else fprintf(self->dumper.out," GRIB_CHECK(grib_set_long(h,\"%s\",%ld),%d);\n",a->name,value,0); if(err) fprintf(self->dumper.out," /* Error accessing %s (%s) */",a->name,grib_get_error_message(err)); if(comment) fprintf(self->dumper.out,"\n"); }
static void dump_values(grib_dumper* d,grib_accessor* a) { grib_dumper_debug *self = (grib_dumper_debug*)d; int i,k,err =0; int more = 0; double* buf = NULL; size_t size=0; long count=0; if( a->length == 0 && (d->option_flags & GRIB_DUMP_FLAG_CODED) != 0) return; grib_value_count(a,&count); size=count; if(size == 1){ dump_double(d,a,NULL); return ; } buf = (double*)grib_context_malloc(d->handle->context,size * sizeof(double)); set_begin_end(d,a); for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," "); fprintf(self->dumper.out,"%ld-%ld %s %s = (%ld,%ld)",self->begin,self->theEnd,a->creator->op, a->name,(long)size,a->length); aliases(d,a); fprintf(self->dumper.out," {"); if(!buf) { if(size == 0) fprintf(self->dumper.out,"}\n"); else fprintf(self->dumper.out," *** ERR cannot malloc(%ld) }\n",(long)size); return; } fprintf(self->dumper.out,"\n"); err = grib_unpack_double(a,buf,&size); if(err){ grib_context_free(d->handle->context,buf); fprintf(self->dumper.out," *** ERR=%d (%s) [grib_dumper_debug::dump_values]\n}",err,grib_get_error_message(err)); return ; } if(size > 100) { more = size - 100; size = 100; } k = 0; while(k < size) { #if 1 int j; for(i = 0; i < d->depth + 3 ; i++) fprintf(self->dumper.out," "); for(j = 0; j < 8 && k < size; j++, k++) { fprintf(self->dumper.out,"%10g",buf[k]); if(k != size-1) fprintf(self->dumper.out,", "); } fprintf(self->dumper.out,"\n"); #else fprintf(self->dumper.out,"%d %g\n",k,buf[k]); #endif } if(more) { for(i = 0; i < d->depth + 3 ; i++) fprintf(self->dumper.out," "); fprintf(self->dumper.out,"... %d more values\n",more); } for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," "); fprintf(self->dumper.out,"} # %s %s \n",a->creator->op, a->name); grib_context_free(d->handle->context,buf); }
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) { grib_rule* r = options->rules; int i; int err=0; double scale = 1; double offset = 0; int set = options->set_values_count; while(r) { if(r->condition) { long ok = 0; GRIB_CHECK_NOLINE(grib_expression_evaluate_long(h,r->condition,&ok),NULL); if(ok) { grib_rule_entry* e = r->entries; while(e) { if (!strcmp(e->name,"skip")) return 0; Assert(set < (sizeof(options->set_values)/sizeof(options->set_values[0]))); options->set_values[set].name = e->name; options->set_values[set].error=grib_expression_set_value(h, e->value,&(options->set_values[set])); set++; e = e->next; } } } r = r->next; } if(options->verbose) { printf("Applying %d:\n",options->infile->handle_count); grib_print_values(options->set_values, set); printf("\n"); } err=grib_set_values(h,options->set_values, set); if( err != GRIB_SUCCESS) { for(i = 0; i < options->set_values_count ; i++) { if(options->set_values[i].error != GRIB_SUCCESS && options->set_values[i].error != GRIB_CONCEPT_NO_MATCH && options->fail) grib_context_log(h->context,GRIB_LOG_ERROR,"unable to set key \"%s\" (%s)", options->set_values[i].name,grib_get_error_message( options->set_values[i].error)); } } grib_get_double(h,"scaleValuesBy",&scale); grib_get_double(h,"offsetValuesBy",&offset); if(scale != 1 || offset != 0) { double *v; size_t size; int i; GRIB_CHECK_NOLINE(grib_get_size(h,"values",&size),0); v = (double*)calloc(size,sizeof(double)); if(!v) { fprintf(stderr,"failed to allocate %ld bytes\n",(long)(size*sizeof(double))); exit(1); } GRIB_CHECK_NOLINE(grib_get_double_array(h,"values",v,&size),0); if(options->verbose) { printf("Applying scale=%g/offset=%g\n",scale,offset); } for(i = 0; i< size; i++) v[i] = v[i] * scale + offset; GRIB_CHECK_NOLINE(grib_set_double_array(h,"values",v,size),0); free(v); } grib_tools_write_message(options,h); return 0; }
int main(int argc,char **argv) { FILE* in, *out, *bad; char *cout, *cbad; size_t data_len = SIZE; long count = 0; unsigned char *data; if(argc != 3 && argc != 4) usage(argv[0]); in = fopen(argv[1],"r"); if(!in) { perror(argv[1]); exit(1); } cout = argv[2]; out = fopen(argv[2],"w"); if(!out) { perror(argv[2]); exit(1); } if(argc == 4) { cbad = argv[3]; bad = fopen(argv[3],"w"); if(!bad) { perror(argv[3]); exit(1); } } else { bad = out; cbad = cout; } for(;;) { size_t len = SIZE; long ret = wmo_read_grib_from_file(in,buffer,&len); if(ret == GRIB_END_OF_FILE && len == 0) break; printf("GRIB %ld: size: %ld code: %ld (%s)\n", ++count, (long)len, ret, grib_get_error_message(ret)); switch(ret) { case 0: if(fwrite(buffer,1,len,out) != len) { perror(cout); exit(1); } break; case GRIB_WRONG_LENGTH: case GRIB_PREMATURE_END_OF_FILE: fseek(in,-4,SEEK_CUR); memset(buffer + len - 4, '7', 4); len = data_len = SIZE; data = (unsigned char*)&buffer[0]; ret = grib_read_any_from_memory(NULL, &data, &data_len, buffer, &len); printf(" -> GRIB %ld: size: %ld code: %ld (%s)\n", count, (long)len, ret, grib_get_error_message(ret)); if(ret == 0) { if(fwrite(buffer,1,len,bad) != len) { perror(cbad); exit(1); } } break; } } if(fclose(in)) { perror(argv[1]); exit(1); } if(fclose(out)) { perror(argv[2]); exit(1); } if(argc == 4) { if(fclose(bad)) { perror(argv[3]); exit(1); } } return 0; }
int main(int argc, char** argv) { int err = 0; size_t values_len=0; double *values = NULL; char error_msg[100] ; size_t slong = sizeof(long) * 8 ; int i; FILE* in = NULL; char* filename ; grib_handle *h = NULL; if (argc<2) usage(argv[0]); filename=argv[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); } for (i=0;i<255;i++) { /* get the size of the values array*/ GRIB_CHECK(grib_get_size(h,"values",&values_len),0); values = malloc(values_len*sizeof(double)); err = grib_get_double_array(h,"values",values,&values_len); GRIB_CHECK(grib_set_long(h,"bitsPerValue",i),0); err = grib_set_double_array(h,"values",values,values_len); free(values); values = 0; /* * check if encoding of the data fails when the number of bpv * is not supported; bpv allowed on decoding is bpv < size of long */ if (i < slong && err == 0) { /* do nothing */ } else if (i >= slong && err == GRIB_INVALID_BPV) { /* do nothing */ } else { sprintf(error_msg,"Error decoding when bpv=%d. Error message:%s",i,grib_get_error_message(err)); perror(error_msg); exit(1); } values = malloc(values_len*sizeof(double)); err = grib_get_double_array(h,"values",values,&values_len); /* * check if decoding of the data fails when the number of bpv * is not supported; bpv allowed on decoding is bpv <= size of long */ if (i <= slong && err == 0) { /* do nothing */ } else if (i > slong && err == GRIB_INVALID_BPV) { /* do nothing */ } else { sprintf(error_msg,"Error decoding when bpv=%d. Error message:%s",i,grib_get_error_message(err)); perror(error_msg); exit(1); } free(values); values = 0; } grib_handle_delete(h); h=0; fclose(in); return 0; }
const char* codes_get_error_message(int code) { return grib_get_error_message(code); }