def test_get_bandname_from_wavelength(self): """Test the right bandname is found provided the wavelength in micro meters.""" bname = utils.get_bandname_from_wavelength('abi', 0.4, self.rsr.rsr) self.assertEqual(bname, 'ch1') with self.assertRaises(AttributeError): utils.get_bandname_from_wavelength('abi', 0.5, self.rsr.rsr) bname = utils.get_bandname_from_wavelength('abi', 0.6, self.rsr.rsr, epsilon=0.05) self.assertEqual(bname, 'ch3') bname = utils.get_bandname_from_wavelength('abi', 0.7, self.rsr.rsr) self.assertEqual(bname, 'ch3') bname = utils.get_bandname_from_wavelength('abi', 0.8, self.rsr.rsr) self.assertEqual(bname, 'ch4') bname = utils.get_bandname_from_wavelength('abi', 1.0, self.rsr.rsr) self.assertEqual(bname, None) # Multiple bands returned due to large epsilon bname = utils.get_bandname_from_wavelength('abi', 11.1, self.rsr.rsr, epsilon=1.0, multiple_bands=True) self.assertEqual(bname, ['ch13', 'ch14']) # uses generic channel mapping where '20' -> 'ch20' bandname = utils.get_bandname_from_wavelength('abi', 3.7, TEST_RSR) self.assertEqual(bandname, 'ch20') bandname = utils.get_bandname_from_wavelength('abi', 3.0, TEST_RSR) self.assertIsNone(bandname)
def test_get_bandname_from_wavelength(self): """Test that it is possible to get the right bandname provided the wavelength in micro meters """ x = utils.get_bandname_from_wavelength('abi', 0.4, self.rsr.rsr) self.assertEqual(x, 'ch1') with self.assertRaises(AttributeError): x = utils.get_bandname_from_wavelength('abi', 0.5, self.rsr.rsr) x = utils.get_bandname_from_wavelength('abi', 0.6, self.rsr.rsr, epsilon=0.05) self.assertEqual(x, 'ch3') x = utils.get_bandname_from_wavelength('abi', 0.7, self.rsr.rsr) self.assertEqual(x, 'ch3') x = utils.get_bandname_from_wavelength('abi', 0.8, self.rsr.rsr) self.assertEqual(x, 'ch4') x = utils.get_bandname_from_wavelength('abi', 1.0, self.rsr.rsr) self.assertEqual(x, None) bandname = utils.get_bandname_from_wavelength('abi', 3.7, TEST_RSR) self.assertEqual(bandname, '20') bandname = utils.get_bandname_from_wavelength('abi', 3.0, TEST_RSR) self.assertIsNone(bandname)
def test_get_bandname_from_wavelength(self): """Test the right bandname is found provided the wavelength in micro meters.""" x = utils.get_bandname_from_wavelength('abi', 0.4, self.rsr.rsr) self.assertEqual(x, 'ch1') with self.assertRaises(AttributeError): x = utils.get_bandname_from_wavelength('abi', 0.5, self.rsr.rsr) x = utils.get_bandname_from_wavelength('abi', 0.6, self.rsr.rsr, epsilon=0.05) self.assertEqual(x, 'ch3') x = utils.get_bandname_from_wavelength('abi', 0.7, self.rsr.rsr) self.assertEqual(x, 'ch3') x = utils.get_bandname_from_wavelength('abi', 0.8, self.rsr.rsr) self.assertEqual(x, 'ch4') x = utils.get_bandname_from_wavelength('abi', 1.0, self.rsr.rsr) self.assertEqual(x, None) # uses generic channel mapping where '20' -> 'ch20' bandname = utils.get_bandname_from_wavelength('abi', 3.7, TEST_RSR) self.assertEqual(bandname, 'ch20') bandname = utils.get_bandname_from_wavelength('abi', 3.0, TEST_RSR) self.assertIsNone(bandname)
def _get_rsr(self): """Get the relative spectral responses. Get the relative spectral responses from file, find the bandname, and convert to the requested wave-spave (wavelength or wave number) """ sensor = RelativeSpectralResponse(self.platform_name, self.instrument) if self.wavespace == WAVE_NUMBER: LOG.debug("Converting to wavenumber...") self.rsr, info = convert2wavenumber(sensor.rsr) else: self.rsr = sensor.rsr info = {'unit': sensor.unit, 'si_scale': sensor.si_scale} self._wave_unit = info['unit'] self._wave_si_scale = info['si_scale'] if isinstance(self.band, str): self.bandname = BANDNAMES.get(self.instrument, BANDNAMES['generic']).get( self.band, self.band) elif isinstance(self.band, Number): self.bandwavelength = self.band self.bandname = get_bandname_from_wavelength( self.instrument, self.band, self.rsr) self.wavelength_or_wavenumber = ( self.rsr[self.bandname][self.detector][self.wavespace] * self._wave_si_scale) self.response = self.rsr[self.bandname][self.detector]['response'] # Get the integral of the spectral response curve: self.rsr_integral = np.trapz(self.response, self.wavelength_or_wavenumber)
def _get_rsr(self): """ Get the relative spectral responses from file, find the bandname, and convert to the requested wave-spave (wavelength or wave number) """ sensor = RelativeSpectralResponse(self.platform_name, self.instrument) if self.wavespace == WAVE_NUMBER: LOG.debug("Converting to wavenumber...") self.rsr, info = convert2wavenumber(sensor.rsr) else: self.rsr = sensor.rsr info = {'unit': sensor.unit, 'si_scale': sensor.si_scale} self._wave_unit = info['unit'] self._wave_si_scale = info['si_scale'] if isinstance(self.band, str): self.bandname = BANDNAMES.get(self.instrument, BANDNAMES['generic']).get(self.band, self.band) elif isinstance(self.band, Number): self.bandwavelength = self.band self.bandname = get_bandname_from_wavelength(self.instrument, self.band, self.rsr) self.wavelength_or_wavenumber = (self.rsr[self.bandname][self.detector][self.wavespace] * self._wave_si_scale) self.response = self.rsr[self.bandname][self.detector]['response'] # Get the integral of the spectral response curve: self.rsr_integral = np.trapz(self.response, self.wavelength_or_wavenumber)
def get_effective_wavelength(self, bandname): """Get the effective wavelength with Rayleigh scattering in mind""" try: rsr = RelativeSpectralResponse(self.platform_name, self.sensor) except(IOError, OSError): LOG.exception( "No spectral responses for this platform and sensor: %s %s", self.platform_name, self.sensor) if isinstance(bandname, (float, integer_types)): LOG.warning( "Effective wavelength is set to the requested band wavelength = %f", bandname) return bandname return None if isinstance(bandname, str): bandname = BANDNAMES.get(self.sensor, BANDNAMES['generic']).get(bandname, bandname) elif isinstance(bandname, (float, integer_types)): if not(0.4 < bandname < 0.8): raise BandFrequencyOutOfRange( 'Requested band frequency should be between 0.4 and 0.8 microns!') bandname = get_bandname_from_wavelength(self.sensor, bandname, rsr.rsr) wvl, resp = rsr.rsr[bandname][ 'det-1']['wavelength'], rsr.rsr[bandname]['det-1']['response'] cwvl = get_central_wave(wvl, resp, weight=1. / wvl**4) LOG.debug("Band name: %s Effective wavelength: %f", bandname, cwvl) return cwvl
def get_effective_wavelength(self, bandname): """Get the effective wavelength with Rayleigh scattering in mind""" try: rsr = RelativeSpectralResponse(self.platform_name, self.sensor) except(IOError, OSError): LOG.exception( "No spectral responses for this platform and sensor: %s %s", self.platform_name, self.sensor) if isinstance(bandname, (float, integer_types)): LOG.warning( "Effective wavelength is set to the requested band wavelength = %f", bandname) return bandname msg = ("Can't get effective wavelength for band %s on platform %s and sensor %s" % (str(bandname), self.platform_name, self.sensor)) raise KeyError(msg) if isinstance(bandname, str): bandname = BANDNAMES.get(self.sensor, BANDNAMES['generic']).get(bandname, bandname) elif isinstance(bandname, (float, integer_types)): if not(0.4 < bandname < 0.8): raise BandFrequencyOutOfRange( 'Requested band frequency should be between 0.4 and 0.8 microns!') bandname = get_bandname_from_wavelength(self.sensor, bandname, rsr.rsr) wvl, resp = rsr.rsr[bandname][ 'det-1']['wavelength'], rsr.rsr[bandname]['det-1']['response'] cwvl = get_central_wave(wvl, resp, weight=1. / wvl**4) LOG.debug("Band name: %s Effective wavelength: %f", bandname, cwvl) return cwvl
rsr = RelativeSpectralResponse(platform, sensor) except IOError: # LOG.exception('Failed getting the rsr data for platform %s ' + # 'and sensor %s', platform, sensor) rsr = None else: break if not rsr: continue something2plot = True LOG.debug("Platform name %s and Sensor: %s", str(rsr.platform_name), str(rsr.instrument)) if req_wvl: bands = get_bandname_from_wavelength(sensor, req_wvl, rsr.rsr, 0.25, multiple_bands=True) if not bands: continue if isinstance(bands, list): for b__ in bands: plt = plot_band(plt, b__, rsr, platform_name_in_legend=(not no_platform_name_in_legend)) else: plt = plot_band(plt, bands, rsr, platform_name_in_legend=(not no_platform_name_in_legend)) elif band: plt = plot_band(plt, band, rsr, platform_name_in_legend=(not no_platform_name_in_legend)) else:
def __init__(self, platform_name, instrument, band, **kwargs): super(Calculator, self).__init__(platform_name, instrument, band, **kwargs) from numbers import Number self.bandname = None self.bandwavelength = None if isinstance(band, str): self.bandname = BANDNAMES.get(self.instrument, BANDNAMES['generic']).get( band, band) self.bandwavelength = self.rsr[ self.bandname]['det-1']['central_wavelength'] elif isinstance(band, Number): self.bandwavelength = band self.bandname = get_bandname_from_wavelength( self.instrument, band, self.rsr) if self.bandwavelength > 3.95 or self.bandwavelength < 3.5: raise NotImplementedError( 'NIR reflectance is not supported outside ' + 'the 3.5-3.95 micron interval') options = get_config() self.solar_flux = kwargs.get('solar_flux', None) if self.solar_flux is None: self._get_solarflux() self._rad3x = None self._rad3x_t11 = None self._solar_radiance = None self._rad3x_correction = 1.0 self._r3x = None self._e3x = None self.lutfile = None if 'detector' in kwargs: self.detector = kwargs['detector'] else: self.detector = 'det-1' platform_sensor = platform_name + '-' + instrument if platform_sensor in options and 'tb2rad_lut_filename' in options[ platform_sensor]: if isinstance(options[platform_sensor]['tb2rad_lut_filename'], dict): for item in options[platform_sensor]['tb2rad_lut_filename']: if item == self.bandname or item == self.bandname.lower(): self.lutfile = options[platform_sensor][ 'tb2rad_lut_filename'][item] break if self.lutfile is None: LOG.warning( "Failed determine LUT filename from config: %s", str(options[platform_sensor]['tb2rad_lut_filename'])) else: self.lutfile = options[platform_sensor]['tb2rad_lut_filename'] if self.lutfile and not self.lutfile.endswith('.npz'): self.lutfile = self.lutfile + '.npz' if self.lutfile and not os.path.exists( os.path.dirname(self.lutfile)): LOG.warning("Directory %s does not exist! Check config file", os.path.dirname(self.lutfile)) self.lutfile = os.path.join(TMPDIR, os.path.basename(self.lutfile)) if self.lutfile is None: LOG.info("No lut filename available in config file. " "Will generate filename automatically") lutname = 'tb2rad_lut_{0}_{1}_{band}'.format( self.platform_name.lower(), self.instrument.lower(), band=self.bandname.lower()) self.lutfile = os.path.join(TMPDIR, lutname + '.npz') LOG.info("lut filename: " + str(self.lutfile)) if not os.path.exists(self.lutfile): self.make_tb2rad_lut(self.lutfile) self.lut = self.read_tb2rad_lut(self.lutfile) LOG.info("LUT file created") else: self.lut = self.read_tb2rad_lut(self.lutfile) LOG.info("File was there and has been read!")
title = args.title if not title: title = 'Relative Spectral Responses' platform = args.platform_name sensor = args.sensor minimum_response = args.minimum_response xlimits = args.xlimits rsr = RelativeSpectralResponse(platform, sensor) if args.bandname: band = args.bandname else: wavelength = args.wavelength band = get_bandname_from_wavelength(sensor, wavelength, rsr.rsr) detectors = rsr.rsr[band].keys() for det in detectors: resp = rsr.rsr[band][det]['response'] wvl = rsr.rsr[band][det]['wavelength'] resp = np.ma.masked_less_equal(resp, minimum_response) wvl = np.ma.masked_array(wvl, resp.mask) resp.compressed() wvl.compressed() plt.plot(wvl, resp) if xlimits: plt.xlim(xlimits[0], xlimits[1]) plt.title(title)
help="The wavelength in micrometers", type=float) parser.add_argument("-t", "--minimum_response", help=("Minimum response: Any response lower than " + "this will be ignored when plotting"), default=0.01, type=float) args = parser.parse_args() wavelength = args.wavelength minimum_response = args.minimum_response for platform, sensor in zip(platforms, sensors): rsr = RelativeSpectralResponse(platform, sensor) band = get_bandname_from_wavelength(sensor, wavelength, rsr.rsr) if not band: continue detectors = rsr.rsr[band].keys() # for det in detectors: det = detectors[0] resp = rsr.rsr[band][det]['response'] wvl = rsr.rsr[band][det]['wavelength'] resp = np.ma.masked_less_equal(resp, minimum_response) wvl = np.ma.masked_array(wvl, resp.mask) resp.compressed() wvl.compressed() plt.plot(wvl, resp, label=sensor)
def __init__(self, platform_name, instrument, band, detector='det-1', wavespace=WAVE_LENGTH, solar_flux=None, sunz_threshold=TERMINATOR_LIMIT, masking_limit=TERMINATOR_LIMIT): """Initialize the Class instance.""" super(Calculator, self).__init__(platform_name, instrument, band, detector=detector, wavespace=wavespace) from numbers import Number self.bandname = None self.bandwavelength = None if isinstance(band, str): self.bandname = BANDNAMES.get(self.instrument, BANDNAMES['generic']).get( band, band) self.bandwavelength = self.rsr[ self.bandname]['det-1']['central_wavelength'] elif isinstance(band, Number): self.bandwavelength = band self.bandname = get_bandname_from_wavelength( self.instrument, band, self.rsr) if self.bandwavelength > 3.95 or self.bandwavelength < 3.5: raise NotImplementedError( 'NIR reflectance is not supported outside ' + 'the 3.5-3.95 micron interval') options = get_config() self.solar_flux = solar_flux if self.solar_flux is None: self._get_solarflux() # The sun-zenith angle limit in degrees defining how far towards the # terminator we try derive a self.detector = detector self.sunz_threshold = sunz_threshold self.masking_limit = masking_limit self._rad3x = None self._rad3x_t11 = None self._solar_radiance = None self._rad3x_correction = 1.0 self._r3x = None self._e3x = None self.lutfile = None platform_sensor = platform_name + '-' + instrument if platform_sensor in options and 'tb2rad_lut_filename' in options[ platform_sensor]: if isinstance(options[platform_sensor]['tb2rad_lut_filename'], dict): for item in options[platform_sensor]['tb2rad_lut_filename']: if item == self.bandname or item == self.bandname.lower(): self.lutfile = options[platform_sensor][ 'tb2rad_lut_filename'][item] break if self.lutfile is None: LOG.warning( "Failed determine LUT filename from config: %s", str(options[platform_sensor]['tb2rad_lut_filename'])) else: self.lutfile = options[platform_sensor]['tb2rad_lut_filename'] if self.lutfile and not self.lutfile.endswith('.npz'): self.lutfile = self.lutfile + '.npz' if self.lutfile and not os.path.exists( os.path.dirname(self.lutfile)): LOG.warning("Directory %s does not exist! Check config file", os.path.dirname(self.lutfile)) self.lutfile = os.path.join(TB2RAD_DIR, os.path.basename(self.lutfile)) if self.lutfile is None: LOG.info("No lut filename available in config file. " "Will generate filename automatically") lutname = 'tb2rad_lut_{0}_{1}_{band}'.format( self.platform_name.lower(), self.instrument.lower(), band=self.bandname.lower()) self.lutfile = os.path.join(TB2RAD_DIR, lutname + '.npz') LOG.info("lut filename: " + str(self.lutfile)) if not os.path.exists(self.lutfile): self.make_tb2rad_lut(self.lutfile) self.lut = self.read_tb2rad_lut(self.lutfile) LOG.info("LUT file created") else: self.lut = self.read_tb2rad_lut(self.lutfile) LOG.info("File was there and has been read!")