Changeset 13830

Show
Ignore:
Timestamp:
02/21/08 14:37:29 (5 months ago)
Author:
warmerdam
Message:

improved error checking, a bit of cleanup

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • spike/grib/gribdataset.cpp

    r13823 r13830  
    6767    const char *GetProjectionRef(); 
    6868        private: 
    69                 void SetMetaData(grib_MetaData* meta); 
     69                void SetGribMetaData(grib_MetaData* meta); 
    7070    FILE        *fp; 
    7171    char  *pszProjection; 
     
    198198    IS_Init (&is); 
    199199 
    200     int bandNr = 0; 
    201  
    202200 
    203201    /* Read GRIB message from file position "start". */ 
     
    210208 
    211209    char * errMsg = errSprintf(NULL); // no intention to show errors, just swallow it and free the memory 
     210    CPLDebug( "GRIB", "%s", errMsg ); 
    212211    free(errMsg); 
    213212    IS_Free(&is); 
     
    215214 
    216215/************************************************************************/ 
    217 /*                           ~MSGRasterBand()                           */ 
     216/*                           ~GRIBRasterBand()                          */ 
    218217/************************************************************************/ 
    219218 
     
    303302        free (buff); 
    304303        char * errMsg = errSprintf(NULL); 
     304        CPLDebug( "GRIB", "%s", errMsg ); 
    305305        free(errMsg); 
    306306        return NULL; 
     
    339339    int msgNum =0;          /* The messageNumber during the inventory. */ 
    340340 
    341     if (GRIB2Inventory (grib_fp, &Inv, &LenInv, 0, &msgNum) >= 0) 
    342     { 
    343         for (uInt4 i = 0; i < LenInv; ++i) 
     341    if (GRIB2Inventory (grib_fp, &Inv, &LenInv, 0, &msgNum) <= 0 ) 
     342    { 
     343        CPLError( CE_Failure, CPLE_OpenFailed,  
     344                  "%s is a grib file, but no raster dataset was successfully identified.", 
     345                  poOpenInfo->pszFilename ); 
     346        delete poDS; 
     347        return NULL; 
     348    } 
     349 
     350/* -------------------------------------------------------------------- */ 
     351/*      Create band objects.                                            */ 
     352/* -------------------------------------------------------------------- */ 
     353    for (uInt4 i = 0; i < LenInv; ++i) 
     354    { 
     355        uInt4 bandNr = i+1; 
     356        if (bandNr == 1) 
    344357        { 
    345             uInt4 bandNr = i+1; 
    346             if (bandNr == 1) 
     358            // important: set DataSet extents before creating first RasterBand in it 
     359            double * data = NULL; 
     360            grib_MetaData* metaData; 
     361            GRIBRasterBand::ReadGribData(grib_fp, 0, Inv[i].subgNum, &data, &metaData); 
     362            if (metaData->gds.Nx < 1 || metaData->gds.Ny < 1 ) 
    347363            { 
    348                 // important: set DataSet extents before creating first RasterBand in it 
    349                 double * data = NULL; 
    350                 grib_MetaData* metaData; 
    351                 GRIBRasterBand::ReadGribData(grib_fp, 0, Inv[i].subgNum, &data, &metaData); 
    352                 poDS->SetMetaData(metaData); // set the DataSet's x,y size, georeference and projection from the first GRIB band 
    353                 GRIBRasterBand* gribBand = new GRIBRasterBand( poDS, bandNr, Inv[i].start, Inv[i].subgNum, Inv[i].longFstLevel); 
    354                 gribBand->m_Grib_Data = data; 
    355                 gribBand->m_Grib_MetaData = metaData; 
    356                 poDS->SetBand( bandNr, gribBand); 
     364                CPLError( CE_Failure, CPLE_OpenFailed,  
     365                          "%s is a grib file, but no raster dataset was successfully identified.", 
     366                          poOpenInfo->pszFilename ); 
     367                delete poDS; 
     368                return NULL; 
    357369            } 
    358             else 
    359                 poDS->SetBand( bandNr, new GRIBRasterBand( poDS, bandNr, Inv[i].start, Inv[i].subgNum, Inv[i].longFstLevel)); 
    360             GRIB2InventoryFree (Inv + i); 
     370 
     371            poDS->SetGribMetaData(metaData); // set the DataSet's x,y size, georeference and projection from the first GRIB band 
     372            GRIBRasterBand* gribBand = new GRIBRasterBand( poDS, bandNr, Inv[i].start, Inv[i].subgNum, Inv[i].longFstLevel); 
     373            gribBand->m_Grib_Data = data; 
     374            gribBand->m_Grib_MetaData = metaData; 
     375            poDS->SetBand( bandNr, gribBand); 
    361376        } 
    362         free (Inv); 
    363     } 
     377        else 
     378            poDS->SetBand( bandNr, new GRIBRasterBand( poDS, bandNr, Inv[i].start, Inv[i].subgNum, Inv[i].longFstLevel)); 
     379        GRIB2InventoryFree (Inv + i); 
     380    } 
     381    free (Inv); 
    364382 
    365383/* -------------------------------------------------------------------- */ 
     
    376394/************************************************************************/ 
    377395 
    378 void GRIBDataset::SetMetaData(grib_MetaData* meta) 
     396void GRIBDataset::SetGribMetaData(grib_MetaData* meta) 
    379397{ 
    380398    nRasterXSize = meta->gds.Nx;