static int compare_values(grib_handle* h1,grib_handle *h2,const char *name) { size_t len1 = 0; size_t len2 = 0; int err; int err1; int err2; int type1,type2; char *sval1 = NULL,*sval2 = NULL; unsigned char *uval1 = NULL,*uval2 = NULL; double *dval1 = NULL, *dval2 = NULL; long *lval1 = NULL, *lval2 = NULL; if((err = grib_get_native_type(h1,name,&type1)) != GRIB_SUCCESS) { printf("Oops... cannot get type of [%s] in 1st field: %s\n",name,grib_get_error_message(err)); return err; } if((err = grib_get_native_type(h2,name,&type2)) != GRIB_SUCCESS) { if(err == GRIB_NOT_FOUND) { printf("[%s] not found in 2nd field\n",name); return err; } printf("Oops... cannot get type of [%s] in 2nd field: %s\n",name,grib_get_error_message(err)); return err; } if(type1 != type2) { printf("Warning, [%s] has different types: 1st field: [%s], 2nd field: [%s]\n", name,grib_get_type_name(type1),grib_get_type_name(type2)); /* return GRIB_TYPE_MISMATCH; */ } if(type1 == GRIB_TYPE_LABEL) return err; if(type1 == GRIB_TYPE_SECTION) return err; if((err = grib_get_size(h1,name,&len1)) != GRIB_SUCCESS) { printf("Oops... cannot get size of [%s] in 1st field: %s\n",name,grib_get_error_message(err)); return err; } if((err = grib_get_size(h2,name,&len2)) != GRIB_SUCCESS) { if(err == GRIB_NOT_FOUND) { printf("[%s] not found in 2nd field\n",name); return err; } printf("Oops... cannot get size of [%s] in 2nd field: %s\n",name,grib_get_error_message(err)); return err; } if(len1 != len2) { printf("[%s] has different size: 1st field: %ld, 2nd field: %ld\n",name,(long)len1,(long)len2); return GRIB_COUNT_MISMATCH; } switch(type1) { case GRIB_TYPE_STRING: sval1 = (char*)grib_context_malloc(h1->context,len1*sizeof(char)); sval2 = (char*)grib_context_malloc(h2->context,len2*sizeof(char)); if((err1 = grib_get_string(h1,name,sval1,&len1)) != GRIB_SUCCESS) { printf("Oops... cannot get string value of [%s] in 1st field: %s\n", name,grib_get_error_message(err1)); } if((err2 = grib_get_string(h2,name,sval2,&len2)) != GRIB_SUCCESS) { printf("Oops... cannot get string value of [%s] in 2nd field: %s\n", name,grib_get_error_message(err2)); } if(err1 == GRIB_SUCCESS && err2 == GRIB_SUCCESS) { if(strcmp(sval1,sval2) != 0) { printf("[%s] string values are different: [%s] and [%s]\n", name,sval1,sval2); err1 = GRIB_VALUE_MISMATCH; } } grib_context_free(h1->context,sval1); grib_context_free(h2->context,sval2); if(err1) return err1; if(err2) return err2; break; case GRIB_TYPE_LONG: lval1 = (long*)grib_context_malloc(h1->context,len1*sizeof(long)); lval2 = (long*)grib_context_malloc(h2->context,len2*sizeof(long)); if((err1 = grib_get_long_array(h1,name,lval1,&len1)) != GRIB_SUCCESS) { printf("Oops... cannot get long value of [%s] in 1st field: %s\n", name,grib_get_error_message(err1)); } if((err2 = grib_get_long_array(h2,name,lval2,&len2)) != GRIB_SUCCESS) { printf("Oops... cannot get long value of [%s] in 2nd field: %s\n", name,grib_get_error_message(err2)); } if(err1 == GRIB_SUCCESS && err2 == GRIB_SUCCESS) { int i; for(i = 0; i < len1; i++) if(lval1[i] != lval2[i]) { if(len1 == 1) printf("[%s] long values are different: [%ld] and [%ld]\n", name,lval1[i],lval2[i]); else printf("[%s] long value %d of %ld are different: [%ld] and [%ld]\n", name,i,(long)len1,lval1[i],lval2[i]); err1 = GRIB_VALUE_MISMATCH; break; } } grib_context_free(h1->context,lval1); grib_context_free(h2->context,lval2); if(err1) return err1; if(err2) return err2; break; case GRIB_TYPE_DOUBLE: dval1 = (double*)grib_context_malloc(h1->context,len1*sizeof(double)); dval2 = (double*)grib_context_malloc(h2->context,len2*sizeof(double)); if((err1 = grib_get_double_array(h1,name,dval1,&len1)) != GRIB_SUCCESS) { printf("Oops... cannot get double value of [%s] in 1st field: %s\n", name,grib_get_error_message(err1)); } if((err2 = grib_get_double_array(h2,name,dval2,&len2)) != GRIB_SUCCESS) { printf("Oops... cannot get double value of [%s] in 2nd field: %s\n", name,grib_get_error_message(err2)); } if(err1 == GRIB_SUCCESS && err2 == GRIB_SUCCESS) { int i; for(i = 0; i < len1; i++) if(!same(dval1[i],dval2[i])) { if(len1 == 1) printf("[%s] double values are different: [%g] and [%g], diff: %g\n", name,dval1[i],dval2[i],dval1[i] - dval2[i]); else printf("[%s] double value %d of %ld are different: [%g] and [%g], diff: %g\n", name,i,(long)len1,dval1[i],dval2[i],dval1[i] - dval2[i]); err1 = GRIB_VALUE_MISMATCH; break; } } grib_context_free(h1->context,dval1); grib_context_free(h2->context,dval2); if(err1) return err1; if(err2) return err2; break; case GRIB_TYPE_BYTES: uval1 = (unsigned char*)grib_context_malloc(h1->context,len1*sizeof(unsigned char)); uval2 = (unsigned char*)grib_context_malloc(h2->context,len2*sizeof(unsigned char)); if((err1 = grib_get_bytes(h1,name,uval1,&len1)) != GRIB_SUCCESS) { printf("Oops... cannot get bytes value of [%s] in 1st field: %s\n", name,grib_get_error_message(err1)); } if((err2 = grib_get_bytes(h2,name,uval2,&len2)) != GRIB_SUCCESS) { printf("Oops... cannot get bytes value of [%s] in 2nd field: %s\n", name,grib_get_error_message(err2)); } if(err1 == GRIB_SUCCESS && err2 == GRIB_SUCCESS) { if(memcmp(uval1,uval2,len1) != 0) { int i; for(i = 0; i < len1; i++) if(uval1[i] != uval2[i]) { if(len1 == 1) printf("[%s] byte values are different: [%02x] and [%02x]\n", name,uval1[i],uval2[i]); else printf("[%s] byte value %d of %ld are different: [%02x] and [%02x]\n", name,i,(long)len1,uval1[i],uval2[i]); err1 = GRIB_VALUE_MISMATCH; break; } err1 = GRIB_VALUE_MISMATCH; } } grib_context_free(h1->context,uval1); grib_context_free(h2->context,uval2); if(err1) return err1; if(err2) return err2; break; case GRIB_TYPE_LABEL: break; default: printf("Cannot compare [%s], unsupported type %d\n",name,type1); return GRIB_UNABLE_TO_COMPARE_ACCESSORS; break; } return GRIB_SUCCESS; }
int codes_get_bytes(grib_handle* h, const char* key, unsigned char* bytes, size_t *length) { return grib_get_bytes(h,key,bytes,length); }
int dump_values(FILE* out,grib_handle* h,const char *name,int missingOK) { size_t len = 0; int err; int type; char *sval = NULL; unsigned char *uval = NULL; double *dval = NULL; long *lval = NULL; int i; if((err = grib_get_type(h,name,&type)) != GRIB_SUCCESS) { printf("# Oops... cannot get type of [%s]: %s\n",name, grib_get_error_message(err)); return err; } if((err = grib_get_size(h,name,&len)) != GRIB_SUCCESS) { printf("# Oops... cannot get size of [%s]: %s\n",name, grib_get_error_message(err)); return err; } switch(type) { case GRIB_TYPE_STRING: sval = grib_context_malloc(h->context,len*sizeof(char)); if((err = grib_get_string(h,name,sval,&len)) != GRIB_SUCCESS) { printf("# Oops... cannot get string value of [%s]: %s\n", name,grib_get_error_message(err)); } else { fprintf(out,"%s='%s';\n",name,sval); } grib_context_free(h->context,sval); if(err) return err; break; case GRIB_TYPE_LONG: lval = grib_context_malloc(h->context,len*sizeof(long)); if((err = grib_get_long_array(h,name,lval,&len)) != GRIB_SUCCESS) { printf("# Oops... cannot get long value of [%s]: %s\n", name,grib_get_error_message(err)); } else { if(len == 1) { if(missingOK && lval[0] == GRIB_MISSING_LONG) fprintf(out,"%s = missing;\n",name); else fprintf(out,"%s = %ld;\n",name,lval[0]); } else { fprintf(out,"%s = {", name); if(len >= 10) fprintf(out," # %ld\n",(long)len); for(i = 0; i < len; i++){ fprintf(out,"%ld, ",lval[i]); if((i+1)%10 == 0) fprintf(out,"\n"); } fprintf(out,"};\n"); } } grib_context_free(h->context,lval); if(err) return err; break; case GRIB_TYPE_DOUBLE: dval = grib_context_malloc(h->context,len*sizeof(double)); if((err = grib_get_double_array(h,name,dval,&len)) != GRIB_SUCCESS) { printf("# Oops... cannot get double value of [%s]: %s\n", name,grib_get_error_message(err)); } else { if(len == 1) { if(missingOK && dval[0] == GRIB_MISSING_DOUBLE) fprintf(out,"%s = missing;\n",name); else { fprintf(out,"%s = %g;\n",name,dval[0]); } } else { fprintf(out,"%s = { \n",name); if(len >= 10) fprintf(out," # %ld\n",(long)len); for(i = 0; i < len; i++) { fprintf(out,"%f, ",dval[i]); if((i+1)%10 == 0) fprintf(out,"\n"); } fprintf(out,"};\n"); } } grib_context_free(h->context,dval); if(err) return err; break; case GRIB_TYPE_BYTES: uval = grib_context_malloc(h->context,len*sizeof(unsigned char)); if((err = grib_get_bytes(h,name,uval,&len)) != GRIB_SUCCESS) { printf("# Oops... cannot get bytes value of [%s]: %s\n", name,grib_get_error_message(err)); } else { fprintf(out,"%s = { \n",name); if(len >= 10) fprintf(out," # %ld\n",(long)len); for(i = 0; i < len; i++) { fprintf(out,"%02x, ",uval[i]); if((i+1)%10 == 0) fprintf(out,"\n"); } fprintf(out,"};\n"); } grib_context_free(h->context,uval); if(err) return err; break; case GRIB_TYPE_LABEL: break; case GRIB_TYPE_SECTION: break; default: printf("Cannot compare [%s], unsupported type %d\n",name,type); return GRIB_UNABLE_TO_COMPARE_ACCESSORS; break; } return GRIB_SUCCESS; }