def check_fields(self): self.clean_warnings() if self._uv_file is not None and self._calibration_file is not None: d, brewer_id = name_to_date_and_brewer_id(self._uv_file) if self.settings.ozone_data_source == DataSource.FILES: brewer_type = BFileOzoneProvider( File(self._b_file) if self._b_file is not None else None ).get_brewer_type() if brewer_type is None: self.show_warning( f"Straylight correction cannot be determined. Using default:" f"{self.settings.default_straylight_correction.value}") # If all fields are valid, we initialize a CalculationInput and enable the button self._calculation_input = CalculationInput( brewer_id, d, self.settings, File(self._uv_file), File(self._b_file) if self._b_file is not None else None, File(self._calibration_file), File(self._arf_file) if self._arf_file is not None else None, ) self._calculate_button.set_enabled(True) else: self._calculate_button.set_enabled(False) self._calculation_input = None
def test_arf_sources(self): calculation_input = CalculationInput( "033", date(2019, 6, 20), Settings(), File("dummy"), File("dummy"), File("dummy"), File("buvic/logic/test/arf_example"), ) arf = calculation_input.arf self.assertEqual(0.974, arf.values[2])
def test_cloud_cover(self): init_logging(logging.DEBUG) calculation_input = CalculationInput( "033", date(2019, 12, 20), Settings(), File("buvic/logic/test/uv_example"), File("dummy"), File("dummy"), File("dummy"), parameter_file_name=File("buvic/logic/test/parameter_example"), ) self.assertTrue(isinstance(calculation_input.cloud_cover, ParameterCloudCover)) self.assertTrue(calculation_input.cloud_cover.is_diffuse(4)) self.assertTrue(calculation_input.cloud_cover.is_diffuse(10)) self.assertTrue(calculation_input.cloud_cover.is_diffuse(15)) calculation_input = CalculationInput( "033", date(2019, 12, 20), Settings(), File("buvic/logic/test/uv_example"), File("dummy"), File("dummy"), File("dummy"), parameter_file_name=None, ) self.assertTrue(isinstance(calculation_input.cloud_cover, DefaultCloudCover)) self.assertFalse(calculation_input.cloud_cover.is_diffuse(4)) self.assertFalse(calculation_input.cloud_cover.is_diffuse(10)) self.assertFalse(calculation_input.cloud_cover.is_diffuse(15))
def test(self): self._test_paths("0010110G.033", File("dummy"), File("dummy")) self._test_paths("path/to/0010110G.033", File("path/to/dummy"), File("path/to/dummy")) self._test_paths("path/to/0010110G.033", File("path/to/uv/dummy"), File("path/to/b/dummy")) self._test_paths("nocoscor/path/to/0010110G.033", File("path/to/uv/dummy"), File("path/to/b/dummy"), no_coscor=True) self._test_paths("path/to/b/0010110G.033", None, File("path/to/b/dummy")) self._test_paths("path/to/uv/0010110G.033", File("path/to/uv/dummy")) self._test_paths("033/2019/0010110G.033")
def test_uv_sources(self): calculation_input = CalculationInput( "033", date(2019, 12, 20), Settings(uv_data_source=DataSource.FILES), File("buvic/logic/test/uv_example"), File("dummy"), File("dummy"), File("dummy"), ) entries = calculation_input.uv_file_entries # 12 sections are present in `uv_example` self.assertEqual(12, len(entries)) calculation_input = CalculationInput( "033", date(2019, 6, 20), Settings(uv_data_source=DataSource.EUBREWNET), File("dummy"), File("dummy"), File("dummy"), File("dummy"), ) # 13 sections are present on EUBREWNET for day 2019-06-20 entries = calculation_input.uv_file_entries self.assertEqual(13, len(entries))
def test_ozone_sources(self): calculation_input = CalculationInput( "033", date(2019, 6, 20), Settings(ozone_data_source=DataSource.FILES), File("dummy"), File("buvic/logic/test/b_example"), File("dummy"), File("dummy"), ) ozone = calculation_input.ozone self.assertEqual(318.8, ozone.values[0]) calculation_input = CalculationInput( "033", date(2019, 6, 20), Settings(ozone_data_source=DataSource.EUBREWNET), File("dummy"), File("dummy"), File("dummy"), File("dummy"), ) ozone = calculation_input.ozone self.assertAlmostEqual(318.3, ozone.values[0], 1)
def test_uvr_sources(self): calculation_input = CalculationInput( "033", date(2019, 12, 20), Settings(uvr_data_source=DataSource.FILES), File("dummy"), File("dummy"), File("buvic/logic/test/uvr_example"), File("dummy"), ) calibration = calculation_input.calibration self.assertEqual("buvic/logic/test/uvr_example", calibration.source) calculation_input = CalculationInput( "033", date(2018, 12, 20), Settings(uvr_data_source=DataSource.EUBREWNET), File("dummy"), File("dummy"), File("dummy"), File("dummy"), ) calibration = calculation_input.calibration self.assertEqual("2018-12-19", calibration.source)
def test_brewer_model_sources(self): calculation_input = CalculationInput( "033", date(2019, 6, 20), Settings(brewer_model_data_source=DataSource.FILES), File("dummy"), File("buvic/logic/test/b_example"), File("dummy"), File("dummy"), ) brewer_type = calculation_input.brewer_type self.assertEqual("mkii", brewer_type) calculation_input = CalculationInput( "033", date(2019, 6, 20), Settings(brewer_model_data_source=DataSource.EUBREWNET), File("dummy"), File("dummy"), File("dummy"), File("dummy"), ) brewer_type = calculation_input.brewer_type self.assertEqual("mkii", brewer_type)
def test(self): file = File("full/path/to/file.txt") self.assertEqual("full/path/to/file.txt", file.full_path) self.assertEqual("file.txt", file.file_name) self.assertEqual("full/path/to", file.path) file = File("full/path/to/file.txt", "full") self.assertEqual("full/path/to/file.txt", file.full_path) self.assertEqual("file.txt", file.file_name) self.assertEqual("path/to", file.path) file = File("full/path/to/file.txt", "full/path/") self.assertEqual("full/path/to/file.txt", file.full_path) self.assertEqual("file.txt", file.file_name) self.assertEqual("to", file.path)
def test_file_loading(self): parameters = FileParameterProvider(File("buvic/logic/test/parameter_example")).get_parameters() self.assertEqual(0.1, parameters.interpolated_albedo(10, 0)) self.assertEqual(0.1, parameters.interpolated_albedo(9, 0)) self.assertEqual(0.1, parameters.interpolated_albedo(11, 0)) self.assertEqual(0.1, parameters.interpolated_albedo(0, 0)) self.assertEqual(0.3, parameters.interpolated_albedo(12, 0)) self.assertEqual(0.3, parameters.interpolated_albedo(13, 0)) self.assertEqual(0.5, parameters.interpolated_albedo(14, 0)) self.assertEqual(0.5, parameters.interpolated_albedo(15, 0)) self.assertEqual(0.5, parameters.interpolated_albedo(100, 0)) self.assertEqual(1, parameters.interpolated_aerosol(10, Angstrom(0, 0)).alpha) self.assertEqual(1, parameters.interpolated_aerosol(9, Angstrom(0, 0)).alpha) self.assertEqual(1, parameters.interpolated_aerosol(0, Angstrom(0, 0)).alpha) self.assertEqual(1.2, parameters.interpolated_aerosol(11, Angstrom(0, 0)).alpha) self.assertEqual(1.2, parameters.interpolated_aerosol(12, Angstrom(0, 0)).alpha) self.assertEqual(1.2, parameters.interpolated_aerosol(13, Angstrom(0, 0)).alpha) self.assertEqual(1.5, parameters.interpolated_aerosol(14, Angstrom(0, 0)).alpha) self.assertEqual(1.5, parameters.interpolated_aerosol(15, Angstrom(0, 0)).alpha) self.assertEqual(1.5, parameters.interpolated_aerosol(100, Angstrom(0, 0)).alpha) self.assertEqual(None, parameters.cloud_cover(9)) self.assertEqual(0, parameters.cloud_cover(10)) self.assertEqual(1, parameters.cloud_cover(11)) self.assertEqual(None, parameters.cloud_cover(12)) self.assertEqual(None, parameters.cloud_cover(13)) self.assertEqual(1, parameters.cloud_cover(14)) self.assertEqual(None, parameters.cloud_cover(15))
def test_cache(): init_logging(logging.DEBUG) calculation_input = CalculationInput( "033", date(2019, 12, 20), Settings(), File("buvic/logic/test/uv_example"), File("buvic/logic/test/b_example"), File("buvic/logic/test/calibration_example"), File("buvic/logic/test/arf_example"), parameter_file_name=File("buvic/logic/test/parameter_example"), ) calculation_input.init_properties() calculation_input.calibration_file_name = "does_not_exist" # No exception is thrown since the calibration value used is cached c = calculation_input.calibration del c
def _match_arf_file(self, file_path: str, res: Match[str]) -> None: """ Action to perform on matched arf files. See `_find_file_recursive` :param file_path: the path of the matched file :param res: the result of the regex match """ brewer_id: str = res.group("brewer_id") if brewer_id not in self._file_dict: self._file_dict[brewer_id] = InstrumentFiles(None) if self._file_dict[brewer_id].arf_file is not None: raise ValueError( f"Multiple ARF files found for brewer with id {brewer_id}.") self._file_dict[brewer_id].arf_file = File(file_path, self._uvdata_dir)
def _match_file( self, file_path: str, res: Match[str], parent_dir: str, field_getter: Callable[[InstrumentFiles], List[File]]) -> None: """ Action to perform when on matched files. This adds the file to one of the list of files from an InstrumentFile. The correct list is retrieved with a given getter. See `_find_file_recursive` :param file_path: the path of the matched file :param res: the result of the regex match :param parent_dir: the root directory of the search :param field_getter: a function to apply on an InstrumentFile to get the list of files of the correct type """ brewer_id = res.group("brewer_id") if brewer_id not in self._file_dict: self._file_dict[brewer_id] = InstrumentFiles(None) field_getter(self._file_dict[brewer_id]).append( File(file_path, parent_dir))
def test(self): file_utils = FileUtils("buvic/logic/test/") file_utils.refresh(Settings()) self.assertEqual( 0, len( file_utils.get_calculation_inputs_between( date(2010, 1, 1), date(2020, 1, 1), "033", Settings()))) self.assertEqual(0, len(file_utils.get_calculation_inputs(Settings()))) file_utils._file_dict["033"] = InstrumentFiles( None, [File("UVR00119.033"), File("UVR00219.033")], [ File("UV00119.033"), File("UV00219.033"), File("UV00319.033"), File("UV00419.033") ], [ File("B00119.033"), File("B00219.033"), File("B00319.033"), File("B00419.033"), File("B00519.033") ], ) file_utils._file_dict["070"] = InstrumentFiles(None, [File("UVR00119.070")], [File("UV00119.070")], [File("B00119.070")]) self.assertEqual( 4, len( file_utils.get_calculation_inputs_between( date(2010, 1, 1), date(2020, 1, 1), "033", Settings()))) self.assertEqual( 2, len( file_utils.get_calculation_inputs_between( date(2019, 1, 1), date(2019, 1, 2), "033", Settings()))) self.assertEqual( 2, len( file_utils.get_calculation_inputs_between( date(2019, 1, 3), date(2019, 1, 4), "033", Settings()))) self.assertEqual( 2, len( file_utils.get_calculation_inputs_between( date(2019, 1, 3), date(2019, 1, 5), "033", Settings()))) self.assertEqual(5, len(file_utils.get_calculation_inputs(Settings()))) self.assertEqual(["033", "070"], file_utils.get_brewer_ids()) self.assertEqual( ["UVR00119.033", "UVR00219.033"], [u.file_name for u in file_utils.get_uvr_files("033")]) self.assertEqual( ["UVR00119.070"], [u.file_name for u in file_utils.get_uvr_files("070")]) date_start, date_end = file_utils.get_date_range("033") self.assertEqual(1, date_start.day) self.assertEqual(1, date_start.month) self.assertEqual(2019, date_start.year) self.assertEqual(4, date_end.day) self.assertEqual(1, date_end.month) self.assertEqual(2019, date_end.year)
date_start = date.fromisoformat(dates_and_brewer_id[0]) date_end = date.fromisoformat(dates_and_brewer_id[1]) brewer_id = dates_and_brewer_id[2] inputs = file_utils.get_calculation_inputs_between(date_start, date_end, brewer_id, settings) cmd.calculate_for_inputs(inputs) elif paths is not None: init_logging(logging.WARN) if input_dir is None: input_dir = "" d, brewer_id = name_to_date_and_brewer_id(paths[0]) b_file = File(input_dir + paths[1], input_dir) if paths[1] is not None else None arf_file = File(input_dir + paths[3], input_dir) if paths[3] is not None else None calculation_input = CalculationInput( brewer_id, d, settings, File(input_dir + paths[0], input_dir), b_file, File(input_dir + paths[2], input_dir), arf_file, ) cmd.calculate_for_input(calculation_input) elif do_all:
def _test_paths(self, expected_output_path: str, uv_file: Optional[File] = None, b_file: Optional[File] = None, **kwargs): result = Result( 0, CalculationInput("033", date(2019, 1, 1), Settings(**kwargs), uv_file, b_file, File("dummy"), File("dummy"), None), 44.4, 0.0, 0.0, Spectrum([], [70], [], [], [], []), ) name = result.get_qasume_name() self.assertEqual(expected_output_path, name)
def test_file_failures(self): with self.assertRaises(ParameterFileParsingError): FileParameterProvider(File("buvic/logic/test/parameter_example_failure_1")).get_parameters() with self.assertRaises(ParameterFileParsingError): FileParameterProvider(File("buvic/logic/test/parameter_example_failure_2")).get_parameters()