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 */
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) {
int n=0;
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].units = grib_context_strdup_persistent(c,units);
}
fclose(f);
@ -416,8 +415,6 @@ void grib_codetable_delete(grib_context* c) {
}
static void dump(grib_accessor* a, grib_dumper* dumper) {
grib_accessor_codetable* self = (grib_accessor_codetable*)a;
char comment[2048];
@ -632,7 +629,6 @@ static void destroy(grib_context* context,grib_accessor* a)
grib_context_free(context, a->vvalue);
a->vvalue=NULL;
}
}
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)
{
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->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->precision = grib_arguments_get_name(a->parent->h,args,self->carg++);
self->edition=1;
a->flags |= GRIB_ACCESSOR_FLAG_DATA;
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->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->edition=1;
a->flags |= GRIB_ACCESSOR_FLAG_DATA;
}
static long value_count(grib_accessor* a)
{
long number_of_values;
grib_accessor_data_g1simple_packing *self =(grib_accessor_data_g1simple_packing*)a;
long number_of_values;
grib_accessor_data_g1simple_packing *self =(grib_accessor_data_g1simple_packing*)a;
/* Special case for when values are cleared */
/*if(a->length == 0)
/* Special case for when values are cleared */
/*if(a->length == 0)
return 0;*/
if(grib_get_long_internal(a->parent->h,self->number_of_values,&number_of_values) != GRIB_SUCCESS)
return 0;
return number_of_values;
if(grib_get_long_internal(a->parent->h,self->number_of_values,&number_of_values) != GRIB_SUCCESS)
return 0;
return number_of_values;
}
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);
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;
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(*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);
}
}
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);
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_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 (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((ret = grib_get_double_internal(a->parent->h,self->reference_value, &reference_value))
!= GRIB_SUCCESS)
return ret;
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((ret = grib_get_long_internal(a->parent->h,self->binary_scale_factor, &binary_scale_factor))
!= GRIB_SUCCESS)
return ret;
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_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_long_internal(a->parent->h,self->bits_per_value,&bits_per_value)) !=
GRIB_SUCCESS)
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->decimal_scale_factor, &decimal_scale_factor))
!= 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->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->offsetsection,&offsetsection)) != 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);
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++;
/*
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);
*/
}
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);
Assert(half_byte <= 0x0f);
if((ret = grib_set_long_internal(a->parent->h,self->half_byte, half_byte))
!= GRIB_SUCCESS)
return ret;
if((ret = grib_set_long_internal(a->parent->h,self->half_byte, half_byte))
!= GRIB_SUCCESS)
return ret;
buf = grib_context_buffer_malloc_clear(a->parent->h->context,buflen);
encoded = buf;
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_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_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_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)
{
grib_accessor_data_g2simple_packing *self =(grib_accessor_data_g2simple_packing*)a;
a->flags |= GRIB_ACCESSOR_FLAG_DATA;
self->edition=2;
grib_accessor_data_g2simple_packing *self =(grib_accessor_data_g2simple_packing*)a;
a->flags |= GRIB_ACCESSOR_FLAG_DATA;
self->edition=2;
}
static long value_count(grib_accessor* a)
{
grib_accessor_data_g2simple_packing *self =(grib_accessor_data_g2simple_packing*)a;
long n_vals= 0;
if(grib_get_long_internal(a->parent->h,self->number_of_values,&n_vals) != GRIB_SUCCESS)
return 0;
return n_vals;
grib_accessor_data_g2simple_packing *self =(grib_accessor_data_g2simple_packing*)a;
long n_vals= 0;
if(grib_get_long_internal(a->parent->h,self->number_of_values,&n_vals) != GRIB_SUCCESS)
return 0;
return n_vals;
}
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_class* super = *(a->cclass->super);
size_t n_vals = *len;
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;
long off = 0;
int ret =0;
double units_factor=1.0;
double units_bias=0.0;
double* val=(double*)cval;
int i;
grib_accessor_data_g2simple_packing* self = (grib_accessor_data_g2simple_packing*)a;
grib_accessor_class* super = *(a->cclass->super);
size_t n_vals = *len;
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;
long off = 0;
int ret =0;
double units_factor=1.0;
double units_bias=0.0;
double* val=(double*)cval;
int i;
if(*len == 0) {
grib_buffer_replace(a, NULL, 0,1,1);
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(*len == 0) {
grib_buffer_replace(a, NULL, 0,1,1);
return GRIB_SUCCESS;
}
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;
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_SUCCESS)
ret = grib_set_long_internal(a->parent->h,self->number_of_values, *len);
if((ret = grib_get_double_internal(a->parent->h,self->reference_value, &reference_value))
!= GRIB_SUCCESS)
return ret;
if (ret!=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(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((ret = grib_get_long_internal(a->parent->h,self->bits_per_value,&bits_per_value)) !=
GRIB_SUCCESS)
return ret;
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((ret = grib_get_long_internal(a->parent->h,self->decimal_scale_factor, &decimal_scale_factor))
!= GRIB_SUCCESS)
return ret;
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;
decimal = grib_power(decimal_scale_factor,10) ;
divisor = grib_power(-binary_scale_factor,2);
ret = super->pack_double(a,val,len);
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);
buf = grib_context_buffer_malloc_clear(a->parent->h->context,buflen);
encoded = buf;
if((ret = grib_get_double_internal(a->parent->h,self->reference_value, &reference_value))
!= GRIB_SUCCESS)
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,
"grib_accessor_data_g2simple_packing : pack_double : packing %s, %d values", a->name, n_vals);
if((ret = grib_get_long_internal(a->parent->h,self->bits_per_value,&bits_per_value)) !=
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)
{
size_t length = *len;
grib_buffer_replace(a, val, length,1,1);
return GRIB_SUCCESS;
size_t length = *len;
grib_buffer_replace(a, val, length,1,1);
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)
{
grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*) nearest;
self->Nj = 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->lonLast = grib_arguments_get_name(h,args,self->cargs++);
self->j=(int*)grib_context_malloc(h->context,2*sizeof(int));
if (!self->j) return GRIB_OUT_OF_MEMORY;
self->k=(int*)grib_context_malloc( nearest->context,4*sizeof(int));
if (!self->k) return GRIB_OUT_OF_MEMORY;
return 0;
grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*) nearest;
self->Nj = 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->lonLast = grib_arguments_get_name(h,args,self->cargs++);
self->j=(int*)grib_context_malloc(h->context,2*sizeof(int));
if (!self->j) return GRIB_OUT_OF_MEMORY;
self->k=(int*)grib_context_malloc( nearest->context,4*sizeof(int));
if (!self->k) return GRIB_OUT_OF_MEMORY;
return 0;
}
static int find(grib_nearest* nearest, grib_handle* h,
double inlat, double inlon,unsigned long flags,
double* outlats,double* outlons, double *values,
double *distances,int *indexes, size_t *len) {
grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*) nearest;
int ret=0,kk=0,ii=0,jj=0;
int j=0;
long* pla=NULL;
long* pl=NULL;
size_t nvalues=0;
grib_iterator* iter=NULL;
double lat=0,lon=0;
long iradius;
double radius;
int ilat=0,ilon=0;
double inlat, double inlon,unsigned long flags,
double* outlats,double* outlons, double *values,
double *distances,int *indexes, size_t *len) {
grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*) nearest;
int ret=0,kk=0,ii=0,jj=0;
int j=0;
long* pla=NULL;
long* pl=NULL;
size_t nvalues=0;
grib_iterator* iter=NULL;
double lat=0,lon=0;
long iradius;
double radius;
int ilat=0,ilon=0;
if( (ret = grib_get_size(h,self->values_key,&nvalues))!= GRIB_SUCCESS)
return ret;
nearest->values_count = nvalues;
if( (ret = grib_get_size(h,self->values_key,&nvalues))!= GRIB_SUCCESS)
return ret;
nearest->values_count = nvalues;
if (grib_is_missing(h,self->radius,&ret)) {
grib_context_log(h->context, GRIB_LOG_DEBUG,"Key '%s' is missing", self->radius);
return ret ? ret : GRIB_GEOCALCULUS_PROBLEM;
}
if (grib_is_missing(h,self->radius,&ret)) {
grib_context_log(h->context, GRIB_LOG_DEBUG,"Key '%s' is missing", self->radius);
return ret ? ret : GRIB_GEOCALCULUS_PROBLEM;
}
if( (ret = grib_get_long(h,self->radius,&iradius))!= GRIB_SUCCESS)
return ret;
radius=((double)iradius)/1000.0;
if( (ret = grib_get_long(h,self->radius,&iradius))!= GRIB_SUCCESS)
return ret;
radius=((double)iradius)/1000.0;
if (!nearest->h || (flags & GRIB_NEAREST_SAME_GRID)==0) {
double dummy=0;
double olat=1.e10;
long n=0;
if (!nearest->h || (flags & GRIB_NEAREST_SAME_GRID)==0) {
double dummy=0;
double olat=1.e10;
long n=0;
ilat=0,ilon=0;
if (grib_is_missing(h,self->Nj,&ret)) {
grib_context_log(h->context, GRIB_LOG_DEBUG,"Key '%s' is missing", self->Nj);
return ret ? ret : GRIB_GEOCALCULUS_PROBLEM;
}
if( (ret = grib_get_long(h,self->Nj,&n))!= GRIB_SUCCESS)
return ret;
self->lats_count=n;
if (self->lats) grib_context_free(nearest->context,self->lats);
self->lats=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;
ilat=0,ilon=0;
if (grib_is_missing(h,self->Nj,&ret)) {
grib_context_log(h->context, GRIB_LOG_DEBUG,"Key '%s' is missing", self->Nj);
return ret ? ret : GRIB_GEOCALCULUS_PROBLEM;
}
self->lons[ilon++]=lon;
}
self->lats_count=ilat;
grib_iterator_delete(iter);
}
nearest->h=h;
if (!self->distances || (flags & GRIB_NEAREST_SAME_POINT)==0
|| (flags & GRIB_NEAREST_SAME_GRID)==0) {
double* lons=NULL;
int nlon=0;
size_t plsize=0;
long nplm1=0;
int nearest_lons_found=0;
double lon_first,lon_last;
int islocal=0;
long plmax;
double dimin;
if ((ret=grib_get_double(h,self->lonFirst,&lon_first))!=GRIB_SUCCESS) {
grib_context_log(h->context,GRIB_LOG_ERROR,
"grib_nearest_latlon_reduced.find(): unable to get %s %s\n",self->lonFirst,
grib_get_error_message(ret));
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);
}
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_nearest_latlon_reduced.find(): unable to get %s %s\n",self->lonLast,
grib_get_error_message(ret));
"grib_nearest_latlon_reduced.find(): unable to get %s %s\n",self->lonFirst,
grib_get_error_message(ret));
return ret;
}
if ((ret=grib_get_double(h,self->lonLast,&lon_last))!=GRIB_SUCCESS) {
grib_context_log(h->context,GRIB_LOG_ERROR,
"grib_nearest_latlon_reduced.find(): unable to get %s %s\n",self->lonLast,
grib_get_error_message(ret));
return ret;
}
plsize=self->lats_count;
if( (ret=grib_get_size(h,self->pl,&plsize))!= GRIB_SUCCESS)
return ret;
pla=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;
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]));
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++;
}
}
self->k[2]+=nlon;
self->k[3]+=nlon;
kk=0;
for (jj=0;jj<2;jj++) {
for (ii=0;ii<2;ii++) {
self->distances[kk]=grib_nearest_distance(radius,inlon,inlat,
self->lons[self->k[kk]],self->lats[self->j[jj]]);
kk++;
}
}
grib_context_free(h->context,pla);
}
kk=0;
for (jj=0;jj<2;jj++) {
for (ii=0;ii<2;ii++) {
distances[kk]=self->distances[kk];
outlats[kk]=self->lats[self->j[jj]];
outlons[kk]=self->lons[self->k[kk]];
grib_get_double_element_internal(h,self->values_key,self->k[kk],&(values[kk]));
indexes[kk]=self->k[kk];
kk++;
}
}
return GRIB_SUCCESS;
return GRIB_SUCCESS;
}
static int destroy(grib_nearest* nearest) {
grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*) nearest;
if (self->lats) grib_context_free(nearest->context,self->lats);
if (self->lons) grib_context_free(nearest->context,self->lons);
if (self->j) grib_context_free(nearest->context,self->j);
if (self->k) grib_context_free(nearest->context,self->k);
if (self->distances) grib_context_free(nearest->context,self->distances);
grib_nearest_latlon_reduced* self = (grib_nearest_latlon_reduced*) nearest;
if (self->lats) grib_context_free(nearest->context,self->lats);
if (self->lons) grib_context_free(nearest->context,self->lons);
if (self->j) grib_context_free(nearest->context,self->j);
if (self->k) grib_context_free(nearest->context,self->k);
if (self->distances) grib_context_free(nearest->context,self->distances);
return GRIB_SUCCESS;
return GRIB_SUCCESS;
}