コード例 #1
0
bool SpectralLibraryManager::generateResampledLibrary(const RasterElement* pRaster)
{
    VERIFY(pRaster != NULL);

    // check that lib sigs are in same units as the raster element
    const RasterDataDescriptor* pDesc = dynamic_cast<const RasterDataDescriptor*>(pRaster->getDataDescriptor());
    VERIFY(pDesc != NULL);
    const Units* pUnits = pDesc->getUnits();
    if (pDesc->getUnits()->getUnitType() != mLibraryUnitType)
    {
        if (Service<DesktopServices>()->showMessageBox("Mismatched Units", "The data are not in the "
                "same units as the spectral library.\n Do you want to continue anyway?", "Yes", "No") == 1)
        {
            return false;
        }
    }

    FactoryResource<Wavelengths> pWavelengths;
    pWavelengths->initializeFromDynamicObject(pRaster->getMetadata(), false);

    // populate the library with the resampled signatures
    PlugInResource pPlugIn("Resampler");
    Resampler* pResampler = dynamic_cast<Resampler*>(pPlugIn.get());
    VERIFY(pResampler != NULL);
    if (pWavelengths->getNumWavelengths() != pDesc->getBandCount())
    {
        mpProgress->updateProgress("Wavelength information in metadata does not match the number of bands "
                                   "in the raster element", 0, ERRORS);
        return false;
    }

    // get resample suitable signatures - leave out signatures that don't cover the spectral range of the data
    std::vector<std::vector<double> > resampledData;
    resampledData.reserve(mSignatures.size());
    std::vector<Signature*> resampledSignatures;
    resampledSignatures.reserve(mSignatures.size());
    std::vector<std::string> unsuitableSignatures;
    std::vector<double> sigValues;
    std::vector<double> sigWaves;
    std::vector<double> rasterWaves = pWavelengths->getCenterValues();
    std::vector<double> rasterFwhm = pWavelengths->getFwhm();
    std::vector<double> resampledValues;
    std::vector<int> bandIndex;
    DataVariant data;
    for (std::vector<Signature*>::const_iterator it = mSignatures.begin(); it != mSignatures.end(); ++it)
    {
        data = (*it)->getData(SpectralLibraryMatch::getNameSignatureWavelengthData());
        VERIFY(data.isValid());
        VERIFY(data.getValue(sigWaves));
        resampledValues.clear();
        data = (*it)->getData(SpectralLibraryMatch::getNameSignatureAmplitudeData());
        VERIFY(data.isValid());
        VERIFY(data.getValue(sigValues));
        double scaleFactor = (*it)->getUnits(
                                 SpectralLibraryMatch::getNameSignatureAmplitudeData())->getScaleFromStandard();
        for (std::vector<double>::iterator sit = sigValues.begin(); sit != sigValues.end(); ++sit)
        {
            *sit *= scaleFactor;
        }

        std::string msg;
        if (pResampler->execute(sigValues, resampledValues, sigWaves, rasterWaves, rasterFwhm, bandIndex, msg) == false
                || resampledValues.size() != rasterWaves.size())
        {
            unsuitableSignatures.push_back((*it)->getName());
            continue;
        }

        resampledData.push_back(resampledValues);
        resampledSignatures.push_back(*it);
    }

    if (resampledSignatures.empty())
    {
        std::string errMsg = "None of the signatures in the library cover the spectral range of the data.";
        if (mpProgress != NULL)
        {
            mpProgress->updateProgress(errMsg, 0, ERRORS);
            return false;
        }
    }
    if (unsuitableSignatures.empty() == false)
    {
        std::string warningMsg = "The following library signatures do not cover the spectral range of the data:\n";
        for (std::vector<std::string>::iterator it = unsuitableSignatures.begin();
                it != unsuitableSignatures.end(); ++it)
        {
            warningMsg += *it + "\n";
        }
        warningMsg += "These signatures will not be searched for in the data.";
        Service<DesktopServices>()->showMessageBox("SpectralLibraryManager", warningMsg);

        StepResource pStep("Spectral LibraryManager", "spectral", "64B6C87A-A6C3-4378-9B6E-221D89D8707B");
        pStep->finalize(Message::Unresolved, warningMsg);
    }

    std::string libName = "Resampled Spectral Library";

    // Try to get the resampled lib element in case session was restored. If NULL, create a new raster element with
    // num rows = num valid signatures, num cols = 1, num bands = pRaster num bands
    RasterElement* pLib = dynamic_cast<RasterElement*>(Service<ModelServices>()->getElement(libName,
                          TypeConverter::toString<RasterElement>(), pRaster));
    if (pLib != NULL)
    {
        // check that pLib has same number of sigs as SpectralLibraryManager
        RasterDataDescriptor* pLibDesc = dynamic_cast<RasterDataDescriptor*>(pLib->getDataDescriptor());
        VERIFY(pLibDesc != NULL);
        if (pLibDesc->getRowCount() != mSignatures.size())
        {
            mpProgress->updateProgress("An error occurred during session restore and some signatures were not restored."
                                       " Check the spectral library before using.", 0, ERRORS);
            Service<ModelServices>()->destroyElement(pLib);
            pLib = NULL;
        }
    }
    bool isNewElement(false);
    if (pLib == NULL)
    {
        pLib = RasterUtilities::createRasterElement(libName,
                static_cast<unsigned int>(resampledData.size()), 1, pDesc->getBandCount(), FLT8BYTES, BIP,
                true, const_cast<RasterElement*>(pRaster));
        isNewElement = true;
    }
    if (pLib == NULL)
    {
        mpProgress->updateProgress("Error occurred while trying to create the resampled spectral library", 0, ERRORS);
        return false;
    }

    RasterDataDescriptor* pLibDesc = dynamic_cast<RasterDataDescriptor*>(pLib->getDataDescriptor());
    VERIFY(pLibDesc != NULL);

    // copy resampled data into new element
    if (isNewElement)
    {
        FactoryResource<DataRequest> pRequest;
        pRequest->setWritable(true);
        pRequest->setRows(pLibDesc->getActiveRow(0), pLibDesc->getActiveRow(pLibDesc->getRowCount()-1), 1);
        DataAccessor acc = pLib->getDataAccessor(pRequest.release());
        for (std::vector<std::vector<double> >::iterator sit = resampledData.begin(); sit != resampledData.end(); ++sit)
        {
            VERIFY(acc->isValid());
            void* pData = acc->getColumn();
            memcpy(acc->getColumn(), &(sit->begin()[0]), pLibDesc->getBandCount() * sizeof(double));
            acc->nextRow();
        }

        // set wavelength info in resampled library
        pWavelengths->applyToDynamicObject(pLib->getMetadata());
        FactoryResource<Units> libUnits;
        libUnits->setUnitType(mLibraryUnitType);
        libUnits->setUnitName(StringUtilities::toDisplayString<UnitType>(mLibraryUnitType));
        pLibDesc->setUnits(libUnits.get());
    }

    pLib->attach(SIGNAL_NAME(Subject, Deleted), Slot(this, &SpectralLibraryManager::resampledElementDeleted));
    mLibraries[pRaster] = pLib;
    mResampledSignatures[pLib] = resampledSignatures;

    const_cast<RasterElement*>(pRaster)->attach(SIGNAL_NAME(Subject, Deleted),
            Slot(this, &SpectralLibraryManager::elementDeleted));

    return true;
}
コード例 #2
0
ファイル: ImporterShell.cpp プロジェクト: Siddharthk/opticks
bool ImporterShell::validate(const DataDescriptor* pDescriptor, string& errorMessage) const
{
   mValidationError = ValidationTest();

   // Check for no validation
   int validationTest = getValidationTest(pDescriptor);
   if (validationTest == NO_VALIDATION)
   {
      return true;
   }

   // Always validate the data descriptor and file descriptor
   if (pDescriptor == NULL)
   {
      errorMessage = "The data set information is invalid.";
      return false;
   }

   const FileDescriptor* pFileDescriptor = pDescriptor->getFileDescriptor();
   if (pFileDescriptor == NULL)
   {
      errorMessage = "The data set does not contain valid file information.";
      return false;
   }

   // Existing file
   const string& filename = pFileDescriptor->getFilename();
   if (validationTest & EXISTING_FILE)
   {
      // Valid filename
      if (filename.empty() == true)
      {
         errorMessage = "The filename is invalid.";
         mValidationError = EXISTING_FILE;
         return false;
      }

      // Existing file
      LargeFileResource file(true);
      if (!file.open(filename.c_str(), O_RDONLY | O_BINARY, S_IREAD))
      {
         errorMessage = "The file: " + filename + " does not exist.";
         mValidationError = EXISTING_FILE;
         return false;
      }
   }

   // Existing data element
   if (validationTest & NO_EXISTING_DATA_ELEMENT)
   {
      const string& name = pDescriptor->getName();
      const string& type = pDescriptor->getType();
      DataElement* pParent = pDescriptor->getParent();

      Service<ModelServices> pModel;
      if (pModel->getElement(name, type, pParent) != NULL)
      {
         errorMessage = "The data set currently exists.  It may have already been imported.";
         mValidationError = NO_EXISTING_DATA_ELEMENT;
         return false;
      }
   }

   // Valid classification
   Service<UtilityServices> pUtilities;
   if (validationTest & VALID_CLASSIFICATION)
   {
      // Existing Classification object
      const Classification* pClassification = pDescriptor->getClassification();
      if (pClassification == NULL)
      {
         errorMessage = "The required classification does not exist.";
         mValidationError = VALID_CLASSIFICATION;
         return false;
      }

      // Unauthorized classification level on the system - warn the user, but continue to load the file
      FactoryResource<Classification> pSystemClassification;
      pSystemClassification->setLevel(pUtilities->getDefaultClassification());
      if (pClassification->hasGreaterLevel(pSystemClassification.get()) == true)
      {
         errorMessage = "THIS FILE CONTAINS CLASSIFIED INFORMATION WHICH SHOULD NOT BE PROCESSED ON THIS SYSTEM!\n"
            "THIS MAY CONSTITUTE A SECURITY VIOLATION WHICH SHOULD BE REPORTED TO YOUR SECURITY OFFICER!\n";
         StepResource pStep("Validate", "app", "1A881267-6A96-4eb2-A9D3-7D30334B0A0B", errorMessage);
      }
   }

   // Valid metadata
   if (validationTest & VALID_METADATA)
   {
      if (pDescriptor->getMetadata() == NULL)
      {
         errorMessage = "The required metadata does not exist.";
         mValidationError = VALID_METADATA;
         return false;
      }
   }

   // Processing location
   if (validationTest & VALID_PROCESSING_LOCATION)
   {
      if (isProcessingLocationSupported(pDescriptor->getProcessingLocation()) == false)
      {
         errorMessage = "The specified processing location is not supported.";
         mValidationError = VALID_PROCESSING_LOCATION;
         return false;
      }
   }

   // If no RasterDataDescriptor or RasterFileDescriptor tests are performed, end here
   if (validationTest < RASTER_SIZE)
   {
      return true;
   }

   // Since raster tests have been specified, always validate the raster data descriptor and raster file descriptor
   const RasterDataDescriptor* pRasterDescriptor = dynamic_cast<const RasterDataDescriptor*>(pDescriptor);
   if (pRasterDescriptor == NULL)
   {
      errorMessage = "The data set does not contain raster information.";
      return false;
   }

   const RasterFileDescriptor* pRasterFileDescriptor =
      dynamic_cast<const RasterFileDescriptor*>(pRasterDescriptor->getFileDescriptor());
   if (pRasterFileDescriptor == NULL)
   {
      errorMessage = "The file does not contain valid raster data.";
      return false;
   }

   // Raster size
   if (validationTest & RASTER_SIZE)
   {
      // Data set size
      unsigned int loadedRows = pRasterDescriptor->getRowCount();
      unsigned int loadedColumns = pRasterDescriptor->getColumnCount();
      unsigned int loadedBands = pRasterDescriptor->getBandCount();

      if ((loadedRows == 0) || (loadedColumns == 0) || (loadedBands == 0))
      {
         errorMessage = "The data set is empty.  Check the size of the rows, columns, and bands.";
         mValidationError = RASTER_SIZE;
         return false;
      }

      // Pixel size
      if (pRasterFileDescriptor->getBitsPerElement() == 0)
      {
         errorMessage = "The number of bits per element is invalid.";
         mValidationError = RASTER_SIZE;
         return false;
      }
   }

   // Data type
   if (validationTest & VALID_DATA_TYPE)
   {
      const std::vector<EncodingType>& dataTypes = pRasterDescriptor->getValidDataTypes();
      if (std::find(dataTypes.begin(), dataTypes.end(), pRasterDescriptor->getDataType()) == dataTypes.end())
      {
         errorMessage = "The data type is not valid for this data set.";
         mValidationError = VALID_DATA_TYPE;
         return false;
      }
   }

   // Header bytes
   if (validationTest & NO_HEADER_BYTES)
   {
      if (pRasterFileDescriptor->getHeaderBytes() > 0)
      {
         errorMessage = "The file has an invalid number of header bytes.";
         mValidationError = NO_HEADER_BYTES;
         return false;
      }
   }

   // Preline and postline bytes
   if (validationTest & NO_PRE_POST_LINE_BYTES)
   {
      if ((pRasterFileDescriptor->getPrelineBytes() > 0) || (pRasterFileDescriptor->getPostlineBytes() > 0))
      {
         errorMessage = "The file has an invalid number of preline and/or postline bytes.";
         mValidationError = NO_PRE_POST_LINE_BYTES;
         return false;
      }
   }

   // Preband and postband bytes
   if (validationTest & NO_PRE_POST_BAND_BYTES)
   {
      if ((pRasterFileDescriptor->getPrebandBytes() > 0) || (pRasterFileDescriptor->getPostbandBytes() > 0))
      {
         errorMessage = "The file has an invalid number of preband and/or postband bytes.";
         mValidationError = NO_PRE_POST_BAND_BYTES;
         return false;
      }
   }

   // Trailer bytes
   if (validationTest & NO_TRAILER_BYTES)
   {
      if (pRasterFileDescriptor->getTrailerBytes() > 0)
      {
         errorMessage = "The file has an invalid number of trailer bytes.";
         mValidationError = NO_TRAILER_BYTES;
         return false;
      }
   }

   // File size
   int64_t requiredSize = RasterUtilities::calculateFileSize(pRasterFileDescriptor);
   if ((validationTest & FILE_SIZE) == FILE_SIZE)
   {
      // Existing file
      LargeFileResource file;
      VERIFY(file.open(filename, O_RDONLY | O_BINARY, S_IREAD) == true);

      // File size
      if (requiredSize < 0)
      {
         errorMessage = "Unable to determine the required file size.";
         mValidationError = FILE_SIZE;
         return false;
      }

      if (file.fileLength() < requiredSize)
      {
         errorMessage = "The size of the file does not match the current parameters.";
         mValidationError = FILE_SIZE;
         return false;
      }
   }

   // Band files
   const vector<const Filename*>& bandFiles = pRasterFileDescriptor->getBandFiles();
   if (validationTest & NO_BAND_FILES)
   {
      if (bandFiles.empty() == false)
      {
         errorMessage = "This data set cannot have band data in multiple files.";
         mValidationError = NO_BAND_FILES;
         return false;
      }
   }

   // Existing band files and band file sizes
   if (validationTest & EXISTING_BAND_FILES)
   {
      // Enough band files for all bands
      unsigned int numBands = pRasterFileDescriptor->getBandCount();
      if (bandFiles.size() < numBands)
      {
         errorMessage = "The number of band files specified is less than the total number of bands to be loaded.";
         mValidationError = EXISTING_BAND_FILES;
         return false;
      }

      // Invalid file for imported bands
      for (vector<const Filename*>::size_type i = 0; i < bandFiles.size(); ++i)
      {
         const Filename* pFilename = bandFiles[i];
         if (pFilename == NULL)
         {
            stringstream streamMessage;
            streamMessage << "Band filename " << i + 1 << " is missing.";
            errorMessage = streamMessage.str();
            mValidationError = EXISTING_BAND_FILES;
            return false;
         }

         // Invalid filename
         string bandFilename = pFilename->getFullPathAndName();
         if (bandFilename.empty() == true)
         {
            stringstream streamMessage;
            streamMessage << "Band filename " << i + 1 << " is invalid.";
            errorMessage = streamMessage.str();
            mValidationError = EXISTING_BAND_FILES;
            return false;
         }

         // Existing file
         LargeFileResource bandFile;
         if (!bandFile.open(bandFilename, O_RDONLY | O_BINARY, S_IREAD))
         {
            stringstream streamMessage;
            streamMessage << "Band file " << i + 1 << " does not exist.";
            errorMessage = streamMessage.str();
            mValidationError = EXISTING_BAND_FILES;
            return false;
         }

         // File size
         if ((validationTest & BAND_FILE_SIZES) == BAND_FILE_SIZES)
         {
            if (requiredSize < 0)
            {
               errorMessage = "Unable to determine the required band file size.";
               mValidationError = BAND_FILE_SIZES;
               return false;
            }

            if (bandFile.fileLength() < requiredSize)
            {
               stringstream streamMessage;
               streamMessage << "The size of band file " << i + 1 << " does not match the required size "
                  "for the current parameters.";
               errorMessage = streamMessage.str();
               mValidationError = BAND_FILE_SIZES;
               return false;
            }
         }
      }
   }

   // Band names
   const DynamicObject* pMetadata = pRasterDescriptor->getMetadata();
   if ((validationTest & VALID_BAND_NAMES) == VALID_BAND_NAMES)
   {
      VERIFY(pMetadata != NULL);

      string namesPath[] = { SPECIAL_METADATA_NAME, BAND_METADATA_NAME, NAMES_METADATA_NAME, END_METADATA_NAME };

      // If band names are present in the metadata, check the number of names against the number of bands
      // If band names are not present in the metadata, then succeed
      const vector<string>* pBandNames = dv_cast<vector<string> >(&pMetadata->getAttributeByPath(namesPath));
      if (pBandNames != NULL)
      {
         if (pBandNames->size() != pRasterFileDescriptor->getBandCount())
         {
            errorMessage = "The number of band names in the metadata does not match the number of bands.";
            mValidationError = VALID_BAND_NAMES;
            return false;
         }
      }
   }

   // Wavelengths
   if ((validationTest & VALID_WAVELENGTHS) == VALID_WAVELENGTHS)
   {
      VERIFY(pMetadata != NULL);

      // If wavelengths are present in the metadata, check the number of wavelengths against the number of bands
      // If wavelengths are not present in the metadata, then succeed
      FactoryResource<Wavelengths> pWavelengths;
      if (pWavelengths->initializeFromDynamicObject(pMetadata, false) == true)
      {
         if (pWavelengths->getNumWavelengths() != pRasterFileDescriptor->getBandCount())
         {
            errorMessage = "The number of wavelengths in the metadata does not match the number of bands.";
            mValidationError = VALID_WAVELENGTHS;
            return false;
         }
      }
   }

   // Interleave conversions
   if (validationTest & NO_INTERLEAVE_CONVERSIONS)
   {
      InterleaveFormatType dataInterleave = pRasterDescriptor->getInterleaveFormat();
      InterleaveFormatType fileInterleave = pRasterFileDescriptor->getInterleaveFormat();
      if ((pRasterFileDescriptor->getBandCount() > 1) && (dataInterleave != fileInterleave))
      {
         errorMessage = "Interleave format conversions are not supported.";
         mValidationError = NO_INTERLEAVE_CONVERSIONS;
         return false;
      }
   }

   // Skip factors
   if (validationTest & NO_ROW_SKIP_FACTOR)
   {
      if (pRasterDescriptor->getRowSkipFactor() > 0)
      {
         errorMessage = "Row skip factors are not supported.";
         mValidationError = NO_ROW_SKIP_FACTOR;
         return false;
      }
   }

   if (validationTest & NO_COLUMN_SKIP_FACTOR)
   {
      if (pRasterDescriptor->getColumnSkipFactor() > 0)
      {
         errorMessage = "Column skip factors are not supported.";
         mValidationError = NO_COLUMN_SKIP_FACTOR;
         return false;
      }
   }

   // Subsets
   if (validationTest & NO_ROW_SUBSETS)
   {
      if (pRasterDescriptor->getRowCount() != pRasterFileDescriptor->getRowCount())
      {
         errorMessage = "Row subsets are not supported.";
         mValidationError = NO_ROW_SUBSETS;
         return false;
      }
   }

   if (validationTest & NO_COLUMN_SUBSETS)
   {
      if (pRasterDescriptor->getColumnCount() != pRasterFileDescriptor->getColumnCount())
      {
         errorMessage = "Column subsets are not supported.";
         mValidationError = NO_COLUMN_SUBSETS;
         return false;
      }
   }

   if (validationTest & NO_BAND_SUBSETS)
   {
      if (pRasterDescriptor->getBandCount() != pRasterFileDescriptor->getBandCount())
      {
         errorMessage = "Band subsets are not supported.";
         mValidationError = NO_BAND_SUBSETS;
         return false;
      }
   }

   // Available memory
   if (validationTest & AVAILABLE_MEMORY)
   {
      unsigned int loadedRows = pRasterDescriptor->getRowCount();
      unsigned int loadedColumns = pRasterDescriptor->getColumnCount();
      unsigned int loadedBands = pRasterDescriptor->getBandCount();
      unsigned int bytesPerElement = pRasterDescriptor->getBytesPerElement();

      uint64_t dataSize = loadedRows * loadedColumns * loadedBands * bytesPerElement;
      uint64_t maxMemoryAvail = pUtilities->getMaxMemoryBlockSize();
#if PTR_SIZE > 4
      uint64_t totalRam = pUtilities->getTotalPhysicalMemory();
      if (totalRam < maxMemoryAvail)
      {
         maxMemoryAvail = totalRam;
      }
#endif

      if (dataSize > maxMemoryAvail)
      {
         errorMessage = "The data set cannot be loaded into memory.  Use a different "
            "processing location or specify a subset.";
         mValidationError = AVAILABLE_MEMORY;
         return false;
      }
   }

   return true;
}