def __call__(self, datasets, **info): refl_data, sensor_aa, sensor_za, solar_aa, solar_za = datasets if refl_data.info.get("rayleigh_corrected"): return refl_data if os.path.isfile(self.dem_file): LOG.debug("Loading CREFL averaged elevation information from: %s", self.dem_file) from netCDF4 import Dataset as NCDataset # HDF4 file, NetCDF library needs to be compiled with HDF4 support nc = NCDataset(self.dem_file, "r") avg_elevation = nc.variables[self.dem_sds][:] else: avg_elevation = None from satpy.composites.crefl_utils import run_crefl, get_coefficients percent = refl_data.info["units"] == "%" coefficients = get_coefficients(refl_data.info["sensor"], refl_data.info["wavelength"], refl_data.info["resolution"]) results = run_crefl(refl_data, coefficients, sensor_aa.info["area"].lons, sensor_aa.info["area"].lats, sensor_aa, sensor_za, solar_aa, solar_za, avg_elevation=avg_elevation, percent=percent, ) info.update(refl_data.info) info["rayleigh_corrected"] = True factor = 100. if percent else 1. proj = Dataset(data=results.data * factor, mask=results.mask, dtype=results.dtype, **info) self.apply_modifier_info(refl_data, proj) return proj
def _call_crefl(self, refl_data, coefficients, angles): from satpy.composites.crefl_utils import run_crefl avg_elevation = self._get_average_elevation() lons, lats = refl_data.attrs['area'].get_lonlats( chunks=refl_data.chunks) is_percent = refl_data.attrs["units"] == "%" use_abi = refl_data.attrs['sensor'] == 'abi' results = run_crefl(refl_data, coefficients, lons, lats, *angles, avg_elevation=avg_elevation, percent=is_percent, use_abi=use_abi) factor = 100. if is_percent else 1. results = results * factor return results
def __call__(self, datasets, optional_datasets, **info): """Create modified DataArray object by applying the crefl algorithm.""" if not optional_datasets or len(optional_datasets) != 4: vis = self.match_data_arrays([datasets[0]])[0] sensor_aa, sensor_za, solar_aa, solar_za = self.get_angles(vis) else: vis, sensor_aa, sensor_za, solar_aa, solar_za = self.match_data_arrays( datasets + optional_datasets) # get the dask array underneath sensor_aa = sensor_aa.data sensor_za = sensor_za.data solar_aa = solar_aa.data solar_za = solar_za.data # angles must be xarrays sensor_aa = xr.DataArray(sensor_aa, dims=['y', 'x']) sensor_za = xr.DataArray(sensor_za, dims=['y', 'x']) solar_aa = xr.DataArray(solar_aa, dims=['y', 'x']) solar_za = xr.DataArray(solar_za, dims=['y', 'x']) refl_data = datasets[0] if refl_data.attrs.get("rayleigh_corrected"): return refl_data if os.path.isfile(self.dem_file): LOG.debug("Loading CREFL averaged elevation information from: %s", self.dem_file) from netCDF4 import Dataset as NCDataset # HDF4 file, NetCDF library needs to be compiled with HDF4 support nc = NCDataset(self.dem_file, "r") # average elevation is stored as a 16-bit signed integer but with # scale factor 1 and offset 0, convert it to float here avg_elevation = nc.variables[self.dem_sds][:].astype(np.float) if isinstance(avg_elevation, np.ma.MaskedArray): avg_elevation = avg_elevation.filled(np.nan) else: avg_elevation = None from satpy.composites.crefl_utils import run_crefl, get_coefficients percent = refl_data.attrs["units"] == "%" coefficients = get_coefficients(refl_data.attrs["sensor"], refl_data.attrs["wavelength"], refl_data.attrs["resolution"]) use_abi = vis.attrs['sensor'] == 'abi' lons, lats = vis.attrs['area'].get_lonlats(chunks=vis.chunks) results = run_crefl(refl_data, coefficients, lons, lats, sensor_aa, sensor_za, solar_aa, solar_za, avg_elevation=avg_elevation, percent=percent, use_abi=use_abi) info.update(refl_data.attrs) info["rayleigh_corrected"] = True factor = 100. if percent else 1. results = results * factor results.attrs = info self.apply_modifier_info(refl_data, results) return results
class ReflectanceCorrector(CompositeBase): """CREFL modifier Uses a python rewrite of the C CREFL code written for VIIRS and MODIS. """ def __init__(self, *args, **kwargs): """Initialize the compositor with values from the user or from the configuration file. If `dem_filename` can't be found or opened then correction is done assuming TOA or sealevel options. :param dem_filename: path to the ancillary 'averaged heights' file default: CMGDEM.hdf environment override: os.path.join(<SATPY_ANCPATH>, <CREFL_ANCFILENAME>) :param dem_sds: variable name to load from the ancillary file """ dem_filename = kwargs.pop( "dem_filename", os.environ.get("CREFL_ANCFILENAME", "CMGDEM.hdf")) if os.path.exists(dem_filename): self.dem_file = dem_filename else: self.dem_file = os.path.join(get_environ_ancpath(), dem_filename) self.dem_sds = kwargs.pop("dem_sds", "averaged elevation") super(ReflectanceCorrector, self).__init__(*args, **kwargs) def __call__(self, (refl_data, sensor_aa, sensor_za, solar_aa, solar_za), **info): if refl_data.info.get("rayleigh_corrected"): return refl_data if os.path.isfile(self.dem_file): LOG.debug("Loading CREFL averaged elevation information from: %s", self.dem_file) from netCDF4 import Dataset nc = Dataset(self.dem_file, "r") avg_elevation = nc.variables[self.dem_sds][:] else: avg_elevation = None from satpy.composites.crefl_utils import run_crefl, get_coefficients percent = refl_data.info["units"] == "%" coefficients = get_coefficients(refl_data.info["sensor"], refl_data.info["wavelength"], refl_data.info["resolution"]) results = run_crefl( refl_data, coefficients, sensor_aa.info["area"].lons, sensor_aa.info["area"].lats, sensor_aa, sensor_za, solar_aa, solar_za, avg_elevation=avg_elevation, percent=percent, ) #info = combine_info(*refl_datasets) info.update(refl_data.info) info["rayleigh_corrected"] = True #info.setdefault("standard_name", "corrected_reflectance") #info["mode"] = self.info.get("mode", "L") factor = 100. if percent else 1. proj = Projectable(data=results.data * factor, mask=results.mask, dtype=results.dtype, **info) self.apply_modifier_info(refl_data, proj) return proj