Пример #1
0
int grib_tool_init(grib_runtime_options* options)
{
	if (options->set_values_count == 0 && !options->repack && !options->constant) {
		printf("ERROR: please provide some keys to set through the -s option or use the -r/-d options\n");
		exit(1);
	}
	if (options->verbose) options->print_header=1;
	/*if (grib_options_on("n:")) {
    noise=atof(grib_options_get_option("n:"));
    options->repack=1;
  }*/

	if (grib_options_on("n:") && grib_options_on("d:")) {
		printf("Error: -n and -d options are incompatible. Choose one of the two please.\n");
		exit(1);
	}

#if 0
	if (options->outfile && options->outfile->name) {
		options->outfile->file = fopen(options->outfile->name,"w");
		if(!options->outfile->file) {
			perror(options->outfile->name);
			exit(1);
		}
	}
#endif
	return 0;
}
Пример #2
0
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
{
    size_t i=0;
    int err=0;

    if (!options->skip) {
        double* v=NULL;
        size_t size=0;
        if ( options->repack ) {
            GRIB_CHECK_NOLINE(grib_get_size(h,"values",&size),0);

            v = (double*)calloc(size,sizeof(double));
            if(!v) {
                fprintf(stderr,"failed to allocate %d bytes\n",(int)(size*sizeof(double)));
                exit(1);
            }

            GRIB_CHECK_NOLINE(grib_get_double_array(h,"values",v,&size),0);
        }

        if (options->set_values_count != 0) {
            err=grib_set_values(h,options->set_values,options->set_values_count);
            if( err != GRIB_SUCCESS && options->fail) exit(err);
        }

        if ( options->repack ) {

            if (grib_options_on("d:")) {
                for(i = 0; i< size; i++)
                    v[i] =  options->constant;
            }
#if 0
            if (grib_options_on("n:")) {
                for(i = 0; i< size; i++)
                    v[i] =  options->constant;
            }
#endif

            if (err == GRIB_SUCCESS) {
                GRIB_CHECK_NOLINE(grib_set_double_array(h,"values",v,size),0);
            }
            free(v);
        }

        if( err != GRIB_SUCCESS && options->fail) exit(err);
    }

    if (!options->skip || !options->strict) grib_tools_write_message(options,h);

    return 0;
}
Пример #3
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;
}
Пример #4
0
/*
 The options have been parsed and the structure
 grib_runtime_options* options has been loaded.
 Initialisation and startup can be done here
*/
int grib_tool_init(grib_runtime_options* options)
{
    char  *theEnd = NULL, *end1=NULL;
    size_t size=4;
    int ret=0;
    double min=0,max=0;
    int i=0;
    char* p=NULL;
    if (options->latlon && grib_options_on("j")) {
        options->verbose=0;
        json_latlon=1;
    }

    if (options->latlon) {

        lat = strtod(options->latlon,&theEnd);
        if (*theEnd != ',') {
            printf("ERROR: wrong latitude value\n");
            exit(1);
        }
        lon= strtod(++theEnd,&end1);

        mode=GRIB_NEAREST_SAME_POINT | GRIB_NEAREST_SAME_GRID;

        if (end1 && *end1 == ',') {
            end1++;
            if (*end1 != '0') {
                p=end1;
                while (*p != ',' && *p !='\0') p++;
                if (*end1 == '4') {
                    options->latlon_mode=4;
                } else if (*end1 == '1') {
                    options->latlon_mode=1;
                } else {
                    printf("ERROR %s: wrong mode given in option -l\n",grib_tool_name);
                    exit(1);
                }
            }
            if (*p == ',') {
                p++;
                options->latlon_mask=strdup(p);
            }
        }
    }

    if (options->latlon && options->latlon_mask) {
        FILE* f=NULL;
        grib_handle* hh;
        int idx=0, land_found=0;
        double min_overall = 0.0;
        int idx_overall = -1;
        f=fopen(options->latlon_mask,"r");
        if(!f) {
            perror(options->latlon_mask);
            exit(1);
        }
        hh=grib_handle_new_from_file(0,f,&ret);
        fclose(f);
        GRIB_CHECK_NOLINE(ret,0);
        n=grib_nearest_new(hh,&ret);
        GRIB_CHECK_NOLINE(ret,0);
        GRIB_CHECK_NOLINE(grib_nearest_find(n,hh,lat,lon,mode,
                options->lats,options->lons,options->mask_values,options->distances,options->indexes,&size),0);
        grib_nearest_delete(n);
        n=NULL;
        grib_handle_delete(hh);

        options->latlon_idx=-1;
        max=options->distances[0];
        for (i=0;i<4;i++)
            if (max<options->distances[i]) {max=options->distances[i];}
        min=max;
        min_overall=max;
        /* See GRIB-213 */
        for (i=0;i<4;i++) {
            if (min_overall >= options->distances[i]) { /* find overall min and index ignoring mask */
                min_overall = options->distances[i];
                idx_overall = i;
            }
            if ((min >= options->distances[i]) && (options->mask_values[i] >= 0.5)) {
                land_found=1; /* found at least one point which is land */
                min = options->distances[i];
                idx = i;
            }
        }
        if (land_found) {
            options->latlon_idx=idx;
        } else {
            options->latlon_idx=idx_overall; /* all points were sea, so pick the min overall */
        }

        if (options->latlon_idx<0){
            min=0;
            options->latlon_idx=0;
            for (i=1;i<4;i++)
                if (min>options->distances[i]) {
                    min = options->distances[i];
                    options->latlon_idx=i;
                }
        }
    }
    if (json_latlon) printf("[\n");

    return 0;
}