Tidying up indentation

This commit is contained in:
Shahram Najm 2013-05-10 11:33:56 +01:00
parent b4a4f7855c
commit f8e9cf0032
4 changed files with 470 additions and 482 deletions

View File

@ -141,7 +141,7 @@ static void init_class(grib_accessor_class* c)
/* END_CLASS_IMP */ /* END_CLASS_IMP */
static int grib_load_codetable(grib_context* c,const char* filename, static int grib_load_codetable(grib_context* c,const char* filename,
const char* recomposed_name,size_t size,grib_codetable* t); const char* recomposed_name,size_t size,grib_codetable* t);
static void init(grib_accessor* a, const long len, grib_arguments* params) { static void init(grib_accessor* a, const long len, grib_arguments* params) {
int n=0; int n=0;
grib_accessor_codetable* self = (grib_accessor_codetable*)a; grib_accessor_codetable* self = (grib_accessor_codetable*)a;
@ -381,7 +381,6 @@ static int grib_load_codetable(grib_context* c,const char* filename,
t->entries[code].title = grib_context_strdup_persistent(c,title); t->entries[code].title = grib_context_strdup_persistent(c,title);
t->entries[code].units = grib_context_strdup_persistent(c,units); t->entries[code].units = grib_context_strdup_persistent(c,units);
} }
fclose(f); fclose(f);
@ -416,8 +415,6 @@ void grib_codetable_delete(grib_context* c) {
} }
static void dump(grib_accessor* a, grib_dumper* dumper) { static void dump(grib_accessor* a, grib_dumper* dumper) {
grib_accessor_codetable* self = (grib_accessor_codetable*)a; grib_accessor_codetable* self = (grib_accessor_codetable*)a;
char comment[2048]; char comment[2048];
@ -632,7 +629,6 @@ static void destroy(grib_context* context,grib_accessor* a)
grib_context_free(context, a->vvalue); grib_context_free(context, a->vvalue);
a->vvalue=NULL; a->vvalue=NULL;
} }
} }
static int get_native_type(grib_accessor* a){ static int get_native_type(grib_accessor* a){

View File

@ -149,195 +149,193 @@ static void init_class(grib_accessor_class* c)
static void init(grib_accessor* a,const long v, grib_arguments* args) static void init(grib_accessor* a,const long v, grib_arguments* args)
{ {
grib_accessor_data_g1simple_packing *self =(grib_accessor_data_g1simple_packing*)a; grib_accessor_data_g1simple_packing *self =(grib_accessor_data_g1simple_packing*)a;
self->half_byte = grib_arguments_get_name(a->parent->h,args,self->carg++); self->half_byte = grib_arguments_get_name(a->parent->h,args,self->carg++);
self->packingType = grib_arguments_get_name(a->parent->h,args,self->carg++); self->packingType = grib_arguments_get_name(a->parent->h,args,self->carg++);
self->ieee_packing = grib_arguments_get_name(a->parent->h,args,self->carg++); self->ieee_packing = grib_arguments_get_name(a->parent->h,args,self->carg++);
self->precision = grib_arguments_get_name(a->parent->h,args,self->carg++); self->precision = grib_arguments_get_name(a->parent->h,args,self->carg++);
self->edition=1; self->edition=1;
a->flags |= GRIB_ACCESSOR_FLAG_DATA; a->flags |= GRIB_ACCESSOR_FLAG_DATA;
} }
static long value_count(grib_accessor* a) static long value_count(grib_accessor* a)
{ {
long number_of_values; long number_of_values;
grib_accessor_data_g1simple_packing *self =(grib_accessor_data_g1simple_packing*)a; grib_accessor_data_g1simple_packing *self =(grib_accessor_data_g1simple_packing*)a;
/* Special case for when values are cleared */
/* Special case for when values are cleared */ /*if(a->length == 0)
/*if(a->length == 0)
return 0;*/ return 0;*/
if(grib_get_long_internal(a->parent->h,self->number_of_values,&number_of_values) != GRIB_SUCCESS) if(grib_get_long_internal(a->parent->h,self->number_of_values,&number_of_values) != GRIB_SUCCESS)
return 0; return 0;
return number_of_values; return number_of_values;
} }
static int pack_double(grib_accessor* a, const double* cval, size_t *len) 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_data_g1simple_packing* self = (grib_accessor_data_g1simple_packing*)a;
grib_accessor_class* super = *(a->cclass->super); grib_accessor_class* super = *(a->cclass->super);
size_t n_vals = *len; size_t n_vals = *len;
long half_byte = 0; long half_byte = 0;
int ret = 0; int ret = 0;
long offsetdata = 0; long offsetdata = 0;
long offsetsection = 0; long offsetsection = 0;
double reference_value = 0; double reference_value = 0;
long binary_scale_factor = 0; long binary_scale_factor = 0;
long bits_per_value = 0; long bits_per_value = 0;
long decimal_scale_factor = 0; long decimal_scale_factor = 0;
double decimal = 1; double decimal = 1;
size_t buflen = 0; size_t buflen = 0;
unsigned char* buf = NULL; unsigned char* buf = NULL;
unsigned char* encoded = NULL; unsigned char* encoded = NULL;
double divisor = 1; double divisor = 1;
int i; int i;
long off = 0; long off = 0;
grib_context* c=a->parent->h->context; grib_context* c=a->parent->h->context;
grib_handle* h=a->parent->h; grib_handle* h=a->parent->h;
char* ieee_packing_s=NULL; char* ieee_packing_s=NULL;
char* packingType_s=NULL; char* packingType_s=NULL;
char* precision_s=NULL; char* precision_s=NULL;
double units_factor=1.0; double units_factor=1.0;
double units_bias=0.0; double units_bias=0.0;
double* val=(double*)cval; double* val=(double*)cval;
double missingValue=9999.0; double missingValue=9999.0;
long constantFieldHalfByte=0; long constantFieldHalfByte=0;
int err=0; int err=0;
if(*len != 0) { if(*len != 0) {
if(self->units_factor && if(self->units_factor &&
(grib_get_double_internal(a->parent->h,self->units_factor,&units_factor)== GRIB_SUCCESS)) { (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); 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);
}
} }
if(self->units_bias && ret = super->pack_double(a,val,len);
(grib_get_double_internal(a->parent->h,self->units_bias,&units_bias)== GRIB_SUCCESS)) { switch (ret) {
grib_set_double_internal(a->parent->h,self->units_bias,0.0); 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_SUCCESS:
break;
default:
grib_context_log(a->parent->h->context,GRIB_LOG_FATAL,"unable to compute packing parameters\n");
return ret;
} }
if (units_factor != 1.0) { if((ret = grib_get_double_internal(a->parent->h,self->reference_value, &reference_value))
if (units_bias != 0.0) != GRIB_SUCCESS)
for (i=0;i<n_vals;i++) val[i]=val[i]*units_factor+units_bias; return ret;
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) { if((ret = grib_get_long_internal(a->parent->h,self->binary_scale_factor, &binary_scale_factor))
long precision=c->ieee_packing==32 ? 1 : 2; != GRIB_SUCCESS)
size_t lenstr=strlen(self->ieee_packing); return ret;
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); if((ret = grib_get_long_internal(a->parent->h,self->bits_per_value,&bits_per_value)) !=
switch (ret) { GRIB_SUCCESS)
case GRIB_CONSTANT_FIELD: return ret;
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_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)) if((ret = grib_get_long_internal(a->parent->h,self->decimal_scale_factor, &decimal_scale_factor))
!= GRIB_SUCCESS) != GRIB_SUCCESS)
return ret; return ret;
if((ret = grib_get_long_internal(a->parent->h,self->binary_scale_factor, &binary_scale_factor)) if((ret = grib_get_long_internal(a->parent->h,self->offsetdata,&offsetdata)) != GRIB_SUCCESS)
!= GRIB_SUCCESS) return ret;
return ret;
if((ret = grib_get_long_internal(a->parent->h,self->bits_per_value,&bits_per_value)) != if((ret = grib_get_long_internal(a->parent->h,self->offsetsection,&offsetsection)) != GRIB_SUCCESS)
GRIB_SUCCESS) return ret;
return ret;
if((ret = grib_get_long_internal(a->parent->h,self->decimal_scale_factor, &decimal_scale_factor)) decimal = grib_power(decimal_scale_factor,10) ;
!= GRIB_SUCCESS) divisor = grib_power(-binary_scale_factor,2);
return ret;
if((ret = grib_get_long_internal(a->parent->h,self->offsetdata,&offsetdata)) != GRIB_SUCCESS) buflen = (((bits_per_value*n_vals)+7)/8)*sizeof(unsigned char);
return ret; if((buflen + (offsetdata-offsetsection)) %2) {
buflen++;
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->length++;
a->parent->h->buffer->ulength++; a->parent->h->buffer->ulength++;
*/ */
} }
half_byte = (buflen*8)-((*len)*bits_per_value); half_byte = (buflen*8)-((*len)*bits_per_value);
grib_context_log(a->parent->h->context,GRIB_LOG_DEBUG, grib_context_log(a->parent->h->context,GRIB_LOG_DEBUG,
"HALF byte: buflen=%d bits_per_value=%ld len=%d half_byte=%ld\n", "HALF byte: buflen=%d bits_per_value=%ld len=%d half_byte=%ld\n",
buflen,bits_per_value,*len,half_byte); buflen,bits_per_value,*len,half_byte);
Assert(half_byte <= 0x0f); Assert(half_byte <= 0x0f);
if((ret = grib_set_long_internal(a->parent->h,self->half_byte, half_byte)) if((ret = grib_set_long_internal(a->parent->h,self->half_byte, half_byte))
!= GRIB_SUCCESS) != GRIB_SUCCESS)
return ret; return ret;
buf = grib_context_buffer_malloc_clear(a->parent->h->context,buflen); buf = grib_context_buffer_malloc_clear(a->parent->h->context,buflen);
encoded = buf; encoded = buf;
grib_encode_double_array(n_vals,val,bits_per_value,reference_value,decimal,divisor,encoded,&off); 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_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_accessor_data_g1simple_packing : pack_double : packing %s, %d values", a->name, n_vals);
grib_buffer_replace(a, buf, buflen,1,1); grib_buffer_replace(a, buf, buflen,1,1);
grib_context_buffer_free(a->parent->h->context,buf); grib_context_buffer_free(a->parent->h->context,buf);
return GRIB_SUCCESS; return GRIB_SUCCESS;
} }

View File

@ -142,122 +142,120 @@ static void init_class(grib_accessor_class* c)
static void init(grib_accessor* a,const long v, grib_arguments* args) static void init(grib_accessor* a,const long v, grib_arguments* args)
{ {
grib_accessor_data_g2simple_packing *self =(grib_accessor_data_g2simple_packing*)a; grib_accessor_data_g2simple_packing *self =(grib_accessor_data_g2simple_packing*)a;
a->flags |= GRIB_ACCESSOR_FLAG_DATA; a->flags |= GRIB_ACCESSOR_FLAG_DATA;
self->edition=2; self->edition=2;
} }
static long value_count(grib_accessor* a) static long value_count(grib_accessor* a)
{ {
grib_accessor_data_g2simple_packing *self =(grib_accessor_data_g2simple_packing*)a; grib_accessor_data_g2simple_packing *self =(grib_accessor_data_g2simple_packing*)a;
long n_vals= 0; long n_vals= 0;
if(grib_get_long_internal(a->parent->h,self->number_of_values,&n_vals) != GRIB_SUCCESS) if(grib_get_long_internal(a->parent->h,self->number_of_values,&n_vals) != GRIB_SUCCESS)
return 0; return 0;
return n_vals; return n_vals;
} }
static int pack_double(grib_accessor* a, const double* cval, size_t *len) static int pack_double(grib_accessor* a, const double* cval, size_t *len)
{ {
grib_accessor_data_g2simple_packing* self = (grib_accessor_data_g2simple_packing*)a; grib_accessor_data_g2simple_packing* self = (grib_accessor_data_g2simple_packing*)a;
grib_accessor_class* super = *(a->cclass->super); grib_accessor_class* super = *(a->cclass->super);
size_t n_vals = *len; size_t n_vals = *len;
double reference_value = 0; double reference_value = 0;
long binary_scale_factor = 0; long binary_scale_factor = 0;
long bits_per_value = 0; long bits_per_value = 0;
long decimal_scale_factor = 0; long decimal_scale_factor = 0;
double decimal = 1; double decimal = 1;
size_t buflen = 0; size_t buflen = 0;
unsigned char* buf = NULL; unsigned char* buf = NULL;
unsigned char* encoded = NULL; unsigned char* encoded = NULL;
double divisor = 1; double divisor = 1;
long off = 0; long off = 0;
int ret =0; int ret =0;
double units_factor=1.0; double units_factor=1.0;
double units_bias=0.0; double units_bias=0.0;
double* val=(double*)cval; double* val=(double*)cval;
int i; int i;
if(*len == 0) { if(*len == 0) {
grib_buffer_replace(a, NULL, 0,1,1); grib_buffer_replace(a, NULL, 0,1,1);
return GRIB_SUCCESS; return GRIB_SUCCESS;
}
if(ret == GRIB_SUCCESS)
ret = grib_set_long_internal(a->parent->h,self->number_of_values, *len);
if (ret!=GRIB_SUCCESS) return ret;
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); if(ret == GRIB_SUCCESS)
switch (ret) { ret = grib_set_long_internal(a->parent->h,self->number_of_values, *len);
case GRIB_CONSTANT_FIELD:
grib_buffer_replace(a, NULL, 0,1,1);
return GRIB_SUCCESS;
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)) if (ret!=GRIB_SUCCESS) return ret;
!= GRIB_SUCCESS)
return ret;
if((ret = grib_get_long_internal(a->parent->h,self->binary_scale_factor, &binary_scale_factor)) if(self->units_factor &&
!= GRIB_SUCCESS) (grib_get_double_internal(a->parent->h,self->units_factor,&units_factor)== GRIB_SUCCESS)) {
return ret; grib_set_double_internal(a->parent->h,self->units_factor,1.0);
}
if((ret = grib_get_long_internal(a->parent->h,self->bits_per_value,&bits_per_value)) != if(self->units_bias &&
GRIB_SUCCESS) (grib_get_double_internal(a->parent->h,self->units_bias,&units_bias)== GRIB_SUCCESS)) {
return ret; grib_set_double_internal(a->parent->h,self->units_bias,0.0);
}
if((ret = grib_get_long_internal(a->parent->h,self->decimal_scale_factor, &decimal_scale_factor)) if (units_factor != 1.0) {
!= GRIB_SUCCESS) if (units_bias != 0.0)
return ret; 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;
decimal = grib_power(decimal_scale_factor,10) ; ret = super->pack_double(a,val,len);
divisor = grib_power(-binary_scale_factor,2); switch (ret) {
case GRIB_CONSTANT_FIELD:
grib_buffer_replace(a, NULL, 0,1,1);
return GRIB_SUCCESS;
case GRIB_SUCCESS:
break;
default:
grib_context_log(a->parent->h->context,GRIB_LOG_ERROR,"unable to compute packing parameters\n");
return ret;
}
buflen = (((bits_per_value*n_vals)+7)/8)*sizeof(unsigned char); if((ret = grib_get_double_internal(a->parent->h,self->reference_value, &reference_value))
buf = grib_context_buffer_malloc_clear(a->parent->h->context,buflen); != GRIB_SUCCESS)
encoded = buf; return ret;
grib_encode_double_array(n_vals,val,bits_per_value,reference_value,decimal,divisor,encoded,&off); if((ret = grib_get_long_internal(a->parent->h,self->binary_scale_factor, &binary_scale_factor))
!= GRIB_SUCCESS)
return ret;
grib_context_log(a->parent->h->context, GRIB_LOG_DEBUG, if((ret = grib_get_long_internal(a->parent->h,self->bits_per_value,&bits_per_value)) !=
"grib_accessor_data_g2simple_packing : pack_double : packing %s, %d values", a->name, n_vals); GRIB_SUCCESS)
return ret;
grib_buffer_replace(a, buf, buflen,1,1); if((ret = grib_get_long_internal(a->parent->h,self->decimal_scale_factor, &decimal_scale_factor))
!= GRIB_SUCCESS)
return ret;
grib_context_buffer_free(a->parent->h->context,buf); decimal = grib_power(decimal_scale_factor,10) ;
divisor = grib_power(-binary_scale_factor,2);
return ret; buflen = (((bits_per_value*n_vals)+7)/8)*sizeof(unsigned char);
buf = 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_g2simple_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 ret;
} }
static int pack_bytes(grib_accessor* a, const unsigned char* val, size_t *len) static int pack_bytes(grib_accessor* a, const unsigned char* val, size_t *len)
{ {
size_t length = *len; size_t length = *len;
grib_buffer_replace(a, val, length,1,1); grib_buffer_replace(a, val, length,1,1);
return GRIB_SUCCESS; return GRIB_SUCCESS;
} }

View File

@ -90,260 +90,256 @@ static void init_class(grib_nearest_class* c)
static int init(grib_nearest* nearest,grib_handle* h,grib_arguments* args) static int init(grib_nearest* nearest,grib_handle* h,grib_arguments* args)
{ {
grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*) nearest; grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*) nearest;
self->Nj = grib_arguments_get_name(h,args,self->cargs++); self->Nj = grib_arguments_get_name(h,args,self->cargs++);
self->pl = grib_arguments_get_name(h,args,self->cargs++); self->pl = grib_arguments_get_name(h,args,self->cargs++);
self->lonFirst = grib_arguments_get_name(h,args,self->cargs++); self->lonFirst = grib_arguments_get_name(h,args,self->cargs++);
self->lonLast = grib_arguments_get_name(h,args,self->cargs++); self->lonLast = grib_arguments_get_name(h,args,self->cargs++);
self->j=(int*)grib_context_malloc(h->context,2*sizeof(int)); self->j=(int*)grib_context_malloc(h->context,2*sizeof(int));
if (!self->j) return GRIB_OUT_OF_MEMORY; if (!self->j) return GRIB_OUT_OF_MEMORY;
self->k=(int*)grib_context_malloc( nearest->context,4*sizeof(int)); self->k=(int*)grib_context_malloc( nearest->context,4*sizeof(int));
if (!self->k) return GRIB_OUT_OF_MEMORY; if (!self->k) return GRIB_OUT_OF_MEMORY;
return 0;
return 0;
} }
static int find(grib_nearest* nearest, grib_handle* h, static int find(grib_nearest* nearest, grib_handle* h,
double inlat, double inlon,unsigned long flags, double inlat, double inlon,unsigned long flags,
double* outlats,double* outlons, double *values, double* outlats,double* outlons, double *values,
double *distances,int *indexes, size_t *len) { double *distances,int *indexes, size_t *len) {
grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*) nearest; grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*) nearest;
int ret=0,kk=0,ii=0,jj=0; int ret=0,kk=0,ii=0,jj=0;
int j=0; int j=0;
long* pla=NULL; long* pla=NULL;
long* pl=NULL; long* pl=NULL;
size_t nvalues=0; size_t nvalues=0;
grib_iterator* iter=NULL; grib_iterator* iter=NULL;
double lat=0,lon=0; double lat=0,lon=0;
long iradius; long iradius;
double radius; double radius;
int ilat=0,ilon=0; int ilat=0,ilon=0;
if( (ret = grib_get_size(h,self->values_key,&nvalues))!= GRIB_SUCCESS) if( (ret = grib_get_size(h,self->values_key,&nvalues))!= GRIB_SUCCESS)
return ret; return ret;
nearest->values_count = nvalues; nearest->values_count = nvalues;
if (grib_is_missing(h,self->radius,&ret)) { if (grib_is_missing(h,self->radius,&ret)) {
grib_context_log(h->context, GRIB_LOG_DEBUG,"Key '%s' is missing", self->radius); grib_context_log(h->context, GRIB_LOG_DEBUG,"Key '%s' is missing", self->radius);
return ret ? ret : GRIB_GEOCALCULUS_PROBLEM; return ret ? ret : GRIB_GEOCALCULUS_PROBLEM;
} }
if( (ret = grib_get_long(h,self->radius,&iradius))!= GRIB_SUCCESS) if( (ret = grib_get_long(h,self->radius,&iradius))!= GRIB_SUCCESS)
return ret; return ret;
radius=((double)iradius)/1000.0; radius=((double)iradius)/1000.0;
if (!nearest->h || (flags & GRIB_NEAREST_SAME_GRID)==0) { if (!nearest->h || (flags & GRIB_NEAREST_SAME_GRID)==0) {
double dummy=0; double dummy=0;
double olat=1.e10; double olat=1.e10;
long n=0; long n=0;
ilat=0,ilon=0; ilat=0,ilon=0;
if (grib_is_missing(h,self->Nj,&ret)) { if (grib_is_missing(h,self->Nj,&ret)) {
grib_context_log(h->context, GRIB_LOG_DEBUG,"Key '%s' is missing", self->Nj); grib_context_log(h->context, GRIB_LOG_DEBUG,"Key '%s' is missing", self->Nj);
return ret ? ret : GRIB_GEOCALCULUS_PROBLEM; return ret ? ret : GRIB_GEOCALCULUS_PROBLEM;
}
if( (ret = grib_get_long(h,self->Nj,&n))!= GRIB_SUCCESS)
return ret;
self->lats_count=n;
if (self->lats) grib_context_free(nearest->context,self->lats);
self->lats=grib_context_malloc( nearest->context,
self->lats_count* sizeof(double));
if (!self->lats) return GRIB_OUT_OF_MEMORY;
if (self->lons) grib_context_free(nearest->context,self->lons);
self->lons=grib_context_malloc( nearest->context,
nearest->values_count*sizeof(double));
if (!self->lons) return GRIB_OUT_OF_MEMORY;
iter=grib_iterator_new(h,0,&ret);
while(grib_iterator_next(iter,&lat,&lon,&dummy)) {
if (olat!=lat) {
self->lats[ilat++]=lat;
olat=lat;
} }
self->lons[ilon++]=lon;
}
self->lats_count=ilat;
grib_iterator_delete(iter);
}
nearest->h=h;
if (!self->distances || (flags & GRIB_NEAREST_SAME_POINT)==0 if ( (ret = grib_get_long(h,self->Nj,&n))!= GRIB_SUCCESS)
|| (flags & GRIB_NEAREST_SAME_GRID)==0) {
double* lons=NULL;
int nlon=0;
size_t plsize=0;
long nplm1=0;
int nearest_lons_found=0;
double lon_first,lon_last;
int islocal=0;
long plmax;
double dimin;
if ((ret=grib_get_double(h,self->lonFirst,&lon_first))!=GRIB_SUCCESS) {
grib_context_log(h->context,GRIB_LOG_ERROR,
"grib_nearest_latlon_reduced.find(): unable to get %s %s\n",self->lonFirst,
grib_get_error_message(ret));
return ret; return ret;
self->lats_count=n;
if (self->lats) grib_context_free(nearest->context,self->lats);
self->lats=grib_context_malloc( nearest->context,
self->lats_count* sizeof(double));
if (!self->lats) return GRIB_OUT_OF_MEMORY;
if (self->lons) grib_context_free(nearest->context,self->lons);
self->lons=grib_context_malloc( nearest->context,
nearest->values_count*sizeof(double));
if (!self->lons) return GRIB_OUT_OF_MEMORY;
iter=grib_iterator_new(h,0,&ret);
while(grib_iterator_next(iter,&lat,&lon,&dummy)) {
if (olat!=lat) {
self->lats[ilat++]=lat;
olat=lat;
}
self->lons[ilon++]=lon;
}
self->lats_count=ilat;
grib_iterator_delete(iter);
} }
if ((ret=grib_get_double(h,self->lonLast,&lon_last))!=GRIB_SUCCESS) { nearest->h=h;
if (!self->distances || (flags & GRIB_NEAREST_SAME_POINT)==0
|| (flags & GRIB_NEAREST_SAME_GRID)==0) {
double* lons=NULL;
int nlon=0;
size_t plsize=0;
long nplm1=0;
int nearest_lons_found=0;
double lon_first,lon_last;
int islocal=0;
long plmax;
double dimin;
if ((ret=grib_get_double(h,self->lonFirst,&lon_first))!=GRIB_SUCCESS) {
grib_context_log(h->context,GRIB_LOG_ERROR, grib_context_log(h->context,GRIB_LOG_ERROR,
"grib_nearest_latlon_reduced.find(): unable to get %s %s\n",self->lonLast, "grib_nearest_latlon_reduced.find(): unable to get %s %s\n",self->lonFirst,
grib_get_error_message(ret)); grib_get_error_message(ret));
return ret; return ret;
}
if ((ret=grib_get_double(h,self->lonLast,&lon_last))!=GRIB_SUCCESS) {
grib_context_log(h->context,GRIB_LOG_ERROR,
"grib_nearest_latlon_reduced.find(): unable to get %s %s\n",self->lonLast,
grib_get_error_message(ret));
return ret;
}
plsize=self->lats_count;
if( (ret=grib_get_size(h,self->pl,&plsize))!= GRIB_SUCCESS)
return ret;
pla=grib_context_malloc(h->context,plsize*sizeof(long));
if (!pla) return GRIB_OUT_OF_MEMORY;
if( (ret=grib_get_long_array(h,self->pl,pla,&plsize))!= GRIB_SUCCESS)
return ret;
pl=pla;
while ((*pl)==0) {pl++;}
plmax=pla[0];
for (j=0;j<plsize;j++) if (plmax<pla[j]) plmax=pla[j];
dimin=360.0/plmax;
if ( 360-fabs(lon_last-lon_first) < 2 * dimin ) {islocal=0;}
else {islocal=1;}
if (islocal)
for (j=0;j<plsize;j++) pla[j]--;
/* printf("XXXX islocal=%d\n",islocal); */
while (inlon<0) inlon+=360;
while (inlon>360) inlon-=360;
ilat=self->lats_count;
if (self->lats[ilat-1] > self->lats[0]) {
if (inlat < self->lats[0] || inlat > self->lats[ilat-1])
return GRIB_OUT_OF_AREA;
} else {
if (inlat > self->lats[0] || inlat < self->lats[ilat-1])
return GRIB_OUT_OF_AREA;
}
if (!self->distances)
self->distances=(double*)grib_context_malloc( nearest->context,4*sizeof(double));
if (!self->distances) return GRIB_OUT_OF_MEMORY;
grib_binary_search(self->lats,ilat-1,inlat,
&(self->j[0]),&(self->j[1]));
nlon=0;
for (jj=0;jj<self->j[0];jj++) nlon+=pl[jj];
nplm1=pl[self->j[0]]-1;
lons=self->lons+nlon;
nearest_lons_found=0;
if (lons[nplm1]>lons[0]) {
if (inlon< lons[0] || inlon > lons[nplm1]) {
if (lons[nplm1]-lons[0]-360 <=
lons[nplm1]-lons[nplm1-1]) {
self->k[0]=0;
self->k[1]=nplm1;
nearest_lons_found=1;
} else return GRIB_OUT_OF_AREA;
}
} else {
if (inlon >lons[0] || inlon< lons[nplm1]) {
if (lons[0]-lons[nplm1]-360 <=
lons[0]-lons[1]) {
self->k[0]=0;
self->k[1]=nplm1;
nearest_lons_found=1;
} else return GRIB_OUT_OF_AREA;
}
}
if (!nearest_lons_found) {
grib_binary_search(lons,pl[self->j[0]]-1,inlon,
&(self->k[0]),&(self->k[1]));
}
self->k[0]+=nlon;
self->k[1]+=nlon;
nlon=0;
for (jj=0;jj<self->j[1];jj++) nlon+=pl[jj];
nplm1=pl[self->j[1]]-1;
lons=self->lons+nlon;
nearest_lons_found=0;
if (lons[nplm1]>lons[0]) {
if (inlon<lons[0] || inlon>lons[nplm1]) {
if (lons[nplm1]-lons[0]-360 <=
lons[nplm1]-lons[nplm1-1]) {
self->k[2]=0;
self->k[3]=nplm1;
nearest_lons_found=1;
} else return GRIB_OUT_OF_AREA;
}
} else {
if (inlon>lons[0] || inlon<lons[nplm1]) {
if (lons[0]-lons[nplm1]-360 <=
lons[0]-lons[1]) {
self->k[2]=0;
self->k[3]=nplm1;
nearest_lons_found=1;
} else return GRIB_OUT_OF_AREA;
}
}
if (!nearest_lons_found) {
grib_binary_search(lons,pl[self->j[1]]-1,inlon,
&(self->k[2]),&(self->k[3]));
}
self->k[2]+=nlon;
self->k[3]+=nlon;
kk=0;
for (jj=0;jj<2;jj++) {
for (ii=0;ii<2;ii++) {
self->distances[kk]=grib_nearest_distance(radius,inlon,inlat,
self->lons[self->k[kk]],self->lats[self->j[jj]]);
kk++;
}
}
grib_context_free(h->context,pla);
} }
plsize=self->lats_count; kk=0;
if( (ret=grib_get_size(h,self->pl,&plsize))!= GRIB_SUCCESS) for (jj=0;jj<2;jj++) {
return ret; for (ii=0;ii<2;ii++) {
pla=grib_context_malloc(h->context,plsize*sizeof(long)); distances[kk]=self->distances[kk];
if (!pla) return GRIB_OUT_OF_MEMORY; outlats[kk]=self->lats[self->j[jj]];
if( (ret=grib_get_long_array(h,self->pl,pla,&plsize))!= GRIB_SUCCESS) outlons[kk]=self->lons[self->k[kk]];
return ret; grib_get_double_element_internal(h,self->values_key,self->k[kk],&(values[kk]));
indexes[kk]=self->k[kk];
pl=pla; kk++;
while ((*pl)==0) {pl++;} }
plmax=pla[0];
for (j=0;j<plsize;j++) if (plmax<pla[j]) plmax=pla[j];
dimin=360.0/plmax;
if ( 360-fabs(lon_last-lon_first) < 2 * dimin ) {islocal=0;}
else {islocal=1;}
if (islocal)
for (j=0;j<plsize;j++) pla[j]--;
/* printf("XXXX islocal=%d\n",islocal); */
while (inlon<0) inlon+=360;
while (inlon>360) inlon-=360;
ilat=self->lats_count;
if (self->lats[ilat-1] > self->lats[0]) {
if (inlat < self->lats[0] || inlat > self->lats[ilat-1])
return GRIB_OUT_OF_AREA;
} else {
if (inlat > self->lats[0] || inlat < self->lats[ilat-1])
return GRIB_OUT_OF_AREA;
}
if (!self->distances)
self->distances=(double*)grib_context_malloc( nearest->context,4*sizeof(double));
if (!self->distances) return GRIB_OUT_OF_MEMORY;
grib_binary_search(self->lats,ilat-1,inlat,
&(self->j[0]),&(self->j[1]));
nlon=0;
for (jj=0;jj<self->j[0];jj++) nlon+=pl[jj];
nplm1=pl[self->j[0]]-1;
lons=self->lons+nlon;
nearest_lons_found=0;
if (lons[nplm1]>lons[0]) {
if (inlon< lons[0] || inlon > lons[nplm1]) {
if (lons[nplm1]-lons[0]-360 <=
lons[nplm1]-lons[nplm1-1]) {
self->k[0]=0;
self->k[1]=nplm1;
nearest_lons_found=1;
} else return GRIB_OUT_OF_AREA;
}
} else {
if (inlon >lons[0] || inlon< lons[nplm1]) {
if (lons[0]-lons[nplm1]-360 <=
lons[0]-lons[1]) {
self->k[0]=0;
self->k[1]=nplm1;
nearest_lons_found=1;
} else return GRIB_OUT_OF_AREA;
}
}
if (!nearest_lons_found) {
grib_binary_search(lons,pl[self->j[0]]-1,inlon,
&(self->k[0]),&(self->k[1]));
}
self->k[0]+=nlon;
self->k[1]+=nlon;
nlon=0;
for (jj=0;jj<self->j[1];jj++) nlon+=pl[jj];
nplm1=pl[self->j[1]]-1;
lons=self->lons+nlon;
nearest_lons_found=0;
if (lons[nplm1]>lons[0]) {
if (inlon<lons[0] || inlon>lons[nplm1]) {
if (lons[nplm1]-lons[0]-360 <=
lons[nplm1]-lons[nplm1-1]) {
self->k[2]=0;
self->k[3]=nplm1;
nearest_lons_found=1;
} else return GRIB_OUT_OF_AREA;
}
} else {
if (inlon>lons[0] || inlon<lons[nplm1]) {
if (lons[0]-lons[nplm1]-360 <=
lons[0]-lons[1]) {
self->k[2]=0;
self->k[3]=nplm1;
nearest_lons_found=1;
} else return GRIB_OUT_OF_AREA;
}
}
if (!nearest_lons_found) {
grib_binary_search(lons,pl[self->j[1]]-1,inlon,
&(self->k[2]),&(self->k[3]));
} }
self->k[2]+=nlon; return GRIB_SUCCESS;
self->k[3]+=nlon;
kk=0;
for (jj=0;jj<2;jj++) {
for (ii=0;ii<2;ii++) {
self->distances[kk]=grib_nearest_distance(radius,inlon,inlat,
self->lons[self->k[kk]],self->lats[self->j[jj]]);
kk++;
}
}
grib_context_free(h->context,pla);
}
kk=0;
for (jj=0;jj<2;jj++) {
for (ii=0;ii<2;ii++) {
distances[kk]=self->distances[kk];
outlats[kk]=self->lats[self->j[jj]];
outlons[kk]=self->lons[self->k[kk]];
grib_get_double_element_internal(h,self->values_key,self->k[kk],&(values[kk]));
indexes[kk]=self->k[kk];
kk++;
}
}
return GRIB_SUCCESS;
} }
static int destroy(grib_nearest* nearest) { static int destroy(grib_nearest* nearest) {
grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*) nearest; grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*) nearest;
if (self->lats) grib_context_free(nearest->context,self->lats); if (self->lats) grib_context_free(nearest->context,self->lats);
if (self->lons) grib_context_free(nearest->context,self->lons); if (self->lons) grib_context_free(nearest->context,self->lons);
if (self->j) grib_context_free(nearest->context,self->j); if (self->j) grib_context_free(nearest->context,self->j);
if (self->k) grib_context_free(nearest->context,self->k); if (self->k) grib_context_free(nearest->context,self->k);
if (self->distances) grib_context_free(nearest->context,self->distances); if (self->distances) grib_context_free(nearest->context,self->distances);
return GRIB_SUCCESS; return GRIB_SUCCESS;
} }