static int pack_double(grib_accessor* a, const double* val, size_t *len)
{ 
	/* This should keep GRIBEX happy  */
	 unsigned char zero =0;
        if(a->offset%2) 
          grib_buffer_replace(a, &zero, 1,1,1);
        else 
	grib_buffer_replace(a, NULL, 0,1,1);
	return GRIB_SUCCESS;
}
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 void resize(grib_accessor* a,size_t new_size)
{
  void* zero = grib_context_malloc_clear(a->parent->h->context,new_size);

  grib_buffer_replace(a,(const unsigned char*)zero,new_size,1,0);
  grib_context_free(a->parent->h->context,zero);
  grib_context_log(a->parent->h->context,GRIB_LOG_DEBUG,"resize: grib_accessor_class_padding.c %ld %ld %s %s\n",(long)new_size,(long)a->length,a->cclass->name,a->name);
  Assert(new_size == a->length);

}
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 int pack_double   (grib_accessor* a, const double* val, size_t *len)
{
    grib_accessor_ibmfloat* self = (grib_accessor_ibmfloat*)a;
    int ret = 0;
    unsigned long i = 0;
    unsigned long rlen = *len;
    size_t buflen  = 0;
    unsigned char *buf = NULL;
    long off = 0;

    if(*len < 1)
    {
        grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, " wrong size for %s it pack at least 1 values ", a->name , rlen );
        *len = 0;
        return GRIB_ARRAY_TOO_SMALL;
    }

    if (rlen == 1){
        /*
    double x = 0;
    grib_nearest_smaller_ibm_float(val[0],&x);
    double y = grib_long_to_ibm(grib_ibm_to_long(val[0]));
    printf("IBMFLOAT val=%.20f nearest_smaller_ibm_float=%.20f long_to_ibm=%.20f\n",val[0],x ,y);
         */
        off = byte_offset(a)*8;
        ret =  grib_encode_unsigned_long(a->parent->h->buffer->data,grib_ibm_to_long(val[0]), &off,  32);
        if (*len > 1)  grib_context_log(a->parent->h->context, GRIB_LOG_WARNING, "grib_accessor_unsigned : Trying to pack %d values in a scalar %s, packing first value",  *len, a->name  );
        if (ret == GRIB_SUCCESS) len[0] = 1;
        return ret;
    }

    buflen = rlen*4;

    buf = (unsigned char*)grib_context_malloc(a->parent->h->context,buflen);

    for(i=0; i < rlen;i++){
        grib_encode_unsigned_longb(buf,grib_ibm_to_long(val[i]), &off,  32);
    }
    ret = grib_set_long_internal(a->parent->h,grib_arguments_get_name(a->parent->h,self->arg,0),rlen);

    if(ret == GRIB_SUCCESS)
        grib_buffer_replace(a, buf, buflen,1,1);
    else
        *len = 0;

    grib_context_free(a->parent->h->context,buf);

    a->length = byte_count(a);

    return ret;
}
static int pack_double(grib_accessor* a, const double* val,size_t *len){
  grib_accessor_g1bitmap* self = (grib_accessor_g1bitmap*)a;

  size_t tlen;

  unsigned char* buf = NULL;
  long i;
  int err = 0;
  long pos = 0;
  long bmaplen = 0;
  const int bit_padding = 16;
  double miss_values = 0;
  tlen = ((*len+bit_padding-1)/bit_padding*bit_padding)/8;

  if((err = grib_get_double_internal(a->parent->h, self->missing_value, &miss_values))
      != GRIB_SUCCESS)
    return err;

  buf = grib_context_malloc_clear(a->parent->h->context,tlen);
  if(!buf) return GRIB_OUT_OF_MEMORY;
  pos=0;
  for(i=0;i<*len;i++)
  {
    if (val[i] == miss_values)
      pos++;
    else{
      bmaplen++;
      grib_set_bit_on(buf, &pos);
    }
  }

  if((err = grib_set_long_internal(a->parent->h, self->unusedBits,tlen*8 - *len ))
      != GRIB_SUCCESS)
    return err;

  grib_buffer_replace(a, buf, tlen,1,1);

  grib_context_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_dummy_field* self =  (grib_accessor_data_dummy_field*)a;


    size_t n_vals = *len;
    int err = 0;

    long   bits_per_value = 0;

    long   half_byte = 0;

    size_t buflen = 0;
    unsigned char*  buf = NULL;

    if (*len ==0) return GRIB_NO_VALUES;

    if((err = grib_get_long_internal(a->parent->h,self->bits_per_value,&bits_per_value)) != GRIB_SUCCESS)
        return err;

    buflen = (1+((bits_per_value*n_vals)/8))*sizeof(unsigned char);

    buf = (unsigned char*)grib_context_malloc_clear(a->parent->h->context,buflen);
    if (!buf) return GRIB_OUT_OF_MEMORY;

    half_byte = (buflen*8)-((*len)*bits_per_value);

    if((err = grib_set_long_internal(a->parent->h,self->half_byte, half_byte)) != GRIB_SUCCESS) {
        grib_context_free(a->parent->h->context,buf);
        return err;
    }
    grib_buffer_replace(a, buf, buflen,1,1);

    grib_context_free(a->parent->h->context,buf);

    return GRIB_SUCCESS;

}
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_raw_packing *self =(grib_accessor_data_raw_packing*)a;

  int bytes = 0;
  unsigned char* buffer = NULL;

  long precision = 0;

  double*  values = (double*)val;
  size_t inlen = *len;

  int free_buffer = 0;
  int free_values = 0;

  int code = GRIB_SUCCESS;


  size_t bufsize = 0;

  if (*len ==0) return GRIB_NO_VALUES;

  if((code = grib_get_long_internal(a->parent->h,self->precision,&precision))
      != GRIB_SUCCESS)
    return code;

  self->dirty=1;

  switch(precision)
  {
    case 1:
      bytes = 4;
      break;

    case 2:
      bytes = 8;
      break;

    default:
      code = GRIB_NOT_IMPLEMENTED;
      goto clean_up;
      break;
  }

  bufsize = bytes*inlen;

  buffer = grib_context_malloc(a->parent->h->context, bufsize);

  if(!buffer)
  {
    code = GRIB_OUT_OF_MEMORY;
    goto clean_up;
  }

  code=grib_ieee_encode_array(a->parent->h->context,values,inlen,bytes,buffer);

clean_up:
  if(free_buffer) free(buffer);
  if(free_values) free(values);

  grib_buffer_replace(a, buffer, bufsize,1,1);

  grib_context_buffer_free(a->parent->h->context,buffer);

  code = grib_set_long(a->parent->h,self->number_of_values, inlen);
  if(code==GRIB_READ_ONLY) code=0;

  return code;

}
int pack_long_unsigned_helper(grib_accessor* a, const long* val, size_t *len, int check)
{
    grib_accessor_unsigned* self = (grib_accessor_unsigned*)a;
    int ret = 0;
    long off = 0;
    long rlen = 0;
    int err = 0;

    size_t buflen  = 0;
    unsigned char *buf = NULL;
    unsigned long i = 0;
    unsigned long missing = 0;

    err=grib_value_count(a,&rlen);
    if (err) return err;

    if(a->flags & GRIB_ACCESSOR_FLAG_CAN_BE_MISSING)
    {
        Assert(self->nbytes <= 4);
        missing = ones[self->nbytes];
    }

    if (a->flags & GRIB_ACCESSOR_FLAG_TRANSIENT) {
        a->vvalue->lval=val[0];

        if(missing && val[0] == GRIB_MISSING_LONG)
            a->vvalue->missing=1;
        else
            a->vvalue->missing=0;

        return GRIB_SUCCESS;
    }

    if(*len < 1)
    {
        grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, "Wrong size for %s it contains %d values ", a->name , 1 );
        len[0] = 0;
        return GRIB_ARRAY_TOO_SMALL;
    }

    if (rlen == 1) {
        long v = val[0];

        if (missing)
            if(v == GRIB_MISSING_LONG)
                v = missing;

        /* Check if value fits into number of bits */
        if (check) {
            const long nbits = self->nbytes*8;
            /* See GRIB-23 and GRIB-262 */
            if (! value_is_missing(v) ) {
                if (v < 0) {
                    grib_context_log(a->parent->h->context, GRIB_LOG_ERROR,
                            "Key \"%s\": Trying to encode a negative value of %ld for key of type unsigned\n", a->name, v);
                    return GRIB_ENCODING_ERROR;
                }
                if (nbits < 32) {
                    unsigned long maxval = (1 << nbits)-1;
                    if (v > maxval) {
                        grib_context_log(a->parent->h->context, GRIB_LOG_ERROR,
                                "Key \"%s\": Trying to encode value of %ld but the maximum allowable value is %ld (number of bits=%ld)\n",
                                a->name, v, maxval, nbits);
                        return GRIB_ENCODING_ERROR;
                    }
                }
            }
        }

        off = a->offset*8;
        ret = grib_encode_unsigned_long(a->parent->h->buffer->data, v, &off, self->nbytes*8);
        if (ret == GRIB_SUCCESS) len[0] = 1;
        if (*len > 1)  grib_context_log(a->parent->h->context, GRIB_LOG_WARNING, "grib_accessor_unsigned : Trying to pack %d values in a scalar %s, packing first value",  *len, a->name  );
        len[0] = 1;
        return ret;
    }

    /* TODO: We assume that there are no missing values if there are more that 1 value */
    buflen = *len*self->nbytes;

    buf = (unsigned char*)grib_context_malloc(a->parent->h->context,buflen);

    for(i=0; i < *len;i++)
        grib_encode_unsigned_long(buf, val[i] ,  &off,  self->nbytes*8);

    ret = grib_set_long_internal(a->parent->h,grib_arguments_get_name(a->parent->h,self->arg,0),*len);

    if(ret == GRIB_SUCCESS)
        grib_buffer_replace(a, buf, buflen,1,1);
    else
        *len = 0;

    grib_context_free(a->parent->h->context,buf);
    return ret;
}
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;

}
예제 #12
0
static int notify_change(grib_action* act, grib_accessor * notified,
                         grib_accessor* changed)
{

  grib_loader loader = { 0,};

  grib_section *old_section = NULL;
  grib_handle *h = notified->parent->h;
  size_t len = 0;
  size_t size = 0;
  int err=0;
  grib_handle* tmp_handle;
  int doit = 0;

  grib_action* la        = NULL;

  grib_context_log(h->context,
      GRIB_LOG_DEBUG,"------------- SECTION action %s (%s) is triggerred by [%s]",
      act->name, notified->name, changed->name);

  la = grib_action_reparse(act,notified,&doit);
  old_section = notified->sub_section;
  Assert(old_section);

  Assert(old_section->h == h);

  /* printf("old = %p\n",(void*)old_section->branch); */
  /* printf("new = %p\n",(void*)la); */

  grib_context_log(h->context,
      GRIB_LOG_DEBUG,"------------- DOIT %ld OLD %p NEW %p",
      doit,old_section->branch,la);


  if(!doit) {
    if(la != NULL || old_section->branch != NULL)
      if(la == old_section->branch)
      {
        grib_context_log(h->context,GRIB_LOG_DEBUG,"IGNORING TRIGGER action %s (%s) is triggerred %p", act->name, notified->name
            ,(void*)la);
        return GRIB_SUCCESS;
      }
  }

  loader.list_is_resized = (la == old_section->branch);

  if (!strcmp(changed->name,"GRIBEditionNumber")) loader.changing_edition=1;
  else loader.changing_edition=0;

  old_section->branch = la;

  tmp_handle = grib_new_handle(h->context);
  if(!tmp_handle)
    return GRIB_OUT_OF_MEMORY;

  tmp_handle->buffer = grib_create_growable_buffer(h->context);
  Assert(tmp_handle->buffer); /* FIXME */

  loader.data          = h;
  loader.lookup_long   = grib_lookup_long_from_handle;
  loader.init_accessor = grib_init_accessor_from_handle;


  Assert(h->kid == NULL);
  tmp_handle->loader = &loader;
  tmp_handle->main  = h;
  h->kid = tmp_handle;
  /* printf("tmp_handle- main %p %p\n",(void*)tmp_handle,(void*)h); */

  grib_context_log(h->context,GRIB_LOG_DEBUG,"------------- CREATE TMP BLOCK ", act->name, notified->name);
  tmp_handle->root  = grib_section_create(tmp_handle,NULL);

  tmp_handle->use_trie=1;

  err=grib_create_accessor(tmp_handle->root, act, &loader);

  grib_section_adjust_sizes(tmp_handle->root,1,0);

  grib_section_post_init(tmp_handle->root);

  /* grib_recompute_sections_lengths(tmp_handle->root); */
  grib_get_block_length(tmp_handle->root,&len);
  grib_context_log(h->context,GRIB_LOG_DEBUG,"-------------  TMP BLOCK IS sectlen=%d buffer=%d", len,  tmp_handle->buffer->ulength);

#if 0
  if(h->context->debug > 10)
    grib_dump_content(tmp_handle,stdout,NULL,0,NULL);
#endif

  /* Assert(tmp_handle->buffer->ulength == len); */
  /* grib_empty_section(h->context,old_section); */

  grib_buffer_replace(notified, tmp_handle->buffer->data, tmp_handle->buffer->ulength,0,1);

  grib_swap_sections(old_section,
      tmp_handle->root->block->first->sub_section);

  Assert(tmp_handle->dependencies == NULL);
  /* printf("grib_handle_delete %p\n",(void*)tmp_handle); */


  grib_handle_delete(tmp_handle);

  h->use_trie = 1;
  h->trie_invalid=1;
  h->kid = NULL;

  grib_section_adjust_sizes(h->root,1,0);

  grib_section_post_init(h->root);

  grib_get_block_length(old_section,&size);

  grib_context_log(h->context,GRIB_LOG_DEBUG,"-------------   BLOCK SIZE %ld, buffer len=%ld", size,len);
  if(h->context->debug > 10)
    grib_dump_content(h,stdout,"debug",~0,NULL);

  Assert(size == len);

  grib_update_paddings(old_section);


  return err;
}
static int pack_long(grib_accessor* a, const long* val, size_t *len)
{
  grib_accessor_signed* self = (grib_accessor_signed*)a;
  int ret = 0;
  long off = 0;
    unsigned long rlen = 0;
    int err=0;
    long count=0;
  size_t buflen  = 0;
  unsigned char *buf = NULL;
  unsigned long i = 0;
  long missing = 0;

    err=grib_value_count(a,&count);
    if (err) return err;
    rlen=count;

  if(*len < 1)
  {
    grib_context_log(a->parent->h->context, GRIB_LOG_ERROR, "Wrong size for %s it contains %d values ", a->name , 1 );
    len[0] = 0;
    return GRIB_ARRAY_TOO_SMALL;
  }

  if(a->flags & GRIB_ACCESSOR_FLAG_CAN_BE_MISSING)
      {
            Assert(self->nbytes <= 4);
                missing = ones[self->nbytes];
                }

  if (rlen == 1){
    long v = val[0];
    if(missing)
         if(v == GRIB_MISSING_LONG)
                     v = missing;

    off = a->offset;
    ret = grib_encode_signed_long(a->parent->h->buffer->data, v ,  off,  a->length);
    if (ret == GRIB_SUCCESS) len[0] = 1;
    if (*len > 1)  grib_context_log(a->parent->h->context, GRIB_LOG_WARNING, "grib_accessor_signed : Trying to pack %d values in a scalar %s, packing first value",  *len, a->name  );
    len[0] = 1;
    return ret;
  }

   /* TODO: We assume that there are no missing values if there are more that 1 value */

  buflen = *len*a->length;

  buf = (unsigned char*)grib_context_malloc(a->parent->h->context,buflen);

  for(i=0; i < *len;i++){
    grib_encode_signed_long(buf, val[i] ,  off,  a->length);
    off+=  a->length;
  }
  ret = grib_set_long_internal(a->parent->h,grib_arguments_get_name(a->parent->h,self->arg,0),*len);

  if(ret == GRIB_SUCCESS)
    grib_buffer_replace(a, buf, buflen,1,1);
  else
    *len = 0;

  grib_context_free(a->parent->h->context,buf);
  return ret;
}
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;
}