Пример #1
0
static int    unpack_long   (grib_accessor* a, long* val, size_t *len)
{
  grib_accessor_sum* self = (grib_accessor_sum*)a;
  int ret = 0;
  size_t size=0;
  long* values=0;
  long i;
  long count=0;

  ret=value_count(a,&count);
  if (ret) return ret;
  size=count;

  if (size==0) {
  	*val=0;
	return ret;
  }
  values=(long*)grib_context_malloc_clear(a->parent->h->context,sizeof(long)*size);
  if (!values) return GRIB_OUT_OF_MEMORY;

  grib_get_long_array(a->parent->h,self->values,values,&size);

  *val=0;
  for (i=0;i<size;i++) 
  	*val+=values[i];

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

  return ret;
}
        /* Note: For this case, lengthOfTimeRange is not related to step and should not be used to calculate step */
        add_time_range = 0;
        if (is_special_expver(h)) {
            add_time_range = 1;
        }
    }
    if (add_time_range) {
        *val = start_step + coded_time_range;
    } else {
        *val = start_step;
    }

    return GRIB_SUCCESS;
}

#define MAX_NUM_TIME_RANGES 16 /* maximum number of time range specifications */
static int unpack_multiple_time_ranges(grib_accessor* a, long* val, size_t *len)
{
    grib_accessor_g2end_step* self = (grib_accessor_g2end_step*)a;
    int i = 0, err = 0;
    grib_handle* h = a->parent->h;
    long numberOfTimeRange = 0, unit = 0, start_step = 0;

    size_t count = 0;
    long arr_typeOfTimeIncrement[MAX_NUM_TIME_RANGES] = {0,};
    long arr_coded_unit[MAX_NUM_TIME_RANGES]          = {0,};
    long arr_coded_time_range[MAX_NUM_TIME_RANGES]    = {0,};

    if((err = grib_get_long_internal(h,self->start_step,&start_step))) return err;
    if((err = grib_get_long_internal(h,self->unit,&unit))) return err;
    if((err = grib_get_long_internal(h,self->numberOfTimeRange, &numberOfTimeRange))) return err;
    if (numberOfTimeRange > MAX_NUM_TIME_RANGES) {
        grib_context_log(h->context, GRIB_LOG_ERROR, "Too many time range specifications!");
        return GRIB_DECODING_ERROR;
    }

    count = numberOfTimeRange;
    /* Get the arrays for the N time ranges */
    if ((err = grib_get_long_array(h, self->typeOfTimeIncrement, arr_typeOfTimeIncrement, &count))) return err;
    if ((err = grib_get_long_array(h, self->coded_unit,          arr_coded_unit,          &count))) return err;
    if ((err = grib_get_long_array(h, self->coded_time_range,    arr_coded_time_range,    &count))) return err;

    /* Look in the array of typeOfTimeIncrements for first entry whose typeOfTimeIncrement == 2 */
    for(i=0; i<count; i++) {
        if (arr_typeOfTimeIncrement[i] == 2) {
            /* Found the required time range. Get the other two keys from it */
            long the_coded_unit = arr_coded_unit[i];
            long the_coded_time_range = arr_coded_time_range[i];

            err = convert_time_range(h, unit, the_coded_unit, &the_coded_time_range);
            if (err != GRIB_SUCCESS) return err;

            *val = start_step + the_coded_time_range;
            return GRIB_SUCCESS;
        }
    }

    grib_context_log(h->context, GRIB_LOG_ERROR,
            "Cannot calculate endStep. No time range specification with typeOfTimeIncrement = 2");
    return GRIB_DECODING_ERROR;
}
Пример #3
0
int codes_get_long_array(grib_handle* h, const char* key, long* vals, size_t *length)
{
    return grib_get_long_array(h,key,vals,length);
}
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;

  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( (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;

	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=(double*)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=(double*)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);
    if (ret) {
        grib_context_log(h->context,GRIB_LOG_ERROR,"unable to create iterator");
        return 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
                       || (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;
	}
	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=(long*)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);
  }

  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;
}
Пример #5
0
static int compare_values(grib_handle* h1,grib_handle *h2,const char *name)
{
  size_t len1 = 0;
  size_t len2 = 0;
  int err;
  int err1;
  int err2;
  int type1,type2;

  char *sval1 = NULL,*sval2 = NULL;
  unsigned char *uval1 = NULL,*uval2 = NULL;
  double *dval1 = NULL, *dval2 = NULL;
  long *lval1 = NULL, *lval2 = NULL;

  if((err = grib_get_native_type(h1,name,&type1)) != GRIB_SUCCESS)
  {
    printf("Oops... cannot get type of [%s] in 1st field: %s\n",name,grib_get_error_message(err));
    return err;
  }

  if((err = grib_get_native_type(h2,name,&type2)) != GRIB_SUCCESS)
  {
    if(err == GRIB_NOT_FOUND)
    {
      printf("[%s] not found in 2nd field\n",name);
      return err;
    }

    printf("Oops... cannot get type of [%s] in 2nd field: %s\n",name,grib_get_error_message(err));
    return err;
  }

  if(type1 != type2)
  {
    printf("Warning, [%s] has different types: 1st field: [%s], 2nd field: [%s]\n",
        name,grib_get_type_name(type1),grib_get_type_name(type2));
    /* return GRIB_TYPE_MISMATCH; */
  }

  if(type1 == GRIB_TYPE_LABEL)
    return err;

  if(type1 == GRIB_TYPE_SECTION)
    return err;


  if((err = grib_get_size(h1,name,&len1)) != GRIB_SUCCESS)
  {
    printf("Oops... cannot get size of [%s] in 1st field: %s\n",name,grib_get_error_message(err));
    return err;
  }

  if((err = grib_get_size(h2,name,&len2)) != GRIB_SUCCESS)
  {
    if(err == GRIB_NOT_FOUND)
    {
      printf("[%s] not found in 2nd field\n",name);
      return err;
    }

    printf("Oops... cannot get size of [%s] in 2nd field: %s\n",name,grib_get_error_message(err));
    return err;
  }

  if(len1 != len2)
  {
    printf("[%s] has different size: 1st field: %ld, 2nd field: %ld\n",name,(long)len1,(long)len2);
    return GRIB_COUNT_MISMATCH;
  }

  switch(type1)
  {
    case GRIB_TYPE_STRING:

      sval1 = (char*)grib_context_malloc(h1->context,len1*sizeof(char));
      sval2 = (char*)grib_context_malloc(h2->context,len2*sizeof(char));

      if((err1 = grib_get_string(h1,name,sval1,&len1)) != GRIB_SUCCESS)
      {
        printf("Oops... cannot get string value of [%s] in 1st field: %s\n",
          name,grib_get_error_message(err1));
      }

      if((err2 = grib_get_string(h2,name,sval2,&len2)) != GRIB_SUCCESS)
      {
        printf("Oops... cannot get string value of [%s] in 2nd field: %s\n",
          name,grib_get_error_message(err2));
      }

      if(err1 == GRIB_SUCCESS && err2 == GRIB_SUCCESS)
      {
        if(strcmp(sval1,sval2) != 0)
        {
          printf("[%s] string values are different: [%s] and [%s]\n",
            name,sval1,sval2);
          err1 = GRIB_VALUE_MISMATCH;
        }
      }

      grib_context_free(h1->context,sval1);
      grib_context_free(h2->context,sval2);

      if(err1) return err1;
      if(err2) return err2;

      break;

    case GRIB_TYPE_LONG:

      lval1 = (long*)grib_context_malloc(h1->context,len1*sizeof(long));
      lval2 = (long*)grib_context_malloc(h2->context,len2*sizeof(long));

      if((err1 = grib_get_long_array(h1,name,lval1,&len1)) != GRIB_SUCCESS)
      {
        printf("Oops... cannot get long value of [%s] in 1st field: %s\n",
          name,grib_get_error_message(err1));
      }

      if((err2 = grib_get_long_array(h2,name,lval2,&len2)) != GRIB_SUCCESS)
      {
        printf("Oops... cannot get long value of [%s] in 2nd field: %s\n",
          name,grib_get_error_message(err2));
      }

      if(err1 == GRIB_SUCCESS && err2 == GRIB_SUCCESS)
      {
        int i;
        for(i = 0; i < len1; i++)
          if(lval1[i] != lval2[i])
          {
              if(len1 == 1)
                printf("[%s] long  values are different: [%ld] and [%ld]\n",
                  name,lval1[i],lval2[i]);
              else
                printf("[%s] long value %d of %ld are different: [%ld] and [%ld]\n",
                  name,i,(long)len1,lval1[i],lval2[i]);

            err1 = GRIB_VALUE_MISMATCH;
            break;
          }
      }

      grib_context_free(h1->context,lval1);
      grib_context_free(h2->context,lval2);

      if(err1) return err1;
      if(err2) return err2;
      break;

    case GRIB_TYPE_DOUBLE:
      dval1 = (double*)grib_context_malloc(h1->context,len1*sizeof(double));
      dval2 = (double*)grib_context_malloc(h2->context,len2*sizeof(double));

      if((err1 = grib_get_double_array(h1,name,dval1,&len1)) != GRIB_SUCCESS)
      {
        printf("Oops... cannot get double value of [%s] in 1st field: %s\n",
          name,grib_get_error_message(err1));
      }

      if((err2 = grib_get_double_array(h2,name,dval2,&len2)) != GRIB_SUCCESS)
      {
        printf("Oops... cannot get double value of [%s] in 2nd field: %s\n",
          name,grib_get_error_message(err2));
      }

      if(err1 == GRIB_SUCCESS && err2 == GRIB_SUCCESS)
      {
        int i;
        for(i = 0; i < len1; i++)
          if(!same(dval1[i],dval2[i]))
          {
              if(len1 == 1)
                printf("[%s] double values are different: [%g] and [%g], diff: %g\n",
                  name,dval1[i],dval2[i],dval1[i] - dval2[i]);
              else
                printf("[%s] double value %d of %ld are different: [%g] and [%g], diff: %g\n",
                  name,i,(long)len1,dval1[i],dval2[i],dval1[i] - dval2[i]);

            err1 = GRIB_VALUE_MISMATCH;
            break;
          }
      }

      grib_context_free(h1->context,dval1);
      grib_context_free(h2->context,dval2);

      if(err1) return err1;
      if(err2) return err2;
      break;

    case GRIB_TYPE_BYTES:

      uval1 = (unsigned char*)grib_context_malloc(h1->context,len1*sizeof(unsigned char));
      uval2 = (unsigned char*)grib_context_malloc(h2->context,len2*sizeof(unsigned char));

      if((err1 = grib_get_bytes(h1,name,uval1,&len1)) != GRIB_SUCCESS)
      {
        printf("Oops... cannot get bytes value of [%s] in 1st field: %s\n",
          name,grib_get_error_message(err1));
      }

      if((err2 = grib_get_bytes(h2,name,uval2,&len2)) != GRIB_SUCCESS)
      {
        printf("Oops... cannot get bytes value of [%s] in 2nd field: %s\n",
          name,grib_get_error_message(err2));
      }

      if(err1 == GRIB_SUCCESS && err2 == GRIB_SUCCESS)
      {
        if(memcmp(uval1,uval2,len1) != 0)
        {
        int i;
        for(i = 0; i < len1; i++)
          if(uval1[i] != uval2[i])
          {
              if(len1 == 1)
                printf("[%s] byte values are different: [%02x] and [%02x]\n",
                  name,uval1[i],uval2[i]);
              else
                printf("[%s] byte value %d of %ld are different: [%02x] and [%02x]\n",
                  name,i,(long)len1,uval1[i],uval2[i]);

            err1 = GRIB_VALUE_MISMATCH;
            break;
          }
          err1 = GRIB_VALUE_MISMATCH;
        }
      }

      grib_context_free(h1->context,uval1);
      grib_context_free(h2->context,uval2);

      if(err1) return err1;
      if(err2) return err2;
      break;

    case GRIB_TYPE_LABEL:
      break;

    default:
      printf("Cannot compare [%s], unsupported type %d\n",name,type1);
      return GRIB_UNABLE_TO_COMPARE_ACCESSORS;
      break;
  }

  return GRIB_SUCCESS;

}
static int unpack_double(grib_accessor* a, double* values, size_t *len)
{
  grib_accessor_data_g1second_order_row_by_row_packing* self =  (grib_accessor_data_g1second_order_row_by_row_packing*)a;
  int ret=0;
  long numberOfGroups,numberOfSecondOrderPackedValues;
  long* groupWidths=0;
  long* firstOrderValues=0;
  long* X=0;
  long numberOfRows,numberOfColumns;
  long *numbersPerRow;
  long pos=0;
  long widthOfFirstOrderValues=0;
  long jPointsAreConsecutive;
  unsigned char* buf = (unsigned char*)a->parent->h->buffer->data;
  long k,i,j,n,Ni,Nj;
  double reference_value;
  long binary_scale_factor;
  long decimal_scale_factor;
  double s,d;
  size_t groupWidthsSize=0;
  int bitmapPresent=0;
  size_t plSize=0;
  long* pl=0;

  buf += grib_byte_offset(a);

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

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

  if (self->bitmap) bitmapPresent=1;
  ret=grib_get_size(a->parent->h,self->pl,&plSize);
  if (ret==GRIB_SUCCESS) {
	  pl=grib_context_malloc_clear(a->parent->h->context,sizeof(long)*plSize);
	  if((ret=grib_get_long_array(a->parent->h,self->pl,pl,&plSize)) != GRIB_SUCCESS)
         return ret;
  }

  if((ret=grib_get_long_internal(a->parent->h,self->Ni,&Ni)) != GRIB_SUCCESS)
         return ret;
  if((ret=grib_get_long_internal(a->parent->h,self->Nj,&Nj)) != GRIB_SUCCESS)
         return ret;
  if (jPointsAreConsecutive) {
  	numberOfRows=Ni;
	numberOfColumns=Nj;
  } else {
  	numberOfRows=Nj;
	numberOfColumns=Ni;
  }

  numbersPerRow=grib_context_malloc_clear(a->parent->h->context,sizeof(long)*numberOfRows);
  if (bitmapPresent) {
	long *bitmap,*pbitmap;
	size_t numberOfPoints=Ni*Nj;

  	if (plSize) {
		numberOfPoints=0;
		for (i=0;i<numberOfRows;i++) numberOfPoints+=pl[i];
	} 
	bitmap=grib_context_malloc_clear(a->parent->h->context,sizeof(long)*numberOfPoints);
	pbitmap=bitmap;
	grib_get_long_array(a->parent->h,self->bitmap,bitmap,&numberOfPoints);
  	if (plSize) {
		for (i=0;i<numberOfRows;i++) {
			for (j=0;j<pl[i];j++) {
				numbersPerRow[i]+=*(bitmap++);
			}
		}
	} else {
		for (i=0;i<numberOfRows;i++) {
			numbersPerRow[i]=0;
			for (j=0;j<Ni;j++) {
				numbersPerRow[i]+=*(bitmap++);
			}
		}
	}

	grib_context_free(a->parent->h->context,pbitmap);
  } else {
  	if (plSize) {
		for (i=0;i<numberOfRows;i++) numbersPerRow[i]=pl[i];
	} else {
		for (i=0;i<numberOfRows;i++) 
			numbersPerRow[i]=numberOfColumns;
	}
  }

  if((ret=grib_get_long_internal(a->parent->h,self->widthOfFirstOrderValues,&widthOfFirstOrderValues)) != 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->decimal_scale_factor,&decimal_scale_factor)) != 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->numberOfSecondOrderPackedValues,
  					&numberOfSecondOrderPackedValues)) != GRIB_SUCCESS)
         return ret;

  groupWidths=grib_context_malloc_clear(a->parent->h->context,sizeof(long)*numberOfGroups);
  groupWidthsSize=numberOfGroups;
  if((ret=grib_get_long_array_internal(a->parent->h,self->groupWidths, groupWidths,&groupWidthsSize)) != GRIB_SUCCESS)
         return ret;

	firstOrderValues=grib_context_malloc_clear(a->parent->h->context,sizeof(long)*numberOfGroups);
	grib_decode_long_array(buf,&pos,widthOfFirstOrderValues,numberOfGroups,firstOrderValues);
	pos = 8 * ( (pos + 7 ) / 8);

	X=grib_context_malloc_clear(a->parent->h->context,sizeof(long)*numberOfSecondOrderPackedValues);
	n=0;
	k=0;
	for (i=0; i<numberOfGroups; i++) {
		if (groupWidths[i]>0) {
				for (j=0;j<numbersPerRow[k];j++) {
					X[n]=grib_decode_unsigned_long(buf,&pos,groupWidths[i]);
					X[n]+=firstOrderValues[i];
					n++;
				}
		} else {
				for (j=0;j<numbersPerRow[k];j++) {
					X[n]=firstOrderValues[i];
					n++;
				}
		}
		k++;
	}

	s = grib_power(binary_scale_factor,2);
	d = grib_power(-decimal_scale_factor,10) ;
	for (i=0; i<n; i++) {
		values[i] = (double) (((X[i]*s)+reference_value)*d);
	}
	grib_context_free(a->parent->h->context,firstOrderValues);
	grib_context_free(a->parent->h->context,X);
	grib_context_free(a->parent->h->context,groupWidths);
	if (plSize) grib_context_free(a->parent->h->context,pl);
  
  return ret;
}
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_reduced* self = (grib_nearest_reduced*) nearest;
  int ret=0,kk=0,ii=0,jj=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 (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 (!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=(double*)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=(double*)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
                       || (flags & GRIB_NEAREST_SAME_GRID)==0) {
    double* lons=NULL;
    int nlon=0;
    size_t plsize=0;
    long nplm1=0;
    int nearest_lons_found=0;
	long global=0;
	double lon_first,lon_last;
	long row_count,ilon_first,ilon_last;

	/*TODO global from the def file*/
	global=1;
	grib_get_long(h,"global",&global);

	if (!global) {
		/*TODO longitudeOfFirstGridPointInDegrees from the def file*/
		if ((ret=grib_get_double(h,"longitudeOfFirstGridPointInDegrees",&lon_first))!=GRIB_SUCCESS) {
			grib_context_log(h->context,GRIB_LOG_ERROR,
				"grib_nearest_reduced.find(): unable to get longitudeOfFirstGridPointInDegrees %s\n",
				grib_get_error_message(ret));
			return ret;
		}
		/*TODO longitudeOfLastGridPointInDegrees from the def file*/
		if ((ret=grib_get_double(h,"longitudeOfLastGridPointInDegrees",&lon_last))!=GRIB_SUCCESS) {
			grib_context_log(h->context,GRIB_LOG_ERROR,
				"grib_nearest_reduced.find(): unable to get longitudeOfLastGridPointInDegrees %s\n",
				grib_get_error_message(ret));
			return ret;
		}
		/* if (lon_last<0) lon_last+=360; */
		/* if (lon_first<0) lon_first+=360; */
	} else {
	  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]));

    plsize=self->lats_count;
	if( (ret=grib_get_size(h,self->pl,&plsize))!= GRIB_SUCCESS)
	       return ret;
    pla=(long*)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++;}

	nlon=0;
	if (global) {
		for (jj=0;jj<self->j[0];jj++) nlon+=pl[jj];
		nplm1=pl[self->j[0]]-1;
	} else {
		nlon=0;
		for (jj=0;jj<self->j[0];jj++) {
	    	row_count=0;ilon_first=0;ilon_last=0;
	       	grib_get_reduced_row(pl[jj],lon_first,lon_last,&row_count,&ilon_first,&ilon_last);
		   	nlon+=row_count;
		}
	    row_count=0;ilon_first=0;ilon_last=0;
	    grib_get_reduced_row(pl[self->j[0]],lon_first,lon_last,&row_count,&ilon_first,&ilon_last);
		nplm1=row_count-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) {
	  if (!global) {
		  row_count=0;ilon_first=0;ilon_last=0;
		  grib_get_reduced_row(pl[self->j[0]],lon_first,lon_last,&row_count,&ilon_first,&ilon_last);
	  } else {
		  row_count=pl[self->j[0]];
	  }

      grib_binary_search(lons,row_count-1,inlon,
                                &(self->k[0]),&(self->k[1]));
	}
    self->k[0]+=nlon;
    self->k[1]+=nlon;

	nlon=0;
	if (global) {
		for (jj=0;jj<self->j[1];jj++) nlon+=pl[jj];
		nplm1=pl[self->j[1]]-1;
	} else {
	    long row_count,ilon_first,ilon_last;
		for (jj=0;jj<self->j[1];jj++) {
	    	row_count=0;ilon_first=0;ilon_last=0;
	       	grib_get_reduced_row(pl[jj],lon_first,lon_last,&row_count,&ilon_first,&ilon_last);
		   	nlon+=row_count;
		}
	    row_count=0;ilon_first=0;ilon_last=0;
	    grib_get_reduced_row(pl[self->j[1]],lon_first,lon_last,&nplm1,&ilon_first,&ilon_last);
		nplm1--;
	}
    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) {
	  if (!global) {
		  row_count=0;ilon_first=0;ilon_last=0;
		  grib_get_reduced_row(pl[self->j[1]],lon_first,lon_last,&row_count,&ilon_first,&ilon_last);
	  } else {
		  row_count=pl[self->j[1]];
	  }

      grib_binary_search(lons,row_count-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);
  }

  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;
}
Пример #8
0
int main(int argc, char** argv) {
	int err = 0;

	size_t i = 0;
	size_t count;
	size_t size;

	long numberOfContributingSpectralBands;
	long values[1024];

	FILE* in = NULL;
	char* filename = "../../data/satellite.grib";
	grib_handle *h = NULL;

	in = fopen(filename,"r");
	if(!in) {
		printf("ERROR: unable to open file %s\n",filename);
		return 1;
	}

	/* create new handle from a message in a file*/
	h = grib_handle_new_from_file(0,in,&err);
	if (h == NULL) {
		printf("Error: unable to create handle from file %s\n",filename);
	}

	numberOfContributingSpectralBands = 2;
	GRIB_CHECK(grib_set_long(h,"numberOfContributingSpectralBands",numberOfContributingSpectralBands),0);

	numberOfContributingSpectralBands = 9;
	GRIB_CHECK(grib_set_long(h,"numberOfContributingSpectralBands",numberOfContributingSpectralBands),0);

	/* get as a long*/
	GRIB_CHECK(grib_get_long(h,"numberOfContributingSpectralBands",&numberOfContributingSpectralBands),0);
	printf("numberOfContributingSpectralBands=%ld\n",numberOfContributingSpectralBands);

	/* get as a long*/
	GRIB_CHECK(grib_get_size(h,"scaledValueOfCentralWaveNumber",&count),0);
	printf("count=%ld\n",(long)count);

	assert(count < sizeof(values)/sizeof(values[0]));

	size = count;
	GRIB_CHECK(grib_get_long_array(h,"scaledValueOfCentralWaveNumber",values,&size),0);
	assert(size == count);

	for(i=0;i<count;i++)
		printf("scaledValueOfCentralWaveNumber %lu = %ld\n",(unsigned long)i,values[i]);

	for(i=0;i<count;i++)
		values[i] = -values[i]; 

	size = count;
	/* size--; */
	GRIB_CHECK(grib_set_long_array(h,"scaledValueOfCentralWaveNumber",values,size),0);
	assert(size == count);

	grib_handle_delete(h);

	fclose(in);
	return 0;
}
Пример #9
0
int dump_values(FILE* out,grib_handle* h,const char *name,int missingOK)
{
	size_t len = 0;
	int err;
	int type;

	char *sval = NULL;
	unsigned char *uval = NULL; 
	double *dval = NULL;
	long *lval = NULL;


	int i;

	if((err = grib_get_type(h,name,&type)) != GRIB_SUCCESS)
	{
		printf("# Oops... cannot get type of [%s]: %s\n",name,
				grib_get_error_message(err));
		return err;
	}

	if((err = grib_get_size(h,name,&len)) != GRIB_SUCCESS)
	{
		printf("# Oops... cannot get size of [%s]: %s\n",name,
				grib_get_error_message(err));
		return err;
	}

	switch(type)
	{
		case GRIB_TYPE_STRING:

			sval = grib_context_malloc(h->context,len*sizeof(char));

			if((err = grib_get_string(h,name,sval,&len)) != GRIB_SUCCESS)
			{
				printf("# Oops... cannot get string value of [%s]: %s\n",
						name,grib_get_error_message(err));
			}
			else
			{
				fprintf(out,"%s='%s';\n",name,sval);
			}

			grib_context_free(h->context,sval);

			if(err) return err;

			break;

		case GRIB_TYPE_LONG:
			lval = grib_context_malloc(h->context,len*sizeof(long));

			if((err = grib_get_long_array(h,name,lval,&len)) != GRIB_SUCCESS)
			{
				printf("# Oops... cannot get long value of [%s]: %s\n",
						name,grib_get_error_message(err));
			}
			else
			{
				if(len == 1)
				{
					if(missingOK && lval[0] == GRIB_MISSING_LONG)
						fprintf(out,"%s = missing;\n",name);
					else
						fprintf(out,"%s = %ld;\n",name,lval[0]);
				}
				else {
					fprintf(out,"%s = {", name);
					if(len >= 10) fprintf(out," # %ld\n",(long)len);
					for(i = 0; i < len; i++){
						fprintf(out,"%ld, ",lval[i]);
						if((i+1)%10 == 0)
							fprintf(out,"\n");
					}
					fprintf(out,"};\n");
				}
			}

			grib_context_free(h->context,lval);

			if(err) return err;
			break;

		case GRIB_TYPE_DOUBLE:
			dval = grib_context_malloc(h->context,len*sizeof(double));

			if((err = grib_get_double_array(h,name,dval,&len)) != GRIB_SUCCESS)
			{
				printf("# Oops... cannot get double value of [%s]: %s\n",
						name,grib_get_error_message(err));
			}
			else
			{
				if(len == 1)
				{
					if(missingOK && dval[0] == GRIB_MISSING_DOUBLE)
						fprintf(out,"%s = missing;\n",name);
					else {
						fprintf(out,"%s = %g;\n",name,dval[0]);
					}
				}
				else {
					fprintf(out,"%s = { \n",name);
					if(len >= 10) fprintf(out," # %ld\n",(long)len);
					for(i = 0; i < len; i++)
					{
						fprintf(out,"%f, ",dval[i]);
						if((i+1)%10 == 0)
							fprintf(out,"\n");
					}
					fprintf(out,"};\n");
				}
			}

			grib_context_free(h->context,dval);

			if(err) return err;
			break;

		case GRIB_TYPE_BYTES:
			uval = grib_context_malloc(h->context,len*sizeof(unsigned char));

			if((err = grib_get_bytes(h,name,uval,&len)) != GRIB_SUCCESS)
			{
				printf("# Oops... cannot get bytes value of [%s]: %s\n",
						name,grib_get_error_message(err));
			}
			else
			{
				fprintf(out,"%s = { \n",name);
					if(len >= 10) fprintf(out," # %ld\n",(long)len);
					for(i = 0; i < len; i++)
					{
						fprintf(out,"%02x, ",uval[i]);
						if((i+1)%10 == 0)
							fprintf(out,"\n");
					}
					fprintf(out,"};\n");

			}

			grib_context_free(h->context,uval);

			if(err) return err;

			break;

		case GRIB_TYPE_LABEL:
			break;

		case GRIB_TYPE_SECTION:
			break;

		default:
			printf("Cannot compare [%s], unsupported type %d\n",name,type);
			return GRIB_UNABLE_TO_COMPARE_ACCESSORS;
			break;
	}

	return GRIB_SUCCESS;

}