Esempio n. 1
0
    def test_save_metadata_no_grids(self, tmp_path):
        p = utilities.yaml_from_dict(tmp_path, 'input.yaml', {
            'save_dt': 1,
            'save_metadata': True
        })
        _delta = DeltaModel(input_file=p)

        # mock the log_info
        _delta.log_info = mock.MagicMock()

        # mock the actual output routines
        _delta.make_figure = mock.MagicMock()
        _delta.save_figure = mock.MagicMock()
        _delta.save_grids = mock.MagicMock()

        exp_path_nc = os.path.join(tmp_path / 'out_dir',
                                   'pyDeltaRCM_output.nc')
        assert os.path.isfile(exp_path_nc)

        for _t in range(0, 3):
            _delta.save_grids_and_figs()
            _delta._save_iter += 1

        # close the file and connect
        _delta.output_netcdf.close()
        ds = netCDF4.Dataset(exp_path_nc, "r", format="NETCDF4")

        # assertions
        assert not ('eta' in ds.variables)
        assert ds['meta']['H_SL'].shape[0] == 4  # init + 3
        assert ds['meta']['L0'][:] == 3
Esempio n. 2
0
    def test_save_one_fig_one_grid(self, tmp_path):
        p = utilities.yaml_from_dict(tmp_path, 'input.yaml', {
            'save_dt': 1,
            'save_eta_grids': True,
            'save_discharge_figs': True
        })
        _delta = DeltaModel(input_file=p)

        # mock the log_info
        _delta.log_info = mock.MagicMock()

        # mock the actual output routines
        _delta.make_figure = mock.MagicMock()
        _delta.save_figure = mock.MagicMock()
        _delta.save_grids = mock.MagicMock()

        assert (_delta._save_eta_grids is True)
        assert (_delta._save_metadata is True)

        # check for the netcdf file
        exp_path_nc = os.path.join(tmp_path / 'out_dir',
                                   'pyDeltaRCM_output.nc')
        assert os.path.isfile(exp_path_nc)
        nc_size_before = os.path.getsize(exp_path_nc)
        assert nc_size_before > 0  # saved once already / inited

        # update a couple times, should increase on each save
        for _t in range(0, 5):
            _delta.save_grids_and_figs()
            _delta._save_iter += 1

        _delta.make_figure.call_count == 5
        _delta.save_figure.call_count == 5
        _delta.save_grids.call_count == 5
Esempio n. 3
0
    def test_save_one_fig_no_grids(self, tmp_path):
        p = utilities.yaml_from_dict(tmp_path, 'input.yaml', {
            'save_dt': 1,
            'save_eta_figs': True
        })
        _delta = DeltaModel(input_file=p)

        # mock the log_info
        _delta.log_info = mock.MagicMock()

        # mock the actual output routines
        _delta.make_figure = mock.MagicMock()
        _delta.save_figure = mock.MagicMock()
        _delta.save_grids = mock.MagicMock()

        # check nothing created at the start
        _delta.make_figure.call_count == 0
        _delta.save_figure.call_count == 0
        _delta.save_grids.call_count == 0

        assert (len(_delta._save_fig_list) > 0)
        assert (_delta._save_eta_figs is True)

        # update the delta a few times
        for _t in range(0, 5):
            _delta.save_grids_and_figs()
            _delta._save_iter += 1

        _delta.make_figure.call_count == 5
        _delta.save_figure.call_count == 5
        _delta.save_grids.call_count == 0
Esempio n. 4
0
    def test_save_no_figs_no_grids(self, tmp_path):
        p = utilities.yaml_from_dict(tmp_path, 'input.yaml', {'save_dt': 1})
        _delta = DeltaModel(input_file=p)

        # mock the log_info
        _delta.log_info = mock.MagicMock()

        # mock the actual output routines
        _delta.make_figure = mock.MagicMock()
        _delta.save_figure = mock.MagicMock()
        _delta.save_grids = mock.MagicMock()

        # check nothing created at the start
        _delta.make_figure.call_count == 0
        _delta.save_figure.call_count == 0
        _delta.save_grids.call_count == 0

        # update the delta a few times
        for _t in range(0, 4):
            _delta._time = (_t * _delta._dt)
            _delta.save_grids_and_figs()

        # check nothing after a number of iterations, greater than dt
        assert _delta._time > _delta.save_dt
        _delta.make_figure.call_count == 0
        _delta.save_figure.call_count == 0
        _delta.save_grids.call_count == 0
Esempio n. 5
0
    def test_save_all_figures_no_grids(self, tmp_path):
        p = utilities.yaml_from_dict(
            tmp_path, 'input.yaml', {
                'save_dt': 1,
                'save_eta_figs': True,
                'save_discharge_figs': True,
                'save_velocity_figs': True,
                'save_stage_figs': True,
                'save_depth_figs': True,
                'save_sedflux_figs': True
            })
        _delta = DeltaModel(input_file=p)

        # mock the log_info
        _delta.log_info = mock.MagicMock()

        # mock the actual output routines
        _delta.make_figure = mock.MagicMock()
        _delta.save_figure = mock.MagicMock()
        _delta.save_grids = mock.MagicMock()

        exp_path_nc = os.path.join(tmp_path / 'out_dir',
                                   'pyDeltaRCM_output.nc')
        assert not os.path.isfile(exp_path_nc)

        for _t in range(0, 5):
            _delta.save_grids_and_figs()
            _delta._save_iter += 1
            _delta._time += _delta._dt

        # assertions
        _delta.make_figure.assert_any_call('eta', 0)
        _delta.make_figure.assert_any_call('stage', 0)
        _delta.make_figure.assert_any_call('depth', 0)
        _delta.make_figure.assert_any_call('qw', 0)
        _delta.make_figure.assert_any_call('uw', 0)
        _delta.make_figure.assert_any_call('qs', 0)
        for _i in range(1, 5):
            _delta.make_figure.assert_any_call('eta', _delta._dt * _i)
        _delta.make_figure.call_count == 5 * 6
        _delta.save_figure.call_count == 5 * 6
        _delta.save_grids.call_count == 0
Esempio n. 6
0
    def test_save_one_grid_metadata_by_default(self, tmp_path):
        p = utilities.yaml_from_dict(
            tmp_path, 'input.yaml', {
                'save_dt': 1,
                'save_metadata': False,
                'save_eta_grids': True,
                'C0_percent': 0.2
            })
        _delta = DeltaModel(input_file=p)

        # mock the log_info
        _delta.log_info = mock.MagicMock()

        # mock the actual output routines
        _delta.make_figure = mock.MagicMock()
        _delta.save_figure = mock.MagicMock()
        _delta.save_grids = mock.MagicMock()

        exp_path_nc = os.path.join(tmp_path / 'out_dir',
                                   'pyDeltaRCM_output.nc')
        assert os.path.isfile(exp_path_nc)

        for _t in range(0, 6):
            _delta.save_grids_and_figs()
            _delta._save_iter += 1

        # close the file and connect
        _delta.output_netcdf.close()
        ds = netCDF4.Dataset(exp_path_nc, "r", format="NETCDF4")

        # assertions
        _arr = ds.variables['eta']
        assert _arr.shape[1] == _delta.eta.shape[0]
        assert _arr.shape[2] == _delta.eta.shape[1]
        assert ('meta' in ds.groups)  # if any grids, save meta too
        assert ds.groups['meta']['H_SL'].shape[0] == _arr.shape[0]
        assert np.all(ds.groups['meta']['C0_percent'][:] == 0.2)
        assert np.all(ds.groups['meta']['f_bedload'][:] == 0.5)