Example #1
0
def test_read_run_info():
    # logger_filename = "test_adapter.log"
    # main_logger = setup_logger('EPASWMM FEWS Python Logger', logger_filename, logging.INFO)
    run_info = read_run_info(os.getcwd() + '//run_info.xml')
    assert run_info["netcdf"] == Path(".//input//rain.nc")
    assert run_info["diagnostic_xml"] == Path(".//log//run_diagnostics.xml")
    assert run_info["properties"]["model-executable"] == Path(
        str(Path(os.getcwd()).parents[0]) + "//bin//swmm5.exe")
    assert run_info["properties"]["swmm_input_file"] == Path(
        ".//model//DonRiver.inp")
    assert run_info["time_zone"] == -5
    assert type(
        run_info["time_zone"]
    ) == float  # using float and not int because of potential for half timezones

    assert type(run_info["start_time"]) == pd.Timestamp
    assert run_info["start_time"].year == 2020
    assert run_info["start_time"].month == 3
    assert run_info["start_time"].day == 18
    assert run_info["start_time"].hour == 20
    assert run_info["start_time"].minute == 00

    assert type(run_info["end_time"]) == pd.Timestamp
    assert run_info["end_time"].year == 2020
    assert run_info["end_time"].month == 3
    assert run_info["end_time"].day == 19
    assert run_info["end_time"].hour == 20
    assert run_info["end_time"].minute == 00

    assert type(run_info["time0"]) == pd.Timestamp
    assert run_info["time0"].year == 2020
    assert run_info["time0"].month == 3
    assert run_info["time0"].day == 19
    assert run_info["time0"].hour == 20
    assert run_info["time0"].minute == 00

    assert type(run_info["last_obs_time"]) == pd.Timestamp
    assert run_info["last_obs_time"].year == 2020
    assert run_info["last_obs_time"].month == 3
    assert run_info["last_obs_time"].day == 19
    assert run_info["last_obs_time"].hour == 20
    assert run_info["last_obs_time"].minute == 00
    print(
        "====================================================================================================="
    )
    print(run_info["properties"]["swmm_input_file"])

    assert run_info["properties"]["UDUNITS"] == Path(
        os.getcwd() + "//model//UDUNITS_lookup.csv")
    assert run_info["properties"]["out_nodes_netcdf"] == Path(
        os.getcwd() + "//output//DonRiver_output_nodes.nc")
    assert run_info["properties"]["out_links_netcdf"] == Path(
        os.getcwd() + "//output//DonRiver_output_links.nc")

    run_info_no_dams = read_run_info(os.getcwd() +
                                     '\\run_info_withoutDamRatingCurve.xml')
    assert "dam_rating_curve" not in run_info_no_dams
Example #2
0
def test_write_netcdf():
    assert os.path.exists(os.getcwd() + "\\output")
    run_info_file = Path(os.getcwd() + "\\run_info.xml")
    run_info = read_run_info(run_info_file)
    properties = run_info["properties"]
    swmm_unit_dict = read_units(properties["UDUNITS"])
    data_dict = read_rpt_file(properties["swmm_output_file"])
    combined_ds_nodes, combined_ds_links = create_xarray_dataset(
        data_dict, swmm_unit_dict)

    # CHECK Nodes NetCDF file is written
    file = properties["out_nodes_netcdf"]
    print("FILE = " + str(file))
    try:
        os.remove(file)
    except OSError:
        print("CAN'T REMOVE FILE = " + str(file))
    assert not os.path.exists(file)
    write_netcdf(combined_ds_nodes, properties["out_nodes_netcdf"])
    assert os.path.exists(file)

    # CHECK Links NetCDF file is written
    file = properties["out_links_netcdf"]
    try:
        os.remove(file)
    except OSError:
        pass
    assert not os.path.exists(file)
    write_netcdf(combined_ds_links, properties["out_links_netcdf"])
    assert os.path.exists(file)
Example #3
0
def test_add_attributes():
    """
    1) Check to make sure it returns an Error if ds.time and ds.station_id NOT existing
    """
    run_info = read_run_info(os.getcwd() + '//run_info.xml')
    data_dict = read_rpt_file(run_info["properties"]["swmm_output_file"])
    swmm_unit_dict = read_units(run_info["properties"]["UDUNITS"])
    ds, _ = create_xarray_dataset(data_dict, swmm_unit_dict)
    assert (list(ds.attrs) == [
        'Conventions', 'title', 'institution', 'source', 'history',
        'references', 'Metadata_Conventions', 'summary', 'date_created',
        'coordinate_system', 'featureType', 'comment'
    ])
    assert ds.attrs["Conventions"] == "CF-1.6"
    assert ds.attrs["title"] == "Data from simulation outputs"
    assert ds.attrs["institution"] == "TRCA"
    assert ds.attrs[
        "source"] == "Don River Hydrology Update Project Number 60528844 December 2018"
    assert ds.attrs["references"] == "https://trca.ca/"
    assert ds.attrs["Metadata_Conventions"] == "Unidata Dataset Discovery v1.0"
    assert ds.attrs["summary"] == "EPA SWMM simulation output"
    assert "EMT" in ds.attrs["date_created"]
    assert ds.attrs["coordinate_system"] == "WGS 1984"
    assert ds.attrs["featureType"] == "timeSeries"
    assert ds.attrs["comment"] == "created from Python script EPA-SWMM-Adaptor"
    assert list(ds.coords) == ["time", "station_id"]
Example #4
0
def test_create_xarray_dataset():
    run_info = read_run_info(os.getcwd() + '//run_info.xml')
    data_dict = read_rpt_file(run_info["properties"]["swmm_output_file"])
    swmm_unit_dict = read_units(run_info["properties"]["UDUNITS"])
    combined_ds_nodes, combined_ds_links = create_xarray_dataset(
        data_dict, swmm_unit_dict)
    assert combined_ds_nodes.sizes == frozendict({
        'time': 94,
        'station_id': 6
    })  # 6 nodes
    assert combined_ds_links.sizes == frozendict({
        'time': 94,
        'station_id': 5
    })  # 5 links
    assert type(combined_ds_nodes) is not None
    assert type(combined_ds_links) is not None
    assert type(combined_ds_nodes) == xr.Dataset
    assert type(combined_ds_links) == xr.Dataset
Example #5
0
def test_read_units():
    """
    Check if reading in the units lookup and attributes information properly.
    """
    run_info_file = Path(os.getcwd() + "\\run_info.xml")
    print("======================" + str(run_info_file))
    global run_info
    run_info = read_run_info(run_info_file)
    file = os.getcwd() + "\\UDUNITS_lookup.csv"
    dict_units = read_units(file)
    assert dict_units['Setting'] == {
        'UDUNITS': 'na',
        'long_name': 'not_available',
        'standard_name': 'not_available'
    }
    assert list(dict_units['Setting'].keys()) == [
        'UDUNITS', 'long_name', 'standard_name'
    ]

    with pytest.raises(SystemExit):
        file = os.getcwd() + "\\UDUNITS_lookup_BAD.csv"
        read_units(file)
Example #6
0
def test_write_runfile():
    run_info = read_run_info((os.getcwd() + '\\run_info.xml'))
    run_info["properties"]["swmm_input_file"] = os.getcwd(
    ) + "//model//standard.inp"
    run_info["properties"]["swmm_output_file"] = os.getcwd(
    ) + "//model//standard.rpt"
    shutil.copy(os.getcwd() + "//model//DonRiver_SOURCE TEST FILE.inp",
                run_info["properties"]["swmm_input_file"])

    rc_dict = read_rating_curve(run_info["dam_rating_curve"])
    rule_dict = read_control_rules(run_info["control_rule"])
    write_runfile(run_info, rc_dict, rule_dict)
    check_file_exists = os.path.exists(
        run_info["properties"]["swmm_input_file"])
    check_file_contents = filecmp.cmp(
        run_info["properties"]["swmm_input_file"],
        # compare the result of the method to the expected contents of the INP file.
        os.getcwd() + '\\model\\DonRiver_expected.inp')
    assert check_file_exists
    assert check_file_contents
    # ONE CURVE
    # Expecting that only LocationX Rating Curve Changed
    run_info["properties"]["swmm_input_file"] = os.getcwd(
    ) + "//model//1curve.inp"
    run_info["properties"]["swmm_output_file"] = os.getcwd(
    ) + "//model//1curve.rpt"
    shutil.copy(os.getcwd() + "//model//DonRiver_SOURCE TEST FILE.inp",
                run_info["properties"]["swmm_input_file"])
    rc_dict = read_rating_curve(os.getcwd() +
                                '\\input\\Dam_rating_curve_1curve.xml')
    write_runfile(run_info, rc_dict, rule_dict)
    with open(run_info["properties"]["swmm_input_file"], 'r') as f:
        lines = f.readlines()
    assert lines[203].strip() == "LocationX     Rating     1     0"
    assert lines[208].strip() == "LocationX               6     40"
    assert lines[211].strip() == "LOC_Y     Rating     101     888"
    assert lines[216].strip() == "LOC_Y               106     888"
    assert lines[220].strip(
    ) == "LOC_Z               Rating    101          777"
    assert lines[225].strip(
    ) == "LOC_Z                          106         777"
    assert lines[238].strip() == "LocationX     Storage     100     999"
    assert lines[243].strip() == "LocationX               600     999"
    assert len(lines) == 347

    # TWO CURVES
    # Expecting that LOC_Y and  LocationX Rating Curve Changed
    run_info["properties"]["swmm_input_file"] = os.getcwd(
    ) + "//model//2curve.inp"
    run_info["properties"]["swmm_output_file"] = os.getcwd(
    ) + "//model//2curve.rpt"
    shutil.copy(os.getcwd() + "//model//DonRiver_SOURCE TEST FILE.inp",
                run_info["properties"]["swmm_input_file"])
    rc_dict = read_rating_curve(os.getcwd() +
                                '\\input\\Dam_rating_curve_2curve.xml')
    write_runfile(run_info, rc_dict, rule_dict)
    with open(run_info["properties"]["swmm_input_file"], 'r') as f:
        lines = f.readlines()
    assert lines[203].strip() == "LocationX     Rating     1     0"
    assert lines[208].strip() == "LocationX               6     40"
    assert lines[211].strip() == "LOC_Y     Rating     2     1.1"
    assert lines[216].strip() == "LOC_Y               10     1.9"
    assert lines[220].strip(
    ) == "LOC_Z               Rating    101          777"
    assert lines[225].strip(
    ) == "LOC_Z                          106         777"
    assert lines[238].strip() == "LocationX     Storage     100     999"
    assert lines[243].strip() == "LocationX               600     999"
    assert len(lines) == 347

    with pytest.raises(SystemExit):
        read_rating_curve(os.getcwd() +
                          '\\input\\Dam_rating_curve_curveDuplicate.xml')

    with pytest.raises(SystemExit):
        read_rating_curve(os.getcwd() +
                          '\\input\\Dam_rating_curve_0curves.xml')