예제 #1
0
def test_ncores_candidate(candidate_setup, output_dir, job_ncores, scheduler,
                          capsys):
    with capsys.disabled():
        print(
            "\nQuestion: The candidate restarts from a 1 core run match restarts from standard run?",
            end='')

    candidate_run_file = output_dir / 'run_candidate' / 'WrfHydroRun.pkl'
    if candidate_run_file.is_file() is False:
        pytest.skip('Candidate run object not found, skipping test.')

    # Load initial run model object
    candidate_run_expected = pickle.load(open(candidate_run_file, "rb"))
    # Set run directory
    run_dir = output_dir.joinpath('ncores_candidate')

    candidate_ncores_job = job_ncores
    candidate_ncores_job.scheduler = scheduler

    # Run
    candidate_ncores_run = wrfhydropy.WrfHydroRun(
        wrf_hydro_setup=candidate_setup,
        run_dir=run_dir,
        jobs=candidate_ncores_job)
    check_run_dir = candidate_ncores_run.run_jobs()

    if scheduler is not None:
        candidate_ncores_run = wrfhydropy.job_tools.restore_completed_scheduled_job(
            check_run_dir)

    #Check against initial run
    ncores_restart_diffs = wrfhydropy.RestartDiffs(candidate_ncores_run,
                                                   candidate_run_expected)

    ## Check hydro restarts
    for diff in ncores_restart_diffs.hydro:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff == None, "Candidate hydro restart files do not match when run with different number of cores"

    ## Check lsm restarts
    for diff in ncores_restart_diffs.lsm:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff == None, "Candidate lsm restart files do not match when run with different number of cores"

    ## Check nudging restarts
    for diff in ncores_restart_diffs.nudging:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff == None, "Candidate nudging restart files do not match when run with different number of cores"
def test_ncores_candidate(candidate_sim, output_dir, capsys):
    with capsys.disabled():
        print(
            "Question: The candidate restarts from a 1 core run match restarts from standard run?"
        )

    candidate_run_file = output_dir / 'run_candidate' / 'WrfHydroRun.pkl'
    if candidate_run_file.is_file() is False:
        pytest.skip('Candidate run object not found, skipping test')

    # Load initial run model object
    candidate_run_expected = pickle.load(open(candidate_run_file, "rb"))
    # Set simulation directory
    simulation_dir = output_dir.joinpath('ncores_candidate')

    # Run the simulation
    candidate_ncores_run = candidate_sim.run(simulation_dir=simulation_dir,
                                             num_cores=1,
                                             mode='w')

    #Check against initial run
    ncores_restart_diffs = wrfhydropy.RestartDiffs(candidate_ncores_run,
                                                   candidate_run_expected)

    ## Check hydro restarts
    for diff in ncores_restart_diffs.hydro:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff == None, "Candidate hydro restart files do not match when run with different number of cores"

    ## Check lsm restarts
    for diff in ncores_restart_diffs.lsm:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff == None, "Candidate lsm restart files do not match when run with different number of cores"

    ## Check nudging restarts
    for diff in ncores_restart_diffs.nudging:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff == None, "Candidate nudging restart files do not match when run with different number of cores"
예제 #3
0
def test_regression(output_dir, capsys):
    with capsys.disabled():
        print(
            "\nQuestion: The candidate standard run restarts match the reference standard restarts?",
            end="")

    # Check for existence of run objects
    candidate_run_file = output_dir / 'run_candidate' / 'WrfHydroRun.pkl'
    reference_run_file = output_dir / 'run_reference' / 'WrfHydroRun.pkl'

    if candidate_run_file.is_file() is False:
        pytest.skip('Candidate run object not found, skipping test')
    if reference_run_file.is_file() is False:
        pytest.skip('Reference run object not found, skipping test')

    # Load run objects
    candidate_run_expected = pickle.load(open(candidate_run_file, "rb"))
    reference_run_expected = pickle.load(open(reference_run_file, "rb"))

    #Check regression
    regression_diffs = wrfhydropy.RestartDiffs(candidate_run_expected,
                                               reference_run_expected)

    ## Check hydro restarts
    for diff in regression_diffs.hydro:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff == None, "Candidate hydro restart files do not regress on reference restart files"

    ## Check lsm restarts
    for diff in regression_diffs.lsm:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff == None, "Candidate lsm restart files do not regress on reference restart files"

    ## Check nudging restarts
    for diff in regression_diffs.nudging:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff == None, "Candidate nudging restart files do not regress on reference restart files"
예제 #4
0
def test_perfrestart_candidate(candidate_setup, output_dir, job_default,
                               scheduler, capsys):
    with capsys.disabled():
        print(
            "\nQuestion: The candidate restarts from a restart run match the restarts from standard run?",
            end='')

    candidate_run_file = output_dir / 'run_candidate' / 'WrfHydroRun.pkl'
    if candidate_run_file.is_file() is False:
        pytest.skip('Candidate run object not found, skipping test')

    # Load initial run model object
    candidate_run_expected = pickle.load(
        open(output_dir / 'run_candidate' / 'WrfHydroRun.pkl', "rb"))

    #Make deep copy since changing namelist optoins
    perfrestart_setup = copy.deepcopy(candidate_setup)

    # Set run directory
    run_dir = output_dir / 'restart_candidate'

    # Establish the run (run after setting external files)
    candidate_perfrestart_job = job_default
    # TODO(JLM): edit scheduler names
    candidate_perfrestart_job.scheduler = scheduler

    # Add the jobs after determining the restart time.
    candidate_perfrestart_run = wrfhydropy.WrfHydroRun(
        wrf_hydro_setup=perfrestart_setup, run_dir=run_dir, mode='r')

    # Symlink restarts files to new directory and modify namelistrestart files
    # Hydro
    hydro_rst = candidate_run_expected.restart_hydro[0]
    new_hydro_rst_path = run_dir.joinpath(hydro_rst.name)
    new_hydro_rst_path.unlink()
    new_hydro_rst_path.symlink_to(hydro_rst)

    perfrestart_setup.hydro_namelist['hydro_nlist'].update(
        {'restart_file': str(new_hydro_rst_path)})

    # LSM
    lsm_rst = candidate_run_expected.restart_lsm[0]
    new_lsm_rst_path = run_dir.joinpath(lsm_rst.name)
    new_lsm_rst_path.unlink()
    new_lsm_rst_path.symlink_to(lsm_rst)

    perfrestart_setup.namelist_hrldas['noahlsm_offline'].update(
        {'restart_filename_requested': str(run_dir.joinpath(lsm_rst.name))})

    # Nudging
    if candidate_run_expected.restart_nudging is not None and \
       len(candidate_run_expected.restart_nudging) > 0:
        nudging_rst = candidate_run_expected.restart_nudging[0]
        new_nudging_rst_path = run_dir.joinpath(nudging_rst.name)
        new_nudging_rst_path.unlink()
        new_nudging_rst_path.symlink_to(nudging_rst)

        perfrestart_setup.hydro_namelist['nudging_nlist'].update(
            {'nudginglastobsfile': str(run_dir.joinpath(nudging_rst.name))})

    # Setup the restart in the run.
    orig_start_time, orig_end_time = wrfhydropy.job_tools.solve_model_start_end_times(
        None, None, candidate_perfrestart_run.setup)

    restart_dt = hydro_rst.open()
    restart_time = dt.datetime.strptime(restart_dt.Restart_Time,
                                        '%Y-%m-%d_%H:%M:%S')

    candidate_perfrestart_job.model_start_time = restart_time
    candidate_perfrestart_job.model_end_time = orig_end_time

    # Run
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        candidate_perfrestart_run.add_jobs(candidate_perfrestart_job)
        check_run_dir = candidate_perfrestart_run.run_jobs()
        if scheduler is not None:
            candidate_perfrestart_run = \
                wrfhydropy.job_tools.restore_completed_scheduled_job(check_run_dir)

    #Check against initial run
    perfstart_restart_diffs = wrfhydropy.RestartDiffs(
        candidate_perfrestart_run, candidate_run_expected)
    ## Check hydro restarts
    for diff in perfstart_restart_diffs.hydro:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff is None, \
            "Candidate hydro restart files do not match when starting from a restart"

    ## Check lsm restarts
    for diff in perfstart_restart_diffs.lsm:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff is None, \
            "Candidate lsm restart files do not match when starting from a restart"

    ## Check nudging restarts
    for diff in perfstart_restart_diffs.nudging:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff is None, \
            "Candidate nudging restart files do not match when starting from a restart"
def test_perfrestart_candidate(candidate_sim, output_dir, capsys):
    with capsys.disabled():
        print(
            "Question: The candidate restarts from a restart run match the restarts from standard run?"
        )

    candidate_run_file = output_dir / 'run_candidate' / 'WrfHydroRun.pkl'
    if candidate_run_file.is_file() is False:
        pytest.skip('Candidate run object not found, skipping test')

    # Load initial run model object
    candidate_run_expected = pickle.load(
        open(output_dir / 'run_candidate' / 'WrfHydroRun.pkl', "rb"))

    #Make deep copy since changing namelist optoins
    perfrestart_sim = copy.deepcopy(candidate_sim)

    # Set simulation directory
    simulation_dir = output_dir / 'restart_candidate'

    #Make directory so that symlinks can be placed
    if simulation_dir.is_dir() is True:
        shutil.rmtree(str(simulation_dir))
    simulation_dir.mkdir(parents=True)

    # Symlink restarts files to new directory and modify namelistrestart files

    # Hydro
    hydro_rst = candidate_run_expected.restart_hydro[0]
    new_hydro_rst_path = simulation_dir.joinpath(hydro_rst.name)
    new_hydro_rst_path.symlink_to(hydro_rst)

    perfrestart_sim.hydro_namelist['hydro_nlist'].update(
        {'restart_file': str(new_hydro_rst_path)})

    # LSM
    lsm_rst = candidate_run_expected.restart_lsm[0]
    new_lsm_rst_path = simulation_dir.joinpath(lsm_rst.name)
    new_lsm_rst_path.symlink_to(lsm_rst)

    perfrestart_sim.namelist_hrldas['noahlsm_offline'].update({
        'restart_filename_requested':
        str(simulation_dir.joinpath(lsm_rst.name))
    })

    # Nudging
    if len(candidate_run_expected.restart_nudging) > 0:
        nudging_rst = candidate_run_expected.restart_nudging[0]
        new_nudging_rst_path = simulation_dir.joinpath(nudging_rst.name)
        new_nudging_rst_path.symlink_to(nudging_rst)

        perfrestart_sim.hydro_namelist['nudging_nlist'].update({
            'nudginglastobsfile':
            str(simulation_dir.joinpath(nudging_rst.name))
        })

    #Move simulation start time to restart time in hydro restart file
    start_dt = hydro_rst.open()
    start_dt = dt.datetime.strptime(start_dt.Restart_Time, '%Y-%m-%d_%H:%M:%S')
    perfrestart_sim.namelist_hrldas['noahlsm_offline'].update({
        'start_year':
        start_dt.year,
        'start_month':
        start_dt.month,
        'start_day':
        start_dt.day,
        'start_hour':
        start_dt.hour,
        'start_min':
        start_dt.minute
    })

    #Adjust duration to be shorter by restart time delta in days
    hydro_rst_dt = perfrestart_sim.hydro_namelist['hydro_nlist']['rst_dt']
    previous_duration = candidate_run_expected.simulation.namelist_hrldas[
        'noahlsm_offline']['kday']
    new_duration = int(previous_duration - hydro_rst_dt / 60 / 24)
    perfrestart_sim.namelist_hrldas['noahlsm_offline'].update(
        {'kday': new_duration})

    # Run the simulation
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        candidate_perfrestart_run = perfrestart_sim.run(
            simulation_dir=simulation_dir, num_cores=2, mode='a')

    #Check against initial run
    perfstart_restart_diffs = wrfhydropy.RestartDiffs(
        candidate_perfrestart_run, candidate_run_expected)
    ## Check hydro restarts
    for diff in perfstart_restart_diffs.hydro:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff == None, "Candidate hydro restart files do not match when starting from a restart"

    ## Check lsm restarts
    for diff in perfstart_restart_diffs.lsm:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff == None, "Candidate lsm restart files do not match when starting from a restart"

    ## Check nudging restarts
    for diff in perfstart_restart_diffs.nudging:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff == None, "Candidate nudging restart files do not match when starting from a restart"
예제 #6
0
def test_channel_only_matches_full(candidate_channel_only_setup, output_dir,
                                   capsys):

    if candidate_channel_only_setup is None:
        pytest.skip("Unsupported configuration for channel-only.")

    with capsys.disabled():
        print(
            "\nQuestion: The candidate channel-only run restarts and CHRTOUT files match "
            + "those of the full model?",
            end="")

    # Check for existence of run objects
    candidate_run_file = output_dir / 'run_candidate' / 'WrfHydroRun.pkl'
    candidate_channel_only_run_file = output_dir / 'run_candidate_channel_only' / 'WrfHydroRun.pkl'

    if candidate_run_file.is_file() is False:
        pytest.skip('Candidate run object not found, skipping test')
    if candidate_channel_only_run_file.is_file() is False:
        pytest.skip(
            'candidate_channel_only run object not found, skipping test')

    # Load run objects
    candidate_run_expected = pickle.load(open(candidate_run_file, "rb"))
    candidate_channel_only_run_expected = pickle.load(
        open(candidate_channel_only_run_file, "rb"))

    exclude_vars = [
        'stc1', 'smc1', 'sh2ox1', 'stc2', 'smc2', 'sh2ox2', 'stc3', 'smc3',
        'sh2ox3', 'stc4', 'smc4', 'sh2ox4', 'infxsrt', 'soldrain', 'sfcheadrt',
        'QBDRYRT', 'infxswgt', 'sfcheadsubrt', 'sh2owgt1', 'sh2owgt2',
        'sh2owgt3', 'sh2owgt4', 'qstrmvolrt', 'hlink', 'lake_inflort'
    ]

    # We still compare these:
    # 'qlink1'
    # 'qlink2'
    # 'resht'
    # 'qlakeo'
    # 'z_gwsubbas'

    # Dont compare metadata in this case, there are different dimensions
    # in the files that always result in a return code of 1.
    nccmp_options = ['--data', '--force', '--quiet']  #, '--metadata']

    # Check diffs
    regression_diffs = wrfhydropy.RestartDiffs(
        candidate_run_expected,
        candidate_channel_only_run_expected,
        nccmp_options=nccmp_options,
        exclude_vars=exclude_vars)

    # Check hydro restarts
    for diff in regression_diffs.hydro:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff is None, \
            "Candidate channel-only hydro restart files do not match full restart files"

    # Check nudging restarts
    for diff in regression_diffs.nudging:
        if diff is not None:
            with capsys.disabled():
                print(diff)
        assert diff is None, \
            "Candidate channel-only nudging restart files do not match full restart files"