def test_dates_steps_6h(self): settings_a = self.setoptions(self.settings_files['full'], opts_to_set=['repStateMaps', 'repEndMaps', 'repDischargeMaps', 'repSnowMaps', 'repLZMaps', 'repUZMaps'], vars_to_set={'StepStart': '30/07/2016 06:00', 'StepEnd': '31/07/2016 18:00', 'DtSec': '21600', 'PathOut': '$(PathRoot)/out/1'} ) mk_path_out('data/LF_ETRS89_UseCase/out/1') lisfloodexe(settings_a) settings_b = self.setoptions(self.settings_files['full'], opts_to_set=['repStateMaps', 'repEndMaps', 'repDischargeMaps', 'repSnowMaps', 'repLZMaps', 'repUZMaps'], vars_to_set={'StepStart': 24225, 'StepEnd': 24231, 'DtSec': '21600', 'PathOut': '$(PathRoot)/out/2'}) mk_path_out('data/LF_ETRS89_UseCase/out/2') lisfloodexe(settings_b) assert settings_a.step_start_int == 24225 assert settings_a.step_end_int == 24231 assert settings_a.step_start == settings_a.step_start_dt.strftime('%d/%m/%Y %H:%M') assert settings_a.step_end == settings_a.step_end_dt.strftime('%d/%m/%Y %H:%M') assert settings_b.step_start_dt == datetime.datetime(2016, 7, 30, 6, 0) assert settings_b.step_end_dt == datetime.datetime(2016, 7, 31, 18, 0) comparator = NetCDFComparator(settings_a.maskpath, array_equal=True) out_a = settings_a.output_dir out_b = settings_b.output_dir comparator.compare_dirs(out_a, out_b)
def run_subcathmenttest_by_dtsec(self, dt_sec, step_end, step_start, report_steps='1..9999', wateruse_on=False): modules_to_unset = self.modules_to_unset if not wateruse_on else [] modules_to_set = self.modules_to_unset if wateruse_on else [] # long run entire domain path_out_domain = mk_path_out('data/LF_ETRS89_UseCase/out/longrun_domain{}'.format(dt_sec)) settings_longrun = self.setoptions(self.settings_files['cold'], opts_to_unset=modules_to_unset, opts_to_set=modules_to_set, vars_to_set={'StepStart': step_start, 'StepEnd': step_end, 'PathOut': path_out_domain, 'ReportSteps': report_steps, 'DtSec': dt_sec}) # ** execute lisfloodexe(settings_longrun) # long run entire on subdomain path_out_subdomain = mk_path_out('data/LF_ETRS89_UseCase/out/longrun_subdomain{}'.format(dt_sec)) settings_longrun_subdomain = self.setoptions(self.settings_files['cold'], opts_to_unset=modules_to_unset, vars_to_set={'StepStart': step_start, 'StepEnd': step_end, 'PathOut': path_out_subdomain, 'ReportSteps': report_steps, 'MaskMap': '$(PathRoot)/maps/subcatchment_mask.map', 'DtSec': dt_sec}) # ** execute lisfloodexe(settings_longrun_subdomain) # ****** compare ******* # compare using the last maskmap (subcatchment_mask.map) settings = LisSettings.instance() nc_comparator = NetCDFComparator(settings.maskpath, array_equal=True) nc_comparator.compare_dirs(path_out_subdomain, path_out_domain)
def main(cliargs): parser = ParserHelpOnError( description='Compare netCDF outputs: {}'.format(version)) parser.add_args() args = parser.parse_args(cliargs) dataset_a = args.dataset_a.as_posix() dataset_b = args.dataset_b.as_posix() maskfile = args.maskarea.as_posix() array_equal = args.array_equal skip_missing = args.skip_missing atol = args.atol rtol = args.rtol max_diff_perc = args.max_diff_percentage max_large_diff_perc = args.max_largediff_percentage save_diff_files = args.save_diffs comparator = NetCDFComparator(maskfile, array_equal=array_equal, for_testing=False, atol=atol, rtol=rtol, max_perc_diff=max_diff_perc, max_perc_large_diff=max_large_diff_perc, save_diff_files=save_diff_files) logger.info('\n\nComparing %s and %s\n\n ', dataset_a, dataset_b) errors = comparator.compare_dirs(dataset_a, dataset_b, skip_missing=skip_missing) for i, e in enumerate(errors): logger.error('%d - %s', i, e)
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
def test_end_state_reported(self): path_out = mk_path_out('data/LF_ETRS89_UseCase/out/testrep') settings = self.setoptions( self.settings_files['base'], ['repEndMaps', 'repStateMaps', 'repDischargeMaps'], vars_to_set={'PathOut': path_out}) lisfloodexe(settings) comparator = NetCDFComparator(settings.maskpath, array_equal=True) end_files = [ os.path.join(settings.output_dir, f) for f in os.listdir(settings.output_dir) if f.endswith('.end.nc') ] state_files = [ os.path.join(settings.output_dir, f) for f in os.listdir(settings.output_dir) if f.endswith('.nc') and '.end.' not in f ] assert end_files assert state_files # assert that unique timestep in end maps is equal to last timestep in state maps for end_file in end_files: basename = end_file.replace('.end.nc', '') state_file = '{}.nc'.format(basename) if not os.path.exists(state_file): continue state_nc = Dataset(state_file) end_nc = Dataset(end_file) var_name = [ k for k in state_nc.variables if len(state_nc.variables[k].dimensions) == 3 ][0] vara = state_nc.variables[var_name] varb = end_nc.variables['{}.end'.format(var_name)] assert 'time' not in end_nc.variables # compare latest timestep in state map with unique timestep in end map comparator.compare_arrays(vara[-1][:, :], varb[:, :], varname=basename) assert not comparator.errors
def test_netcdfcomp_files(self): comp = NetCDFComparator('tests/data/area.nc', for_testing=True) comp.compare_files('tests/data/folder_a/ta.nc', 'tests/data/folder_b/ta.nc') with pytest.raises(AssertionError) as excinfo: comp.compare_files('tests/data/folder_a/ta.nc', 'tests/data/folder_a/tp.nc') assert 'have different variables names' in str(excinfo.value)
def test_netcdfcomp_identical(self): comp = NetCDFComparator('tests/data/folder_d/mask.nc', array_equal=True, for_testing=True) comp.compare_files('tests/data/folder_d/a.nc', 'tests/data/folder_d/a.nc') with pytest.raises(AssertionError) as excinfo: comp.compare_files('tests/data/folder_d/a.nc', 'tests/data/folder_d/b.nc') assert 'Arrays are not equal' in str(excinfo.value)
def test_netcdfcomp_dirs(self): comp = NetCDFComparator('tests/data/area.nc', for_testing=True) comp.compare_dirs('tests/data/folder_a/', 'tests/data/folder_b/', skip_missing=True) with pytest.raises(AssertionError) as excinfo: comp.compare_dirs('tests/data/folder_a/', 'tests/data/folder_b/', skip_missing=False) assert 'is missing in tests/data/folder_b' in str(excinfo.value)
def test_netcdfcomp_no_asserts(self): comp = NetCDFComparator('tests/data/folder_d/mask.nc', array_equal=True, for_testing=False) comp.compare_files('tests/data/folder_d/a.nc', 'tests/data/folder_d/a.nc') assert not comp.errors comp.compare_files('tests/data/folder_d/a.nc', 'tests/data/folder_d/b.nc') assert comp.errors assert 'a.nc/field@0 is different' in comp.errors
def test_get_cuts_withmaskfile_compare(self): maskfile = 'tests/data/submask/subcatchment_mask.map' x_min, x_max, y_min, y_max = get_cuts(mask=maskfile) x_minr, x_maxr, y_minr, y_maxr = np.round(x_min, 2), np.round( x_max, 2), np.round(y_min, 2), np.round(y_max, 2) assert (x_minr, x_maxr, y_minr, y_maxr) == (4052500.0, 4232500.0, 2332500.0, 2542500.0) fin = 'tests/data/submask/dis.nc' fout = 'tests/data/submask/dis_cut.nc' cutmap(fin, fout, x_min, x_max, y_min, y_max) with Dataset(fout) as nc: lons = nc.variables['x'][:] lats = nc.variables['y'][:] res_x = np.round(np.min(lons), 2) res_y = np.round(np.min(lats), 2) assert res_x == 4052500.0 assert res_y == 2332500.0 comparator = NetCDFComparator(mask=maskfile, array_equal=True) comparator.compare_files(fin, fout) comparator = NetCDFComparator(array_equal=True) comparator.compare_files(fout, 'tests/data/submask/dis_subdomain.nc') os.unlink(fout)
def test_netcdfcomp_diffdirs(self): comp = NetCDFComparator('tests/data/folder_d/mask.nc', for_testing=False, save_diff_files=True, array_equal=True) assert comp.diff_folder is not None assert isinstance(comp.diff_folder, Path) assert comp.diff_folder.exists() comp.compare_files('tests/data/folder_d/a.nc', 'tests/data/folder_d/a.nc') assert not comp.diff_folder.joinpath('a.nc').exists() assert not comp.diff_folder.joinpath('b.nc').exists() assert not comp.diff_folder.joinpath('diff.nc').exists() comp.compare_files('tests/data/folder_d/a.nc', 'tests/data/folder_d/b.nc') assert comp.diff_folder.joinpath('a_a.nc').exists( ), 'Diff file A does not exist in %s' % comp.diff_folder assert comp.diff_folder.joinpath( 'a_b.nc').exists(), 'Diff file B does not exist' assert comp.diff_folder.joinpath( 'a_diff.nc').exists(), 'Diff file diff.nc does not exist'
def test_netcdfcomp_tol(self): comp = NetCDFComparator('tests/data/folder_d/mask.nc', atol=0.05, rtol=0.1, for_testing=True) comp.compare_files('tests/data/folder_d/a.nc', 'tests/data/folder_d/b.nc') comp = NetCDFComparator('tests/data/folder_d/mask.nc', atol=0.005, rtol=0.01, for_testing=True) comp.compare_files('tests/data/folder_d/a.nc', 'tests/data/folder_d/b.nc') with pytest.raises(AssertionError) as excinfo: comp = NetCDFComparator('tests/data/folder_d/mask.nc', atol=0.0005, rtol=0.001, for_testing=True) comp.compare_files('tests/data/folder_d/a.nc', 'tests/data/folder_d/b.nc') assert 'of different values - max diff:' in str(excinfo.value)
def test_netcdfcomp_submask(self): comp = NetCDFComparator('tests/data/submask/subcatchment_mask.map') comp.compare_files('tests/data/submask/dis_subdomain.nc', 'tests/data/submask/dis.nc')
def test_netcdfcomp_no_mask(self): comp = NetCDFComparator(array_equal=True) comp.compare_files('tests/data/folder_a/ta.nc', 'tests/data/folder_b/ta.nc') comp.compare_files('tests/data/folder_d/a.nc', 'tests/data/folder_d/a.nc') comp = NetCDFComparator() comp.compare_files('tests/data/folder_a/ta.nc', 'tests/data/folder_b/ta.nc') comp.compare_files('tests/data/folder_d/a.nc', 'tests/data/folder_d/a.nc') with pytest.raises(ValueError) as excinfo: comp.compare_files('tests/data/submask/dis_subdomain.nc', 'tests/data/submask/dis.nc') assert 'operands could not be broadcast together with shapes (43,37) (57,80)' in str( excinfo.value)
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')