////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
//
// Class:   NetCDFFileIO
//
// Filename: ReadL1BNetCDF.cpp
//
// Header Files: NetCDFFileIO.h
//
// Description: This class is designed to provide a series
//              of utilities to read the Level 1B
//              routine NetCDF files that the GUVI DP POC
//              is producing.  These are as follows:
//                  GUVI Imaging Disk Level 1B data product
//                  GUVI Imaging Limb Level 1B data product
//                  GUVI Static Imaging Level 1B data product
//
// Author: M. Weiss
//
// Date: April, 1999
//
// Change History:
// ---------------
// Oct. 99      MW  modified to match new data file definitions.
//                  Added usage of GUVIBaseTypes.h.
// Nov. 99      MW  Added check so that negative errors aren't 
//                  scaled
// Jan. 02      MW  Added calls to nc_close in ReadL1BImagingDisk,
//                  ReadL1BImagingLimb and ReadL1BStaticImaging
//                  methods.  Changed SecsOfDay to MsecsOfDay. 
//                  Utilization of Vector from standard template
//                  library to store a full orbits worth of data.
//                  Changed processing and output parameter to 
//                  return a full orbits worth of data.
// Feb. 02      MW  Changed array index names from per* to Num*.
//                  Removed color index from Pierce point lat/lon.
//                  Moved Array indices from ImagingDisk1BPixelData,
//                  ImagingLimb1BPixelData and StaticImaging1BPixelData
//                  to individual fields in record of PixelData.
// Feb. 02      BW  DQI for the L1B data files got moved from the / scan
//                  basis to the per pixel basis.
///
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////

// include files
#include /local/netcdf3.4/include/netcdf.h>

#include 
#include "NetCDFFileIO.h"
#include "LogUtil.h"


////////////////////////////////////////////////////////////
//
// Method: ReadLevel1BImagingDisk
//
// Description: This method reads the GUVI level 1B imaging
//                disk data from a netcdf file.  This method 
//                returns a single orbits worth of imaging 
//                data for every call
//
// Inputs: 
//    Name              Description
//    ----              -----------
//    inFileName        file name of data to be read
//    orbitIData        a pointer to the data to be read
//
// Return: 
//    Name              Description
//    ----              -----------
//    n/a               true = successful read
//                      false = unsuccessful read
//
bool NetCDFFileIO::ReadLevel1BImagingDisk(
    string                inFileName, 
    OrbitOfImagingDisk1B& orbitIData )
{
    // to store a single scans worth of imaging data into
    ImagingDisk1BFormat imagingData;

    // indicates which scan number we're currently on
    int        scanNum;
    size_t     numScans;
    int        status;
    int        ncid;    // netcdf file id

    // the following are used for the netcdf variable
    // ids
    int        scanDim;
    int        xtrkDim;
    int        alongTrkDim;
    int        colorDim;
    int        dcDim;
    int        bcDim;

    // netcdf variables defined in file
    int        timeID;
    int        detectorID;
    int        slitID;
    int        dcID;
    int        bcID;
    int        dqID;
    int        pptLatID;
    int        pptLonID;
    int        radianceID;
    int        calErrorID;
    int        CountErrorID;

    short      tempError;    // holding value for scaled error

    // open the file specified by inFileName 
    status = nc_open(inFileName.c_str(), NC_NOWRITE, &ncid);
    
    // check if the file open was successful
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't open netcdf file, name\n", 
              inFileName.c_str());
        return false;
    }

    // get the netcdf dimension ids
    status = nc_inq_dimid(ncid, "NumScans", &scanDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find scan dim\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_dimid(ncid, "NumAcrossTrackPixels", &xtrkDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find xtrk dim\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_dimid(ncid, "NumAlongTrackPixels", &alongTrkDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find along track dim\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_dimid(ncid, "NumColors", &colorDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find color dim\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_dimid(ncid, "NumDarkCountPixels", &dcDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find dark count pixels dim\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_dimid(ncid, "NumBackgroundCountPixels", &bcDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find background count pixels dim\n");
        nc_close(ncid);
        return false;
    }

    // get all of the variable ids.  doing it here since it's not dependent 
    // upon which indices you're looking at in a dimensional field
    status = nc_inq_varid(ncid, "Time", &timeID);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find time\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "Detector", &detectorID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find Detector\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "Slit", &slitID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find slit\n");
        nc_close(ncid);
        return false;
    }
    size_t countdc[] = {1, NUM_DARK_COUNT_PIXELS};
    status = nc_inq_varid(ncid, "DarkCountPixels", &dcID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find variable dark counts\n");
        nc_close(ncid);
        return false;
    }
    size_t countbc[] = 
        {1, NUM_X_BACKGROUND_PIXELS * NUM_Y_BACKGROUND_PIXELS};
    status = nc_inq_varid(ncid, "BackgroundPixels", &bcID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find variable backgrund counts\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "PiercePointLatitude", &pptLatID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find pierce pt lat\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "PiercePointLongitude", &pptLonID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find pierce pt lon\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "RadianceData", &radianceID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find variable radiance data\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "CalibrationError", 
        &calErrorID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find variable cal error\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "CountError", &CountErrorID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find variable count error\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "DQI", &dqID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't find data quality\n");
        nc_close(ncid);
        return false;
    }

    // find out how many scans are in the file so that they can all be 
    // processed
    status = nc_inq_dimlen(ncid, scanDim, &numScans);
    if (status != NC_NOERR)
    {
        LogMessage(LOG_ERROR,
          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't get num of dimensions for perScan\n");
        nc_close(ncid);
        return false;
    }

    // now get data from the data product file for all scans
    for (scanNum = 0; scanNum  0)
                        imagingData.PixelData.CalibrationError[i][j][k] /= 
                            (Float)UNCERTAINTY_SCALE_FACTOR;

                    status = nc_get_var1_short(ncid, 
                            CountErrorID, dim4, &tempError);
                    if (status != NC_NOERR) 
                    {
                         LogMessage(LOG_ERROR, 
                          "NetCDFFileIO.ReadLevel1BImagingDisk: Can't read count error\n");
                        nc_close(ncid);
                        return false;
                    }

                    // a negative value for error indicates that the data
                    // is totally invalid so don't scale that
                    imagingData.PixelData.CountError[i][j][k] = (Float)tempError;
                    if (tempError > 0)
                        imagingData.PixelData.CountError[i][j][k] /= 
                            (Float)UNCERTAINTY_SCALE_FACTOR;

                    status = nc_get_var1_float(ncid, 
                      dqID, dim4, &imagingData.PixelData.DQI[i][j][k]);
                    if (status != NC_NOERR) 
                    {
                        LogMessage(LOG_ERROR, 
                         "NetCDFFileIO.ReadLevel1BImagingDisk: Can't read data quality\n");
                        nc_close(ncid);
                        return false;
                    }

                } // of for colors

            } // of for all all along track pixels

        }    // of for all cross track pixels

        orbitIData.push_back(imagingData);

     }    // for all scans

    // close the netcdf file
    nc_close(ncid);

    return true;

}


////////////////////////////////////////////////////////////
//
// Method: ReadLevel1BImagingLimb
//
// Description: This method reads the GUVI level 1B imaging
//              limb data from a netcdf file.  This method 
//              returns a single orbits worth of imaging 
//              data for every call
//
// Inputs: 
//    Name                 Description
//    ----              -----------
//    inFileName        file name of data to be read
//    orbitIData       a pointer to the data to be read
//
// Return: 
//    Name              Description
//    ----              -----------
//    n/a               true = successful read
//                      false = unsuccessful read
//
bool NetCDFFileIO::ReadLevel1BImagingLimb(
    string                inFileName, 
    OrbitOfImagingLimb1B& orbitIData )
{
    // a single scans worth of data
    ImagingLimb1BFormat imagingData;

    // indicates which scan number we're currently on
    int        scanNum;
    size_t     numScans;
    int        status;
    int        ncid;    // netcdf file id

    // the following are used for the netcdf variable
    // ids
    int        scanDim;
    int        xtrkDim;
    int        alongTrkDim;
    int        colorDim;
    int        dcDim;
    int        bcDim;

    // netcdf variables defined in file
    int        timeID;
    int        detectorID;
    int        slitID;
    int        dcID;
    int        bcID;
    int        dqID;
    int        radianceID;
    int        calErrorID;
    int        CountErrorID;
    int        zaID;
    int        raID;
    int        decID;
    int        tanptLatID;
    int        tanptLonID;
    int        tanptAltID;
    int        bkgdStarID;

    short      tempError;    // holding value for scaled error

    // open the file specified by inFileName 
    status = nc_open(inFileName.c_str(), NC_NOWRITE, &ncid);
    
    // check if the file open was successful
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't open netcdf file, name\n", 
              inFileName.c_str() );
        return false;
    }

    // get the netcdf dimension ids
    status = nc_inq_dimid(ncid, "NumScans", &scanDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find scan dim\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_dimid(ncid, "NumCrossTrackPixels", &xtrkDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find xtrk dim\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_dimid(ncid, "NumAlongTrackPixels", &alongTrkDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find along track dim\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_dimid(ncid, "NumColors", &colorDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find color dim\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_dimid(ncid, "NumDarkCountPixels", &dcDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find dark count pixels dim\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_dimid(ncid, "NumBackgroundCountPixels", &bcDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find background count pixels dim\n");
        nc_close(ncid);
        return false;
    }

    // get all of the variable ids.  doing it here since it's not dependent 
    // upon which indices you're looking at in a dimensional field
    status = nc_inq_varid(ncid, "Time", &timeID);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find time\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "Detector", &detectorID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find Detector\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "Slit", &slitID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find slit\n");
        nc_close(ncid);
        return false;
    }
    size_t countdc[] = {1, NUM_DARK_COUNT_PIXELS};
    status = nc_inq_varid(ncid, "DarkCountPixels", &dcID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find variable dark counts\n");
        nc_close(ncid);
        return false;
    }
    size_t countbc[] = 
        {1, NUM_X_BACKGROUND_PIXELS * NUM_Y_BACKGROUND_PIXELS};
    status = nc_inq_varid(ncid, "BackgroundPixels", &bcID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find variable backgrund counts\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "BackgroundStarAtPixel", &bkgdStarID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find variable background star\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "ZenithAngle", &zaID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find zenith angle\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "RA", &raID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find variable ra\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "DEC", &decID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find variable dec\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "TangentPointLatitude", &tanptLatID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find variable tan pt lat\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "TangentPointLongitude", &tanptLonID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find variable tan pt lon\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "TangentPointAltitude", &tanptAltID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find variable tan pt alt\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "RadianceData", &radianceID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find variable radiance data\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "CalibrationError", 
        &calErrorID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find variable cal error\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "CountError", &CountErrorID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find variable count error\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "DQI", &dqID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't find data quality\n");
        nc_close(ncid);
        return false;
    }

    // find out how many scans are in the file so that they can all be 
    // processed
    status = nc_inq_dimlen(ncid, scanDim, &numScans);
    if (status != NC_NOERR)
    {
        LogMessage(LOG_ERROR,
          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't get num of dimensions for perScan\n");
        nc_close(ncid);
        return false;
    }

    // now get data from the data product file for all scans
    for (scanNum = 0; scanNum  0)
                        imagingData.PixelData.CalibrationError[i][j][k] /= 
                            (Float)UNCERTAINTY_SCALE_FACTOR;

                    status = nc_get_var1_short(ncid, 
                        CountErrorID, dim4, &tempError);
                    if (status != NC_NOERR) 
                    {
                         LogMessage(LOG_ERROR, 
                          "NetCDFFileIO.ReadLevel1BImagingLimb: Can't read count error\n");
                        nc_close(ncid);
                        return false;
                    }

                    // a negative value for error indicates that the data
                    // is totally invalid so don't scale that
                    imagingData.PixelData.CountError[i][j][k] =
                        (Float)tempError;
                    if (tempError > 0)
                        imagingData.PixelData.CountError[i][j][k] /= 
                            (Float)UNCERTAINTY_SCALE_FACTOR;

                    status = nc_get_var1_float(ncid, 
                      dqID, dim4, &imagingData.PixelData.DQI[i][j][k]);
                    if (status != NC_NOERR) 
                    {
                       LogMessage(LOG_ERROR, 
                        "NetCDFFileIO.ReadLevel1BImagingLimb: Can't read data quality\n");
                       nc_close(ncid);
                       return false;
                    }

                } // of for colors

            } // of for all all along track pixels

        }    // of for all cross track pixels

        orbitIData.push_back(imagingData);

     }    // for all scans

    nc_close(ncid);

    return true;

}


////////////////////////////////////////////////////////////
//
// Method: ReadLevel1BStaticImaging
//
// Description: This method reads the GUVI level 1B static
//              imaging data from a netcdf file.  This method 
//              returns a single orbits worth of static 
//              imaging data for every call
//
// Inputs: 
//    Name                 Description
//    ----              -----------
//    inFileName        file name of data to be read
//    orbitSiData      a pointer to the data to be read
//
// Return: 
//    Name              Description
//    ----              -----------
//    n/a               true = successful read
//                      false = unsucessful read
//
bool NetCDFFileIO::ReadLevel1BStaticImaging(
    string                  inFileName, 
    OrbitOfStaticImaging1B& orbitSiData )
{
    // to store a single scans worth of data
    StaticImaging1BFormat    siData;

    // indicates which scan number we're currently on
    Integer        scanNum;
    size_t         numScans;
    Integer        status;
    Integer        ncid;    // netcdf file id

    // the following are used for the netcdf variable
    // ids
    Integer        scanDim;
    Integer        xtrkDim;
    Integer        alongTrkDim;
    Integer        colorDim;
    Integer        dcDim;
    Integer        bcDim;

    // netcdf variables defined in file
    Integer        timeID;
    Integer        fineTimeID;
    Integer        detectorID;
    Integer        slitID;
    Integer        dcID;
    Integer        bcID;
    Integer        dqID;
    Integer        radianceID;
    Integer        calErrorID;
    Integer        CountErrorID;
    Integer        raID;
    Integer        decID;
    Integer        tanptLatID;
    Integer        tanptLonID;
    Integer        tanptAltID;
    Integer        bkgdStarID;

    Short          tempError;    // holding value for scaled error

    // open the file specified by inFileName 
    status = nc_open(inFileName.c_str(), NC_NOWRITE, &ncid);
    
    // check if the file open was successful
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't open netcdf file, name\n", 
              inFileName.c_str() );
        return false;
    }

    // get the netcdf dimension ids
    status = nc_inq_dimid(ncid, "NumScans", &scanDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find scan dim\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_dimid(ncid, "NumAcrossTrackPixels", &xtrkDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find xtrk dim\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_dimid(ncid, "NumAlongTrackPixels", &alongTrkDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find along track dim\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_dimid(ncid, "NumColors", &colorDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find color dim\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_dimid(ncid, "NumDarkCountPixels", &dcDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find dark count pixels dim\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_dimid(ncid, "NumBackgroundCountPixels", &bcDim);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find background count pixels dim\n");
        nc_close(ncid);
        return false;
    }

    // get all of the variable ids.  doing it here since it's 
    // not dependent upon which indices you're looking at in a 
    // dimensional field

    status = nc_inq_varid(ncid, "Time", &timeID);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find time\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "FineTime", &fineTimeID);
    if (status != NC_NOERR)
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find fine time\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "Detector", &detectorID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find Detector\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "Slit", &slitID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find slit\n");
        nc_close(ncid);
        return false;
    }
    size_t countdc[] = {1, NUM_DARK_COUNT_PIXELS};
    status = nc_inq_varid(ncid, "DarkCountPixels", &dcID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find variable dark counts\n");
        nc_close(ncid);
        return false;
    }
    size_t countbc[] = 
        {1, NUM_X_BACKGROUND_PIXELS * NUM_Y_BACKGROUND_PIXELS};
    status = nc_inq_varid(ncid, "BackgroundPixels", &bcID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find variable backgrund counts\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "DataQualityIndicator", &dqID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find data quality\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "BackgroundStarAtPixel", &bkgdStarID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find variable background star\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "TangentPointLatitude", &tanptLatID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find variable tan pt lat\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "TangentPointLongitude", &tanptLonID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find variable tan pt lon\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "TangentPointAltitude", &tanptAltID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find variable tan pt alt\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "RA", &raID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find variable RA\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "DEC", &decID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find variable DEC\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "RadianceData", &radianceID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find variable radiance data\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "CalibrationError", &calErrorID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find variable cal error\n");
        nc_close(ncid);
        return false;
    }
    status = nc_inq_varid(ncid, "CountError", &CountErrorID);
    if (status != NC_NOERR) 
    { 
        LogMessage(LOG_ERROR, 
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't find variable count error\n");
        nc_close(ncid);
        return false;
    }

    // find out how many scans are in the file so that they can all be 
    // processed
    status = nc_inq_dimlen(ncid, scanDim, &numScans);
    if (status != NC_NOERR)
    {
        LogMessage(LOG_ERROR,
          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't get num of dimensions for perScan\n");
        nc_close(ncid);
        return false;
    }

    // now get data from the data product file for all scans
    for (scanNum = 0; scanNum  0)
                        siData.PixelData.CalibrationError[i][j][k] /= 
                            (Float)UNCERTAINTY_SCALE_FACTOR;

                    status = nc_get_var1_short(ncid, 
                        CountErrorID, dim4, &tempError);
                    if (status != NC_NOERR) 
                    {
                         LogMessage(LOG_ERROR, 
                          "NetCDFFileIO.ReadLevel1BStaticImaging: Can't read count error\n");
                        nc_close(ncid);
                        return false;
                    }

                    // a negative value for error indicates that the data
                    // is totally invalid so don't scale that
                    siData.PixelData.CountError[i][j][k] = (Float)tempError;
                    if (tempError > 0)
                         siData.PixelData.CountError[i][j][k] /=
                             (Float)UNCERTAINTY_SCALE_FACTOR;

                    status = nc_get_var1_float(ncid, 
                      dqID, dim4, &siData.PixelData.DQI[i][j][k]);
                    if (status != NC_NOERR) 
                    {
                       LogMessage(LOG_ERROR, 
                        "NetCDFFileIO.ReadLevel1BStaticImaging: Can't read data quality\n");
                       nc_close(ncid);
                       return false;
                    }

                } // of for all colors

            } // of for all along track pixels

        }    // of for all cross track pixels

        orbitSiData.push_back(siData);

     }    // for all scans

    nc_close(ncid);
    return true;

}


// End of file



Page Last Modified: February 24, 2014