Ejemplo n.º 1
0
void codes_index_delete(grib_index* index)
{
    grib_index_delete(index);
}
Ejemplo n.º 2
0
Archivo: Grib.cpp Proyecto: WFRT/Comps
float InputGrib::getValueCore(const Key::Input& iKey) const {
#ifdef WITH_GRIB
   // Check that date hasn't been checked before
   std::map<int,std::map<float, bool> >::const_iterator it0 = mMissingFiles.find(iKey.date);
   if(it0 != mMissingFiles.end()) {
      // Date has missing files
      std::map<float, bool>::const_iterator it1 = it0->second.find(iKey.offset);
      if(it1 != it0->second.end()) {
         writeMissingToCache(iKey);
         return Global::MV;
      }
   }

   std::string localVariable;
   bool found = getLocalVariableName(iKey.variable, localVariable);
   assert(found);

   int numLocations = Input::getNumLocations();

   std::string filename = getFilename(iKey);
   std::stringstream ss;
   ss << "InputGrib: Loading " << filename << " " << iKey.date << " " << iKey.offset << " " << iKey.location << " " << localVariable;
   Global::logger->write(ss.str(), Logger::message);
   bool foundVariable = false;
   FILE* fid = fopen(filename.c_str(),"r");
   float value = Global::MV;
   if(fid) {
      // GRIB File found
      int err = 0;
      grib_handle* h = NULL;

      // double s = Global::clock();

      // Try to use an index to read file as this is much faster. Fall back on just reading the 
      // GRIB file.
      grib_index* gribIndex = getIndex(iKey, localVariable);
      bool validIndex = (gribIndex != NULL);
      if(!validIndex) {
         std::stringstream ss;
         ss << "InputGrib: No index file available for " << filename;
         Global::logger->write(ss.str(), Logger::message);
      }

      int counter = 1;
      // Loop over available variables (in index or in file)
      while(1) {
         // Read message from file or index
         if(!validIndex) {
            h = grib_handle_new_from_file(0,fid,&err);
         }
         else {
            h = grib_handle_new_from_index(gribIndex,&err);
         }
         if(h == NULL)
            break; // No more messages to process

         std::string currVariable = getVariableName(h);
         std::stringstream ss;
         ss << "InputGrib: Reading message #" << counter << ": " << currVariable;
         Global::logger->write(ss.str(), Logger::message);

         // Check if the current variable is defined in the variable list
         int variableId;
         found = getVariableIdFromLocalVariable(currVariable, variableId);
         if(!found) {
            std::stringstream ss;
            ss << "InputGrib: Found variable " << currVariable << " in " << filename << " but this is not mapped to any variable in namelist" << std::endl;
            Global::logger->write(ss.str(), Logger::message);
         }
         else {
            // Only read the current variable if necessary
            if(mCacheOtherVariables || currVariable == localVariable) {
               std::vector<float> currValues;
               currValues.resize(numLocations, Global::MV);
               int numValid = 0;

               // Check that the message has the right number of locations
               size_t N;
               GRIB_CHECK(grib_get_size(h,"values",&N),0);
               if(N == numLocations) {
                  foundVariable = foundVariable || (currVariable == localVariable);
                  double* arr = new double[N];

                  GRIB_CHECK(grib_get_double_array(h,"values",arr,&N),0);
                  currValues.assign(arr, arr + numLocations);
                  for(int i = 0; i < (int) currValues.size(); i++) {
                     if(currValues[i] == mMV)
                        currValues[i] = Global::MV;
                     else
                        numValid++;
                  }
                  std::stringstream ss;
                  ss << "InputGrib: Number of valid values: " << numValid;
                  Global::logger->write(ss.str(), Logger::message);
                  delete arr;
               }
               else {
                  std::stringstream ss;
                  ss << "GribInput: Discarding variable " << currVariable << " in " << filename
                     << " because it has incorrect number of locations";
                  Global::logger->write(ss.str(), Logger::debug);
               }

               Key::Input key = iKey;
               key.offset = getOffset(h);
               // Cache values
               for(int i = 0; i < numLocations; i++) {
                  key.location = i;
                  key.variable = variableId;
                  if(key.location == iKey.location && currVariable == localVariable) {
                     // Found the value
                     value = currValues[i];
                  }
                  if(mCacheOtherLocations || key.location == iKey.location) {
                     //if(currVariable == localVariable)
                     //   std::cout << currValues[i] << std::endl;
                     Input::addToCache(key, currValues[i]);
                  }
               }
            }
         }
         if(h) {
            grib_handle_delete(h);
         }

         // Quit reading file if we have found the variable we need
         if(!mCacheOtherVariables && (currVariable == localVariable)) {
            break;
         }
         counter++;
      }
      if(!foundVariable) {
         // File was there, but couldn't find variable
         std::stringstream ss;
         ss << "InputGrib: Could not find variable " << localVariable << " in " << filename;
         Global::logger->write(ss.str(), Logger::warning);
         writeMissingToCache(iKey);
      }
      if(validIndex) {
         grib_index_delete(gribIndex);
      }

      // double e = Global::clock();
      //std::cout << "Grib read time: " << e - s << " seconds" << std::endl;

      fclose(fid);
      return value;
   }
   else {
      // GRIB file not found
      std::stringstream ss;
      ss << "GribInput: File not found: " << filename;
      Global::logger->write(ss.str(), Logger::message);

      std::vector<float> currValues;
      currValues.resize(numLocations, Global::MV);
      for(int i = 0; i < numLocations; i++) {
         Key::Input key = iKey;
         key.location = i;
         if(mCacheOtherLocations || key.location == iKey.location)
            Input::addToCache(key, currValues[i]);
      }
      return Global::MV;
   }
#else
   return Global::MV;
#endif
}