def createTemplateEasy(sensorType, height, srf_size=None, corr_dx=None, corr_dy=None, lut_size=None): """ Create a template dataset in EASY FCDR format for the sensor given as argument. :param sensorType: the sensor type to create the template for :param height: the height in pixels of the data product :param srf_size: if set, the length of the spectral response function in frequency steps :param corr_dx: correlation length across track :param corr_dy: correlation length along track :param lut_size: size of a BT/radiance conversion lookup table :return the template dataset """ dataset = xr.Dataset() WriterUtils.add_standard_global_attributes(dataset) template_factory = TemplateFactory() sensor_template = template_factory.get_sensor_template(sensorType) sensor_template.add_original_variables(dataset, height, srf_size) sensor_template.add_easy_fcdr_variables(dataset, height, corr_dx, corr_dy, lut_size) sensor_template.add_template_key(dataset) return dataset
def createTemplateFull(sensorType, height): """ Create a template dataset in FULL FCDR format for the sensor given as argument. :param sensorType: the sensor type to create the template for :param height the hheight in pixels of the data product :return the template dataset """ dataset = xr.Dataset() WriterUtils.add_standard_global_attributes(dataset) template_factory = TemplateFactory() sensor_template = template_factory.get_sensor_template(sensorType) sensor_template.add_original_variables(dataset, height) sensor_template.add_full_fcdr_variables(dataset, height) sensor_template.add_template_key(dataset) return dataset
def write(ds, file, compression_level=None, overwrite=False): """ Save a dataset to NetCDF file. :param ds: The dataset :param file: File path :param compression_level: the file compression level, 0 - 9, default is 5 :param overwrite: set true to overwrite existing files """ if os.path.isfile(file): if overwrite is True: os.remove(file) else: raise IOError("The file already exists: " + file) # trigger mapping of sensor specific flags to the global flag variable template_factory = TemplateFactory() flag_mapper = template_factory.get_flag_mapper( ds.attrs["template_key"]) flag_mapper.map_global_flags(ds) # set up compression parameter for ALL variables. Unfortunately, xarray does not allow # one set of compression params per file, only per variable. tb 2017-01-25 if compression_level is None: compression_level = 5 comp = dict(zlib=True, complevel=compression_level) encoding = dict() for var_name in ds.data_vars: var_encoding = dict(comp) var_encoding.update(ds[var_name].encoding) encoding.update({var_name: var_encoding}) ds.to_netcdf(file, format='netCDF4', engine='netcdf4', encoding=encoding)
class TemplateFactoryTest(unittest.TestCase): def setUp(self): self.factory = TemplateFactory() def test_get_sensor_template(self): amsub = self.factory.get_sensor_template("AMSUB") self.assertIsNotNone(amsub) mhs = self.factory.get_sensor_template("MHS") self.assertIsNotNone(mhs) ssmts = self.factory.get_sensor_template("SSMT2") self.assertIsNotNone(ssmts) avhrr = self.factory.get_sensor_template("AVHRR") self.assertIsNotNone(avhrr) hirs_2 = self.factory.get_sensor_template("HIRS2") self.assertIsNotNone(hirs_2) hirs_3 = self.factory.get_sensor_template("HIRS3") self.assertIsNotNone(hirs_3) hirs_4 = self.factory.get_sensor_template("HIRS4") self.assertIsNotNone(hirs_4) mviri = self.factory.get_sensor_template("MVIRI") self.assertIsNotNone(mviri) mviri_static = self.factory.get_sensor_template("MVIRI_STATIC") self.assertIsNotNone(mviri_static) def test_get_flag_mapper(self): amsub = self.factory.get_flag_mapper("AMSUB") self.assertIsNotNone(amsub) self.assertIsInstance(amsub, DefaultFlagMapper) mhs = self.factory.get_flag_mapper("MHS") self.assertIsNotNone(mhs) self.assertIsInstance(mhs, DefaultFlagMapper) ssmt2 = self.factory.get_flag_mapper("SSMT2") self.assertIsNotNone(ssmt2) self.assertIsInstance(ssmt2, DefaultFlagMapper) avhrr = self.factory.get_flag_mapper("AVHRR") self.assertIsNotNone(avhrr) self.assertIsInstance(avhrr, AVHRR_FlagMapper) hirs2 = self.factory.get_flag_mapper("HIRS2") self.assertIsNotNone(hirs2) self.assertIsInstance(hirs2, HIRS_FlagMapper) hirs3 = self.factory.get_flag_mapper("HIRS3") self.assertIsNotNone(hirs3) self.assertIsInstance(hirs3, HIRS_FlagMapper) hirs4 = self.factory.get_flag_mapper("HIRS4") self.assertIsNotNone(hirs4) self.assertIsInstance(hirs4, HIRS_FlagMapper) mviri = self.factory.get_flag_mapper("MVIRI") self.assertIsNotNone(mviri) self.assertIsInstance(mviri, MVIRI_FlagMapper)
def setUp(self): self.factory = TemplateFactory()