static int pack_double(grib_accessor* a, const double* val, size_t *len) { grib_accessor_data_g2shsimple_packing* self = (grib_accessor_data_g2shsimple_packing*)a; int err = GRIB_SUCCESS; size_t coded_n_vals = *len-1; size_t n_vals = *len; if (*len ==0) return GRIB_NO_VALUES; self->dirty=1; if((err = grib_set_double_internal(grib_handle_of_accessor(a),self->real_part,*val)) != GRIB_SUCCESS) return err; { /* Make sure we can decode it again */ double ref = 1e-100; grib_get_double_internal(grib_handle_of_accessor(a),self->real_part,&ref); Assert(ref == *val); } val++; if((err = grib_set_double_array_internal(grib_handle_of_accessor(a),self->coded_values,val,coded_n_vals)) != GRIB_SUCCESS) return err; *len = n_vals; if((err = grib_set_long_internal(grib_handle_of_accessor(a),self->numberOfValues,(long)n_vals)) != GRIB_SUCCESS) return err; return err; }
static int pack_double(grib_accessor* a, const double* val, size_t *len) { grib_accessor_data_shsimple_packing* self = (grib_accessor_data_shsimple_packing*)a; int err = GRIB_SUCCESS; size_t coded_n_vals = *len-1; size_t n_vals = *len; self->dirty=1; if (*len ==0) return GRIB_NO_VALUES; if((err = grib_set_double_internal(a->parent->h,self->real_part,*val)) != GRIB_SUCCESS) return err; val++; if((err = grib_set_double_array_internal(a->parent->h,self->coded_values,val,coded_n_vals)) != GRIB_SUCCESS) return err; *len = n_vals; return err; }
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; }
static int pack_double(grib_accessor* a, const double* val, size_t *len) { grib_accessor_data_complex_packing* self = (grib_accessor_data_complex_packing*)a; size_t i = 0; int ret = GRIB_SUCCESS; long hcount = 0; long lcount = 0; long hpos = 0; long lup = 0; long mmax = 0; long n_vals = 0; double *scals = NULL; double s = 0; double d = 0; unsigned char* buf = NULL; size_t buflen = 0; size_t hsize = 0; size_t lsize = 0; unsigned char* hres = NULL; unsigned char* lres = NULL; long lpos = 0; long maxv = 0; long offsetdata = 0; long bits_per_value = 0; double reference_value = 0; long binary_scale_factor = 0; long decimal_scale_factor = 0; long laplacianOperatorIsSet = 0; double laplacianOperator = 0; long sub_j= 0; long sub_k= 0; long sub_m= 0; long pen_j= 0; long pen_k= 0; long pen_m= 0; long GRIBEX_sh_bug_present =0; long ieee_floats =0; double min = 0; double max = 0; double current_val = 0; short mixmax_unset = 0; int bytes; encode_float_proc encode_float = NULL; if (*len ==0) return GRIB_NO_VALUES; 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->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->GRIBEX_sh_bug_present,&GRIBEX_sh_bug_present)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->ieee_floats,&ieee_floats)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->laplacianOperatorIsSet,&laplacianOperatorIsSet)) != GRIB_SUCCESS) return ret; if((ret = grib_get_double_internal(a->parent->h,self->laplacianOperator,&laplacianOperator)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->sub_j,&sub_j)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->sub_k,&sub_k)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->sub_m,&sub_m)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->pen_j,&pen_j)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->pen_k,&pen_k)) != GRIB_SUCCESS) return ret; if((ret = grib_get_long_internal(a->parent->h,self->pen_m,&pen_m)) != GRIB_SUCCESS) return ret; self->dirty=1; switch (ieee_floats) { case 0: encode_float =grib_ibm_to_long; bytes=4; break; case 1: encode_float =grib_ieee_to_long; bytes=4; break; case 2: encode_float =grib_ieee64_to_long; bytes=8; break; default: return GRIB_NOT_IMPLEMENTED; } Assert (sub_j == sub_k); Assert( sub_j == sub_m); Assert (pen_j == pen_k); Assert( pen_j == pen_m); n_vals = (pen_j+1)*(pen_j+2); d = grib_power(decimal_scale_factor,10) ; if(*len != n_vals){ grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,"COMPLEX_PACKING : wrong number of values, expected %d - got %d",n_vals,*len); return GRIB_INTERNAL_ERROR; } if (pen_j == sub_j) { double* values; if (d) { values=(double*)grib_context_malloc_clear(a->parent->h->context,sizeof(double)*n_vals); for (i=0;i<n_vals;i++) values[i]=val[i]*d; } else { values=(double*)val; } buflen=n_vals*bytes; buf = (unsigned char*)grib_context_malloc_clear(a->parent->h->context,buflen); grib_ieee_encode_array(a->parent->h->context,values,n_vals,bytes,buf); if (d) grib_context_free(a->parent->h->context,values); grib_buffer_replace(a, buf, buflen,1,1); grib_context_free(a->parent->h->context,buf); return 0; } if(!laplacianOperatorIsSet) { laplacianOperator = calculate_pfactor(a->parent->h->context,val,pen_j,sub_j); if((ret = grib_set_double_internal(a->parent->h,self->laplacianOperator,laplacianOperator)) != GRIB_SUCCESS) return ret; grib_get_double_internal(a->parent->h,self->laplacianOperator,&laplacianOperator); } /* printf("PACKING LAPLACE set=%ld value=%.20f\n",laplacianOperatorIsSet,laplacianOperator); */ hsize = 4*(sub_k+1)*(sub_k+2); lsize = ((n_vals - ((sub_k+1)*(sub_k+2)))*bits_per_value)/8; buflen = hsize+lsize; buf = (unsigned char*)grib_context_malloc(a->parent->h->context,buflen); hres = buf; lres = buf+hsize; maxv = pen_j+1; lpos = 0; hpos = 0; scals = (double*) grib_context_malloc(a->parent->h->context,maxv*sizeof(double)); Assert(scals); scals[0] =0; for(i=1;i<maxv;i++) scals[i] = ((double)pow(i*(i+1),laplacianOperator)); i=0; mmax = 0; maxv = pen_j+1; i=0; lcount=0; hcount=0; sub_k = sub_j; while(maxv>0) { lup=mmax; if(sub_k>=0) { i += 2*(sub_k+1); lup += sub_k+1 ; hcount += sub_k+1 ; sub_k--; } for(lcount=hcount; lcount < maxv ; lcount++) { current_val = ((val[i++]*d) * scals[lup]); if(mixmax_unset == 0){ max = current_val; min = current_val; mixmax_unset = 1; } if(current_val > max) max = current_val; if(current_val < min) min = current_val; current_val = ((val[i++]*d) * scals[lup]); if(current_val > max) max = current_val; if(current_val < min) min = current_val; lup++; } maxv--; hcount=0; mmax++; } if (grib_get_nearest_smaller_value(a->parent->h,self->reference_value,min,&reference_value) !=GRIB_SUCCESS) { grib_context_log(a->parent->h->context,GRIB_LOG_ERROR, "unable to find nearest_smaller_value of %g for %s",min,self->reference_value); exit(GRIB_INTERNAL_ERROR); } binary_scale_factor = grib_get_binary_scale_fact(max,reference_value,bits_per_value,&ret); if (ret==GRIB_UNDERFLOW) { d=0; binary_scale_factor = 0; reference_value=0; } s = grib_power(-binary_scale_factor,2); /* printf("D : %.30f\n",d); */ i=0; mmax = 0; maxv = pen_j+1; i=0; lcount=0; hcount=0; sub_k = sub_j; while(maxv>0) { lup=mmax; if(sub_k>=0) { for(hcount=0;hcount<sub_k+1;hcount++) { if ( GRIBEX_sh_bug_present && hcount==sub_k ) { /* _test(val[i]*d*scals[lup],1); */ grib_encode_unsigned_long(hres, encode_float((val[i++]*d)*scals[lup]) , &hpos, 32); /* _test(val[i]*d*scals[lup],1); */ grib_encode_unsigned_long(hres, encode_float((val[i++]*d)*scals[lup]) , &hpos, 32); }else{ /* _test(val[i]*d,0); */ grib_encode_unsigned_long(hres, encode_float(val[i++]*d) , &hpos, 32); /* _test(val[i]*d,0); */ grib_encode_unsigned_long(hres, encode_float(val[i++]*d) , &hpos, 32); } lup++; } sub_k--; } #if FAST_BIG_ENDIAN grib_encode_double_array_complex((maxv-hcount)*2,&(val[i]),bits_per_value,reference_value,&(scals[lup]),d,s,lres,&lpos); i+=(maxv-hcount)*2; #else if (bits_per_value % 8) { for(lcount=hcount; lcount < maxv ; lcount++) { current_val = (((((val[i++]*d) * scals[lup])-reference_value)*s)+0.5); if(current_val < 0) grib_context_log(a->parent->h->context,GRIB_LOG_ERROR, "COMPLEX_PACKING : negative coput before packing (%g)", current_val); grib_encode_unsigned_longb(lres, current_val, &lpos, bits_per_value); current_val = (((((val[i++]*d) * scals[lup])-reference_value)*s)+0.5); if(current_val < 0) grib_context_log(a->parent->h->context,GRIB_LOG_ERROR, "COMPLEX_PACKING : negative coput before packing (%g)", current_val); grib_encode_unsigned_longb(lres, current_val, &lpos, bits_per_value); lup++; } } else { for(lcount=hcount; lcount < maxv ; lcount++) { current_val = (((((val[i++]*d) * scals[lup])-reference_value)*s)+0.5); if(current_val < 0) grib_context_log(a->parent->h->context,GRIB_LOG_ERROR, "COMPLEX_PACKING : negative coput before packing (%g)", current_val); grib_encode_unsigned_long(lres, current_val, &lpos, bits_per_value); current_val = (((((val[i++]*d) * scals[lup])-reference_value)*s)+0.5); if(current_val < 0) grib_context_log(a->parent->h->context,GRIB_LOG_ERROR, "COMPLEX_PACKING : negative coput before packing (%g)", current_val); grib_encode_unsigned_long(lres, current_val, &lpos, bits_per_value); lup++; } } #endif maxv--; hcount=0; mmax++; } if(((hpos/8) != hsize) &&((lpos/8) != lsize)) { grib_context_log(a->parent->h->context,GRIB_LOG_ERROR, "COMPLEX_PACKING : Mismatch in packing between high resolution and low resolution part"); grib_context_free(a->parent->h->context,buf); grib_context_free(a->parent->h->context,scals); return GRIB_INTERNAL_ERROR; } buflen = ((hpos + lpos)/8); if((ret = grib_set_double_internal(a->parent->h,self->reference_value, reference_value)) != GRIB_SUCCESS) return ret; { /* Make sure we can decode it again */ double ref = 1e-100; grib_get_double_internal(a->parent->h,self->reference_value,&ref); Assert(ref == reference_value); } if((ret = grib_set_long_internal(a->parent->h,self->binary_scale_factor, binary_scale_factor)) != GRIB_SUCCESS) return ret; grib_buffer_replace(a, buf, buflen,1,1); grib_context_free(a->parent->h->context,buf); grib_context_free(a->parent->h->context,scals); return ret; }
int grib_init_accessor_from_array(grib_loader* loader,grib_accessor* ga,grib_arguments* default_value) { grib_handle* h = ga->parent->h; int retval =GRIB_SUCCESS; char* strvalue; size_t len=0; int type=0; double* dvalue=0; long* lvalue=0; long lval=0; double dval=0; int i=0; grib_associative_array* ar=NULL; grib_runtime_type* value=NULL; ar=(grib_associative_array*)loader->data; grib_context_log(h->context,GRIB_LOG_DEBUG, "Initialize %s", ga->name); retval= grib_associative_array_get(ar,ga->name,&value); if((retval != GRIB_ARRAY_SUCCESS) && default_value) { grib_context_log(h->context,GRIB_LOG_DEBUG, "Setting %s to default value", ga->name); grib_pack_expression(ga,grib_arguments_get_expression(h,default_value,0)); return GRIB_SUCCESS; } if(ga->flags & GRIB_ACCESSOR_FLAG_READ_ONLY) { grib_context_log(h->context,GRIB_LOG_DEBUG, "Setting %s ignored (read only)", ga->name); return GRIB_SUCCESS; } if (retval != GRIB_ARRAY_SUCCESS) { grib_context_log(h->context,GRIB_LOG_DEBUG, "Grib array error %d",retval); if ((ga->flags & GRIB_ACCESSOR_FLAG_CAN_BE_MISSING)) { grib_context_log(h->context,GRIB_LOG_DEBUG, "Setting %s ignored (optional or can be missing)", ga->name); return GRIB_SUCCESS; } else { grib_context_log(h->context,GRIB_LOG_ERROR, "%s required", ga->name); return retval; } } if (value && grib_runtimetype_get_type(value,&type) == GRIB_RUNTIMETYPE_SUCCESS) { if (type == GRIB_RUNTIMETYPE_CHAR ) { grib_runtimetype_get_char(value,&strvalue,&len); switch(grib_accessor_get_native_type(ga)) { case GRIB_TYPE_STRING: grib_context_log(h->context,GRIB_LOG_DEBUG, "Setting %s to string %s", ga->name, strvalue); grib_set_string_internal(h,ga->name,strvalue,&len); break; case GRIB_TYPE_LONG: if (!strcmp(strvalue,"MISSING")) lval=GRIB_MISSING_LONG; else lval=atol(strvalue); grib_context_log(h->context,GRIB_LOG_DEBUG, "Setting %s to long %d", ga->name, lval); grib_set_long_internal(h,ga->name,lval); break; case GRIB_TYPE_DOUBLE: if (!strcmp(strvalue,"MISSING")) dval=GRIB_MISSING_DOUBLE; else if (sscanf(strvalue,"%lg",&dval)!=1) { grib_context_log(h->context,GRIB_LOG_ERROR, "Unable to set %s wrong value format", ga->name); } grib_context_log(h->context,GRIB_LOG_DEBUG, "Setting %s to double %lg (%s)", ga->name, dval,strvalue); grib_set_double_internal(h,ga->name,dval); break; case GRIB_TYPE_BYTES: grib_context_log(h->context,GRIB_LOG_DEBUG, "Setting %s to string %s", ga->name, strvalue); grib_set_bytes_internal(h,ga->name,(unsigned char*)strvalue,&len); break; case GRIB_TYPE_LABEL: break; default: grib_context_log(h->context,GRIB_LOG_ERROR, "Setting %s, cannot establish type %d [%s]" , ga->name,grib_accessor_get_native_type(ga),ga->creator->cclass->name); break; } } else { if (grib_runtimetype_get_double(value,&dvalue,&len) == GRIB_RUNTIMETYPE_SUCCESS) { switch(grib_accessor_get_native_type(ga)) { case GRIB_TYPE_LONG: lvalue=(long*)malloc(sizeof(long)*len); for (i=0;i<len;i++) lvalue[i]=(long)dvalue[i]; grib_context_log(h->context,GRIB_LOG_DEBUG, "Setting long array %s",ga->name); grib_set_long_array(h,ga->name,lvalue,len); free(lvalue); break; case GRIB_TYPE_DOUBLE: grib_context_log(h->context,GRIB_LOG_DEBUG, "Setting double array %s",ga->name); grib_set_double_array(h,ga->name,dvalue,len); break; default: grib_context_log(h->context,GRIB_LOG_ERROR, "Setting array %s, wrong type [%d]" , ga->name,grib_accessor_get_native_type(ga)); break; } } else grib_context_log(h->context,GRIB_LOG_ERROR, "Unable to set %s to double, wrong type (%d)", ga->name,type); } #if 0 switch(grib_accessor_get_native_type(ga)) { case GRIB_TYPE_STRING: if (grib_runtimetype_get_char(value,&strvalue,&strvalue_size) == GRIB_RUNTIMETYPE_SUCCESS) { grib_context_log(h->context,GRIB_LOG_DEBUG, "Setting %s to string %s", ga->name, strvalue); len=strlen(strvalue); grib_set_string_internal(h,ga->name,strvalue,&len); } else grib_context_log(h->context,GRIB_LOG_ERROR, "Unable to set %s to string, wrong type", ga->name); break; case GRIB_TYPE_LONG: if (grib_runtimetype_get_long(value,&lval,&len) == GRIB_RUNTIMETYPE_SUCCESS) { grib_context_log(h->context,GRIB_LOG_DEBUG, "Setting %s to long %d", ga->name, lval); grib_set_long_internal(h,ga->name,*lval); } else grib_context_log(h->context,GRIB_LOG_ERROR, "Unable to set %s to long, wrong type", ga->name); break; case GRIB_TYPE_DOUBLE: if (grib_runtimetype_get_double(value,&dval,&len) == GRIB_RUNTIMETYPE_SUCCESS) { grib_context_log(h->context,GRIB_LOG_DEBUG, "Setting %s to long %d", ga->name, dval); grib_set_double_internal(h,ga->name,*dval); } else grib_context_log(h->context,GRIB_LOG_ERROR, "Unable to set %s to double, wrong type", ga->name); break; case GRIB_TYPE_BYTES: if (grib_runtimetype_get_char(value,&strvalue,&len) == GRIB_RUNTIMETYPE_SUCCESS) { grib_context_log(h->context,GRIB_LOG_DEBUG, "Setting %s to string %s", ga->name, strvalue); grib_set_bytes_internal(h,ga->name,(unsigned char*)strvalue,&len); } else grib_context_log(h->context,GRIB_LOG_ERROR, "Unable to set %s to string, wrong type", ga->name); break; case GRIB_TYPE_LABEL: break; default: grib_context_log(h->context,GRIB_LOG_ERROR, "Setting %s, cannot establish type %d [%s]" , ga->name,grib_accessor_get_native_type(ga),ga->creator->cclass->name); break; } #endif } return retval; }
static int pack_double(grib_accessor* a, const double* cval, size_t *len) { grib_accessor_data_jpeg2000_packing *self =(grib_accessor_data_jpeg2000_packing*)a; grib_accessor_class* super = *(a->cclass->super); size_t n_vals = *len; int err = 0; int i; double reference_value = 0; long binary_scale_factor = 0; long bits_per_value = 0; long decimal_scale_factor = 0; double decimal = 1; size_t simple_packing_size = 0; unsigned char* buf = NULL; double divisor = 1; long width; long height; long ni; long nj; long target_compression_ratio; long type_of_compression_used; long scanning_mode; long list_defining_points; long number_of_data_points; int ret =0; j2k_encode_helper helper; double units_factor=1.0; double units_bias=0.0; double* val=(double*)cval; self->dirty=1; if(*len == 0){ grib_buffer_replace(a, NULL, 0, 1, 1); return GRIB_SUCCESS; } 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; ret = super->pack_double(a,val,len); switch (ret) { case GRIB_CONSTANT_FIELD: grib_buffer_replace(a, NULL, 0,1,1); return GRIB_SUCCESS; break; case GRIB_SUCCESS: break; default: grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,"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; decimal = grib_power(decimal_scale_factor,10) ; divisor = grib_power(-binary_scale_factor,2); simple_packing_size = (((bits_per_value*n_vals)+7)/8)*sizeof(unsigned char); buf = (unsigned char*)grib_context_malloc_clear(a->parent->h->context,simple_packing_size+EXTRA_BUFFER_SIZE); if(!buf) { err = GRIB_OUT_OF_MEMORY; goto cleanup; } if((err = grib_get_long_internal(a->parent->h,self->ni,&ni)) != GRIB_SUCCESS) return err; if((err = grib_get_long_internal(a->parent->h,self->nj,&nj)) != GRIB_SUCCESS) return err; if((err = grib_get_long_internal(a->parent->h,self->type_of_compression_used,&type_of_compression_used)) != GRIB_SUCCESS) return err; if((err = grib_get_long_internal(a->parent->h,self->target_compression_ratio,&target_compression_ratio)) != GRIB_SUCCESS) return err; if((err = grib_get_long_internal(a->parent->h,self->scanning_mode,&scanning_mode)) != GRIB_SUCCESS) return err; if((err = grib_get_long_internal(a->parent->h,self->list_defining_points,&list_defining_points)) != GRIB_SUCCESS) return err; if((err = grib_get_long_internal(a->parent->h,self->number_of_data_points,&number_of_data_points)) != GRIB_SUCCESS) return err; width = ni; height = nj; if((scanning_mode & (1<<5)) != 0) { long tmp = width; width = height; height = tmp; } /* The grid is not regular */ if(list_defining_points != 0) { width = *len; height = 1; } /* There is a bitmap */ if(*len != number_of_data_points) { width = *len; height = 1; } if(width*height != *len) { /* fprintf(stderr,"width=%ld height=%ld len=%d\n",(long)width,(long)height,(long)*len); */ Assert(width*height == *len); } switch( type_of_compression_used) { case 0: Assert(target_compression_ratio == 255); helper.compression = 0; break; case 1: Assert(target_compression_ratio != 255); Assert(target_compression_ratio != 0); helper.compression = target_compression_ratio; break; default: err = GRIB_NOT_IMPLEMENTED; goto cleanup; } helper.jpeg_buffer = buf; helper.width = width; helper.height = height; /* See GRIB-438 */ if (bits_per_value == 0) { const long bits_per_value_adjusted = 1; grib_context_log(a->parent->h->context, GRIB_LOG_DEBUG, "grib_accessor_class_data_jpeg2000_packing(%s) : bits per value was zero, changed to %d", self->jpeg_lib==OPENJPEG_LIB ? "openjpeg" : "jasper", bits_per_value_adjusted); bits_per_value = bits_per_value_adjusted; } helper.bits_per_value = bits_per_value; helper.buffer_size = simple_packing_size + EXTRA_BUFFER_SIZE; helper.values = val; helper.no_values = n_vals; helper.reference_value = reference_value; helper.divisor = divisor; helper.decimal = decimal; helper.jpeg_length = 0; switch (self->jpeg_lib) { case OPENJPEG_LIB: if ( (err = grib_openjpeg_encode(a->parent->h->context,&helper)) != GRIB_SUCCESS ) goto cleanup; break; case JASPER_LIB: if ( (err = grib_jasper_encode(a->parent->h->context,&helper)) != GRIB_SUCCESS ) goto cleanup; break; } if(helper.jpeg_length > simple_packing_size) grib_context_log(a->parent->h->context, GRIB_LOG_WARNING, "grib_accessor_data_jpeg2000_packing(%s) : jpeg data (%ld) larger than input data (%ld)", self->jpeg_lib==OPENJPEG_LIB ? "openjpeg" : "jasper", helper.jpeg_length, simple_packing_size); Assert( helper.jpeg_length <= helper.buffer_size); if(self->dump_jpg) { FILE *f = fopen(self->dump_jpg,"w"); if(f) { if(fwrite(helper.jpeg_buffer,helper.jpeg_length,1,f) != 1) perror(self->dump_jpg); if (fclose(f) != 0) perror(self->dump_jpg); } else perror(self->dump_jpg); } grib_buffer_replace(a, helper.jpeg_buffer, helper.jpeg_length, 1, 1); cleanup: grib_context_free(a->parent->h->context,buf); if(err == GRIB_SUCCESS) err = grib_set_long_internal(a->parent->h,self->number_of_values, *len); return err; }
static int pack_double(grib_accessor* a, const double* val, size_t *len) { grib_accessor_data_2order_packing* self = (grib_accessor_data_2order_packing*)a; grib_handle* gh = grib_handle_of_accessor(a); size_t i = 0; size_t j = 0; size_t n_vals = *len; size_t buff_len = 0; double divisor = 1; long vcount = 0; int err = 0; long bias = 0; double reference_value; long binary_scale_factor; long bits_per_value; /*long bit_per_val_rectified_for_gribex;*/ long decimal_scale_factor; long n1 = 0; long n2 = 0; long p1 = 0; long n_unused_bits = 0; long used_bits = 0; long offsetsection = 0; long snd_bitmap = 0; long snd_ordr_wdiff = 0; long matrix_values = 0; long general_ext = 0; long boustrophedonic = 0; long two_ordr_spd = 0; long plus1_spd = 0; long octet_start_group = 0; long width_spd_sp_desc = 0; unsigned char* buf = NULL; unsigned char* buf_size_of_groups = NULL; unsigned char* buf_width_of_group = NULL; unsigned char* buf_refs = NULL; unsigned char* buf_vals = NULL; double d = 0; unsigned long* sec_val = NULL; unsigned long* group_val = NULL; short n_sp_diff = 0; unsigned char* bitmap=NULL; grib_accessor* abitmap=NULL; size_t bitmap_len=0; #if KEEP_OLD == 1 #else second_order_packed* sd = NULL; #endif long bitp = 0; long pointer_of_group_size = 0; long pointer_of_group_width = 0; long refsp = 0; long nap = 0; long offsetdata = 0; double max; double min; int extraValues=0; size_t nv =0; if (*len ==0) return GRIB_NO_VALUES; if((err = grib_get_long_internal(gh,self->offsetsection,&offsetsection)) != GRIB_SUCCESS) return err; if((err = grib_get_long_internal(gh,self->offsetdata,&offsetdata)) != GRIB_SUCCESS) return err; if((err = grib_get_long_internal(gh,self->bits_per_value,&bits_per_value)) != GRIB_SUCCESS) return err; if((err = grib_get_long_internal(gh,self->decimal_scale_factor, &decimal_scale_factor)) != GRIB_SUCCESS) return err; if((err = grib_get_long_internal(gh,self->matrix_values, &matrix_values)) != GRIB_SUCCESS)return err; if((err = grib_get_long_internal(gh,self->snd_bitmap, &snd_bitmap)) != GRIB_SUCCESS) return err; if((err = grib_get_long_internal(gh,self->snd_ordr_wdiff, &snd_ordr_wdiff)) != GRIB_SUCCESS) return err; if((err = grib_get_long_internal(gh,self->general_ext, &general_ext)) != GRIB_SUCCESS) return err; if((err = grib_get_long_internal(gh,self->boustrophedonic, &boustrophedonic)) != GRIB_SUCCESS) return err; if((err = grib_get_long_internal(gh,self->width_spd_sp_desc, &width_spd_sp_desc)) != GRIB_SUCCESS) return err; if((err = grib_get_long_internal(gh,self->nap, &nap)) != GRIB_SUCCESS) return err; if ((abitmap=grib_find_accessor(gh,self->bitmap))!=NULL) { bitmap_len=grib_byte_count(abitmap); bitmap=(unsigned char*)grib_context_malloc_clear(a->context,sizeof(char)*bitmap_len); err=grib_unpack_bytes(abitmap,bitmap,&bitmap_len); if (err) {grib_context_free(a->context,bitmap); return err;} } two_ordr_spd = 1; plus1_spd = 0; if(bits_per_value == 0) return GRIB_NOT_IMPLEMENTED; n_sp_diff = two_ordr_spd*2+plus1_spd; /* calculation of integer array */ sec_val = (unsigned long*)grib_context_malloc(a->context,(n_vals)*sizeof(long)); d = grib_power(decimal_scale_factor,10) ; max = val[0]; min = max; for(i=0;i< n_vals;i++) { if ( val[i] > max ) max = val[i]; else if ( val[i] < min ) min = val[i]; } min *= d; max *= d; /*bit_per_val_rectified_for_gribex = bits_per_value+8-bits_per_value%8;*/ if (grib_get_nearest_smaller_value(gh,self->reference_value,min,&reference_value) !=GRIB_SUCCESS) { grib_context_log(a->context,GRIB_LOG_ERROR, "unable to find nearest_smaller_value of %g for %s",min,self->reference_value); return GRIB_INTERNAL_ERROR; } /* the scale factor in Grib 1 is adjusted in gribex, for "normalization purpose" ... ?*/ binary_scale_factor = grib_get_binary_scale_fact(max,reference_value,bits_per_value,&err); divisor = grib_power(-binary_scale_factor,2); for(i=0;i< n_vals;i++) sec_val[i] = (unsigned long)((((val[i]*d)-reference_value)*divisor)+0.5); /* reverse the rows*/ if(boustrophedonic) reverse_rows(sec_val,n_vals,nap,bitmap,bitmap_len); if(snd_ordr_wdiff) if((err = spatial_difference(a->context,sec_val, n_vals, n_sp_diff, &bias))){ grib_context_free(a->context,sec_val); return err; } /* for(i=0;i < 10;i++) printf("packing value [%d] %g %ld %ld bias %ld <<\n", i, val[i],sec_val[i],binary_scale_factor,bias ); */ nv = n_vals-n_sp_diff; group_val = sec_val+n_sp_diff; #if KEEP_OLD == 1 #else sd = grib_get_second_order_groups(a->context, group_val, nv); #endif bitp = 0; p1 = sd->size_of_group_array; bitp = (width_spd_sp_desc*(n_sp_diff+1)) ; octet_start_group = (bitp+7)/8; bitp = (p1*sd->nbits_per_widths); octet_start_group += (bitp+7)/8; bitp = (octet_start_group*8) + (sd->nbits_per_group_size*p1); n1 = (bitp+7)/8; bitp = n1*8 + bits_per_value*p1; n2 = (bitp+7)/8; used_bits = n2*8; buff_len = (n2+sd->packed_byte_count); if((a->offset+buff_len)%2) buff_len++; buf = NULL; buf = (unsigned char*)grib_context_malloc_clear(a->context,buff_len); buf_width_of_group = buf; buf_size_of_groups = buf+octet_start_group; buf_refs = buf+n1; buf_vals = buf+n2; pointer_of_group_size = 0; pointer_of_group_width = 0; refsp = 0; bitp = 0; for(i=0;i < n_sp_diff;i++) grib_encode_unsigned_longb(buf_width_of_group,sec_val[i], &pointer_of_group_width, width_spd_sp_desc); grib_encode_signed_longb(buf_width_of_group, bias, &pointer_of_group_width, width_spd_sp_desc); if(pointer_of_group_width%8) pointer_of_group_width = 8+(pointer_of_group_width-(pointer_of_group_width%8)); vcount = n_sp_diff; for(i = 0;i<p1;i++){ grib_encode_unsigned_longb(buf_width_of_group,sd->array_of_group_width[i],&pointer_of_group_width,sd->nbits_per_widths); grib_encode_unsigned_longb(buf_size_of_groups,sd->array_of_group_size[i], &pointer_of_group_size ,sd->nbits_per_group_size); grib_encode_unsigned_longb(buf_refs, sd->array_of_group_refs[i], &refsp , bits_per_value); used_bits += sd->array_of_group_size[i]*sd->array_of_group_width[i]; for(j=0; j < sd->array_of_group_size[i];j++){ grib_encode_unsigned_longb(buf_vals, sec_val[vcount+j]-sd->array_of_group_refs[i] ,&bitp , sd->array_of_group_width[i]); /* if(vcount+j < 10) printf(">>packing value [%ld] %g %ld %ld nb %ld <<\n", vcount+j, val[vcount+j],sec_val[vcount+j],binary_scale_factor,sd->array_of_group_refs[i] );*/ } vcount+= sd->array_of_group_size[i]; } n_unused_bits = (buff_len*8)-used_bits; grib_buffer_replace(a, buf,buff_len,1,1); if((buff_len + (offsetdata-offsetsection)) %2) { buff_len ++; grib_update_size(a,buff_len); } octet_start_group += 1+ a->offset-offsetsection; if((err = grib_set_long_internal(gh,self->octet_start_group, octet_start_group)) != GRIB_SUCCESS) return err; n1 += 1+a->offset-offsetsection; if((err = grib_set_long_internal(gh,self->n1,n1)) != GRIB_SUCCESS) return err; n2 += 1+a->offset-offsetsection; if((err = grib_set_long_internal(gh,self->n2,n2)) != GRIB_SUCCESS) return err; extraValues=0; while (p1 > 65535) { p1-=65536; extraValues++; } if((err = grib_set_long_internal(gh,self->p1,p1)) != GRIB_SUCCESS) return err; if((err = grib_set_long_internal(gh,self->extraValues,extraValues)) != GRIB_SUCCESS) return err; if((err = grib_set_long_internal(gh,self->p2,n_vals - n_sp_diff)) != GRIB_SUCCESS) return err; if((err = grib_set_double_internal(gh,self->reference_value, reference_value)) != GRIB_SUCCESS)return err; { /* Make sure we can decode it again */ double ref = 1e-100; grib_get_double_internal(gh,self->reference_value,&ref); Assert(ref == reference_value); } if((err = grib_set_long_internal(gh,self->binary_scale_factor, binary_scale_factor)) != GRIB_SUCCESS) return err; if((err = grib_set_long_internal(gh,self->decimal_scale_factor, decimal_scale_factor)) != GRIB_SUCCESS) return err; if((err = grib_set_long_internal(gh,self->width_widths, sd->nbits_per_widths)) != GRIB_SUCCESS) return err; if((err = grib_set_long_internal(gh,self->width_lengths, sd->nbits_per_group_size)) != GRIB_SUCCESS) return err; err = grib_set_long_internal(gh,self->half_byte, n_unused_bits); if(err != GRIB_SUCCESS) return err; #if KEEP_OLD == 1 #else grib_free_second_order_groups(a->context,sd); #endif ; grib_context_free(a->context,buf); grib_context_free(a->context,sec_val); if (bitmap!=NULL) grib_context_free(a->context,bitmap); return GRIB_SUCCESS; }