Cleaned up indentation and formatting

This commit is contained in:
Shahram Najm 2014-01-10 14:07:02 +00:00
parent d1d5138b4d
commit b2f4653aae
11 changed files with 1625 additions and 1596 deletions

File diff suppressed because it is too large Load Diff

View File

@ -39,7 +39,7 @@ grib_option grib_options[]={
{"M",0,0,0,1,0},
{"U",0,0,1,0,0},
{"H",0,0,1,0,0},
{"T:",0,0,0,1,0},
{"T:",0,0,0,1,0},
{"S",0,0,1,0,0},
{"g",0,0,0,1,0},
{"G",0,0,0,1,0},
@ -50,74 +50,74 @@ grib_option grib_options[]={
int grib_options_count=sizeof(grib_options)/sizeof(grib_option);
int main(int argc, char *argv[]) {
int ret=grib_tool(argc,argv);
return ret;
int ret=grib_tool(argc,argv);
return ret;
}
int grib_tool_before_getopt(grib_runtime_options* options) {
return 0;
return 0;
}
int grib_tool_init(grib_runtime_options* options) {
#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);
}
}
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;
return 0;
}
int grib_tool_new_filename_action(grib_runtime_options* options,const char* file) {
return 0;
return 0;
}
int grib_tool_new_file_action(grib_runtime_options* options,grib_tools_file* file) {
return 0;
return 0;
}
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h) {
double* v;
size_t size=0;
if ( options->repack ) {
GRIB_CHECK_NOLINE(grib_get_size(h,"values",&size),0);
double* v;
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 %ld bytes\n",(long)(size*sizeof(double)));
exit(1);
}
v = (double*)calloc(size,sizeof(double));
if(!v) {
fprintf(stderr,"failed to allocate %ld bytes\n",(long)(size*sizeof(double)));
exit(1);
}
GRIB_CHECK_NOLINE(grib_get_double_array(h,"values",v,&size),0);
GRIB_CHECK_NOLINE(grib_set_double_array(h,"values",v,size),0);
free(v);
}
grib_tools_write_message(options,h);
return 0;
GRIB_CHECK_NOLINE(grib_get_double_array(h,"values",v,&size),0);
GRIB_CHECK_NOLINE(grib_set_double_array(h,"values",v,size),0);
free(v);
}
grib_tools_write_message(options,h);
return 0;
}
int grib_tool_skip_handle(grib_runtime_options* options, grib_handle* h) {
grib_handle_delete(h);
return 0;
grib_handle_delete(h);
return 0;
}
void grib_tool_print_key_values(grib_runtime_options* options,grib_handle* h) {
grib_print_key_values(options,h);
grib_print_key_values(options,h);
}
int grib_tool_finalise_action(grib_runtime_options* options)
{
int err=0;
grib_file_close_all(&err);
if (err != GRIB_SUCCESS) {
perror(grib_tool_name);
exit(err);
}
/* if (options->outfile->file)
fclose(options->outfile->file); */
return 0;
int err=0;
grib_file_close_all(&err);
if (err != GRIB_SUCCESS) {
perror(grib_tool_name);
exit(err);
}
/* if (options->outfile->file)
fclose(options->outfile->file); */
return 0;
}

View File

@ -10,61 +10,64 @@
#include "grib_api_internal.h"
void usage(char* prog) {
printf("usage: %s infile1 infile2 ... \n",prog);
exit(1);
void usage(char* prog)
{
printf("usage: %s infile1 infile2 ... \n",prog);
exit(1);
}
static int check_file(FILE* in,long *count) {
void* mesg=NULL;
size_t size=0;
off_t offset=0;
int err=0;
grib_context* c=grib_context_get_default();
static int check_file(FILE* in,long *count)
{
void* mesg=NULL;
size_t size=0;
off_t offset=0;
int err=0;
grib_context* c=grib_context_get_default();
if (!in) return 1;
if (!in) return 1;
while ( (mesg=wmo_read_any_from_file_malloc ( in,0, &size,&offset,&err))!=NULL && err==GRIB_SUCCESS) {
grib_context_free(c,mesg);
(*count)++;
}
while ( (mesg=wmo_read_any_from_file_malloc ( in,0, &size,&offset,&err))!=NULL && err==GRIB_SUCCESS) {
grib_context_free(c,mesg);
(*count)++;
}
if (err==GRIB_END_OF_FILE) err=GRIB_SUCCESS;
if (err==GRIB_END_OF_FILE) err=GRIB_SUCCESS;
return err;
return err;
}
int main(int argc,char* argv[]) {
int main(int argc,char* argv[])
{
FILE* infh;
char* filename;
int i;
int err=0;
long n=0,nn=0;
FILE* infh;
char* filename;
int i;
int err=0;
long n=0,nn=0;
if (argc <2) usage(argv[0]);
if (argc <2) usage(argv[0]);
n=0;
for (i=1;i<argc;i++) {
filename=argv[i];
n=0;
for (i=1;i<argc;i++) {
filename=argv[i];
infh=fopen(filename,"r");
if (!infh) {
perror(filename);
exit(1);
}
infh=fopen(filename,"r");
if (!infh) {
perror(filename);
exit(1);
}
nn=0;
err=check_file(infh,&nn);
if (err!=0) {
fprintf(stderr,"Invalid grib message(s) found in %s\n", filename);
exit(err);
}
n+=nn;
nn=0;
err=check_file(infh,&nn);
if (err!=0) {
fprintf(stderr,"Invalid grib message(s) found in %s\n", filename);
exit(err);
}
n+=nn;
fclose(infh);
}
printf("%ld\n",n);
fclose(infh);
}
printf("%ld\n",n);
return 0;
return 0;
}

View File

@ -21,7 +21,7 @@ grib_option grib_options[]={
{"O",0,"Octet mode. WMO documentation style dump.\n",0,1,0},
{"D",0,0,0,1,0},
{"d",0,"Print all data values.\n",0,1,0},
{"j",0,0,0,1,0},
{"j",0,0,0,1,0},
{"C",0,0,0,1,0},
{"t",0,0,0,1,0},
{"H",0,0,0,1,0},
@ -45,130 +45,141 @@ static int json=0;
int grib_options_count=sizeof(grib_options)/sizeof(grib_option);
/**
*grib_dump
*Dump the content of a grib file
*
*/
int main(int argc, char *argv[]) { return grib_tool(argc,argv);}
int grib_tool_before_getopt(grib_runtime_options* options) {
return 0;
*grib_dump
*Dump the content of a grib file
*
*/
int main(int argc, char *argv[])
{
return grib_tool(argc,argv);
}
int grib_tool_init(grib_runtime_options* options) {
int grib_tool_before_getopt(grib_runtime_options* options)
{
return 0;
}
int opt=grib_options_on("C")+grib_options_on("O")+grib_options_on("D")+grib_options_on("j");
int grib_tool_init(grib_runtime_options* options)
{
options->dump_mode = "default";
int opt=grib_options_on("C")+grib_options_on("O")+grib_options_on("D")+grib_options_on("j");
if (opt > 1) {
printf("%s: simultaneous j/C/O/D options not allowed\n",grib_tool_name);
exit(1);
}
options->dump_mode = "default";
if (grib_options_on("j")) {
options->dump_mode = "json";
json=1;
}
if (opt > 1) {
printf("%s: simultaneous j/C/O/D options not allowed\n",grib_tool_name);
exit(1);
}
if (grib_options_on("C")) {
options->dump_mode = "c_code";
if (grib_options_on("d"))
options->dump_flags = 0;
else
options->dump_flags = GRIB_DUMP_FLAG_NO_DATA;
}
if (grib_options_on("j")) {
options->dump_mode = "json";
json=1;
}
if (grib_options_on("O")) {
options->dump_mode = "wmo";
options->dump_flags = GRIB_DUMP_FLAG_CODED
| GRIB_DUMP_FLAG_OCTECT
| GRIB_DUMP_FLAG_VALUES
| GRIB_DUMP_FLAG_READ_ONLY;
}
if (grib_options_on("C")) {
options->dump_mode = "c_code";
if (grib_options_on("d"))
options->dump_flags = 0;
else
options->dump_flags = GRIB_DUMP_FLAG_NO_DATA;
}
if (grib_options_on("D")) {
options->dump_mode = "debug";
options->dump_flags = GRIB_DUMP_FLAG_VALUES
| GRIB_DUMP_FLAG_READ_ONLY;
}
if (grib_options_on("O")) {
options->dump_mode = "wmo";
options->dump_flags = GRIB_DUMP_FLAG_CODED
| GRIB_DUMP_FLAG_OCTECT
| GRIB_DUMP_FLAG_VALUES
| GRIB_DUMP_FLAG_READ_ONLY;
}
if (grib_options_on("a"))
options->dump_flags |= GRIB_DUMP_FLAG_ALIASES;
if (grib_options_on("D")) {
options->dump_mode = "debug";
options->dump_flags = GRIB_DUMP_FLAG_VALUES
| GRIB_DUMP_FLAG_READ_ONLY;
}
if (grib_options_on("t"))
options->dump_flags |= GRIB_DUMP_FLAG_TYPE;
if (grib_options_on("a"))
options->dump_flags |= GRIB_DUMP_FLAG_ALIASES;
if (grib_options_on("H"))
options->dump_flags |= GRIB_DUMP_FLAG_HEXADECIMAL;
if (grib_options_on("t"))
options->dump_flags |= GRIB_DUMP_FLAG_TYPE;
if (grib_options_on("H"))
options->dump_flags |= GRIB_DUMP_FLAG_HEXADECIMAL;
/* In JSON mode we want to print all data values */
if ( grib_options_on("d") || grib_options_on("j") )
options->dump_flags |= GRIB_DUMP_FLAG_ALL_DATA;
options->dump_flags |= GRIB_DUMP_FLAG_ALL_DATA;
return 0;
return 0;
}
int grib_tool_new_filename_action(grib_runtime_options* options,const char* file) {
return 0;
int grib_tool_new_filename_action(grib_runtime_options* options,const char* file)
{
return 0;
}
int grib_tool_new_file_action(grib_runtime_options* options,grib_tools_file* file) {
char tmp[1024];
if (!options->current_infile->name) return 0;
if (json) return 0;
sprintf(tmp,"FILE: %s ",options->current_infile->name);
if (!grib_options_on("C"))
fprintf(stdout,"***** %s\n",tmp);
return 0;
int grib_tool_new_file_action(grib_runtime_options* options,grib_tools_file* file)
{
char tmp[1024];
if (!options->current_infile->name) return 0;
if (json) return 0;
sprintf(tmp,"FILE: %s ",options->current_infile->name);
if (!grib_options_on("C"))
fprintf(stdout,"***** %s\n",tmp);
return 0;
}
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h) {
long length=0;
char tmp[1024];
char identifier[100];
size_t idlen=100;
int i,err=0;
if (grib_get_long(h,"totalLength",&length) != GRIB_SUCCESS)
length=-9999;
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
{
long length=0;
char tmp[1024];
char identifier[100];
size_t idlen=100;
int i,err=0;
if (grib_get_long(h,"totalLength",&length) != GRIB_SUCCESS)
length=-9999;
if (!options->skip) {
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->skip) {
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);
}
for (i=0;i<options->print_keys_count;i++)
grib_set_flag(h,options->print_keys[i].name,GRIB_ACCESSOR_FLAG_DUMP);
for (i=0;i<options->print_keys_count;i++)
grib_set_flag(h,options->print_keys[i].name,GRIB_ACCESSOR_FLAG_DUMP);
if(json) {
}
else {
sprintf(tmp,"MESSAGE %d ( length=%ld )",options->handle_count,length);
if (!grib_options_on("C"))
fprintf(stdout,"#============== %-38s ==============\n",tmp);
if (!strcmp(options->dump_mode,"default")) {
GRIB_CHECK_NOLINE(grib_get_string(h,"identifier",identifier,&idlen),0);
printf("%s {\n",identifier);
}
}
if(json) {
}
else {
sprintf(tmp,"MESSAGE %d ( length=%ld )",options->handle_count,length);
if (!grib_options_on("C"))
fprintf(stdout,"#============== %-38s ==============\n",tmp);
if (!strcmp(options->dump_mode,"default")) {
GRIB_CHECK_NOLINE(grib_get_string(h,"identifier",identifier,&idlen),0);
printf("%s {\n",identifier);
}
}
grib_dump_content(h,stdout,options->dump_mode,options->dump_flags,0);
grib_dump_content(h,stdout,options->dump_mode,options->dump_flags,0);
if (!strcmp(options->dump_mode,"default"))
printf("}\n");
return 0;
if (!strcmp(options->dump_mode,"default"))
printf("}\n");
return 0;
}
int grib_tool_skip_handle(grib_runtime_options* options, grib_handle* h) {
grib_handle_delete(h);
return 0;
int grib_tool_skip_handle(grib_runtime_options* options, grib_handle* h)
{
grib_handle_delete(h);
return 0;
}
void grib_tool_print_key_values(grib_runtime_options* options,grib_handle* h) {
grib_print_key_values(options,h);
void grib_tool_print_key_values(grib_runtime_options* options,grib_handle* h)
{
grib_print_key_values(options,h);
}
int grib_tool_finalise_action(grib_runtime_options* options) {
return 0;
int grib_tool_finalise_action(grib_runtime_options* options)
{
return 0;
}

View File

@ -15,7 +15,6 @@
#include "grib_tools.h"
grib_option grib_options[]={
/* {id, args, help}, on, command_line, value */
{"f",0,0,0,1,0},
@ -28,80 +27,89 @@ grib_option grib_options[]={
{"V",0,0,0,1,0},
{"g",0,0,0,1,0},
{"G",0,0,0,1,0},
{"T:",0,0,0,1,0},
{"T:",0,0,0,1,0},
{"7",0,0,0,1,0},
{"v",0,0,0,1,0}
};
char* grib_tool_description="Apply the rules defined in rules_file to each grib "
"message\n\tin the grib files provided as arguments.";
"message\n\tin the grib files provided as arguments.";
char* grib_tool_name="grib_filter";
char* grib_tool_usage="[options] rules_file "
"grib_file grib_file ...";
"grib_file grib_file ...";
int grib_options_count=sizeof(grib_options)/sizeof(grib_option);
int main(int argc, char *argv[]) { return grib_tool(argc,argv);}
int grib_tool_before_getopt(grib_runtime_options* options) {
return 0;
int main(int argc, char *argv[])
{
return grib_tool(argc,argv);
}
int grib_tool_init(grib_runtime_options* options) {
options->action = grib_action_from_filter(options->infile_extra->name);
if (!options->action) {
fprintf(stderr,"%s: error unable to create action\n",options->infile_extra->name);
exit(1);
}
if ( options->outfile && options->outfile->name )
options->action->context->outfilename=options->outfile->name;
return 0;
int grib_tool_before_getopt(grib_runtime_options* options)
{
return 0;
}
int grib_tool_new_filename_action(grib_runtime_options* options,const char* file) {
return 0;
int grib_tool_init(grib_runtime_options* options)
{
options->action = grib_action_from_filter(options->infile_extra->name);
if (!options->action) {
fprintf(stderr,"%s: error unable to create action\n",options->infile_extra->name);
exit(1);
}
if ( options->outfile && options->outfile->name )
options->action->context->outfilename=options->outfile->name;
return 0;
}
int grib_tool_new_filename_action(grib_runtime_options* options,const char* file)
{
return 0;
}
int grib_tool_new_file_action(grib_runtime_options* options,grib_tools_file* file) {
return 0;
int grib_tool_new_file_action(grib_runtime_options* options,grib_tools_file* file)
{
return 0;
}
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h) {
int err=0;
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
{
int err=0;
if (options->current_infile->name) {
size_t len=strlen(options->current_infile->name);
grib_set_string(h,"file",options->current_infile->name,&len);
}
if (options->current_infile->name) {
size_t len=strlen(options->current_infile->name);
grib_set_string(h,"file",options->current_infile->name,&len);
}
err=grib_handle_apply_action(h,options->action);
if (err != GRIB_SUCCESS && options->fail) {
printf("ERROR: %s\n",grib_get_error_message(err));
exit(err);
}
return 0;
err=grib_handle_apply_action(h,options->action);
if (err != GRIB_SUCCESS && options->fail) {
printf("ERROR: %s\n",grib_get_error_message(err));
exit(err);
}
return 0;
}
int grib_tool_skip_handle(grib_runtime_options* options, grib_handle* h) {
grib_handle_delete(h);
return 0;
int grib_tool_skip_handle(grib_runtime_options* options, grib_handle* h)
{
grib_handle_delete(h);
return 0;
}
void grib_tool_print_key_values(grib_runtime_options* options,grib_handle* h) {
grib_print_key_values(options,h);
void grib_tool_print_key_values(grib_runtime_options* options,grib_handle* h)
{
grib_print_key_values(options,h);
}
int grib_tool_finalise_action(grib_runtime_options* options)
{
int err=0;
grib_file_close_all(&err);
if (err != GRIB_SUCCESS) {
perror(grib_tool_name);
exit(err);
}
int err=0;
grib_file_close_all(&err);
if (err != GRIB_SUCCESS) {
perror(grib_tool_name);
exit(err);
}
grib_file_pool_clean();
return 0;
grib_file_pool_clean();
return 0;
}

View File

@ -32,7 +32,7 @@ grib_option grib_options[]={
{"S",0,0,1,0,0},
{"g",0,0,0,1,0},
{"G",0,0,0,1,0},
{"T:",0,0,0,1,0},
{"T:",0,0,0,1,0},
{"7",0,0,0,1,0},
{"v",0,0,1,0,0},
{"X:",0,0,0,1,0},
@ -51,7 +51,10 @@ double lon=0;
int mode=0;
grib_nearest* n=NULL;
int main(int argc, char *argv[]) { return grib_tool(argc,argv);}
int main(int argc, char *argv[])
{
return grib_tool(argc,argv);
}
int grib_tool_before_getopt(grib_runtime_options* options)
{
@ -102,8 +105,6 @@ int grib_tool_init(grib_runtime_options* options)
options->latlon_mask=strdup(p);
}
}
}
if (options->latlon && options->latlon_mask) {
@ -156,7 +157,6 @@ int grib_tool_new_filename_action(grib_runtime_options* options,const char* file
return 0;
}
int grib_tool_new_file_action(grib_runtime_options* options,grib_tools_file* file)
{
return 0;

View File

@ -47,226 +47,226 @@ int grib_options_count=sizeof(grib_options)/sizeof(grib_option);
int main(int argc, char *argv[]) { return grib_tool(argc,argv);}
int grib_tool_before_getopt(grib_runtime_options* options) {
return 0;
return 0;
}
int grib_tool_init(grib_runtime_options* options) {
return 0;
return 0;
}
int grib_tool_new_filename_action(grib_runtime_options* options,const char* file) {
return 0;
return 0;
}
int grib_tool_new_file_action(grib_runtime_options* options,grib_tools_file* file) {
return 0;
return 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;
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 (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;
}
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);
iter=grib_iterator_new(h,0,&err);
data_values=(double*)calloc(numberOfPoints+1, sizeof(double));
if (!data_values) {
fprintf(dump_file, "ERROR: failed to allocate %ld bytes for data values (number of points=%ld)\n",
(numberOfPoints+1)*sizeof(double), numberOfPoints);
exit(GRIB_OUT_OF_MEMORY);
}
data_values=(double*)calloc(numberOfPoints+1, sizeof(double));
if (!data_values) {
fprintf(dump_file, "ERROR: failed to allocate %ld bytes for data values (number of points=%ld)\n",
(numberOfPoints+1)*sizeof(double), numberOfPoints);
exit(GRIB_OUT_OF_MEMORY);
}
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);
}
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* theEnd=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,&theEnd);
if (theEnd==NULL) missing_value=mval;
grib_set_double(h,"missingValue",missing_value);
}
skip_missing=1;
if (grib_options_on("m:")) {
char* theEnd=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,&theEnd);
if (theEnd==NULL) missing_value=mval;
grib_set_double(h,"missingValue",missing_value);
}
if (iter)
fprintf(dump_file,"Latitude, Longitude, ");
if (iter)
fprintf(dump_file,"Latitude, Longitude, ");
fprintf(dump_file,"Value");
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);
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");
fprintf(dump_file,"\n");
if (print_keys)
values=get_key_values(options,h);
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 (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 (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++;
}
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++;
}
}
}
} 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);
if (iter) grib_iterator_delete(iter);
free(data_values);
if (iter) {
free(lats);
free(lons);
}
free(data_values);
if (iter) {
free(lats);
free(lons);
}
return 0;
return 0;
}
int grib_tool_skip_handle(grib_runtime_options* options, grib_handle* h) {
grib_handle_delete(h);
return 0;
grib_handle_delete(h);
return 0;
}
void grib_tool_print_key_values(grib_runtime_options* options,grib_handle* h) {
grib_print_key_values(options,h);
grib_print_key_values(options,h);
}
int grib_tool_finalise_action(grib_runtime_options* options) {
return 0;
return 0;
}
static void print_key_values(grib_values* values,int values_count) {
int i=0;
for (i=0; i<values_count; i++) {
fprintf(dump_file," %s",values[i].string_value);
}
int i=0;
for (i=0; i<values_count; i++) {
fprintf(dump_file," %s",values[i].string_value);
}
}
static grib_values* get_key_values(grib_runtime_options* options,grib_handle* h) {
int i=0;
int ret=0;
char value[MAX_STRING_LEN]={0,};
char* notfound="not found";
int i=0;
int ret=0;
char value[MAX_STRING_LEN]={0,};
char* notfound="not found";
for (i=0;i<options->print_keys_count;i++) {
size_t len=MAX_STRING_LEN;
ret=GRIB_SUCCESS;
for (i=0;i<options->print_keys_count;i++) {
size_t len=MAX_STRING_LEN;
ret=GRIB_SUCCESS;
if (grib_is_missing(h,options->print_keys[i].name,&ret) && ret==GRIB_SUCCESS) {
options->print_keys[i].type=GRIB_TYPE_MISSING;
sprintf(value,"MISSING");
if (grib_is_missing(h,options->print_keys[i].name,&ret) && ret==GRIB_SUCCESS) {
options->print_keys[i].type=GRIB_TYPE_MISSING;
sprintf(value,"MISSING");
} else if ( ret != GRIB_NOT_FOUND ){
if (options->print_keys[i].type == GRIB_TYPE_UNDEFINED) {
grib_get_native_type(h,options->print_keys[i].name,&(options->print_keys[i].type));
}
} else if ( ret != GRIB_NOT_FOUND ){
if (options->print_keys[i].type == GRIB_TYPE_UNDEFINED) {
grib_get_native_type(h,options->print_keys[i].name,&(options->print_keys[i].type));
}
switch (options->print_keys[i].type) {
case GRIB_TYPE_STRING:
ret=grib_get_string( h,options->print_keys[i].name,value,&len);
break;
case GRIB_TYPE_DOUBLE:
ret=grib_get_double( h,options->print_keys[i].name,
&(options->print_keys[i].double_value));
sprintf(value,"%g",options->print_keys[i].double_value);
break;
case GRIB_TYPE_LONG:
ret=grib_get_long( h,options->print_keys[i].name,
&(options->print_keys[i].long_value));
sprintf(value,"%ld",(long)options->print_keys[i].long_value);
break;
default:
fprintf(dump_file,"invalid type for %s\n",options->print_keys[i].name);
exit(1);
switch (options->print_keys[i].type) {
case GRIB_TYPE_STRING:
ret=grib_get_string( h,options->print_keys[i].name,value,&len);
break;
case GRIB_TYPE_DOUBLE:
ret=grib_get_double( h,options->print_keys[i].name,
&(options->print_keys[i].double_value));
sprintf(value,"%g",options->print_keys[i].double_value);
break;
case GRIB_TYPE_LONG:
ret=grib_get_long( h,options->print_keys[i].name,
&(options->print_keys[i].long_value));
sprintf(value,"%ld",(long)options->print_keys[i].long_value);
break;
default:
fprintf(dump_file,"invalid type for %s\n",options->print_keys[i].name);
exit(1);
}
}
}
}
if (ret != GRIB_SUCCESS) {
if (options->fail) GRIB_CHECK_NOLINE(ret,options->print_keys[i].name);
if (ret == GRIB_NOT_FOUND) strcpy(value,notfound);
else {
fprintf(dump_file,"%s %s\n",grib_get_error_message(ret),options->print_keys[i].name);
exit(ret);
}
}
options->print_keys[i].string_value=strdup(value);
}
return options->print_keys;
if (ret != GRIB_SUCCESS) {
if (options->fail) GRIB_CHECK_NOLINE(ret,options->print_keys[i].name);
if (ret == GRIB_NOT_FOUND) strcpy(value,notfound);
else {
fprintf(dump_file,"%s %s\n",grib_get_error_message(ret),options->print_keys[i].name);
exit(ret);
}
}
options->print_keys[i].string_value=strdup(value);
}
return options->print_keys;
}

View File

@ -21,10 +21,10 @@ grib_option grib_options[]={
{"F:",0,0,1,1,"%g"},
{"P:",0,0,0,1,0},
{"w:",0,0,0,1,0},
{"j",0,"json output\n",0,1,0},
{"j",0,"json output\n",0,1,0},
{"B:",0,0,0,1,0},
{"l:",0,0,0,1,0},
{"s:",0,0,0,1,0},
{"s:",0,0,0,1,0},
{"i:",0,0,0,1,0},
{"n:",0,0,1,1,"ls"},
{"m",0,0,0,1,0},
@ -272,7 +272,7 @@ int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
char value[MAX_STRING_LEN];
size_t len=MAX_STRING_LEN;
printf("%s",new_handle);
printf( "{\n");
printf("{\n");
print_key_values(options,h);
printf("\n, \"selected\" : %d",options->latlon_idx);
printf(", \"method\" : ");

View File

@ -109,113 +109,113 @@ int grib_options_help_count=sizeof(grib_options_help_list)/sizeof(grib_options_h
void usage()
{
int i=0;
printf("\nNAME \t%s\n\n",grib_tool_name);
printf("DESCRIPTION\n\t%s\n\n",grib_tool_description);
printf("USAGE \n\t%s %s\n\n",grib_tool_name,grib_tool_usage);
printf("OPTIONS\n");
for (i=0;i<grib_options_count;i++) {
if (grib_options[i].command_line)
printf("\t-%c %s\t%s",grib_options[i].id[0],
grib_options_get_args(grib_options[i].id),
grib_options_get_help(grib_options[i].id));
}
printf("\n\n");
exit(1);
int i=0;
printf("\nNAME \t%s\n\n",grib_tool_name);
printf("DESCRIPTION\n\t%s\n\n",grib_tool_description);
printf("USAGE \n\t%s %s\n\n",grib_tool_name,grib_tool_usage);
printf("OPTIONS\n");
for (i=0;i<grib_options_count;i++) {
if (grib_options[i].command_line)
printf("\t-%c %s\t%s",grib_options[i].id[0],
grib_options_get_args(grib_options[i].id),
grib_options_get_help(grib_options[i].id));
}
printf("\n\n");
exit(1);
}
char* grib_options_get_option(const char* id)
{
int i=0;
for (i=0; i<grib_options_count; i++) {
if (!strcmp(id,grib_options[i].id))
return grib_options[i].value;
}
return NULL;
int i=0;
for (i=0; i<grib_options_count; i++) {
if (!strcmp(id,grib_options[i].id))
return grib_options[i].value;
}
return NULL;
}
int grib_options_command_line(const char* id)
{
int i=0;
for (i=0; i<grib_options_count; i++) {
if (!strcmp(id,grib_options[i].id))
return grib_options[i].command_line;
}
return 0;
int i=0;
for (i=0; i<grib_options_count; i++) {
if (!strcmp(id,grib_options[i].id))
return grib_options[i].command_line;
}
return 0;
}
int grib_options_on(const char* id)
{
int i=0;
for (i=0; i<grib_options_count; i++) {
if (!strcmp(id,grib_options[i].id))
return grib_options[i].on;
}
return 0;
int i=0;
for (i=0; i<grib_options_count; i++) {
if (!strcmp(id,grib_options[i].id))
return grib_options[i].on;
}
return 0;
}
int grib_get_runtime_options(int argc,char** argv,grib_runtime_options* options)
{
int i=0,c=0;
char* optstr=(char*)calloc(1,2*grib_options_count*sizeof(char));
int i=0,c=0;
char* optstr=(char*)calloc(1,2*grib_options_count*sizeof(char));
if (!optstr) return GRIB_OUT_OF_MEMORY;
if (!optstr) return GRIB_OUT_OF_MEMORY;
for (i=0;i<grib_options_count;i++)
if (grib_options[i].command_line)
strncat(optstr,grib_options[i].id,2);
for (i=0;i<grib_options_count;i++)
if (grib_options[i].command_line)
strncat(optstr,grib_options[i].id,2);
while ((c = getopt (argc, argv, optstr)) != -1) {
if (c == '?') usage();
i=0;
while ( i < grib_options_count && grib_options[i].id[0] != c) i++;
grib_options[i].on=1;
if ( grib_options[i].id[1] == ':' )
grib_options[i].value=optarg;
}
free(optstr);
return 0;
while ((c = getopt (argc, argv, optstr)) != -1) {
if (c == '?') usage();
i=0;
while ( i < grib_options_count && grib_options[i].id[0] != c) i++;
grib_options[i].on=1;
if ( grib_options[i].id[1] == ':' )
grib_options[i].value=optarg;
}
free(optstr);
return 0;
}
int grib_process_runtime_options(grib_context* context,int argc,char** argv,grib_runtime_options* options)
{
int i=0,ret=0;
int has_output=0;int has_input_extra=0,nfiles=0;
char *karg=NULL,*warg=NULL,*sarg=NULL,*barg=NULL;
if (grib_options_on("V")) {
printf("\ngrib_api Version ");
grib_print_api_version(stdout);
printf("\n\n");
exit(0);
}
int i=0,ret=0;
int has_output=0;int has_input_extra=0,nfiles=0;
char *karg=NULL,*warg=NULL,*sarg=NULL,*barg=NULL;
if (grib_options_on("B:"))
options->orderby=grib_options_get_option("B:");
if (grib_options_on("V")) {
printf("\ngrib_api Version ");
grib_print_api_version(stdout);
printf("\n\n");
exit(0);
}
if (grib_options_on("x")) options->headers_only=1;
else options->headers_only=0;
if (grib_options_on("B:"))
options->orderby=grib_options_get_option("B:");
if (grib_options_on("T:")) {
char* x=grib_options_get_option("T:");
if ( *x == 'T' ) options->mode=MODE_GTS;
else if ( *x == 'B' ) options->mode=MODE_BUFR;
else options->mode=MODE_GRIB;
}
if (grib_options_on("x")) options->headers_only=1;
else options->headers_only=0;
if (grib_options_on("F:"))
options->format=grib_options_get_option("F:");
else
options->format=strdup("%g");
if (grib_options_on("T:")) {
char* x=grib_options_get_option("T:");
if ( *x == 'T' ) options->mode=MODE_GTS;
else if ( *x == 'B' ) options->mode=MODE_BUFR;
else options->mode=MODE_GRIB;
}
if (grib_options_on("i:")) {
options->index_on=1;
options->index=atoi(grib_options_get_option("i:"));
}
if (grib_options_on("F:"))
options->format=grib_options_get_option("F:");
else
options->format=strdup("%g");
if (grib_options_on("l:"))
options->latlon=grib_options_get_option("l:");
if (grib_options_on("i:")) {
options->index_on=1;
options->index=atoi(grib_options_get_option("i:"));
}
if (grib_options_on("l:"))
options->latlon=grib_options_get_option("l:");
if (grib_options_on("X:"))
options->infile_offset=atol(grib_options_get_option("X:"));
@ -223,217 +223,217 @@ int grib_process_runtime_options(grib_context* context,int argc,char** argv,grib
/* Check at compile time to ensure our file offset is at least 64 bits */
COMPILE_TIME_ASSERT( sizeof(options->infile_offset) >= 8 );
has_output=grib_options_on("U");
has_input_extra=grib_options_on("I");
options->repack=grib_options_on("r");
options->gts=grib_options_on("g");
has_output=grib_options_on("U");
has_input_extra=grib_options_on("I");
options->repack=grib_options_on("r");
options->gts=grib_options_on("g");
if (grib_options_on("d:")) {
options->constant=atof(grib_options_get_option("d:"));
options->repack=1;
}
if (grib_options_on("G")) grib_gribex_mode_on(context);
nfiles=argc-optind;
if ( nfiles < (1+has_output+has_input_extra) && !options->infile ) usage();
if (has_input_extra) {
options->infile_extra=(grib_tools_file*)calloc(1,sizeof(grib_tools_file));
options->infile_extra->name=argv[optind];
}
if (!options->infile) {
for (i=optind+has_input_extra;i<argc-has_output;i++) {
grib_tools_file* p=NULL;
grib_tools_file* infile=(grib_tools_file*)calloc(1,sizeof(grib_tools_file));
infile->name=argv[i];
if (!options->infile) options->infile=infile;
else {
p=options->infile;
while(p->next) p=p->next;
p->next=infile;
}
if (grib_options_on("d:")) {
options->constant=atof(grib_options_get_option("d:"));
options->repack=1;
}
}
if (has_output) {
options->outfile=(grib_tools_file*)calloc(1,sizeof(grib_tools_file));
options->outfile->name=strdup(argv[argc-1]);
}
if (grib_options_on("G")) grib_gribex_mode_on(context);
if (grib_options_on("o:")) {
options->outfile=(grib_tools_file*)calloc(1,sizeof(grib_tools_file));
options->outfile->name=grib_options_get_option("o:");
}
nfiles=argc-optind;
if ( nfiles < (1+has_output+has_input_extra) && !options->infile ) usage();
options->print_number=grib_options_on("N");
options->print_header=grib_options_on("H");
options->verbose=grib_options_on("v");
if (has_input_extra) {
options->infile_extra=(grib_tools_file*)calloc(1,sizeof(grib_tools_file));
options->infile_extra->name=argv[optind];
}
if (grib_options_on("q") && grib_options_command_line("q"))
options->verbose=0;
if (!options->infile) {
for (i=optind+has_input_extra;i<argc-has_output;i++) {
grib_tools_file* p=NULL;
grib_tools_file* infile=(grib_tools_file*)calloc(1,sizeof(grib_tools_file));
infile->name=argv[i];
if (!options->infile) options->infile=infile;
else {
p=options->infile;
while(p->next) p=p->next;
p->next=infile;
}
}
}
options->fail=!grib_options_on("f");
if (grib_options_get_option("W:"))
options->default_print_width=atoi(grib_options_get_option("W:"));
if (has_output) {
options->outfile=(grib_tools_file*)calloc(1,sizeof(grib_tools_file));
options->outfile->name=strdup(argv[argc-1]);
}
if (grib_options_on("n:"))
options->name_space=grib_options_get_option("n:");
if (grib_options_on("m"))
options->name_space=strdup("mars");
if (grib_options_on("o:")) {
options->outfile=(grib_tools_file*)calloc(1,sizeof(grib_tools_file));
options->outfile->name=grib_options_get_option("o:");
}
if (grib_options_on("P:")) karg=grib_options_get_option("P:");
else if (grib_options_on("p:")) {
karg=grib_options_get_option("p:");
options->name_space=NULL;
}
options->requested_print_keys_count=MAX_KEYS;
ret = parse_keyval_string(grib_tool_name,karg,0,GRIB_TYPE_UNDEFINED,
options->requested_print_keys,&(options->requested_print_keys_count));
if (ret == GRIB_INVALID_ARGUMENT) usage();
GRIB_CHECK_NOLINE(ret,0);
options->print_number=grib_options_on("N");
options->print_header=grib_options_on("H");
options->verbose=grib_options_on("v");
options->strict=grib_options_on("S");
if (grib_options_on("q") && grib_options_command_line("q"))
options->verbose=0;
if (grib_options_on("M")) grib_multi_support_off(context);
else grib_multi_support_on(context);
options->fail=!grib_options_on("f");
if (grib_options_get_option("W:"))
options->default_print_width=atoi(grib_options_get_option("W:"));
if (grib_options_on("g")) grib_gts_header_on(context);
else grib_gts_header_off(context);
if (grib_options_on("n:"))
options->name_space=grib_options_get_option("n:");
if (grib_options_on("V")) {
printf("\ngrib_api Version ");
grib_print_api_version(stdout);
printf("\n\n");
}
if (grib_options_on("m"))
options->name_space=strdup("mars");
if (grib_options_on("s:")) {
sarg=grib_options_get_option("s:");
options->set_values_count=MAX_KEYS;
ret=parse_keyval_string(grib_tool_name, sarg,1,GRIB_TYPE_UNDEFINED,options->set_values,&(options->set_values_count));
if (grib_options_on("P:")) karg=grib_options_get_option("P:");
else if (grib_options_on("p:")) {
karg=grib_options_get_option("p:");
options->name_space=NULL;
}
options->requested_print_keys_count=MAX_KEYS;
ret = parse_keyval_string(grib_tool_name,karg,0,GRIB_TYPE_UNDEFINED,
options->requested_print_keys,&(options->requested_print_keys_count));
if (ret == GRIB_INVALID_ARGUMENT) usage();
}
GRIB_CHECK_NOLINE(ret,0);
if (grib_options_on("b:")) {
barg=grib_options_get_option("b:");
options->set_values_count=MAX_KEYS;
ret=parse_keyval_string(grib_tool_name,barg,0,GRIB_TYPE_STRING,options->set_values,&(options->set_values_count));
if (ret == GRIB_INVALID_ARGUMENT) usage();
}
options->strict=grib_options_on("S");
if (grib_options_on("c:")) {
sarg=grib_options_get_option("c:");
options->compare_count=MAX_KEYS;
ret=parse_keyval_string(grib_tool_name,sarg,0,GRIB_TYPE_UNDEFINED,options->compare,
if (grib_options_on("M")) grib_multi_support_off(context);
else grib_multi_support_on(context);
if (grib_options_on("g")) grib_gts_header_on(context);
else grib_gts_header_off(context);
if (grib_options_on("V")) {
printf("\ngrib_api Version ");
grib_print_api_version(stdout);
printf("\n\n");
}
if (grib_options_on("s:")) {
sarg=grib_options_get_option("s:");
options->set_values_count=MAX_KEYS;
ret=parse_keyval_string(grib_tool_name, sarg,1,GRIB_TYPE_UNDEFINED,options->set_values,&(options->set_values_count));
if (ret == GRIB_INVALID_ARGUMENT) usage();
}
if (grib_options_on("b:")) {
barg=grib_options_get_option("b:");
options->set_values_count=MAX_KEYS;
ret=parse_keyval_string(grib_tool_name,barg,0,GRIB_TYPE_STRING,options->set_values,&(options->set_values_count));
if (ret == GRIB_INVALID_ARGUMENT) usage();
}
if (grib_options_on("c:")) {
sarg=grib_options_get_option("c:");
options->compare_count=MAX_KEYS;
ret=parse_keyval_string(grib_tool_name,sarg,0,GRIB_TYPE_UNDEFINED,options->compare,
&(options->compare_count));
if (ret == GRIB_INVALID_ARGUMENT) usage();
}
if (grib_options_on("e")) {
for (i=0;i<names_count; i++) {
options->compare[i+options->compare_count].name=names[i];
options->compare[i+options->compare_count].type=GRIB_NAMESPACE;
if (ret == GRIB_INVALID_ARGUMENT) usage();
}
if (grib_options_on("e")) {
for (i=0;i<names_count; i++) {
options->compare[i+options->compare_count].name=names[i];
options->compare[i+options->compare_count].type=GRIB_NAMESPACE;
}
options->compare_count+=names_count;
}
options->compare_count+=names_count;
}
warg=grib_options_get_option("w:");
warg=grib_options_get_option("w:");
options->constraints_count=MAX_KEYS;
ret=parse_keyval_string(grib_tool_name,warg,1,GRIB_TYPE_UNDEFINED,
options->constraints,&(options->constraints_count));
if (ret == GRIB_INVALID_ARGUMENT) usage();
options->constraints_count=MAX_KEYS;
ret=parse_keyval_string(grib_tool_name,warg,1,GRIB_TYPE_UNDEFINED,
options->constraints,&(options->constraints_count));
if (ret == GRIB_INVALID_ARGUMENT) usage();
return GRIB_SUCCESS;
return GRIB_SUCCESS;
}
char* grib_options_get_help(char* id)
{
int i=0;
char msg[]="ERROR: help not found for option ";
char* err=(char*)calloc(1,sizeof(msg)+3);
sprintf(err,"%s%c\n",msg,*id);
for (i=0; i<grib_options_count;i++) {
if (!strcmp(id,grib_options[i].id)) {
if (grib_options[i].help != NULL) return grib_options[i].help;
else break;
}
}
for (i=0; i<grib_options_help_count;i++) {
if (!strcmp(id,grib_options_help_list[i].id)) {
return grib_options_help_list[i].help != NULL ? grib_options_help_list[i].help : err;
}
}
return err;
int i=0;
char msg[]="ERROR: help not found for option ";
char* err=(char*)calloc(1,sizeof(msg)+3);
sprintf(err,"%s%c\n",msg,*id);
for (i=0; i<grib_options_count;i++) {
if (!strcmp(id,grib_options[i].id)) {
if (grib_options[i].help != NULL) return grib_options[i].help;
else break;
}
}
for (i=0; i<grib_options_help_count;i++) {
if (!strcmp(id,grib_options_help_list[i].id)) {
return grib_options_help_list[i].help != NULL ? grib_options_help_list[i].help : err;
}
}
return err;
}
char* grib_options_get_args(char* id)
{
int i=0;
char empty[]="";
char msg[]="ERROR: help not found for option -";
char* err=NULL;
if (id[1] != ':') return strdup(empty);
err=(char*)calloc(1,sizeof(msg)+3);
sprintf(err,"%s%c\n",msg,*id);
for (i=0; i<grib_options_count;i++) {
if (!strcmp(id,grib_options[i].id)) {
if (grib_options[i].args != NULL) return grib_options[i].args;
else break;
}
}
for (i=0; i<grib_options_help_count;i++) {
if (!strcmp(id,grib_options_help_list[i].id)) {
return grib_options_help_list[i].args != NULL ? grib_options_help_list[i].args : err;
}
}
return err;
int i=0;
char empty[]="";
char msg[]="ERROR: help not found for option -";
char* err=NULL;
if (id[1] != ':') return strdup(empty);
err=(char*)calloc(1,sizeof(msg)+3);
sprintf(err,"%s%c\n",msg,*id);
for (i=0; i<grib_options_count;i++) {
if (!strcmp(id,grib_options[i].id)) {
if (grib_options[i].args != NULL) return grib_options[i].args;
else break;
}
}
for (i=0; i<grib_options_help_count;i++) {
if (!strcmp(id,grib_options_help_list[i].id)) {
return grib_options_help_list[i].args != NULL ? grib_options_help_list[i].args : err;
}
}
return err;
}
void usage_doxygen()
{
int i=0;
printf("/*! \\page %s %s\n",grib_tool_name,grib_tool_name);
printf("\\section DESCRIPTION \n %s\n\n",grib_tool_description);
printf("\\section USAGE \n %s \n %s\n\n",grib_tool_name,grib_tool_usage);
printf("\\section OPTIONS \n");
for (i=0;i<grib_options_count;i++) {
if (grib_options[i].command_line) {
printf("-%c %s \\n",
grib_options[i].id[0],
grib_options_get_args(grib_options[i].id));
printf(" %s \\n \\n ",
grib_options_get_help(grib_options[i].id));
int i=0;
printf("/*! \\page %s %s\n",grib_tool_name,grib_tool_name);
printf("\\section DESCRIPTION \n %s\n\n",grib_tool_description);
printf("\\section USAGE \n %s \n %s\n\n",grib_tool_name,grib_tool_usage);
printf("\\section OPTIONS \n");
for (i=0;i<grib_options_count;i++) {
if (grib_options[i].command_line) {
printf("-%c %s \\n",
grib_options[i].id[0],
grib_options_get_args(grib_options[i].id));
printf(" %s \\n \\n ",
grib_options_get_help(grib_options[i].id));
}
}
}
exit(1);
exit(1);
}
#if 0
void usage_doxygen() {
int i=0;
printf("/*! \\page %s %s\n",grib_tool_name,grib_tool_name);
printf("\\section DESCRIPTION \n%s\n\n",grib_tool_description);
printf("\\section USAGE \n%s \n%s\n\n",grib_tool_name,grib_tool_usage);
printf("\\section OPTIONS\n");
printf("<table frame=void border=0>\n");
for (i=0;i<grib_options_count;i++) {
if (grib_options[i].command_line) {
printf("<tr>\n");
printf("<td colspan=2>-%c %s</td>\n",
grib_options[i].id[0],
grib_options_get_args(grib_options[i].id));
printf("</tr><tr>\n");
printf("<td width=20></td><td>%s</td>",
grib_options_get_help(grib_options[i].id));
printf("</tr><tr><td></td></tr>\n");
}
}
printf("</table>\n");
exit(1);
int i=0;
printf("/*! \\page %s %s\n",grib_tool_name,grib_tool_name);
printf("\\section DESCRIPTION \n%s\n\n",grib_tool_description);
printf("\\section USAGE \n%s \n%s\n\n",grib_tool_name,grib_tool_usage);
printf("\\section OPTIONS\n");
printf("<table frame=void border=0>\n");
for (i=0;i<grib_options_count;i++) {
if (grib_options[i].command_line) {
printf("<tr>\n");
printf("<td colspan=2>-%c %s</td>\n",
grib_options[i].id[0],
grib_options_get_args(grib_options[i].id));
printf("</tr><tr>\n");
printf("<td width=20></td><td>%s</td>",
grib_options_get_help(grib_options[i].id));
printf("</tr><tr><td></td></tr>\n");
}
}
printf("</table>\n");
exit(1);
}
#endif

View File

@ -15,149 +15,156 @@
#include "grib_tools.h"
grib_option grib_options[]={
/* {id, args, help}, on, command_line, value*/
{"s:",0,0,1,1,0},
{"r",0,0,0,1,0},
{"d:",0,0,0,1,0},
/*{"n:","noise percentage","\n\t\tAdd noise to the data values. The noise added is the given percentage of the data value.\n",0,1,0},*/
{"p:",0,0,1,1,0},
{"P:",0,0,0,1,0},
{"w:","key[:{s/d/l}]=value,key[:{s/d/l}]=value,...",
"\n\t\tWhere clause.\n\t\tSet is only executed for grib messages matching all the "
"key/value constraints.\n\t\tIf a grib message does not match the constraints it is"
" copied unchanged\n\t\tto the output_grib_file. This behaviour can be changed "
"setting the option -S.\n\t\tFor each key a string (key:s) or a double (key:d) or"
" a long (key:l)\n\t\ttype can be defined. Default type is string.\n",0,1,0},
{"q",0,0,1,0,0},
{"7",0,0,0,1,0},
{"S",0,0,0,1,0},
{"U",0,0,1,0,0},
{"V",0,0,0,1,0},
{"M",0,0,0,1,0},
{"g",0,0,0,1,0},
{"G",0,0,0,1,0},
{"T:",0,0,0,1,0},
{"f",0,0,0,1,0},
{"v",0,0,0,1,0}
/* {id, args, help}, on, command_line, value*/
{"s:",0,0,1,1,0},
{"r",0,0,0,1,0},
{"d:",0,0,0,1,0},
/*{"n:","noise percentage","\n\t\tAdd noise to the data values. The noise added is the given percentage of the data value.\n",0,1,0},*/
{"p:",0,0,1,1,0},
{"P:",0,0,0,1,0},
{"w:","key[:{s/d/l}]=value,key[:{s/d/l}]=value,...",
"\n\t\tWhere clause.\n\t\tSet is only executed for grib messages matching all the "
"key/value constraints.\n\t\tIf a grib message does not match the constraints it is"
" copied unchanged\n\t\tto the output_grib_file. This behaviour can be changed "
"setting the option -S.\n\t\tFor each key a string (key:s) or a double (key:d) or"
" a long (key:l)\n\t\ttype can be defined. Default type is string.\n",0,1,0},
{"q",0,0,1,0,0},
{"7",0,0,0,1,0},
{"S",0,0,0,1,0},
{"U",0,0,1,0,0},
{"V",0,0,0,1,0},
{"M",0,0,0,1,0},
{"g",0,0,0,1,0},
{"G",0,0,0,1,0},
{"T:",0,0,0,1,0},
{"f",0,0,0,1,0},
{"v",0,0,0,1,0}
};
char* grib_tool_description="Sets key/value pairs in the input grib file and writes"
"\n\teach message to the output_grib_file."
"\n\tIt fails when an error occurs (e.g. key not found).";
"\n\teach message to the output_grib_file."
"\n\tIt fails when an error occurs (e.g. key not found).";
char* grib_tool_name="grib_set";
char* grib_tool_usage="[options] grib_file grib_file ... output_grib_file";
int grib_options_count=sizeof(grib_options)/sizeof(grib_option);
int main(int argc, char *argv[]) { return grib_tool(argc,argv);}
int grib_tool_before_getopt(grib_runtime_options* options) {
return 0;
int main(int argc, char *argv[])
{
return grib_tool(argc,argv);
}
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:")) {
int grib_tool_before_getopt(grib_runtime_options* options)
{
return 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 (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);
}
}
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;
return 0;
}
int grib_tool_new_filename_action(grib_runtime_options* options,const char* file) {
return 0;
int grib_tool_new_filename_action(grib_runtime_options* options,const char* file)
{
return 0;
}
int grib_tool_new_file_action(grib_runtime_options* options,grib_tools_file* file) {
return 0;
int grib_tool_new_file_action(grib_runtime_options* options,grib_tools_file* file)
{
return 0;
}
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h) {
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
{
int i=0;
int err=0;
int 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);
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);
}
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);
}
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 (options->set_values_count != 0)
err=grib_set_values(h,options->set_values,options->set_values_count);
if ( options->repack ) {
if ( options->repack ) {
if (grib_options_on("d:")) {
for(i = 0; i< size; i++)
v[i] = options->constant;
}
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;
}
if (grib_options_on("n:")) {
for(i = 0; i< size; i++)
v[i] = options->constant;
}
#endif
GRIB_CHECK_NOLINE(grib_set_double_array(h,"values",v,size),0);
free(v);
}
GRIB_CHECK_NOLINE(grib_set_double_array(h,"values",v,size),0);
free(v);
}
if( err != GRIB_SUCCESS && options->fail) exit(err);
}
if( err != GRIB_SUCCESS && options->fail) exit(err);
}
if (!options->skip || !options->strict) grib_tools_write_message(options,h);
if (!options->skip || !options->strict) grib_tools_write_message(options,h);
return 0;
return 0;
}
int grib_tool_skip_handle(grib_runtime_options* options, grib_handle* h) {
grib_handle_delete(h);
return 0;
int grib_tool_skip_handle(grib_runtime_options* options, grib_handle* h)
{
grib_handle_delete(h);
return 0;
}
void grib_tool_print_key_values(grib_runtime_options* options,grib_handle* h) {
grib_print_key_values(options,h);
void grib_tool_print_key_values(grib_runtime_options* options,grib_handle* h)
{
grib_print_key_values(options,h);
}
int grib_tool_finalise_action(grib_runtime_options* options)
{
int err=0;
grib_file_close_all(&err);
if (err != GRIB_SUCCESS) {
perror(grib_tool_name);
exit(err);
}
int err=0;
grib_file_close_all(&err);
if (err != GRIB_SUCCESS) {
perror(grib_tool_name);
exit(err);
}
return 0;
return 0;
}

View File

@ -14,72 +14,72 @@
#include "string.h"
char *optarg; /* global argument pointer */
int optind = 0; /* global argv index */
char *optarg; /* global argument pointer */
int optind = 0; /* global argv index */
int opterr = 0;
int getopt(int argc, char *argv[], const char *optstring)
{
static char *next = 0;
char c;
char *cp;
if (optind == 0)
next = 0;
static char *next = 0;
char c;
char *cp;
if (optind == 0)
next = 0;
optarg = 0;
optarg = 0;
if (next == 0 || *next == '\0')
{
if (optind == 0)
optind++;
if (next == 0 || *next == '\0')
{
if (optind == 0)
optind++;
if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
{
optarg = 0;
if (optind < argc)
optarg = argv[optind];
return -1;
}
if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
{
optarg = 0;
if (optind < argc)
optarg = argv[optind];
return -1;
}
if (strcmp(argv[optind], "--") == 0)
{
optind++;
optarg = NULL;
if (optind < argc)
optarg = argv[optind];
return -1;
}
if (strcmp(argv[optind], "--") == 0)
{
optind++;
optarg = NULL;
if (optind < argc)
optarg = argv[optind];
return -1;
}
next = argv[optind];
next++; /* skip past - */
optind++;
}
next = argv[optind];
next++; /* skip past - */
optind++;
}
c = *next++;
cp = strrchr(optstring, c);
c = *next++;
cp = strrchr(optstring, c);
if (cp == 0 || c == ':')
return '?';
if (cp == 0 || c == ':')
return '?';
cp++;
if (*cp == ':')
{
if (*next != '\0')
{
optarg = next;
next = 0;
}
else if (optind < argc)
{
optarg = argv[optind];
optind++;
}
else
{
return '?';
}
}
cp++;
if (*cp == ':')
{
if (*next != '\0')
{
optarg = next;
next = 0;
}
else if (optind < argc)
{
optarg = argv[optind];
optind++;
}
else
{
return '?';
}
}
return c;
return c;
}
#endif