Пример #1
0
def test_clmin_round_trip(clm):
    path = Path(get_absolute_path('drv_clmin.dat'))
    if path.exists():
        path.unlink()

    CLMExporter(clm).write_input()

    new_clm = Run('clm', __file__)
    CLMImporter(new_clm).input_file(path)
    verify_clmin_data(new_clm)
Пример #2
0
def test_vegp_round_trip(clm):
    path = Path(get_absolute_path('drv_vegp.dat'))
    if path.exists():
        path.unlink()

    CLMExporter(clm).write_parameters()

    new_clm = Run('clm', __file__)
    veg_params = new_clm.Solver.CLM.Vegetation.Parameters

    # Set the land cover items
    veg_params.LandNames = veg_params.LandNames

    CLMImporter(new_clm).parameters_file(path)
    verify_vegp_data(new_clm)
Пример #3
0
def test_overwrite_failure(clm):
    # First, copy the files over
    copy_driver_files()

    # Import the files
    CLMImporter(clm) \
        .set_default_land_names() \
        .files()

    exporter = CLMExporter(clm)

    # These should all raise exceptions
    should_raise = [
        exporter.write,
        exporter.write_input,
        exporter.write_map,
        exporter.write_parameters,
    ]
    for func in should_raise:
        assert_exception(func, NotOverwritableException)

    # Cleanup
    remove_driver_files()
Пример #4
0
def test_fewer_land_names(clm):
    vegm_file = Path(get_absolute_path('drv_vegm.dat'))
    vegp_file = Path(get_absolute_path('drv_vegp.dat'))
    for path in (vegm_file, vegp_file):
        if path.exists():
            path.unlink()

    CLMExporter(clm).write_map().write_parameters()

    new_clm = Run('clm', __file__)
    veg_params = new_clm.Solver.CLM.Vegetation.Parameters
    veg_map = new_clm.Solver.CLM.Vegetation.Map

    # Set only two land cover items
    veg_params.LandNames = 'forest_en forest_eb'

    CLMImporter(new_clm) \
        .map_file(vegm_file) \
        .parameters_file(vegp_file)

    assert veg_params.LandNames == ['forest_en', 'forest_eb']
    assert 'water' not in veg_params.keys()
    assert 'water' not in veg_map.keys()

    veg_map.LandFrac.forest_en.Value = 0.6
    veg_map.LandFrac.forest_eb.Value = 0.4
    veg_map.LandFrac.forest_en.Type = 'Constant'
    veg_map.LandFrac.forest_eb.Type = 'Constant'

    new_clm.ComputationalGrid.NX = 5
    new_clm.ComputationalGrid.NY = 5

    CLMExporter(new_clm).write_map().write_parameters()

    vegm_data = read_clm(vegm_file, type='vegm')
    vegp_data = read_clm(vegp_file, type='vegp')

    # These should be 0.6 and 0.4
    shape = vegm_data.shape[:2]
    assert np.array_equal(vegm_data[:, :, 5], np.full(shape, 0.6))
    assert np.array_equal(vegm_data[:, :, 6], np.full(shape, 0.4))

    # All of the vegm data after the 2 land types should be zero
    last_land_ind = 6
    zeros = np.zeros(shape)
    for i in range(last_land_ind + 1, vegm_data.shape[2]):
        assert np.array_equal(vegm_data[:, :, i], zeros)

    # All of the vegp data after the second land type should be equal
    # to the default
    default_vegp = CLMExporter(new_clm)._default_vegp_values
    for key, val in vegp_data.items():
        for i in range(2, len(val)):
            assert val[i] == default_vegp[key]

    # Make sure at least one of the other values is correct
    key = 'z0m'
    ind = 1
    val = 2.2
    assert vegp_data[key][ind] != default_vegp[key]
    assert vegp_data[key][ind] == val
Пример #5
0
def test_overwrite_success(clm):
    # First, copy the files over
    copy_driver_files()

    # Import the files
    CLMImporter(clm) \
        .set_default_land_names() \
        .files()

    exporter = CLMExporter(clm)

    # Allow overwriting to occur
    clm_solver = clm.Solver.CLM
    clm_solver.OverwriteDrvClmin = True
    clm_solver.OverwriteDrvVegm = True
    clm_solver.OverwriteDrvVegp = True

    # These should all run
    should_run = [
        exporter.write,
        exporter.write_input,
        exporter.write_map,
        exporter.write_parameters,
    ]
    for func in should_run:
        func()

    # Turn these off. We can still overwrite since the files were generated.
    clm_solver.OverwriteDrvClmin = False
    clm_solver.OverwriteDrvVegm = False
    clm_solver.OverwriteDrvVegp = False

    for func in should_run:
        func()

    # Re-copy the driver files over
    copy_driver_files()

    # These should all raise exceptions now
    for func in should_run:
        assert_exception(func, NotOverwritableException)

    # Set the argparse argument to be True
    clm._process_args_.overwrite_clm_driver_files = True
    update_run_from_args(clm, clm._process_args_)

    # These should now be true
    assert clm_solver.OverwriteDrvClmin is True
    assert clm_solver.OverwriteDrvVegm is True
    assert clm_solver.OverwriteDrvVegp is True

    # And the functions should run again
    for func in should_run:
        func()

    # Turn these off to clean up.
    clm._process_args_.overwrite_clm_driver_files = False
    clm_solver.OverwriteDrvClmin = False
    clm_solver.OverwriteDrvVegm = False
    clm_solver.OverwriteDrvVegp = False

    # Cleanup
    remove_driver_files()
Пример #6
0
def test_write_allowed(clm):
    # First, copy the files over
    copy_driver_files()

    originally_written_times = driver_file_written_times()

    def files_unchanged():
        return originally_written_times == driver_file_written_times()

    # Import the files
    CLMImporter(clm) \
        .set_default_land_names() \
        .files()

    exporter = CLMExporter(clm)

    warning_func = '_print_not_written_warning'
    # This should not print any warnings even though it didn't write,
    # because we did not change any settings.
    assert was_called(exporter, warning_func, exporter.write_allowed) is False

    # Prove the files were not written to
    assert files_unchanged()

    # Change some settings. Warnings should be printed in every case.
    clm_solver = clm.Solver.CLM
    clm_solver.Input.Timing.StartYear = '2020'
    assert was_called(exporter, warning_func, exporter.write_allowed) is True

    # Prove the files were not written to
    assert files_unchanged()

    # Re-import the files to erase history
    CLMImporter(clm) \
        .set_default_land_names() \
        .files()

    clm_solver.Vegetation.Map.Sand.Type = 'Constant'
    clm_solver.Vegetation.Map.Sand.Value = 0.3
    assert was_called(exporter, warning_func, exporter.write_allowed) is True

    # Prove the files were not written to
    assert files_unchanged()

    # Re-import the files to erase history
    CLMImporter(clm) \
        .set_default_land_names() \
        .files()

    clm_solver.Vegetation.Parameters.forest_en.WaterType = 5
    assert was_called(exporter, warning_func, exporter.write_allowed) is True

    # Prove the files were not written to
    assert files_unchanged()

    # Re-import the files to erase history
    CLMImporter(clm) \
        .set_default_land_names() \
        .files()
    # Should not be called this time
    assert was_called(exporter, warning_func, exporter.write_allowed) is False

    # Prove the files were not written to
    assert files_unchanged()

    # Cleanup
    remove_driver_files()
Пример #7
0
write_array(get_absolute_path(file_name), forest_eb_mat)

clm.Solver.CLM.Vegetation.Map.LandFrac.forest_eb.Type = 'PFBFile'
clm.Solver.CLM.Vegetation.Map.LandFrac.forest_eb.FileName = file_name

clm.Solver.CLM.Vegetation.Map.LandFrac.forest_dn.Type = 'Constant'
clm.Solver.CLM.Vegetation.Map.LandFrac.forest_dn.Value = 0.5

clm.Solver.CLM.Vegetation.Map.LandFrac.forest_db.Type = 'Constant'
clm.Solver.CLM.Vegetation.Map.LandFrac.forest_db.Value = 0.0

#---------------------------------------------------------
# Testing clm data reader for veg mapping
#---------------------------------------------------------

# Reading drv_vegm.dat into 3D array
vegm_data = read_clm('../../tcl/clm/drv_vegm.dat', type='vegm')
if not vegm_data[1, 1, 14] == 1:
    sys.exit(1)

# ---------------------------------------------------------
# Testing clm data writers
# ---------------------------------------------------------

path = get_absolute_path('drv_vegm.dat')
if Path(path).exists():
    Path(path).unlink()

CLMExporter(clm) \
    .write_map()
Пример #8
0
# Testing clm data writers
# ---------------------------------------------------------

# Remove any leftover driver files...
paths = [
    'drv_clmin.dat',
    'drv_vegm.dat',
    'drv_vegp.dat',
]
for path in paths:
    path = Path(path)
    if path.exists():
        path.unlink()

CLMExporter(clm) \
    .write_input() \
    .write_map(vegm_data) \
    .write_parameters(vegp_data)

# Re-read the exported files back in to ensure they are the same
new_clmin_data = read_clm('drv_clmin.dat', type='clmin')
new_vegm_data = read_clm('drv_vegm.dat', type='vegm')
new_vegp_data = read_clm('drv_vegp.dat', type='vegp')

# FIXME: because some CLM keys are not yet registered in the pf-keys yaml
# files, the new clm data does not match.
# assert new_clmin_data == clmin_data
assert np.array_equal(new_vegm_data, vegm_data)
assert new_vegp_data == vegp_data

clm.Solver.CLM.Vegetation.Parameters.LandNames = 'forest_en forest_eb forest_dn forest_db'