int main(int argc,char* argv[]) {
    struct stat finfo;
    char shortName[20]={0,};
    size_t len=20;
    grib_handle* h=NULL;
    size_t fsize;
    unsigned char* data=NULL;
    unsigned char* p=NULL;
    void* pdata=NULL;
    int error=0;
    int count=0;
    char* filename=NULL;
    FILE* f=NULL;
    long level=0;
    grib_context* c=grib_context_get_default();

    if (argc==3 && !strcmp(argv[1],"-m")) {
        grib_multi_support_on(0);
        filename=argv[2];
    }
    else if (argc==2) filename=argv[1];
    else usage(argv[0]);

    f=fopen(filename,"r");
    if (!f) {perror(filename);exit(1);}

    fstat(fileno((FILE*)f),&finfo);
    fsize=finfo.st_size;

    data=(unsigned char*)malloc(fsize);
    p=data;

    if (!data) {
        fprintf(stderr,"unable to allocate %ld bytes\n",(long)fsize);
        exit(1);
    }

    if( fread(data, 1, fsize, f)  != fsize) {
        perror(filename);
        exit(1);
    }
    fclose(f);
    pdata=&data;

    while ((h=grib_handle_new_from_multi_message(c,(void**)pdata,&fsize,&error))!=NULL) {
        count++;
        len=20;
        GRIB_CHECK(grib_get_string(h,"shortName",shortName,&len),"shortName");
        GRIB_CHECK(grib_get_long(h,"level",&level),"level");
        printf("%d %s %ld\n",count,shortName,level);
        grib_handle_delete(h);
    }

    free(p);

    return 0;
}
Beispiel #2
0
int main(int argc, char** argv)
{
	int err = 0;
	long parameterCategory=0,parameterNumber=0,discipline=0;
	FILE* in = NULL;
	char* filename = "../../data/multi.grib2";
	grib_handle *h = NULL;

	/* turn on support for multi fields messages */
	grib_multi_support_on(0);

	/* turn off support for multi fields messages */
	/* grib_multi_support_off(0); */

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

	while ((h = grib_handle_new_from_file(0,in,&err)) != NULL ) {

		GRIB_CHECK(err,0);

		GRIB_CHECK(grib_get_long(h,"discipline",&discipline),0);
		printf("discipline=%ld\n",discipline);

		GRIB_CHECK(grib_get_long(h,"parameterCategory",&parameterCategory),0);
		printf("parameterCategory=%ld\n",parameterCategory);

		GRIB_CHECK(grib_get_long(h,"parameterNumber",&parameterNumber),0);
		printf("parameterNumber=%ld\n",parameterNumber);

		if ( discipline == 0 && parameterCategory==2) {
			if (parameterNumber == 2) printf("-------- u -------\n");
			if (parameterNumber == 3) printf("-------- v -------\n");
		}
		grib_handle_delete(h);
	}

	fclose(in);
	return 0;
}
Beispiel #3
0
GribFile::GribFile(const std::string & fileName) :
		fileName_(fileName),
		gribFile_(0)
{
    WDB_LOG & log = WDB_LOG::getInstance( "wdb.gribLoad.openGrib" );
    log.debugStream() << "Attempting to open GRIB file " << fileName_;

 	grib_multi_support_on(0);

    gribFile_ = fopen( fileName_.c_str(), "r" );

    if ( ! gribFile_ ) {
        std::ostringstream errorMessage;
        errorMessage << "Could not open the GRIB file " << fileName_;
        log.errorStream() << errorMessage.str();
        throw std::runtime_error( errorMessage.str() );
    }

    log.debugStream() << "Opened GRIB file successfully";
}
Beispiel #4
0
void codes_grib_multi_support_on(grib_context* c)
{
    grib_multi_support_on(c);
}