Пример #1
0
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h) {
  int err=0;
  double *lat=0,*lon=0,*val=0;
  double missing_value=9999.;
  int skip_missing=1;
  char *kmiss=NULL, *p=NULL;
  char *missing_string=NULL;
  int i=0;
  grib_values* values=NULL;
  grib_iterator* iter = NULL;
  char* format=NULL;
  char* default_format="%.10e";
  int print_keys=grib_options_on("p:");
  long numberOfPoints=0;
  double *data_values=0,*lats=0,*lons=0;
  int n = 0;
  size_t size=0;

  if (grib_options_on("F:"))
    format=grib_options_get_option("F:");
  else
    format=default_format;

  if ((err=grib_get_long(h,"numberOfPoints",&numberOfPoints)) !=GRIB_SUCCESS) {
    fprintf(dump_file,"ERROR: unable to get number of points\n");
    return err;
  }

  iter=grib_iterator_new(h,0,&err);

  data_values=(double*)calloc(numberOfPoints+1,sizeof(double));

  if (iter) {
    lats=(double*)calloc(numberOfPoints+1,sizeof(double));
    lons=(double*)calloc(numberOfPoints+1,sizeof(double));
    lat=lats; lon=lons; val=data_values;
    while(grib_iterator_next(iter,lat++,lon++,val++)) {}
  } else if (err==GRIB_NOT_IMPLEMENTED || err==GRIB_SUCCESS){
    size=numberOfPoints;
    grib_get_double_array(h,"values",data_values,&size);
    if (size!=numberOfPoints) {
      if (!grib_options_on("q"))
       fprintf(dump_file,"ERROR: wrong number of points %d\n",(int)numberOfPoints);
      if (grib_options_on("f")) exit(1);
    }
  } else {
    grib_context_log(h->context,GRIB_LOG_ERROR,
                           "%s",grib_get_error_message(err));
    exit(err);
  }

  skip_missing=1;
  if (grib_options_on("m:")) {
    char* end=0;
    double mval=0;
    skip_missing=0;
    kmiss=grib_options_get_option("m:");
    p=kmiss;
    while (*p != ':' && *p != '\0') p++;
    if (*p == ':' && *(p+1) != '\0') {
      *p='\0';
      missing_string=strdup(p+1);
    } else {
      missing_string=strdup(kmiss);
    }
    mval=strtod(kmiss,&end);
    if (end==NULL) missing_value=mval;
    grib_set_double(h,"missingValue",missing_value);
  }

  if (iter)
    fprintf(dump_file,"Latitude, Longitude, ");

  fprintf(dump_file,"Value");

  if (print_keys)
    for (i=0;i<options->print_keys_count; i++)
      fprintf(dump_file,", %s",options->print_keys[i].name);

  fprintf(dump_file,"\n");

  if (print_keys)
    values=get_key_values(options,h);

  if (skip_missing==0){
    for (i=0;i<numberOfPoints;i++) {
      if (iter) fprintf(dump_file,"%9.3f%9.3f ",lats[i],lons[i]);

      if (data_values[i] == missing_value)
        fprintf(dump_file,"%s",missing_string);
      else
        fprintf(dump_file,format,data_values[i]);

      if (print_keys)
        print_key_values(values,options->print_keys_count);
      fprintf(dump_file,"\n");
      n++;
    }

  } else if ( skip_missing==1 ){
    for (i=0;i<numberOfPoints;i++) {
      if (data_values[i] != missing_value){
        if (iter) fprintf(dump_file,"%9.3f%9.3f ",lats[i],lons[i]);
        fprintf(dump_file,format,data_values[i]);
        if (print_keys)
          print_key_values(values,options->print_keys_count);
        fprintf(dump_file,"\n");
        n++;
      }
    }
  }

  if (iter) grib_iterator_delete(iter);

  free(data_values);
  if (iter) {
    free(lats);
    free(lons);
  }

  return 0;
}
Пример #2
0
int codes_set_double(grib_handle* h, const char* key, double val)
{
    return grib_set_double(h,key,val);
}
Пример #3
0
int main(int argc, char** argv)
{
  int err = 0;
  size_t size=0;

  FILE* in = NULL;
  char* infile = "../../data/regular_latlon_surface.grib1";
  FILE* out = NULL;
  char* outfile = "out.grib1";
  grib_handle *h = NULL;
  const void* buffer = NULL;
  size_t values_len;
  double* values;
  double missing=9999;
  int i=0;

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

  out = fopen(outfile,"w");
  if(!out) {
    printf("ERROR: unable to open output file %s\n",outfile);
        fclose(in);
    return 1;
  }

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

  GRIB_CHECK(grib_set_double(h,"missingValue",missing),0);

  /* get the size of the values array*/
  GRIB_CHECK(grib_get_size(h,"values",&values_len),0);

  values = (double*)malloc(values_len*sizeof(double));

  /* get data values*/
  GRIB_CHECK(grib_get_double_array(h,"values",values,&values_len),0);

  GRIB_CHECK(grib_set_long(h,"bitmapPresent",1),0);

  for(i = 0; i < 10; i++)
    values[i]=missing;

  GRIB_CHECK(grib_set_double_array(h,"values",values,values_len),0);

  /* get the coded message in a buffer */
  GRIB_CHECK(grib_get_message(h,&buffer,&size),0);

  /* write the buffer in a file*/
  if(fwrite(buffer,1,size,out) != size)
  {
     perror(outfile);
     exit(1);
  }

  /* delete handle */
  grib_handle_delete(h);

  fclose(in);
  fclose(out);

  return 0;
}