00001 /* 00002 * The information in this file is 00003 * Copyright(c) 2010 Ball Aerospace & Technologies Corporation 00004 * and is subject to the terms and conditions of the 00005 * GNU Lesser General Public License Version 2.1 00006 * The license text is available from 00007 * http://www.gnu.org/licenses/lgpl.html 00008 */ 00009 00010 #ifndef WAVELENGTHS_H 00011 #define WAVELENGTHS_H 00012 00013 #include "DynamicObject.h" 00014 #include "SpecialMetadata.h" 00015 #include "TypesFile.h" 00016 00017 #include <string> 00018 #include <vector> 00019 00020 /** 00021 * A convenience class for working with wavelength values. 00022 * 00023 * This class provides a common object for working with wavelength values. It 00024 * contains start, center, and end wavelength values and the units associated 00025 * with the values. The values and units can be set directly or initialized 00026 * from special metadata attributes in a DynamicObject or from another 00027 * Wavelengths object. The Wavelengths object can be used as an input or 00028 * output plug-in arg. Additional convenience static methods are provided to 00029 * convert wavelength values between units and to query the number of 00030 * wavelength values in a DynamicObject. 00031 * 00032 * Wavelength objects should be created with an object factory resource as 00033 * follows: 00034 * @code 00035 * FactoryResource<Wavelengths> pWavelengths; 00036 * @endcode 00037 * 00038 * When the Wavelengths object is created, the start, center, and end value 00039 * vectors are empty (i.e. isEmpty() returns \c true), and the units are 00040 * set to WavelengthUnitsType::MICRONS. 00041 * 00042 * @see \ref specialmetadata 00043 */ 00044 class Wavelengths 00045 { 00046 public: 00047 /** 00048 * Sets starting wavelength values. 00049 * 00050 * The number of starting values must equal the number of center and ending 00051 * values if they are present. 00052 * 00053 * @param startValues 00054 * The starting wavelength values. 00055 * @param valueUnits 00056 * The units of the starting wavelength values. If the units are 00057 * not the same units returned by getUnits(), the values are 00058 * automatically converted before they are set. 00059 * 00060 * @return Returns \c true if the start values are successfully set; 00061 * returns \c false if any of the following occur: 00062 * - \em startValues is not empty and \em valueUnits is invalid 00063 * - \em startValues is not empty and the size is different than 00064 * the number of center and ending values (if present) 00065 * - \em startValues contains all zero or negative values 00066 * 00067 * @see setCenterValues(), setEndValues(), convertValue() 00068 */ 00069 virtual bool setStartValues(const std::vector<double>& startValues, WavelengthUnitsType valueUnits) = 0; 00070 00071 /** 00072 * Returns the starting wavelength values. 00073 * 00074 * @return Returns the starting wavelength values. The values are in the 00075 * units as returned by getUnits(). 00076 * 00077 * @see getCenterValues(), getEndValues() 00078 */ 00079 virtual const std::vector<double>& getStartValues() const = 0; 00080 00081 /** 00082 * Queries whether starting wavelength values are present. 00083 * 00084 * This is a convenience method that is equivalent to calling 00085 * !getStartValues().empty(). 00086 * 00087 * @return Returns \c true if starting wavelengths are present; otherwise 00088 * returns \c false. 00089 * 00090 * @see getStartValues(), isEmpty() 00091 */ 00092 virtual bool hasStartValues() const = 0; 00093 00094 /** 00095 * Sets center wavelength values. 00096 * 00097 * The number of center values must equal the number of starting and ending 00098 * values if they are present. 00099 * 00100 * @param centerValues 00101 * The center wavelength values. 00102 * @param valueUnits 00103 * The units of the center wavelength values. If the units are 00104 * not the same units returned by getUnits(), the values are 00105 * automatically converted before they are set. 00106 * 00107 * @return Returns \c true if the center values are successfully set; 00108 * returns \c false if any of the following occur: 00109 * - \em centerValues is not empty and \em valueUnits is invalid 00110 * - \em centerValues is not empty and the size is different than 00111 * the number of starting and ending values (if present) 00112 * - \em centerValues contains all zero or negative values 00113 * 00114 * @see setStartValues(), setEndValues(), convertValue() 00115 */ 00116 virtual bool setCenterValues(const std::vector<double>& centerValues, WavelengthUnitsType valueUnits) = 0; 00117 00118 /** 00119 * Returns the center wavelength values. 00120 * 00121 * @return Returns the center wavelength values. The values are in the 00122 * units as returned by getUnits(). 00123 * 00124 * @see getStartValues(), getEndValues() 00125 */ 00126 virtual const std::vector<double>& getCenterValues() const = 0; 00127 00128 /** 00129 * Queries whether center wavelength values are present. 00130 * 00131 * This is a convenience method that is equivalent to calling 00132 * !getCenterValues().empty(). 00133 * 00134 * @return Returns \c true if center wavelengths are present; otherwise 00135 * returns \c false. 00136 * 00137 * @see getCenterValues(), isEmpty() 00138 */ 00139 virtual bool hasCenterValues() const = 0; 00140 00141 /** 00142 * Sets ending wavelength values. 00143 * 00144 * The number of ending values must equal the number of starting and center 00145 * values if they are present. 00146 * 00147 * @param endValues 00148 * The ending wavelength values. 00149 * @param valueUnits 00150 * The units of the ending wavelength values. If the units are 00151 * not the same units returned by getUnits(), the values are 00152 * automatically converted before they are set. 00153 * 00154 * @return Returns \c true if the ending values are successfully set; 00155 * returns \c false if any of the following occur: 00156 * - \em endValues is not empty and \em valueUnits is invalid 00157 * - \em endValues is not empty and the size is different than 00158 * the number of starting and center values (if present) 00159 * - \em endValues contains all zero or negative values 00160 * 00161 * @see setStartValues(), setCenterValues(), convertValue() 00162 */ 00163 virtual bool setEndValues(const std::vector<double>& endValues, WavelengthUnitsType valueUnits) = 0; 00164 00165 /** 00166 * Returns the ending wavelength values. 00167 * 00168 * @return Returns the ending wavelength values. The values are in the 00169 * units as returned by getUnits(). 00170 * 00171 * @see getStartValues(), getCenterValues() 00172 */ 00173 virtual const std::vector<double>& getEndValues() const = 0; 00174 00175 /** 00176 * Queries whether ending wavelength values are present. 00177 * 00178 * This is a convenience method that is equivalent to calling 00179 * !getEndValues().empty(). 00180 * 00181 * @return Returns \c true if ending wavelengths are present; otherwise 00182 * returns \c false. 00183 * 00184 * @see getEndValues(), isEmpty() 00185 */ 00186 virtual bool hasEndValues() const = 0; 00187 00188 /** 00189 * Assigns units to the wavelength values. 00190 * 00191 * @param units 00192 * The new wavelength units. 00193 * @param convertValues 00194 * If set to \c true, all wavelength values are converted to the 00195 * new units. If set to \c false, no wavelength values are 00196 * modified. 00197 * 00198 * @see getUnits(), convertValue() 00199 */ 00200 virtual void setUnits(WavelengthUnitsType units, bool convertValues = true) = 0; 00201 00202 /** 00203 * Returns the units of the start, center, and end wavelength values. 00204 * 00205 * @return The wavelength units. 00206 * 00207 * @see getEndValues(), isEmpty() 00208 */ 00209 virtual WavelengthUnitsType getUnits() const = 0; 00210 00211 /** 00212 * Clears the wavelength values and units. 00213 * 00214 * This method clears the start, center, and end wavelength values, and 00215 * sets the units to WavelengthUnitsType::MICRONS. After calling this 00216 * method, isEmpty() returns \c true. 00217 */ 00218 virtual void clear() = 0; 00219 00220 /** 00221 * Queries whether start, center, or end wavelength values are present. 00222 * 00223 * @return Returns \c true if start, center, and end wavelengths are 00224 * not present (i.e. all wavelength vectors are empty); otherwise 00225 * returns \c false. 00226 * 00227 * @see getNumWavelengths() 00228 */ 00229 virtual bool isEmpty() const = 0; 00230 00231 /** 00232 * Returns the number of wavelength values. 00233 * 00234 * @return Returns the number of wavelengths values if start, center, or 00235 * end wavelength values are present; otherwise returns 0. 00236 * 00237 * @see isEmpty(), getNumWavelengths(const DynamicObject*) 00238 */ 00239 virtual unsigned int getNumWavelengths() const = 0; 00240 00241 /** 00242 * Adjusts all wavelength values by a given scale factor. 00243 * 00244 * This method multiplies the start, center, and end wavelengths by the 00245 * given scale factor. The units are not changed. 00246 * 00247 * @param dScaleFactor 00248 * The scale factor by which to adjust the wavelength values. 00249 * This method does nothing if the scale factor is zero or 00250 * negative. 00251 * 00252 * @see calculateFwhm() 00253 */ 00254 virtual void scaleValues(double dScaleFactor) = 0; 00255 00256 /** 00257 * Populates the start and end wavelengths by calculating the full width 00258 * at half maximum (FWHM) of the center wavelength values. 00259 * 00260 * This method does nothing if center wavelength values are not set. 00261 * 00262 * @param dConstant 00263 * An optional constant that is multiplied by the wavelength width 00264 * when calculating the FWHM. 00265 * 00266 * @see getFwhm(), scaleValues() 00267 */ 00268 virtual void calculateFwhm(double dConstant = 1.0) = 0; 00269 00270 /** 00271 * Returns the calculated full width at half maximum (FWHM) values. 00272 * 00273 * If start or end wavelengths are not set, this method calculates the FWHM 00274 * and returns the values for each wavelength. 00275 * 00276 * @return The FWHM for each center wavelength value. 00277 * 00278 * @see calculateFwhm(), scaleValues() 00279 */ 00280 virtual std::vector<double> getFwhm() = 0; 00281 00282 /** 00283 * Sets the start, center, and end wavelength values and units to those 00284 * of another Wavelengths object. 00285 * 00286 * @param pWavelengths 00287 * The Wavelengths object from which to set the wavelength values 00288 * and units. 00289 * 00290 * @return Returns \c true if the start, center, and end values and units 00291 * are successfully set from the Wavelengths object; otherwise 00292 * returns \c false. 00293 * 00294 * @see initializeFromDynamicObject() 00295 */ 00296 virtual bool initializeFromWavelengths(const Wavelengths* pWavelengths) = 0; 00297 00298 /** 00299 * Sets the start, center, and end wavelength values and units based on 00300 * special metadata attributes. 00301 * 00302 * @param pData 00303 * The DynamicObject containing the special metadata attributes 00304 * from which to set the wavelength values and units. 00305 * @param convertToDisplayUnits 00306 * If set to \c true, all wavelength values are converted to the 00307 * units set in the #WAVELENGTH_DISPLAY_UNITS_METADATA_NAME 00308 * special metadata attribute. If set to \c false, the units will 00309 * be set to WavelengthUnitsType::MICRONS, which is how the values 00310 * are stored in the DynamicObject. 00311 * 00312 * @return Returns \c true if the start, center, and end values and units 00313 * are successfully set from the special metadata attributes; 00314 * otherwise returns \c false. 00315 * 00316 * @see initializeFromWavelengths(), \ref specialmetadata 00317 */ 00318 virtual bool initializeFromDynamicObject(const DynamicObject* pData, bool convertToDisplayUnits) = 0; 00319 00320 /** 00321 * Sets wavelength values and units special metadata attributes in a 00322 * DynamicObject. 00323 * 00324 * If starting, center, or ending values are not present, the existing 00325 * special metadata attribute is removed. If isEmpty() returns \c true, 00326 * the units attribute is removed. 00327 * 00328 * The wavelength values are set into the DynamicObject in microns, 00329 * regardless of the units returned by getUnits(). The units returned by 00330 * getUnits() are set into the #WAVELENGTH_DISPLAY_UNITS_METADATA_NAME 00331 * attribute as the display units. 00332 * 00333 * @param pData 00334 * The DynamicObject in which to set the wavelength values and 00335 * units special metadata attributes. 00336 * 00337 * @return Returns \c true if the start, center, and end values and units 00338 * special metadata attributes are successfully updated in the 00339 * DynamicObject; otherwise returns \c false. 00340 * 00341 * @see initializeFromDynamicObject(), \ref specialmetadata 00342 */ 00343 virtual bool applyToDynamicObject(DynamicObject* pData) = 0; 00344 00345 /** 00346 * Returns the number of wavelength values contained in the special metadata 00347 * attributes of a DynamicObject. 00348 * 00349 * This method is a convenience method to get the number of wavelength 00350 * values in a DynamicObject without creating a Wavelengths instance and 00351 * without making copies of its wavelength vectors. 00352 * 00353 * @param pData 00354 * The DynamicObject from which to get the number of wavelength 00355 * values contained in the special metadata attributes. This 00356 * should be the DynamicObject containing the "Special" attribute. 00357 * 00358 * @return Returns the number of wavelengths values in the DynamicObject 00359 * if start, center, or end wavelength values are present; 00360 * otherwise returns 0. 00361 * 00362 * @see getNumWavelengths(), isEmpty(), \ref specialmetadata 00363 */ 00364 static unsigned int getNumWavelengths(const DynamicObject* pData) 00365 { 00366 if (pData != NULL) 00367 { 00368 // Center values 00369 const std::vector<double>* pCenterValues = 00370 dv_cast<std::vector<double> >(&pData->getAttributeByPath(CENTER_WAVELENGTHS_METADATA_PATH)); 00371 if (pCenterValues != NULL) 00372 { 00373 return pCenterValues->size(); 00374 } 00375 00376 // Start values 00377 const std::vector<double>* pStartValues = 00378 dv_cast<std::vector<double> >(&pData->getAttributeByPath(START_WAVELENGTHS_METADATA_PATH)); 00379 if (pStartValues != NULL) 00380 { 00381 return pStartValues->size(); 00382 } 00383 00384 // End values 00385 const std::vector<double>* pEndValues = 00386 dv_cast<std::vector<double> >(&pData->getAttributeByPath(END_WAVELENGTHS_METADATA_PATH)); 00387 if (pEndValues != NULL) 00388 { 00389 return pEndValues->size(); 00390 } 00391 } 00392 00393 return 0; 00394 } 00395 00396 /** 00397 * Converts a wavelength value from one set of units to another. 00398 * 00399 * @param value 00400 * The wavelength value to convert. 00401 * @param valueUnits 00402 * The units of the given value. 00403 * @param newUnits 00404 * The units to which the value should be converted. 00405 * 00406 * @return Returns the converted wavelength value in the new units. 00407 * Returns \em value if either units parameter is invalid. 00408 */ 00409 static double convertValue(double value, WavelengthUnitsType valueUnits, WavelengthUnitsType newUnits) 00410 { 00411 if ((valueUnits.isValid() == true) && (newUnits.isValid() == true) && (valueUnits != newUnits)) 00412 { 00413 // Convert the value to microns 00414 switch (valueUnits) 00415 { 00416 case NANOMETERS: 00417 value *= 0.001; 00418 break; 00419 00420 case INVERSE_CENTIMETERS: 00421 if (value != 0.0) 00422 { 00423 value = (1.0 / value) * 10000.0; 00424 } 00425 break; 00426 00427 default: 00428 break; 00429 } 00430 00431 // Convert the value from microns to the new units 00432 switch (newUnits) 00433 { 00434 case NANOMETERS: 00435 value *= 1000.0; 00436 break; 00437 00438 case INVERSE_CENTIMETERS: 00439 if (value != 0.0) 00440 { 00441 value = 1.0 / (value * 0.0001); 00442 } 00443 break; 00444 00445 default: 00446 break; 00447 } 00448 } 00449 00450 return value; 00451 } 00452 00453 /** 00454 * The type string that should be returned from PlugIn::getType() for types 00455 * of plug-ins that import or export wavelengths. 00456 * 00457 * @return Returns a string populated with "Wavelength". 00458 */ 00459 static std::string WavelengthType() 00460 { 00461 return "Wavelength"; 00462 } 00463 00464 /** 00465 * A default name for a Wavelengths plug-in argument. 00466 * 00467 * @return Returns a string populated with "Wavelengths". 00468 * 00469 * @see WavelengthFileArg() 00470 */ 00471 static std::string WavelengthsArg() 00472 { 00473 return "Wavelengths"; 00474 } 00475 00476 /** 00477 * A default name for a wavelengths file plug-in argument. 00478 * 00479 * @return Returns a string populated with "Wavelength File". 00480 * 00481 * @see WavelengthsArg() 00482 */ 00483 static std::string WavelengthFileArg() 00484 { 00485 return "Wavelength File"; 00486 } 00487 00488 protected: 00489 /** 00490 * This should be destroyed by calling ObjectFactory::destroyObject(). 00491 */ 00492 virtual ~Wavelengths() 00493 {} 00494 }; 00495 00496 #endif