示例#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_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()
示例#5
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)
示例#6
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)