コード例 #1
0
def test_variable_rename():
    """Tests to make sure that variable renaming works"""
    loc = data_locations['binary']
    data_files = [os.path.join(loc, f) for f in os.listdir(loc)]
    out_dir = '.'
    params = {
        'start': dates['binary'][0],
        'stop': dates['binary'][1],
        'forcing_fmt': 'binary',
        'domain_fmt': 'netcdf',
        'state_fmt': 'netcdf',
        'domain': './metsim/data/stehekin.nc',
        'state': './metsim/data/state_vic.nc',
        'forcing': data_files,
        'method': 'mtclim',
        'scheduler': 'threading',
        'time_step': "60",
        'out_dir': out_dir,
        'out_state': os.path.join(out_dir, 'state.nc'),
        'out_vars': {
            'prec': {
                'out_name': 'pptrate'
            },
            'shortwave': {
                'out_name': 'SWRadAtm'
            }
        },
        'forcing_vars': in_vars_section['binary'],
        'domain_vars': domain_section['binary']
    }
    ms = MetSim(params)
    ms.run()
    ds = ms.open_output()
    assert 'pptrate' in ds.variables
    assert 'SWRadAtm' in ds.variables
コード例 #2
0
def test_yaml_config():
    filename = './examples/example_yaml.yaml'
    conf = io.read_config(DummyOpts(filename))
    out_dir = tempfile.mkdtemp('results')
    conf['out_dir'] = out_dir
    ms = MetSim(conf)
    ms.run()
    assert ms.open_output() is not None
コード例 #3
0
def test_examples(kind):
    filename = './examples/example_{kind}.conf'.format(kind=kind)
    conf = io.read_config(DummyOpts(filename))
    out_dir = tempfile.mkdtemp('results')
    conf['out_dir'] = out_dir
    ms = MetSim(conf)
    ms.run()
    assert ms.open_output() is not None
コード例 #4
0
def test_time_offset():
    """Tests to make sure that the time_offset option works"""
    loc = data_locations['binary']
    data_files = [os.path.join(loc, f) for f in os.listdir(loc)]
    out_vars = [
        'prec', 'temp', 'shortwave', 'longwave', 'vapor_pressure', 'wind',
        'rel_humid', 'spec_humid', 'air_pressure'
    ]
    out_dir = '.'
    params = {
        'start': dates['binary'][0],
        'stop': dates['binary'][1],
        'forcing_fmt': 'binary',
        'domain_fmt': 'netcdf',
        'state_fmt': 'netcdf',
        'domain': './metsim/data/stehekin.nc',
        'state': './metsim/data/state_vic.nc',
        'forcing': data_files,
        'method': 'mtclim',
        'scheduler': 'threading',
        'time_step': "60",
        'out_dir': out_dir,
        'out_state': os.path.join(out_dir, 'state.nc'),
        'out_vars': {n: metsim.metsim.available_outputs[n]
                     for n in out_vars},
        'forcing_vars': in_vars_section['binary'],
        'domain_vars': domain_section['binary']
    }
    params1 = dict()
    params1.update(params)
    params2 = dict()
    params2.update(params)
    params1['period_ending'] = False
    params2['period_ending'] = True

    # Set up the MetSim object
    ms1 = MetSim(params1)
    ms2 = MetSim(params2)
    assert ms1._times[1:] == ms2._times[:-1]
コード例 #5
0
ファイル: ms.py プロジェクト: shulele/MetSim
def main():
    """Runs MetSim"""
    from metsim.metsim import MetSim
    setup = init(parse(sys.argv[1:]))
    ms = MetSim(setup)
    if ms.params['nprocs'] > 1:
        ms.launch()
    else:
        ms.run()
コード例 #6
0
def test_setup(test_params, domain_file):
    """Tests the setup of the MetSim object"""
    in_fmt = test_params['forcing_fmt']
    loc = data_locations[in_fmt]

    # Get the files and make sure the right amount exist
    if in_fmt == 'binary' or in_fmt == 'ascii':
        data_files = [os.path.join(loc, f) for f in os.listdir(loc)]
        assert len(data_files) == 16
    else:
        data_files = loc
        assert data_files == './metsim/data/test.nc'
    test_params['forcing'] = data_files

    # Test construction
    ms = MetSim(test_params)
    return ms
コード例 #7
0
def test_coordinate_dimension_matchup():
    """
    This test checks that MetSim correctely adds a coordinate
    if an input dataset is missing coordinate variables for the
    chunked dimensions.
    """
    var_rename = OrderedDict(latitude='lat',
                             longitude='lon',
                             mask='mask',
                             elevation='elev',
                             pptrate='prec',
                             maxtemp='t_max',
                             mintemp='t_min')
    filename = './examples/example_dimtest.conf'
    conf = io.read_config(DummyOpts(filename))
    conf['out_dir'] = tempfile.mkdtemp('results')
    ms = MetSim(conf)
    ds = xr.open_dataset('./metsim/data/dim_test.nc')
    assert 'hru' not in ds.coords
    assert 'hru' in ms.met_data.coords
コード例 #8
0
ファイル: test_metsim.py プロジェクト: Python3pkg/MetSim
def test_disaggregation_values():
    """Tests to make sure values are being generated correctly"""
    # Set parameters
    loc = data_locations['binary']
    data_files = [os.path.join(loc, f) for f in os.listdir(loc)]
    out_vars = [
        'prec', 'temp', 'shortwave', 'longwave', 'vapor_pressure', 'wind',
        'rel_humid'
    ]
    params = {
        'start': dates['binary'][0],
        'stop': dates['binary'][1],
        'in_format': 'binary',
        'out_format': 'ascii',
        'domain': './tests/data/stehekin.nc',
        'forcings': data_files,
        'method': 'mtclim',
        'time_step': "60",
        't_max_lr': 0.00065,
        't_min_lr': 0.00065,
        'out_dir': "./tmp",
        'out_vars': out_vars,
        'in_vars': in_vars_section['binary'],
        'domain_vars': domain_section['binary']
    }
    # The location we will test against
    loc = (1, 4)

    # Set up the MetSim object
    ms = MetSim(params)
    ms.load(params['forcings'])

    # Run MetSim and load in the validated data
    ms.run([loc])
    out = ms.output.isel(lat=loc[0], lon=loc[1]).to_dataframe()[:-1][out_vars]
    good = pd.read_table('./tests/data/validated_48.3125_-120.5625',
                         names=out_vars)
    good.index = out.index

    # Make sure the data comes out right
    assert type(out) is pd.DataFrame
    for var in ms.params['out_vars']:
        # Check to make sure each variable has normalized
        # rmse of less than 0.02
        h = max([good[var].max(), out[var].max()])
        l = min([good[var].min(), out[var].min()])
        nrmse = np.sqrt((good[var] - out[var]).pow(2).mean()) / (h - l)
        assert nrmse < 0.02
コード例 #9
0
def test_disaggregation_values():
    """Tests to make sure values are being generated correctly"""
    # Set parameters
    loc = data_locations['binary']
    data_files = [os.path.join(loc, f) for f in os.listdir(loc)]
    out_vars = [
        'prec', 'temp', 'shortwave', 'longwave', 'vapor_pressure', 'wind',
        'rel_humid', 'spec_humid', 'air_pressure'
    ]
    out_dir = tempfile.mkdtemp('results')
    params = {
        'start': dates['binary'][0],
        'stop': dates['binary'][1],
        'forcing_fmt': 'binary',
        'domain_fmt': 'netcdf',
        'state_fmt': 'netcdf',
        'domain': './metsim/data/stehekin.nc',
        'state': './metsim/data/state_vic.nc',
        'forcing': data_files,
        'method': 'mtclim',
        'scheduler': 'threading',
        'time_step': "60",
        'out_dir': out_dir,
        'out_state': os.path.join(out_dir, 'state.nc'),
        'out_vars': {n: metsim.metsim.available_outputs[n]
                     for n in out_vars},
        'forcing_vars': in_vars_section['binary'],
        'domain_vars': domain_section['binary']
    }
    # The location we will test against
    loc = (1, 4)

    def check_data(out, good, tol=0.03):
        assert isinstance(out, pd.DataFrame)
        for var in ms.params['out_vars'].keys():
            # Check to make sure each variable has normalized
            # rmse of less than 0.02
            h = max([good[var].max(), out[var].max()])
            l = min([good[var].min(), out[var].min()])
            nrmse = np.sqrt((good[var] - out[var]).pow(2).mean()) / (h - l)
            print(var, nrmse)
            assert nrmse < tol

    # Set up the MetSim object
    ms = MetSim(params)

    # Run MetSim and load in the validated data
    ms.run()
    ds = ms.open_output()
    out = ds.isel(lat=loc[0], lon=loc[1]).to_dataframe()[out_vars]
    good = pd.read_table('./metsim/data/validated_48.3125_-120.5625',
                         names=out_vars)
    good.index = out.index

    # Make sure the data comes out right
    check_data(out, good)
    ds.close()

    # Now do 3 hourly
    params['time_step'] = '180'
    ms = MetSim(params)
    ms.run()
    ds = ms.open_output()
    out = ds.isel(lat=loc[0], lon=loc[1]).to_dataframe()[out_vars]
    good = pd.read_table('./metsim/data/three_hourly_48.3125_-120.5625',
                         names=out_vars)
    good.index = out.index

    # Make sure the data comes out right
    check_data(out, good, tol=0.2)
    ds.close()
コード例 #10
0
def test_unit_conversion():
    """Tests to make sure that variable renaming works"""
    loc = data_locations['binary']
    data_files = [os.path.join(loc, f) for f in os.listdir(loc)]
    out_dir = '.'
    params = {
        'start': dates['binary'][0],
        'stop': dates['binary'][1],
        'forcing_fmt': 'binary',
        'domain_fmt': 'netcdf',
        'state_fmt': 'netcdf',
        'domain': './metsim/data/stehekin.nc',
        'state': './metsim/data/state_vic.nc',
        'forcing': data_files,
        'method': 'mtclim',
        'scheduler': 'threading',
        'time_step': "60",
        'out_dir': out_dir,
        'out_state': os.path.join(out_dir, 'state.nc'),
        'out_vars': {
            'prec': {
                'out_name': 'pptrate',
                'units': 'mm s-1'
            },
            'temp': {
                'out_name': 'airtemp',
                'units': 'K'
            }
        },
        'forcing_vars': in_vars_section['binary'],
        'domain_vars': domain_section['binary']
    }

    params1 = dict()
    params1.update(params)
    params2 = dict()
    params2.update(params)
    params2['out_vars'] = {
        'prec': {
            'out_name': 'pptrate',
            'units': 'mm timestep-1'
        },
        'temp': {
            'out_name': 'airtemp',
            'units': 'C'
        }
    }
    ms1 = MetSim(params1)
    ms1.run()
    ds1 = ms1.open_output().load()
    ds1.close()
    time_step = int(params['time_step'])
    sec_per_min = 60.
    tol = 1e-4

    ms2 = MetSim(params2)
    ms2.run()
    ds2 = ms2.open_output().load()

    assert np.allclose(ds1['airtemp'].mean(),
                       ds2['airtemp'].mean() + 273.15,
                       atol=tol)
    assert np.allclose(time_step * sec_per_min * ds1['pptrate'].mean(),
                       ds2['pptrate'].mean(),
                       atol=tol)
コード例 #11
0
ファイル: ms.py プロジェクト: lhmet-forks/MetSim
def main():
    """Runs MetSim"""
    from metsim.metsim import MetSim
    setup = init(parse(sys.argv[1:]))
    ms = MetSim(setup)
    ms.run()
コード例 #12
0
def test_passthrough():
    """Test to make sure passing through previously estimated
       variables doesn't alter the values from disaggregation"""
    loc = data_locations['binary']
    data_files = [os.path.join(loc, f) for f in os.listdir(loc)]
    out_dir = '.'
    params = {
        'start': dates['binary'][0],
        'stop': dates['binary'][1],
        'forcing_fmt': 'binary',
        'domain_fmt': 'netcdf',
        'state_fmt': 'netcdf',
        'domain': './metsim/data/stehekin.nc',
        'state': './metsim/data/state_vic.nc',
        'forcing': data_files,
        'method': 'mtclim',
        'scheduler': 'threading',
        'time_step': "60",
        'out_dir': out_dir,
        'out_state': os.path.join(out_dir, 'state.nc'),
        'out_vars': {
            'prec': {
                'out_name': 'prec'
            },
            'temp': {
                'out_name': 'temp'
            },
            'longwave': {
                'out_name': 'longwave'
            },
            'vapor_pressure': {
                'out_name': 'vapor_pressure'
            },
            'shortwave': {
                'out_name': 'shortwave'
            }
        },
        'forcing_vars': in_vars_section['binary'],
        'domain_vars': domain_section['binary']
    }

    # Second run will be to use mtclim and hourly disagg
    params1 = dict()
    params1.update(params)
    params1['out_prefix'] = 'mtclim'
    ms1 = MetSim(params1)
    ms1.run()
    with ms1.open_output() as ds:
        mtclim_ds = ds.load()

    # Third run will be to use passthrough and hourly disagg
    # with input data from teh first run
    params2 = dict()
    params2.update(params)
    params2['method'] = 'passthrough'
    params2['out_prefix'] = 'passthrough'
    params2['forcing_vars'] = OrderedDict(prec='prec',
                                          t_max='t_max',
                                          t_min='t_min',
                                          wind='wind',
                                          shortwave='shortwave',
                                          vapor_pressure='vapor_pressure')
    params2['forcing_fmt'] = 'netcdf'
    params2['forcing'] = './metsim/data/passthrough.nc'
    ms2 = MetSim(params2)
    ms2.run()
    with ms2.open_output() as ds:
        passthrough_ds = ds.load()

    tol = 1e-4
    assert np.allclose(passthrough_ds['shortwave'].mean(),
                       mtclim_ds['shortwave'].mean(),
                       atol=tol)
    assert np.allclose(passthrough_ds['vapor_pressure'].mean(),
                       mtclim_ds['vapor_pressure'].mean(),
                       atol=tol)
    assert np.allclose(passthrough_ds['longwave'].mean(),
                       mtclim_ds['longwave'].mean(),
                       atol=tol)