Example #1
0
 def test_tss_tolerance(self):
     comp = TSSComparator(for_testing=True, rtol=0.001, atol=0.0001)
     comp.compare_files('tests/data/folder_a/test_tol_ok_1.tss',
                        'tests/data/folder_b/test_tol_ok_2.tss')
     with pytest.raises(AssertionError) as excinfo:
         comp.compare_files('tests/data/folder_a/test_tol_fail_1.tss',
                            'tests/data/folder_b/test_tol_fail_2.tss')
     assert 'Not equal to tolerance rtol=0.001, atol=0.0001\n\nMismatched elements: 1 / 2 (50%)\nMax absolute difference: 0.007' in str(
         excinfo.value)
     comp = TSSComparator(for_testing=True, rtol=0.00001, atol=0.000001)
     with pytest.raises(AssertionError) as excinfo:
         comp.compare_files('tests/data/folder_a/test_tol_ok_1.tss',
                            'tests/data/folder_b/test_tol_ok_2.tss')
     assert 'Not equal to tolerance rtol=1e-05, atol=1e-06\n\nMismatched elements: 1 / 2 (50%)\nMax absolute difference: 5.e-05' in str(
         excinfo.value)
Example #2
0
    def compare_reference(cls,
                          variable='dis',
                          check='map',
                          step_length='86400'):
        """
        :param variable: variable to check. Default 'dis' (Discharge)
        :param check: either 'map' or 'tss'. Default 'map'
        :param step_length: DtSec (86400 for daily and 21600 for 6h run)
        """

        settings = LisSettings.instance()
        binding = settings.binding
        reference = cls.reference_files[variable][step_length][check]

        if check == 'map':
            output_map = os.path.normpath(
                binding[cls.reference_files[variable]['report_map']]) + '.nc'
            comparator = NetCDFComparator(settings.maskpath)
            comparator.compare_files(reference, output_map)
        elif check == 'tss':
            output_tss = binding[cls.reference_files[variable]['report_tss']]
            comparator = TSSComparator()
            comparator.compare_files(reference, output_tss)
        # If there are differences, test fails before reaching this line (AssertionError(s) in comparator methods)
        assert True
Example #3
0
    def test_tss(self):
        with pytest.raises(AssertionError) as excinfo:
            comp = TSSComparator(array_equal=True)
            comp.compare_files('tests/data/folder_a/qLakeOut.tss',
                               'tests/data/folder_b/qLakeOut.tss')

        assert 'tests/data/folder_a/qLakeOut.tss different from tests/data/folder_b/qLakeOut.tss' in str(
            excinfo.value)
        comp.compare_files('tests/data/folder_a/qLakeIn.tss',
                           'tests/data/folder_b/qLakeIn.tss')

        with pytest.raises(AssertionError) as excinfo:
            comp.compare_dirs('tests/data/folder_a/',
                              'tests/data/folder_b/',
                              skip_missing=True)
        assert 'tests/data/folder_a/qLakeOut.tss different from tests/data/folder_b/qLakeOut.tss' in str(
            excinfo.value)
Example #4
0
 def test_tss_timestep_tol(self):
     comp = TSSComparator(for_testing=True, rtol=0.00001, atol=0.000001)
     timestep = 62
     comp.compare_files('tests/data/folder_b/qLakeIn.tss',
                        'tests/data/folder_b/qLakeInSingleTimestep.tss',
                        timestep)
     with pytest.raises(AssertionError) as excinfo:
         comp.compare_files('tests/data/folder_b/qLakeOut.tss',
                            'tests/data/folder_b/qLakeOut3.tss', timestep)
     assert 'Not equal to tolerance rtol=1e-05, atol=1e-06\n\nMismatched elements: 1 / 2 (50%)' in str(
         excinfo.value)
Example #5
0
 def test_tss_timestep(self):
     comp = TSSComparator(for_testing=True, array_equal=True)
     timestep = 62
     comp.compare_files('tests/data/folder_a/qLakeIn.tss',
                        'tests/data/folder_b/qLakeInSingleTimestep.tss',
                        timestep)
     with pytest.raises(AssertionError) as excinfo:
         comp.compare_files('tests/data/folder_b/qLakeOut.tss',
                            'tests/data/folder_b/qLakeOut2.tss', timestep)
     assert '62 not found in tests/data/folder_b/qLakeOut2.tss' in str(
         excinfo.value)
Example #6
0
    def run_warmstart_by_dtsec(self,
                               dt_sec,
                               step_end,
                               step_start,
                               report_steps='1..9999'):
        modules_to_unset = [
            'simulateReservoirs',
            'repsimulateReservoirs',
            'wateruse',
            'useWaterDemandAveYear',
            'wateruseRegion',
        ]
        check_every = 13  # steps
        # init
        path_out_init = mk_path_out(
            'data/LF_ETRS89_UseCase/out/init{}'.format(dt_sec))
        settings_prerun = self.setoptions(self.settings_files['prerun'],
                                          opts_to_unset=modules_to_unset,
                                          vars_to_set={
                                              'DtSec': dt_sec,
                                              'PathOut': path_out_init,
                                              'StepStart': step_start,
                                              'ReportSteps': report_steps,
                                              'StepEnd': step_end
                                          })
        step_end_dt = settings_prerun.step_end_dt
        # ** execute
        lisfloodexe(settings_prerun)

        # long run
        lzavin_path = settings_prerun.binding['LZAvInflowMap']
        avgdis_path = settings_prerun.binding['AvgDis']
        path_out_reference = mk_path_out(
            'data/LF_ETRS89_UseCase/out/longrun_reference{}'.format(dt_sec))
        settings_longrun = self.setoptions(self.settings_files['cold'],
                                           opts_to_unset=modules_to_unset,
                                           vars_to_set={
                                               'StepStart': step_start,
                                               'StepEnd': step_end,
                                               'LZAvInflowMap': lzavin_path,
                                               'PathOut': path_out_reference,
                                               'AvgDis': avgdis_path,
                                               'ReportSteps': report_steps,
                                               'DtSec': dt_sec
                                           })
        # ** execute
        lisfloodexe(settings_longrun)

        # warm run (1. Cold start)
        run_number = 1
        cold_start_step_end = step_start
        path_out = mk_path_out('data/LF_ETRS89_UseCase/out/run{}_{}'.format(
            dt_sec, run_number))
        settings_coldstart = self.setoptions(self.settings_files['cold'],
                                             opts_to_unset=modules_to_unset,
                                             vars_to_set={
                                                 'StepStart': step_start,
                                                 'StepEnd':
                                                 cold_start_step_end,
                                                 'LZAvInflowMap': lzavin_path,
                                                 'PathOut': path_out,
                                                 'AvgDis': avgdis_path,
                                                 'ReportSteps': report_steps,
                                                 'DtSec': dt_sec
                                             })
        # ** execute
        lisfloodexe(settings_coldstart)

        # warm run (2. single step warm start/stop with initial conditions from previous run)
        prev_settings = settings_coldstart
        warm_step_start = prev_settings.step_end_dt + timedelta(seconds=dt_sec)
        warm_step_end = warm_step_start
        timestep_init = prev_settings.step_end_dt.strftime('%d/%m/%Y %H:%M')
        nc_comparator = NetCDFComparator(settings_prerun.maskpath)
        tss_comparator = TSSComparator(array_equal=True)
        while warm_step_start <= step_end_dt:
            run_number += 1
            path_init = prev_settings.output_dir
            path_out = mk_path_out(
                'data/LF_ETRS89_UseCase/out/run{}_{}'.format(
                    dt_sec, run_number))

            settings_warmstart = self.setoptions(
                self.settings_files['warm'],
                opts_to_unset=modules_to_unset,
                vars_to_set={
                    'StepStart': warm_step_start.strftime('%d/%m/%Y %H:%M'),
                    'StepEnd': warm_step_end.strftime('%d/%m/%Y %H:%M'),
                    'LZAvInflowMap': lzavin_path,
                    'PathOut': path_out,
                    'PathInit': path_init,
                    'timestepInit': timestep_init,
                    'AvgDis': avgdis_path,
                    'ReportSteps': report_steps,
                    'DtSec': dt_sec
                })
            # ** execute
            lisfloodexe(settings_warmstart)

            # checking values at current timestep (using datetime)
            if not (run_number % check_every):
                # ****** compare *******
                # compare every 13 timesteps to speed up test
                timestep_dt = settings_warmstart.step_end_dt  # NetCDFComparator takes datetime.datetime as timestep
                timestep = settings_warmstart.step_end_int
                nc_comparator.compare_dirs(path_out,
                                           path_out_reference,
                                           timestep=timestep_dt)
                tss_comparator.compare_dirs(path_out,
                                            path_out_reference,
                                            timestep=timestep)

            # setup for next warm start/stop
            prev_settings = settings_warmstart
            warm_step_start = prev_settings.step_end_dt + timedelta(
                seconds=dt_sec)
            warm_step_end = warm_step_start
            timestep_init = prev_settings.step_end_dt.strftime(
                '%d/%m/%Y %H:%M')