Example #1
0
    def _get_options_from_config(self):
        """Get configuration settings from configuration file"""

        options = get_config()
        self.output_dir = options.get('rsr_dir', './')
        self.path = options[self.platform_name + '-' + self.instrument]['path']
        self.options = options
Example #2
0
    def __init__(self, bandname, platform_name, sort=True):
        """Init Modis RSR
        """
        self.platform_name = platform_name
        self.bandname = bandname
        self.filenames = {}
        self.requested_band_filename = None
        self.is_sw = False
        if bandname in SHORTWAVE_BANDS:
            self.is_sw = True
        self.scales = {}
        for bname in MODIS_BAND_NAMES:
            self.filenames[bname] = None

        self.rsr = None
        self._sort = sort

        options = get_config()
        self.path = options[platform_name + '-modis'].get('path')
        self.output_dir = options.get('rsr_dir', './')

        self._get_bandfilenames()
        LOG.debug("Filenames: %s", str(self.filenames))
        if os.path.exists(self.filenames[bandname]):
            self.requested_band_filename = self.filenames[bandname]
            self._load()
        else:
            raise IOError("Couldn't find an existing file for this band: " +
                          str(self.bandname))
Example #3
0
    def _get_options_from_config(self):
        """Get configuration settings from configuration file"""

        options = get_config()
        self.output_dir = options.get('rsr_dir', './')
        self.path = options[self.platform_name + '-' + self.instrument]['path']
        self.options = options
Example #4
0
    def __init__(self, aerosol_type, atm_type='us-standard'):
        """Initialize class and determine LUT to use."""
        options = get_config()
        self.do_download = False
        self._lutfiles_version_uptodate = False

        if 'download_from_internet' in options and options[
                'download_from_internet']:
            self.do_download = True

        if atm_type not in ATMOSPHERES:
            raise AttributeError(
                'Atmosphere type not supported! ' +
                'Need to be one of {}'.format(str(ATMOSPHERES)))

        self._aerosol_type = aerosol_type
        if aerosol_type not in AEROSOL_TYPES:
            raise AttributeError(
                'Aerosol type not supported! ' +
                'Need to be one of {0}'.format(str(AEROSOL_TYPES)))

        if atm_type not in ATMOSPHERES.keys():
            LOG.error("Atmosphere type %s not supported", atm_type)

        LOG.info("Atmosphere chosen: %s", atm_type)

        if (self._aerosol_type in ATM_CORRECTION_LUT_VERSION
                and self._get_lutfiles_version()
                == ATM_CORRECTION_LUT_VERSION[self._aerosol_type]['version']):
            self.lutfiles_version_uptodate = True
Example #5
0
    def __init__(self, aerosol_type, atm_type='us-standard'):
        """Initialize class and determine LUT to use."""
        options = get_config()
        self.do_download = False
        self._lutfiles_version_uptodate = False

        if 'download_from_internet' in options and options['download_from_internet']:
            self.do_download = True

        if atm_type not in ATMOSPHERES:
            raise AttributeError('Atmosphere type not supported! ' +
                                 'Need to be one of {}'.format(str(ATMOSPHERES)))

        self._aerosol_type = aerosol_type
        if aerosol_type not in AEROSOL_TYPES:
            raise AttributeError('Aerosol type not supported! ' +
                                 'Need to be one of {0}'.format(str(AEROSOL_TYPES)))

        if atm_type not in ATMOSPHERES.keys():
            LOG.error("Atmosphere type %s not supported", atm_type)

        LOG.info("Atmosphere chosen: %s", atm_type)

        if (self._aerosol_type in ATM_CORRECTION_LUT_VERSION and
                self._get_lutfiles_version() == ATM_CORRECTION_LUT_VERSION[self._aerosol_type]['version']):
            self.lutfiles_version_uptodate = True
Example #6
0
    def __init__(self, platform_name=None, instrument=None, **kwargs):
        """Create the instance either from platform name and instrument or from
        filename and load the data"""
        self.platform_name = platform_name
        self.instrument = instrument
        self.filename = None
        if not self.instrument or not self.platform_name:
            if 'filename' in kwargs:
                self.filename = kwargs['filename']
            else:
                raise AttributeError(
                    "platform name and sensor or filename must be specified")
        else:
            self._check_instrument()

        self.rsr = {}
        self.description = "Unknown"
        self.band_names = None
        self.unit = '1e-6 m'
        self.si_scale = 1e-6  # How to scale the wavelengths to become SI unit
        self._wavespace = WAVE_LENGTH

        options = get_config()
        self.rsr_dir = options['rsr_dir']
        self.do_download = False
        self._rsr_data_version_uptodate = False

        if 'download_from_internet' in options and options[
                'download_from_internet']:
            self.do_download = True

        if self._get_rsr_data_version() == RSR_DATA_VERSION:
            self._rsr_data_version_uptodate = True

        if not self.filename:
            self._get_filename()

        if not os.path.exists(self.filename) or not os.path.isfile(
                self.filename):
            errmsg = ('pyspectral RSR file does not exist! Filename = ' +
                      str(self.filename))
            if self.instrument and self.platform_name:
                fmatch = glob(
                    os.path.join(
                        self.rsr_dir,
                        '*{0}*{1}*.h5'.format(self.instrument,
                                              self.platform_name)))
                errmsg = (
                    errmsg +
                    '\nFiles matching instrument and satellite platform' +
                    ': ' + str(fmatch))

            raise IOError(errmsg)

        LOG.debug('Filename: %s', str(self.filename))
        self.load()
Example #7
0
    def __init__(self, bandname, platform_name):
        self.platform_name = platform_name
        self.bandname = bandname
        self.filename = None
        self.rsr = None

        options = get_config()
        self.output_dir = options.get('rsr_dir', './')

        self.bandfilenames = {}

        self._get_bandfilenames(**options)
        self._get_bandfile(**options)
        LOG.debug("Filename: %s", str(self.filename))
        self._load()
Example #8
0
    def __init__(self, bandname, platform_name):
        self.platform_name = platform_name
        self.bandname = bandname
        self.filename = None
        self.rsr = None

        options = get_config()
        self.output_dir = options.get('rsr_dir', './')

        self.bandfilenames = {}

        self._get_bandfilenames(**options)
        self._get_bandfile(**options)
        LOG.debug("Filename: %s", str(self.filename))
        self._load()
Example #9
0
    def __init__(self, bandname, platform_name='Envisat'):
        """Read the aatsr relative spectral responses for all channels"""

        self.platform_name = platform_name
        self.instrument = 'aatsr'
        self.bandname = bandname
        self.rsr = None

        options = get_config()

        self.aatsr_path = options[
            self.platform_name + '-' + self.instrument].get('path')

        self.output_dir = options.get('rsr_dir', './')

        self._load()
Example #10
0
    def __init__(self, platform_name, wavespace='wavelength'):
        """
        """
        self.platform_name = platform_name
        self.filename = None
        self.rsr = None

        options = get_config()

        self.output_dir = options.get('rsr_dir', './')
        ahi_options = options[platform_name + '-ahi']
        self.filename = ahi_options['path']
        LOG.debug("Filenames: " + str(self.filename))
        if os.path.exists(self.filename):
            self._load()
        else:
            raise IOError("Couldn't find an existing file for this band: " +
                          str(self.bandname))
Example #11
0
    def __init__(self, platform_name, wavespace='wavelength'):
        """
        """
        self.platform_name = platform_name
        self.filename = None
        self.rsr = None

        options = get_config()

        self.output_dir = options.get('rsr_dir', './')
        ahi_options = options[platform_name + '-ahi']
        self.filename = ahi_options['path']
        LOG.debug("Filenames: " + str(self.filename))
        if os.path.exists(self.filename):
            self._load()
        else:
            raise IOError("Couldn't find an existing file for this band: " +
                          str(self.bandname))
Example #12
0
    def __init__(self):
        """Create the instance either from platform name and instrument or from
        filename and load the data"""

        self.rsr = {}
        self.description = "Unknown"
        self.band_names = None
        self.unit = '1e-6 m'
        self.si_scale = 1e-6  # How to scale the wavelengths to become SI unit
        self._wavespace = WAVE_LENGTH

        options = get_config()
        self.rsr_dir = options['rsr_dir']
        self.do_download = False
        self._rsr_data_version_uptodate = False

        if 'download_from_internet' in options and options['download_from_internet']:
            self.do_download = True

        if self._get_rsr_data_version() == RSR_DATA_VERSION:
            self._rsr_data_version_uptodate = True
Example #13
0
    def __init__(self, wavespace='wavelength'):
        """
        Read the seviri relative spectral responses for all channels and all
        MSG satellites.

        Optional input: 'wavespace'. Equals 'wavelength' (units of micron's) on
        default. Can be 'wavenumber' in which case the unit is in cm-1.

        """
        options = get_config()

        self.seviri_path = options['seviri'].get('path')
        if not os.path.exists(self.seviri_path):
            self.seviri_path = os.path.join(DATA_PATH,
                                            options['seviri'].get('filename'))

        LOG.debug("Original RSR file from EUMETSAT: {}".format(
            self.seviri_path))

        self.output_dir = options.get('rsr_dir', './')

        self.rsr = None
        self._load()
        self.wavespace = wavespace
        if wavespace not in ['wavelength', 'wavenumber']:
            raise AttributeError("wavespace has to be either " +
                                 "'wavelength' or 'wavenumber'!")

        self.unit = 'micrometer'
        if wavespace == 'wavenumber':
            # Convert to wavenumber:
            self.convert2wavenumber()

        self.central_wavenumber = None
        self.central_wavelength = None

        self.get_centrals()
Example #14
0
    def __init__(self, wavespace='wavelength'):
        """
        Read the seviri relative spectral responses for all channels and all
        MSG satellites.

        Optional input: 'wavespace'. Equals 'wavelength' (units of micron's) on
        default. Can be 'wavenumber' in which case the unit is in cm-1.

        """
        options = get_config()

        self.seviri_path = options['seviri'].get('path')
        if not os.path.exists(self.seviri_path):
            self.seviri_path = os.path.join(
                DATA_PATH, options['seviri'].get('filename'))

        LOG.debug("Original RSR file from EUMETSAT: {}".format(self.seviri_path))

        self.output_dir = options.get('rsr_dir', './')

        self.rsr = None
        self._load()
        self.wavespace = wavespace
        if wavespace not in ['wavelength', 'wavenumber']:
            raise AttributeError("wavespace has to be either " +
                                 "'wavelength' or 'wavenumber'!")

        self.unit = 'micrometer'
        if wavespace == 'wavenumber':
            # Convert to wavenumber:
            self.convert2wavenumber()

        self.central_wavenumber = None
        self.central_wavelength = None

        self.get_centrals()
Example #15
0
    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!")
Example #16
0
    'desert_aerosol'] = "https://zenodo.org/record/1000414/files/pyspectral_rayleigh_correction_luts.tgz"
HTTPS_RAYLEIGH_LUTS[
    'marine_clean_aerosol'] = "https://zenodo.org/record/896869/files/pyspectral_rayleigh_correction_luts.tgz"
HTTPS_RAYLEIGH_LUTS[
    'marine_polluted_aerosol'] = "https://zenodo.org/record/896875/files/pyspectral_rayleigh_correction_luts.tgz"
HTTPS_RAYLEIGH_LUTS[
    'marine_tropical_aerosol'] = "https://zenodo.org/record/896879/files/pyspectral_rayleigh_correction_luts.tgz"
HTTPS_RAYLEIGH_LUTS[
    'rural_aerosol'] = "https://zenodo.org/record/896881/files/pyspectral_rayleigh_correction_luts.tgz"
HTTPS_RAYLEIGH_LUTS[
    'urban_aerosol'] = "https://zenodo.org/record/896887/files/pyspectral_rayleigh_correction_luts.tgz"
HTTPS_RAYLEIGH_LUTS[
    'rayleigh_only'] = "https://zenodo.org/record/888971/files/pyspectral_rayleigh_correction_luts.tgz"


CONF = get_config()
LOCAL_RSR_DIR = CONF.get('rsr_dir')
LOCAL_RAYLEIGH_DIR = CONF.get('rayleigh_dir')

try:
    os.makedirs(LOCAL_RSR_DIR)
except OSError:
    if not os.path.isdir(LOCAL_RSR_DIR):
        raise

RAYLEIGH_LUT_DIRS = {}
for sub_dir_name in HTTPS_RAYLEIGH_LUTS:
    dirname = os.path.join(LOCAL_RAYLEIGH_DIR, sub_dir_name)
    RAYLEIGH_LUT_DIRS[sub_dir_name] = dirname

Example #17
0
    def __init__(self, platform_name, sensor, **kwargs):
        """Initialize class and determine LUT to use."""
        self.platform_name = platform_name
        self.sensor = sensor
        self.coeff_filename = None
        options = get_config()
        self.do_download = False
        self._lutfiles_version_uptodate = False

        atm_type = kwargs.get('atmosphere', 'us-standard')
        if atm_type not in ATMOSPHERES:
            raise AttributeError('Atmosphere type not supported! ' +
                                 'Need to be one of {}'.format(str(ATMOSPHERES)))

        aerosol_type = kwargs.get('aerosol_type', 'marine_clean_aerosol')
        self._aerosol_type = aerosol_type

        if aerosol_type not in AEROSOL_TYPES:
            raise AttributeError('Aerosol type not supported! ' +
                                 'Need to be one of {0}'.format(str(AEROSOL_TYPES)))

        rayleigh_dir = RAYLEIGH_LUT_DIRS[aerosol_type]

        if atm_type not in ATMOSPHERES.keys():
            LOG.error("Atmosphere type %s not supported", atm_type)

        LOG.info("Atmosphere chosen: %s", atm_type)

        # Try fix instrument naming
        instr = INSTRUMENTS.get(platform_name, sensor)
        if instr != sensor:
            sensor = instr
            LOG.warning("Inconsistent sensor/satellite input - " +
                        "sensor set to %s", sensor)

        self.sensor = sensor.replace('/', '')

        if 'download_from_internet' in options and options['download_from_internet']:
            self.do_download = True

        if (self._aerosol_type in ATM_CORRECTION_LUT_VERSION and
                self._get_lutfiles_version() == ATM_CORRECTION_LUT_VERSION[self._aerosol_type]['version']):
            self._lutfiles_version_uptodate = True

        ext = atm_type.replace(' ', '_')
        lutname = "rayleigh_lut_{0}.h5".format(ext)
        self.reflectance_lut_filename = os.path.join(rayleigh_dir, lutname)

        if not self._lutfiles_version_uptodate and self.do_download:
            LOG.info("Will download from internet...")
            download_luts(aerosol_type=aerosol_type)

        if (not os.path.exists(self.reflectance_lut_filename) or
                not os.path.isfile(self.reflectance_lut_filename)):
            raise IOError('pyspectral file for Rayleigh scattering correction ' +
                          'does not exist! Filename = ' +
                          str(self.reflectance_lut_filename))

        LOG.debug('LUT filename: %s', str(self.reflectance_lut_filename))
        self._rayl = None
        self._wvl_coord = None
        self._azid_coord = None
        self._satz_sec_coord = None
        self._sunz_sec_coord = None
Example #18
0
    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!")
Example #19
0
                 'rayleigh_only', 'rural_aerosol', 'urban_aerosol']

ATMOSPHERES = {'subarctic summer': 4, 'subarctic winter': 5,
               'midlatitude summer': 6, 'midlatitude winter': 7,
               'tropical': 8, 'us-standard': 9}


HTTPS_RAYLEIGH_LUTS = {}
URL_PREFIX = "https://zenodo.org/record/1288441/files/pyspectral_atm_correction_luts"
for atype in AEROSOL_TYPES:
    name = {'rayleigh_only': 'no_aerosol'}.get(atype, atype)
    url = "{prefix}_{name}.tgz".format(prefix=URL_PREFIX, name=name)
    HTTPS_RAYLEIGH_LUTS[atype] = url


CONF = get_config()
LOCAL_RSR_DIR = CONF.get('rsr_dir')
LOCAL_RAYLEIGH_DIR = CONF.get('rayleigh_dir')

try:
    os.makedirs(LOCAL_RSR_DIR)
except OSError:
    if not os.path.isdir(LOCAL_RSR_DIR):
        raise

RAYLEIGH_LUT_DIRS = {}
for sub_dir_name in HTTPS_RAYLEIGH_LUTS:
    dirname = os.path.join(LOCAL_RAYLEIGH_DIR, sub_dir_name)
    RAYLEIGH_LUT_DIRS[sub_dir_name] = dirname

TB2RAD_DIR = CONF.get('tb2rad_dir', tempfile.gettempdir())