Example #1
0
def test_download_usgs_daily_avg():
    """
    This tests downloading USGS daily avg data
    """
    print("TEST 12: TEST DOWNLOAD USGS DAILY AVERAGE DATA")

    out_streamflow_file = os.path.join(OUTPUT_DATA_PATH, "gage_streamflow.csv")
    out_stream_id_file = os.path.join(OUTPUT_DATA_PATH, "gage_rivid.csv")

    rapid_manager = RAPID(rapid_executable_location=RAPID_EXE_PATH,
                          cygwin_bin_location=CYGWIN_BIN_PATH)

    rapid_manager.generate_usgs_avg_daily_flows_opt(
        reach_id_gage_id_file=os.path.join(INPUT_DATA_PATH,
                                           "usgs_gage_id_rivid.csv"),
        start_datetime=datetime(2000, 1, 1),
        end_datetime=datetime(2000, 1, 3),
        out_streamflow_file=out_streamflow_file,
        out_stream_id_file=out_stream_id_file)

    compare_streamflow_file = os.path.join(COMPARE_DATA_PATH,
                                           "gage_streamflow.csv")
    assert (compare_csv_decimal_files(out_streamflow_file,
                                      compare_streamflow_file,
                                      header=False))

    compare_stream_id_file = os.path.join(COMPARE_DATA_PATH, "gage_rivid.csv")
    assert (compare_csv_decimal_files(out_stream_id_file,
                                      compare_stream_id_file,
                                      header=False))

    remove_files(out_streamflow_file, out_stream_id_file)
Example #2
0
def test_download_usgs_daily_avg():
    """
    This tests downloading USGS daily avg data
    """
    print("TEST 12: TEST DOWNLOAD USGS DAILY AVERAGE DATA")

    out_streamflow_file=os.path.join(OUTPUT_DATA_PATH,"gage_streamflow.csv")
    out_stream_id_file=os.path.join(OUTPUT_DATA_PATH,"gage_rivid.csv")
    
    rapid_manager = RAPID(rapid_executable_location=RAPID_EXE_PATH,
                          cygwin_bin_location=CYGWIN_BIN_PATH)
    
    rapid_manager.generate_usgs_avg_daily_flows_opt(reach_id_gage_id_file=os.path.join(INPUT_DATA_PATH,"usgs_gage_id_rivid.csv"),
                                                    start_datetime=datetime(2000,1,1),
                                                    end_datetime=datetime(2000,1,3),
                                                    out_streamflow_file=out_streamflow_file, 
                                                    out_stream_id_file=out_stream_id_file)
                
    compare_streamflow_file=os.path.join(COMPARE_DATA_PATH,"gage_streamflow.csv")
    ok_(compare_csv_decimal_files(out_streamflow_file, compare_streamflow_file, header=False))

    compare_stream_id_file=os.path.join(COMPARE_DATA_PATH,"gage_rivid.csv")
    ok_(compare_csv_decimal_files(out_stream_id_file, compare_stream_id_file, header=False))
    
    remove_files(out_streamflow_file,
                 out_stream_id_file)
Example #3
0
def test_goodness_of_fit():
    """
    This tests the goodness of fit functions
    """
    print("TEST 14: TEST GOODNESS OF FIT FUNCTIONS")

    reach_id_file = os.path.join(INPUT_DATA_PATH, 'obs_reach_id.csv')
    observed_file = os.path.join(INPUT_DATA_PATH, 'obs_flow.csv')
    #using CF-compliant file
    cf_input_qout_file = os.path.join(COMPARE_DATA_PATH,
                                      'Qout_nasa_lis_3hr_20020830_CF.nc')
    cf_out_analysis_file = os.path.join(
        OUTPUT_DATA_PATH, 'cf_goodness_of_fit_results-daily.csv')
    find_goodness_of_fit(cf_input_qout_file,
                         reach_id_file,
                         observed_file,
                         cf_out_analysis_file,
                         daily=True)

    cf_goodness_of_fit_file_solution = os.path.join(
        COMPARE_DATA_PATH, 'cf_goodness_of_fit_analysis.csv')
    ok_(
        compare_csv_decimal_files(cf_out_analysis_file,
                                  cf_goodness_of_fit_file_solution))
    #using original RAPID file
    raw_goodness_of_fit_file_solution = os.path.join(
        COMPARE_DATA_PATH, 'raw_goodness_of_fit_analysis.csv')
    original_input_qout_file = os.path.join(
        COMPARE_DATA_PATH, 'Qout_nasa_lis_3hr_20020830_original.nc')
    original_out_analysis_file = os.path.join(
        OUTPUT_DATA_PATH, 'original_goodness_of_fit_results-daily.csv')
    find_goodness_of_fit(original_input_qout_file,
                         reach_id_file,
                         observed_file,
                         original_out_analysis_file,
                         steps_per_group=8)

    ok_(
        compare_csv_decimal_files(original_out_analysis_file,
                                  raw_goodness_of_fit_file_solution))

    #using new RAPID file
    new_input_qout_file = os.path.join(COMPARE_DATA_PATH,
                                       'Qout_nasa_lis_3hr_20020830.nc')
    new_out_analysis_file = os.path.join(OUTPUT_DATA_PATH,
                                         'goodness_of_fit_results-daily.csv')
    find_goodness_of_fit(new_input_qout_file,
                         reach_id_file,
                         observed_file,
                         new_out_analysis_file,
                         steps_per_group=8)

    ok_(
        compare_csv_decimal_files(new_out_analysis_file,
                                  raw_goodness_of_fit_file_solution))

    remove_files(cf_out_analysis_file, original_out_analysis_file,
                 new_out_analysis_file)
Example #4
0
def test_goodness_of_fit():
    """
    This tests the goodness of fit functions
    """
    print("TEST 14: TEST GOODNESS OF FIT FUNCTIONS")

    reach_id_file = os.path.join(INPUT_DATA_PATH, 'obs_reach_id.csv')
    observed_file = os.path.join(INPUT_DATA_PATH, 'obs_flow.csv')
    #using CF-compliant file
    cf_input_qout_file = os.path.join(COMPARE_DATA_PATH,
                                      'Qout_nasa_lis_3hr_20020830_CF.nc')
    cf_out_analysis_file = os.path.join(
        OUTPUT_DATA_PATH, 'cf_goodness_of_fit_results-daily.csv')
    find_goodness_of_fit(cf_input_qout_file,
                         reach_id_file,
                         observed_file,
                         cf_out_analysis_file,
                         daily=True)

    cf_goodness_of_fit_file_solution = os.path.join(
        COMPARE_DATA_PATH, 'cf_goodness_of_fit_analysis.csv')
    assert (compare_csv_decimal_files(cf_out_analysis_file,
                                      cf_goodness_of_fit_file_solution))

    reach_id_file = os.path.join(INPUT_DATA_PATH, 'obs_reach_id_1.csv')
    observed_file = os.path.join(INPUT_DATA_PATH, 'obs_flow_1.csv')
    #using CF-compliant file single input
    cf_out_analysis_file_1 = os.path.join(
        OUTPUT_DATA_PATH, 'cf_goodness_of_fit_results_1-daily.csv')
    find_goodness_of_fit(cf_input_qout_file,
                         reach_id_file,
                         observed_file,
                         cf_out_analysis_file_1,
                         daily=True)

    cf_goodness_of_fit_file_solution_1 = os.path.join(
        COMPARE_DATA_PATH, 'cf_goodness_of_fit_analysis_1.csv')
    assert (compare_csv_decimal_files(cf_out_analysis_file_1,
                                      cf_goodness_of_fit_file_solution_1))

    observed_simulated_file = os.path.join(COMPARE_DATA_PATH,
                                           'goodness_of_fit_obs_sim.csv')
    goodness_obs_sim_solution = os.path.join(OUTPUT_DATA_PATH,
                                             'goodness_of_fit_obs_sim.txt')
    # test print goodness of fit to file
    find_goodness_of_fit_csv(observed_simulated_file,
                             out_file=goodness_obs_sim_solution)
    goodness_obs_sim = os.path.join(COMPARE_DATA_PATH,
                                    'goodness_of_fit_obs_sim.txt')
    assert (fcmp(goodness_obs_sim, goodness_obs_sim_solution))
    # test print goodness of fit to console
    find_goodness_of_fit_csv(observed_simulated_file)

    remove_files(cf_out_analysis_file, cf_out_analysis_file_1)
Example #5
0
def test_generate_qinit_file():
    """
    This tests the qinit file function to create an input qinit file for RAPID
    """
    print("TEST 11: TEST GENERATE QINIT FILE")
    rapid_manager = RAPID(rapid_executable_location=RAPID_EXE_PATH,
                          cygwin_bin_location=CYGWIN_BIN_PATH,
                          rapid_connect_file=os.path.join(
                              INPUT_DATA_PATH, 'rapid_connect.csv'))

    #test with original rapid outpur
    input_qout_file = os.path.join(COMPARE_DATA_PATH,
                                   'Qout_nasa_lis_3hr_20020830.nc')
    original_qout_file = os.path.join(OUTPUT_DATA_PATH,
                                      'Qout_nasa_lis_3hr_20020830.nc')
    copy(input_qout_file, original_qout_file)

    qinit_original_rapid_qout = os.path.join(OUTPUT_DATA_PATH,
                                             'qinit_original_rapid_qout.csv')
    rapid_manager.update_parameters(Qout_file=original_qout_file)
    rapid_manager.generate_qinit_from_past_qout(
        qinit_file=qinit_original_rapid_qout)

    qinit_original_rapid_qout_solution = os.path.join(
        COMPARE_DATA_PATH, 'qinit_original_rapid_qout.csv')
    ok_(
        compare_csv_decimal_files(qinit_original_rapid_qout,
                                  qinit_original_rapid_qout_solution,
                                  header=False))

    #test with CF rapid output and alternate time index
    cf_input_qout_file = os.path.join(COMPARE_DATA_PATH,
                                      'Qout_nasa_lis_3hr_20020830_CF.nc')
    cf_qout_file = os.path.join(OUTPUT_DATA_PATH,
                                'Qout_nasa_lis_3hr_20020830_CF.nc')
    copy(cf_input_qout_file, cf_qout_file)

    qinit_cf_rapid_qout = os.path.join(OUTPUT_DATA_PATH,
                                       'qinit_cf_rapid_qout.csv')
    rapid_manager.update_parameters(Qout_file=cf_qout_file)
    rapid_manager.generate_qinit_from_past_qout(qinit_file=qinit_cf_rapid_qout,
                                                time_index=5)

    qinit_cf_rapid_qout_solution = os.path.join(COMPARE_DATA_PATH,
                                                'qinit_cf_rapid_qout.csv')
    ok_(
        compare_csv_decimal_files(qinit_cf_rapid_qout,
                                  qinit_cf_rapid_qout_solution,
                                  header=False))

    remove_files(original_qout_file, qinit_original_rapid_qout, cf_qout_file,
                 qinit_cf_rapid_qout)
Example #6
0
def test_gen_weight_table_gldas2():
    """
    Checks generating weight table for GLDAS V2 grid
    """
    print("TEST 6: TEST GENERATE WEIGHT TABLE FOR GLDAS V2 GRIDS")
    generated_weight_table_file = os.path.join(OUTPUT_DATA_PATH,
                                               "weight_gldas2.csv")
    #rapid_connect
    rapid_connect_file = os.path.join(COMPARE_DATA_PATH, "x-x",
                                      "rapid_connect.csv")

    lsm_grid = os.path.join(LSM_INPUT_DATA_PATH, "gldas2",
                            "GLDAS_NOAH025_3H.A20101231.0000.020.nc4")
    CreateWeightTableLDAS(in_ldas_nc=lsm_grid,
                          in_nc_lon_var="lon",
                          in_nc_lat_var="lat",
                          in_catchment_shapefile=os.path.join(
                              GIS_INPUT_DATA_PATH, 'catchment.shp'),
                          river_id="FEATUREID",
                          in_connectivity_file=rapid_connect_file,
                          out_weight_table=generated_weight_table_file)

    generated_weight_table_file_solution = os.path.join(
        COMPARE_DATA_PATH, "x-x", "weight_gldas2.csv")
    assert (compare_csv_decimal_files(generated_weight_table_file,
                                      generated_weight_table_file_solution))

    remove_files(generated_weight_table_file)
Example #7
0
def test_gen_weight_table_lis_no_intersect():
    """
    Checks generating weight table for LIS grid with no intersect
    """
    print("TEST 8: TEST GENERATE WEIGTH TABLE FOR LIS GRIDS WITH NO INTERSECT")
    generated_weight_table_file = os.path.join(OUTPUT_DATA_PATH, 
                                               "weight_lis_no_intersect.csv")
    #rapid_connect
    rapid_connect_file = os.path.join(GIS_INPUT_DATA_PATH, "uk-no_intersect",
                                      "rapid_connect_45390.csv")

    lsm_grid = os.path.join(LSM_INPUT_DATA_PATH, "lis", "LIS_HIST_201101210000.d01.nc")
    CreateWeightTableLDAS(in_ldas_nc=lsm_grid,
                          in_nc_lon_var="lon",
                          in_nc_lat_var="lat", 
                          in_catchment_shapefile=os.path.join(GIS_INPUT_DATA_PATH, 'uk-no_intersect', 'Catchment_thames_drainID45390.shp'), 
                          river_id="DrainLnID", 
                          in_connectivity_file=rapid_connect_file, 
                          out_weight_table=generated_weight_table_file)
                                                         
    generated_weight_table_file_solution = os.path.join(COMPARE_DATA_PATH, "uk-no_intersect",
                                                        "weight_lis_no_intersect.csv")
    ok_(compare_csv_decimal_files(generated_weight_table_file, 
                                  generated_weight_table_file_solution))

    remove_files(generated_weight_table_file)
Example #8
0
def test_gen_weight_table_lis_no_intersect():
    """
    Checks generating weight table for LIS grid with no intersect
    """
    print("TEST 8: TEST GENERATE WEIGHT TABLE FOR LIS GRIDS WITH NO INTERSECT")
    generated_weight_table_file = os.path.join(OUTPUT_DATA_PATH,
                                               "weight_lis_no_intersect.csv")
    #rapid_connect
    rapid_connect_file = os.path.join(GIS_INPUT_DATA_PATH, "uk-no_intersect",
                                      "rapid_connect_45390.csv")

    lsm_grid = os.path.join(LSM_INPUT_DATA_PATH, "lis",
                            "LIS_HIST_201101210000.d01.nc")
    CreateWeightTableLDAS(in_ldas_nc=lsm_grid,
                          in_nc_lon_var="lon",
                          in_nc_lat_var="lat",
                          in_catchment_shapefile=os.path.join(
                              GIS_INPUT_DATA_PATH, 'uk-no_intersect',
                              'Catchment_thames_drainID45390.shp'),
                          river_id="DrainLnID",
                          in_connectivity_file=rapid_connect_file,
                          out_weight_table=generated_weight_table_file)

    generated_weight_table_file_solution = os.path.join(
        COMPARE_DATA_PATH, "uk-no_intersect", "weight_lis_no_intersect.csv")
    assert (compare_csv_decimal_files(generated_weight_table_file,
                                      generated_weight_table_file_solution))

    remove_files(generated_weight_table_file)
Example #9
0
def test_gen_weight_table_era5_land_mask():
    """
    Checks generating weight table for ERA5 grid with land mask.
    """
    print("TEST 18: TEST GENERATE WEIGHT TABLE FOR ERA5 GRID WITH LAND MASK.")
    generated_weight_table_file = os.path.join(
        OUTPUT_DATA_PATH, "weight_mendocino_era5_land_mask.csv")

    #rapid_connect
    rapid_connect_file = os.path.join(
        COMPARE_DATA_PATH, "mendocino_nhdplus_catchment",
        "rapid_connectivity_mendocino_sample.csv")

    lsm_grid = os.path.join(LSM_INPUT_DATA_PATH, "era5_land_mask",
                            "era5_land-sea_mask_mendocino_subset.nc")

    CreateWeightTableECMWF(
        in_ecmwf_nc=lsm_grid,
        in_catchment_shapefile=os.path.join(
            GIS_INPUT_DATA_PATH, 'mendocino_nhdplus_catchment',
            'NHDCat_mendocino_watershed_hopland_sample.shp'),
        river_id="FEATUREID",
        in_connectivity_file=rapid_connect_file,
        out_weight_table=generated_weight_table_file,
        in_ecmwf_mask_var='lsm')

    generated_weight_table_file_solution = os.path.join(
        COMPARE_DATA_PATH, 'mendocino_nhdplus_catchment',
        'weight_mendocino_era5_land_mask.csv')

    assert (compare_csv_decimal_files(generated_weight_table_file,
                                      generated_weight_table_file_solution))

    remove_files(generated_weight_table_file)
Example #10
0
def test_gen_weight_table_era_t511_24hr():
    """
    Checks generating weight table for ERA T511 24hr grid
    """
    print("TEST 5: TEST GENERATE WEIGHT TABLE FOR ERA T511 24hr GRIDS")
    generated_weight_table_file = os.path.join(OUTPUT_DATA_PATH,
                                               "weight_era_t511.csv")
    #rapid_connect
    rapid_connect_file = os.path.join(COMPARE_DATA_PATH, "x-x",
                                      "rapid_connect.csv")

    lsm_grid = os.path.join(LSM_INPUT_DATA_PATH, "erai24",
                            "19990109_erai_runoff.grib.nc")
    CreateWeightTableECMWF(in_ecmwf_nc=lsm_grid,
                           in_catchment_shapefile=os.path.join(
                               GIS_INPUT_DATA_PATH, 'catchment.shp'),
                           river_id="FEATUREID",
                           in_connectivity_file=rapid_connect_file,
                           out_weight_table=generated_weight_table_file)

    generated_weight_table_file_solution = os.path.join(
        COMPARE_DATA_PATH, "x-x", "weight_era_t511.csv")
    assert (compare_csv_decimal_files(generated_weight_table_file,
                                      generated_weight_table_file_solution))

    remove_files(generated_weight_table_file)
Example #11
0
def test_gen_weight_table_joules():
    """
    Checks generating weight table for Joules grid
    """
    print("TEST 9: TEST GENERATE WEIGTH TABLE FOR Joules GRIDS")
    generated_weight_table_file = os.path.join(OUTPUT_DATA_PATH, 
                                               "weight_joules.csv")
    #rapid_connect
    rapid_connect_file = os.path.join(COMPARE_DATA_PATH, "u-k",
                                      "rapid_connect.csv")

    lsm_grid = os.path.join(LSM_INPUT_DATA_PATH, "joules", "ukv_test.runoff.20080803_00.nc")
    CreateWeightTableLDAS(in_ldas_nc=lsm_grid,
                          in_nc_lon_var="east_west",
                          in_nc_lat_var="north_south", 
                          in_catchment_shapefile=os.path.join(GIS_INPUT_DATA_PATH, 'u-k', 'CatchmentSubset.shp'), 
                          river_id="DrainLnID", 
                          in_connectivity_file=rapid_connect_file, 
                          out_weight_table=generated_weight_table_file)
                                                         
    generated_weight_table_file_solution = os.path.join(COMPARE_DATA_PATH, "u-k",
                                                        "weight_joules.csv")
    ok_(compare_csv_decimal_files(generated_weight_table_file, 
                                  generated_weight_table_file_solution))

    remove_files(generated_weight_table_file)
Example #12
0
def test_gen_weight_table_lis():
    """
    Checks generating weight table for LIS grid
    """
    print("TEST 7: TEST GENERATE WEIGTH TABLE FOR LIS GRIDS")
    generated_weight_table_file = os.path.join(OUTPUT_DATA_PATH, 
                                               "weight_lis.csv")
    #rapid_connect
    rapid_connect_file = os.path.join(COMPARE_DATA_PATH, "u-k",
                                      "rapid_connect.csv")

    lsm_grid = os.path.join(LSM_INPUT_DATA_PATH, "lis", "LIS_HIST_201101210000.d01.nc")
    CreateWeightTableLDAS(in_ldas_nc=lsm_grid,
                          in_nc_lon_var="lon",
                          in_nc_lat_var="lat", 
                          in_catchment_shapefile=os.path.join(GIS_INPUT_DATA_PATH, 'u-k', 'CatchmentSubset.shp'), 
                          river_id="DrainLnID", 
                          in_connectivity_file=rapid_connect_file, 
                          out_weight_table=generated_weight_table_file)
                                                         
    generated_weight_table_file_solution = os.path.join(COMPARE_DATA_PATH, "u-k",
                                                        "weight_lis.csv")
    ok_(compare_csv_decimal_files(generated_weight_table_file, 
                                  generated_weight_table_file_solution))

    remove_files(generated_weight_table_file)
Example #13
0
def test_gen_muskingum_kfac1():
    """
    Checks generating Muskingum Kfac option 1
    """
    print("TEST 14: TEST GENERATE MUSKINGUM KFAC OPTION 1")
    generated_kfac_file = os.path.join(OUTPUT_DATA_PATH, 
                                       "kfac1.csv")
    #rapid_connect
    rapid_connect_file = os.path.join(COMPARE_DATA_PATH, "x-x",
                                      "rapid_connect.csv")
    CreateMuskingumKfacFile(in_drainage_line=os.path.join(GIS_INPUT_DATA_PATH, 'flowline.shp'),
                            river_id="COMID",
                            length_id="LENGTHKM",
                            slope_id="Slope",
                            celerity=1000.0/3600.0,
                            formula_type=1,
                            in_connectivity_file=rapid_connect_file,
                            out_kfac_file=generated_kfac_file)
                            
    #CHECK OUTPUT   
    #kfac
    generated_kfac_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                "kfac1.csv")
    ok_(compare_csv_decimal_files(generated_kfac_file, 
                                  generated_kfac_file_solution))
    remove_files(generated_kfac_file)
Example #14
0
def test_gen_muskingum_kfac1():
    """
    Checks generating Muskingum Kfac option 1
    """
    print("TEST 14: TEST GENERATE MUSKINGUM KFAC OPTION 1")
    generated_kfac_file = os.path.join(OUTPUT_DATA_PATH, "kfac1.csv")
    #rapid_connect
    rapid_connect_file = os.path.join(COMPARE_DATA_PATH, "x-x",
                                      "rapid_connect.csv")
    CreateMuskingumKfacFile(in_drainage_line=os.path.join(
        GIS_INPUT_DATA_PATH, 'flowline.shp'),
                            river_id="COMID",
                            length_id="LENGTHKM",
                            slope_id="Slope",
                            celerity=1000.0 / 3600.0,
                            formula_type=1,
                            in_connectivity_file=rapid_connect_file,
                            out_kfac_file=generated_kfac_file)

    #CHECK OUTPUT
    #kfac
    generated_kfac_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                "kfac1.csv")
    assert (compare_csv_decimal_files(generated_kfac_file,
                                      generated_kfac_file_solution))
    remove_files(generated_kfac_file)
Example #15
0
def test_weight_table_with_area_id():
    """
    Checks generating weight table with area id
    """
    print("TEST 17: TEST GENERATE WEIGHT TABLE WITH INVALID POLYGON")
    generated_weight_table_file = os.path.join(OUTPUT_DATA_PATH,
                                               "weight_area.csv")
    # rapid_connect
    rapid_connect_file = os.path.join(COMPARE_DATA_PATH, "x-x",
                                      "rapid_connect.csv")

    lsm_grid = os.path.join(LSM_INPUT_DATA_PATH, "gldas2",
                            "GLDAS_NOAH025_3H.A20101231.0000.020.nc4")
    CreateWeightTableLDAS(in_ldas_nc=lsm_grid,
                          in_nc_lon_var="lon",
                          in_nc_lat_var="lat",
                          in_catchment_shapefile=os.path.join(
                              GIS_INPUT_DATA_PATH, 'test_catchments.shp'),
                          river_id="DrainLnID",
                          area_id="Shape_Area",
                          in_connectivity_file=rapid_connect_file,
                          out_weight_table=generated_weight_table_file)

    generated_weight_table_file_solution = os.path.join(
        COMPARE_DATA_PATH, "x-x", "weight_area.csv")
    assert (compare_csv_decimal_files(generated_weight_table_file,
                                      generated_weight_table_file_solution))

    remove_files(generated_weight_table_file)
Example #16
0
def test_gen_weight_table_joules():
    """
    Checks generating weight table for Joules grid
    """
    print("TEST 9: TEST GENERATE WEIGHT TABLE FOR Joules GRIDS")
    generated_weight_table_file = os.path.join(OUTPUT_DATA_PATH,
                                               "weight_joules.csv")
    #rapid_connect
    rapid_connect_file = os.path.join(COMPARE_DATA_PATH, "u-k",
                                      "rapid_connect.csv")

    lsm_grid = os.path.join(LSM_INPUT_DATA_PATH, "joules",
                            "ukv_test.runoff.20080803_00.nc")
    CreateWeightTableLDAS(in_ldas_nc=lsm_grid,
                          in_nc_lon_var="east_west",
                          in_nc_lat_var="north_south",
                          in_catchment_shapefile=os.path.join(
                              GIS_INPUT_DATA_PATH, 'u-k',
                              'CatchmentSubset.shp'),
                          river_id="DrainLnID",
                          in_connectivity_file=rapid_connect_file,
                          out_weight_table=generated_weight_table_file)

    generated_weight_table_file_solution = os.path.join(
        COMPARE_DATA_PATH, "u-k", "weight_joules.csv")
    assert (compare_csv_decimal_files(generated_weight_table_file,
                                      generated_weight_table_file_solution))

    remove_files(generated_weight_table_file)
Example #17
0
def test_gen_weight_table_gldas2():
    """
    Checks generating weight table for GLDAS V2 grid
    """
    print("TEST 6: TEST GENERATE WEIGTH TABLE FOR GLDAS V2 GRIDS")
    generated_weight_table_file = os.path.join(OUTPUT_DATA_PATH, 
                                               "weight_gldas2.csv")
    #rapid_connect
    rapid_connect_file = os.path.join(COMPARE_DATA_PATH, "x-x",
                                      "rapid_connect.csv")

    lsm_grid = os.path.join(LSM_INPUT_DATA_PATH, "gldas2", "GLDAS_NOAH025_3H.A20101231.0000.020.nc4")
    CreateWeightTableLDAS(in_ldas_nc=lsm_grid,
                          in_nc_lon_var="lon",
                          in_nc_lat_var="lat", 
                          in_catchment_shapefile=os.path.join(GIS_INPUT_DATA_PATH, 'catchment.shp'), 
                          river_id="FEATUREID", 
                          in_connectivity_file=rapid_connect_file, 
                          out_weight_table=generated_weight_table_file)
                                                         
    generated_weight_table_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                        "weight_gldas2.csv")
    ok_(compare_csv_decimal_files(generated_weight_table_file, 
                                  generated_weight_table_file_solution))

    remove_files(generated_weight_table_file)
Example #18
0
    def test_generate_seasonal_qinit(self):
        """
        Checks generating seasonal qinit from rapid Qout
        """
        rapid_manager = RAPID(Qout_file=os.path.join(self.INPUT_DATA_PATH, 'Qout_erai_t511_24hr_19800101to19861231.nc'),
                              rapid_connect_file=os.path.join(self.COMPARE_DATA_PATH, 'gis', 'x-x', 'rapid_connect.csv')
                              )

        seasonal_init_file_name = 'Qinit_seasonal_avg_jan_1.csv'
        generated_seasonal_init_file = os.path.join(self.OUTPUT_DATA_PATH, seasonal_init_file_name)

        rapid_manager.generate_seasonal_intitialization(qinit_file=generated_seasonal_init_file,
                                                        datetime_start_initialization=datetime(1984,1,1))

        compare_seasonal_init_file = os.path.join(self.COMPARE_DATA_PATH, generated_seasonal_init_file)

        compare_csv_decimal_files(generated_seasonal_init_file,compare_seasonal_init_file)
Example #19
0
def test_goodness_of_fit():
    """
    This tests the goodness of fit functions
    """
    print("TEST 14: TEST GOODNESS OF FIT FUNCTIONS")

    reach_id_file = os.path.join(INPUT_DATA_PATH, 'obs_reach_id.csv') 
    observed_file = os.path.join(INPUT_DATA_PATH, 'obs_flow.csv') 
    #using CF-compliant file
    cf_input_qout_file = os.path.join(COMPARE_DATA_PATH, 'Qout_nasa_lis_3hr_20020830_CF.nc')
    cf_out_analysis_file = os.path.join(OUTPUT_DATA_PATH, 'cf_goodness_of_fit_results-daily.csv') 
    find_goodness_of_fit(cf_input_qout_file, reach_id_file, observed_file,
                         cf_out_analysis_file, daily=True)

    cf_goodness_of_fit_file_solution = os.path.join(COMPARE_DATA_PATH, 'cf_goodness_of_fit_analysis.csv') 
    ok_(compare_csv_decimal_files(cf_out_analysis_file, cf_goodness_of_fit_file_solution))
    #using original RAPID file
    raw_goodness_of_fit_file_solution = os.path.join(COMPARE_DATA_PATH, 'raw_goodness_of_fit_analysis.csv') 
    original_input_qout_file = os.path.join(COMPARE_DATA_PATH, 'Qout_nasa_lis_3hr_20020830_original.nc')
    original_out_analysis_file = os.path.join(OUTPUT_DATA_PATH, 'original_goodness_of_fit_results-daily.csv') 
    find_goodness_of_fit(original_input_qout_file, reach_id_file, observed_file,
                         original_out_analysis_file, steps_per_group=8)

    ok_(compare_csv_decimal_files(original_out_analysis_file, raw_goodness_of_fit_file_solution))

    #using new RAPID file
    new_input_qout_file = os.path.join(COMPARE_DATA_PATH, 'Qout_nasa_lis_3hr_20020830.nc')
    new_out_analysis_file = os.path.join(OUTPUT_DATA_PATH, 'goodness_of_fit_results-daily.csv') 
    find_goodness_of_fit(new_input_qout_file, reach_id_file, observed_file,
                         new_out_analysis_file, steps_per_group=8)

    ok_(compare_csv_decimal_files(new_out_analysis_file, raw_goodness_of_fit_file_solution))

    reach_id_file = os.path.join(INPUT_DATA_PATH, 'obs_reach_id_1.csv') 
    observed_file = os.path.join(INPUT_DATA_PATH, 'obs_flow_1.csv') 
    #using CF-compliant file single input
    cf_out_analysis_file_1 = os.path.join(OUTPUT_DATA_PATH, 'cf_goodness_of_fit_results_1-daily.csv') 
    find_goodness_of_fit(cf_input_qout_file, reach_id_file, observed_file,
                         cf_out_analysis_file_1, daily=True)

    cf_goodness_of_fit_file_solution_1 = os.path.join(COMPARE_DATA_PATH, 'cf_goodness_of_fit_analysis_1.csv') 
    ok_(compare_csv_decimal_files(cf_out_analysis_file_1, cf_goodness_of_fit_file_solution_1))

    remove_files(cf_out_analysis_file,
                 original_out_analysis_file,
                 new_out_analysis_file)
Example #20
0
    def test_run_era_interim_inflow(self):
        """
        Checks generating inflow file from ERA Interim LSM
        """
        print("TEST 1: TEST GENERATE INFLOW FILE FROM ERA INTERIM DATA")
        
        rapid_input_path, rapid_output_path = self._setup_automated("x-x")

        #run main process    
        run_lsm_rapid_process(
            rapid_executable_location=self.RAPID_EXE_PATH,
            cygwin_bin_location=self.CYGWIN_BIN_PATH,
            rapid_io_files_location=self.OUTPUT_DATA_PATH,
            lsm_data_location=os.path.join(self.LSM_INPUT_DATA_PATH, 'erai3'), 
            simulation_start_datetime=datetime(1980, 1, 1),
            simulation_end_datetime=datetime(2014, 1, 31),
            generate_rapid_namelist_file=False,
            run_rapid_simulation=True,
            generate_return_periods_file=False,
            generate_seasonal_initialization_file=False,
            generate_initialization_file=True,
            use_all_processors=True,
        )
        
        #CHECK OUTPUT    
        #m3_riv
        m3_file_name = "m3_riv_bas_erai_t511_3hr_20030121to20030122.nc"
        generated_m3_file = os.path.join(rapid_output_path, m3_file_name)
        generated_m3_file_solution = os.path.join(self.INFLOW_COMPARE_DATA_PATH, m3_file_name)
        self._compare_m3(generated_m3_file,generated_m3_file_solution)
        
        #qout file
        qout_file_name = "Qout_erai_t511_3hr_20030121to20030122.nc"
        generated_qout_file = os.path.join(rapid_output_path, qout_file_name)
        generated_qout_file_solution = os.path.join(self.INFLOW_COMPARE_DATA_PATH, qout_file_name)
        d1 = Dataset(generated_qout_file)
        d2 = Dataset(generated_qout_file_solution)
        assert_almost_equal(d1.variables['Qout'][:], d2.variables['Qout'][:], decimal=5)
        ok_((d1.variables['rivid'][:] == d2.variables['rivid'][:]).all())
        ok_((d1.variables['time'][:] == d2.variables['time'][:]).all())
        if 'lat' in d2.variables.keys():
            ok_((d1.variables['lat'][:] == d2.variables['lat'][:]).all())
        if 'lon' in d2.variables.keys():
            ok_((d1.variables['lon'][:] == d2.variables['lon'][:]).all())
        d1.close()
        d2.close()
                                                     
        #initialization file
        qinit_file_name = "qinit_erai_t511_3hr_20030121to20030122.csv"
        generated_qinit_file = os.path.join(rapid_input_path, qinit_file_name)
        generated_qinit_file_solution = os.path.join(self.INFLOW_COMPARE_DATA_PATH, qinit_file_name)
    
        ok_(compare_csv_decimal_files(generated_qinit_file, generated_qinit_file_solution))
        
        #additional cleanup
        remove_files(generated_qinit_file)
Example #21
0
    def test_generate_seasonal_qinit(self):
        """
        Checks generating seasonal qinit from rapid Qout
        """
        print("TEST 3: TEST GENERATE SEASONAL QINIT FROM RAPID QOUT")
    
        rapid_manager = RAPID(Qout_file=os.path.join(self.INPUT_DATA_PATH, 'Qout_erai_t511_24hr_19800101to19861231.nc'),
                              rapid_connect_file=os.path.join(self.COMPARE_DATA_PATH, 'gis', 'x-x', 'rapid_connect.csv')
                              )
        
        seasonal_init_file_name = 'Qinit_seasonal_avg_jan_1.csv'
        generated_seasonal_init_file = os.path.join(self.OUTPUT_DATA_PATH, seasonal_init_file_name)

        rapid_manager.generate_seasonal_intitialization(qinit_file=generated_seasonal_init_file,
                                                        datetime_start_initialization=datetime(1984,1,1))
        
        compare_seasonal_init_file = os.path.join(self.COMPARE_DATA_PATH, generated_seasonal_init_file)
        
        compare_csv_decimal_files(generated_seasonal_init_file,compare_seasonal_init_file)
Example #22
0
def test_generate_qinit_file():
    """
    This tests the qinit file function to create an input qinit file for RAPID
    """
    print("TEST 11: TEST GENERATE QINIT FILE")
    rapid_manager = RAPID(rapid_executable_location=RAPID_EXE_PATH,
                          cygwin_bin_location=CYGWIN_BIN_PATH,
                          rapid_connect_file=os.path.join(INPUT_DATA_PATH, 'rapid_connect.csv')
                         )

    #test with original rapid outpur
    input_qout_file = os.path.join(COMPARE_DATA_PATH, 'Qout_nasa_lis_3hr_20020830.nc')
    original_qout_file = os.path.join(OUTPUT_DATA_PATH, 'Qout_nasa_lis_3hr_20020830.nc')
    copy(input_qout_file, original_qout_file)

    qinit_original_rapid_qout = os.path.join(OUTPUT_DATA_PATH, 'qinit_original_rapid_qout.csv')
    rapid_manager.update_parameters(Qout_file=original_qout_file)
    rapid_manager.generate_qinit_from_past_qout(qinit_file=qinit_original_rapid_qout)
    
    qinit_original_rapid_qout_solution = os.path.join(COMPARE_DATA_PATH, 'qinit_original_rapid_qout.csv')
    ok_(compare_csv_decimal_files(qinit_original_rapid_qout, qinit_original_rapid_qout_solution, header=False))

    #test with CF rapid output and alternate time index
    cf_input_qout_file = os.path.join(COMPARE_DATA_PATH, 'Qout_nasa_lis_3hr_20020830_CF.nc')
    cf_qout_file = os.path.join(OUTPUT_DATA_PATH, 'Qout_nasa_lis_3hr_20020830_CF.nc')
    copy(cf_input_qout_file, cf_qout_file)

    qinit_cf_rapid_qout = os.path.join(OUTPUT_DATA_PATH, 'qinit_cf_rapid_qout.csv')
    rapid_manager.update_parameters(Qout_file=cf_qout_file)
    rapid_manager.generate_qinit_from_past_qout(qinit_file=qinit_cf_rapid_qout,
                                                time_index=5)
                                                
    qinit_cf_rapid_qout_solution = os.path.join(COMPARE_DATA_PATH, 'qinit_cf_rapid_qout.csv')
    ok_(compare_csv_decimal_files(qinit_cf_rapid_qout, qinit_cf_rapid_qout_solution, header=False))

    remove_files(original_qout_file, 
                 qinit_original_rapid_qout,
                 cf_qout_file,
                 qinit_cf_rapid_qout
                 )
Example #23
0
def test_gen_static_nhd_connect_rapid_input():
    """
    Checks generating static NHDPlus connect RAPID input
    """
    print("TEST 2: TEST GENERATE STATIC NHDPlus CONNECT RAPID INPUT DATA")
    generated_rapid_connect_file = os.path.join(OUTPUT_DATA_PATH, 
                                                "rapid_connect_nhd.csv")
    CreateNetworkConnectivityNHDPlus(in_drainage_line=os.path.join(GIS_INPUT_DATA_PATH, 'flowline.shp'),
                                     out_connectivity_file=generated_rapid_connect_file)
    #rapid_connect
    generated_rapid_connect_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                         "rapid_connect.csv")
                                                         
    ok_(compare_csv_decimal_files(generated_rapid_connect_file, 
                                  generated_rapid_connect_file_solution))

    remove_files(generated_rapid_connect_file)
Example #24
0
def test_gen_static_nhd_connect_rapid_input():
    """
    Checks generating static NHDPlus connect RAPID input
    """
    print("TEST 2: TEST GENERATE STATIC NHDPlus CONNECT RAPID INPUT DATA")
    generated_rapid_connect_file = os.path.join(OUTPUT_DATA_PATH, 
                                                "rapid_connect_nhd.csv")
    CreateNetworkConnectivityNHDPlus(in_drainage_line=os.path.join(GIS_INPUT_DATA_PATH, 'flowline.shp'),
                                     out_connectivity_file=generated_rapid_connect_file)
    #rapid_connect
    generated_rapid_connect_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                         "rapid_connect.csv")
                                                         
    ok_(compare_csv_decimal_files(generated_rapid_connect_file, 
                                  generated_rapid_connect_file_solution))

    remove_files(generated_rapid_connect_file)
Example #25
0
def test_gen_muskingum_x_drainage():
    """
    Checks generating Muskingum X from draiange line
    """
    print("TEST 15: TEST GENERATE MUSKINGUM X FROM DRAINAGE LINE")
    generated_x_file = os.path.join(OUTPUT_DATA_PATH, 
                                    "x_drain.csv")
                                    
    CreateMuskingumXFileFromDranageLine(in_drainage_line=os.path.join(GIS_INPUT_DATA_PATH, 'u-k', "DrainageLineSubset.shp"),
                                        x_id="Musk_x",
                                        out_x_file=generated_x_file)
                            
    #CHECK OUTPUT   
    generated_x_file_solution = os.path.join(COMPARE_DATA_PATH, "u-k",
                                             "x_drain.csv")
    ok_(compare_csv_decimal_files(generated_x_file, 
                                  generated_x_file_solution))
    remove_files(generated_x_file)
Example #26
0
def test_gen_muskingum_x_drainage():
    """
    Checks generating Muskingum X from draiange line
    """
    print("TEST 15: TEST GENERATE MUSKINGUM X FROM DRAINAGE LINE")
    generated_x_file = os.path.join(OUTPUT_DATA_PATH, "x_drain.csv")

    CreateMuskingumXFileFromDranageLine(in_drainage_line=os.path.join(
        GIS_INPUT_DATA_PATH, 'u-k', "DrainageLineSubset.shp"),
                                        x_id="Musk_x",
                                        out_x_file=generated_x_file)

    #CHECK OUTPUT
    generated_x_file_solution = os.path.join(COMPARE_DATA_PATH, "u-k",
                                             "x_drain.csv")
    assert (compare_csv_decimal_files(generated_x_file,
                                      generated_x_file_solution))
    remove_files(generated_x_file)
Example #27
0
def test_gen_weight_table_era_t511_24hr():
    """
    Checks generating weight table for ERA T511 24hr grid
    """
    print("TEST 5: TEST GENERATE WEIGTH TABLE FOR ERA T511 24hr GRIDS")
    generated_weight_table_file = os.path.join(OUTPUT_DATA_PATH, 
                                               "weight_era_t511.csv")
    #rapid_connect
    rapid_connect_file = os.path.join(COMPARE_DATA_PATH,"x-x",
                                      "rapid_connect.csv")

    lsm_grid = os.path.join(LSM_INPUT_DATA_PATH, "erai24", "19990109_erai_runoff.grib.nc")
    CreateWeightTableECMWF(in_ecmwf_nc=lsm_grid, 
                           in_catchment_shapefile=os.path.join(GIS_INPUT_DATA_PATH, 'catchment.shp'), 
                           river_id="FEATUREID", 
                           in_connectivity_file=rapid_connect_file, 
                           out_weight_table=generated_weight_table_file)
                                                         
    generated_weight_table_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                        "weight_era_t511.csv")
    ok_(compare_csv_decimal_files(generated_weight_table_file, 
                                  generated_weight_table_file_solution))

    remove_files(generated_weight_table_file)
Example #28
0
def test_gen_weight_table_era_t255():
    """
    Checks generating weight table for ERA T255 grid
    """
    print("TEST 4: TEST GENERATE WEIGTH TABLE FOR ERA T255 GRIDS")
    generated_weight_table_file = os.path.join(OUTPUT_DATA_PATH, 
                                               "weight_era_t255.csv")
    #rapid_connect
    rapid_connect_file = os.path.join(COMPARE_DATA_PATH, "x-x",
                                      "rapid_connect.csv")

    lsm_grid = os.path.join(LSM_INPUT_DATA_PATH, "erai3t255", "era_interim_runoff_20140820.nc")
    CreateWeightTableECMWF(in_ecmwf_nc=lsm_grid, 
                           in_catchment_shapefile=os.path.join(GIS_INPUT_DATA_PATH, 'catchment.shp'), 
                           river_id="FEATUREID", 
                           in_connectivity_file=rapid_connect_file, 
                           out_weight_table=generated_weight_table_file)
                                                         
    generated_weight_table_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                        "weight_era_t255.csv")
    ok_(compare_csv_decimal_files(generated_weight_table_file, 
                                  generated_weight_table_file_solution))

    remove_files(generated_weight_table_file)
Example #29
0
def test_gen_weight_table_lis_land_mask():
    """
    Checks generating weight table for LIS grid with land mask.
    """
    print("TEST 20: TEST GENERATE WEIGHT TABLE FOR LIS GRID WITH LAND MASK.")
    generated_weight_table_file = os.path.join(
        OUTPUT_DATA_PATH, "weight_lis_land_fraction_mendocino_subset.csv")

    #rapid_connect
    rapid_connect_file = os.path.join(
        COMPARE_DATA_PATH, "mendocino_nhdplus_catchment",
        "rapid_connectivity_mendocino_sample.csv")

    lsm_grid = os.path.join(LSM_INPUT_DATA_PATH, "lis_land_mask",
                            "lisglobalmask557ww_mendocino_subset.nc")

    CreateWeightTableLDAS(in_ldas_nc=lsm_grid,
                          in_nc_lon_var='lon',
                          in_nc_lat_var='lat',
                          in_catchment_shapefile=os.path.join(
                              GIS_INPUT_DATA_PATH,
                              'mendocino_nhdplus_catchment',
                              'NHDCat_mendocino_watershed_hopland_sample.shp'),
                          river_id='FEATUREID',
                          in_connectivity_file=rapid_connect_file,
                          out_weight_table=generated_weight_table_file,
                          in_land_area_fraction_var='LANDMASK')

    generated_weight_table_file_solution = os.path.join(
        COMPARE_DATA_PATH, 'mendocino_nhdplus_catchment',
        'weight_lis_land_fraction_mendocino_subset.csv')

    assert (compare_csv_decimal_files(generated_weight_table_file,
                                      generated_weight_table_file_solution))

    remove_files(generated_weight_table_file)
Example #30
0
def test_gen_static_rapid_input():
    """
    Checks generating static RAPID input
    """
    print("TEST 1: TEST GENERATE STATIC RAPID INPUT DATA")
    CreateAllStaticECMWFRAPIDFiles(
        in_drainage_line=os.path.join(GIS_INPUT_DATA_PATH, 'flowline.shp'),
        river_id="COMID",
        length_id="LENGTHKM",
        slope_id="Slope",
        next_down_id="NextDownID",
        in_catchment=os.path.join(GIS_INPUT_DATA_PATH, 'catchment.shp'),
        catchment_river_id="FEATUREID",
        rapid_output_folder=OUTPUT_DATA_PATH,
        kfac_length_units="km",
    )

    #CHECK OUTPUT
    #comid_lat_lon_z
    generated_comid_lat_lon_z_file = os.path.join(OUTPUT_DATA_PATH,
                                                  "comid_lat_lon_z.csv")
    generated_comid_lat_lon_z_file_solution = os.path.join(
        COMPARE_DATA_PATH, "x-x", "comid_lat_lon_z.csv")
    assert (compare_csv_decimal_files(generated_comid_lat_lon_z_file,
                                      generated_comid_lat_lon_z_file_solution))

    #rapid_connect
    generated_rapid_connect_file = os.path.join(OUTPUT_DATA_PATH,
                                                "rapid_connect.csv")
    generated_rapid_connect_file_solution = os.path.join(
        COMPARE_DATA_PATH, "x-x", "rapid_connect.csv")
    assert (compare_csv_decimal_files(generated_rapid_connect_file,
                                      generated_rapid_connect_file_solution))

    #riv_bas_id
    generated_riv_bas_id_file = os.path.join(OUTPUT_DATA_PATH,
                                             "riv_bas_id.csv")
    generated_riv_bas_id_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                      "riv_bas_id.csv")
    assert (compare_csv_decimal_files(generated_riv_bas_id_file,
                                      generated_riv_bas_id_file_solution))

    #kfac
    generated_kfac_file = os.path.join(OUTPUT_DATA_PATH, "kfac.csv")
    generated_kfac_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                "kfac.csv")
    assert (compare_csv_decimal_files(generated_kfac_file,
                                      generated_kfac_file_solution))

    #k
    generated_k_file = os.path.join(OUTPUT_DATA_PATH, "k.csv")
    generated_k_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x", "k.csv")
    assert (compare_csv_decimal_files(generated_k_file,
                                      generated_k_file_solution))

    #x
    generated_x_file = os.path.join(OUTPUT_DATA_PATH, "x.csv")
    generated_x_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x", "x.csv")
    assert (compare_csv_decimal_files(generated_x_file,
                                      generated_x_file_solution))

    #weight_ecmwf_t1279
    generated_weight_ecmwf_t1279_file = os.path.join(OUTPUT_DATA_PATH,
                                                     "weight_ecmwf_t1279.csv")
    generated_weight_ecmwf_t1279_file_solution = os.path.join(
        COMPARE_DATA_PATH, "x-x", "weight_ecmwf_t1279.csv")
    assert (compare_csv_decimal_files(
        generated_weight_ecmwf_t1279_file,
        generated_weight_ecmwf_t1279_file_solution))

    #weight_ecmwf_tco369
    generated_weight_ecmwf_tco639_file = os.path.join(
        OUTPUT_DATA_PATH, "weight_ecmwf_tco639.csv")
    generated_weight_ecmwf_tco639_file_solution = os.path.join(
        COMPARE_DATA_PATH, "x-x", "weight_ecmwf_tco639.csv")
    assert (compare_csv_decimal_files(
        generated_weight_ecmwf_tco639_file,
        generated_weight_ecmwf_tco639_file_solution))

    #weight_era_t511
    generated_weight_era_t511_file = os.path.join(OUTPUT_DATA_PATH,
                                                  "weight_era_t511.csv")
    generated_weight_era_t511_file_solution = os.path.join(
        COMPARE_DATA_PATH, "x-x", "weight_era_t511.csv")
    assert (compare_csv_decimal_files(generated_weight_era_t511_file,
                                      generated_weight_era_t511_file_solution))

    remove_files(generated_comid_lat_lon_z_file, generated_rapid_connect_file,
                 generated_riv_bas_id_file, generated_kfac_file,
                 generated_k_file, generated_x_file,
                 generated_weight_ecmwf_t1279_file,
                 generated_weight_ecmwf_tco639_file,
                 generated_weight_era_t511_file)
Example #31
0
    def test_run_era_interim_inflow(self):
        """
        Checks generating inflow file from ERA Interim LSM
        """
        rapid_input_path, rapid_output_path = self._setup_automated("x-x")

        # run main process
        output_file_info = run_lsm_rapid_process(
            rapid_executable_location=RAPID_EXE_PATH,
            cygwin_bin_location=self.CYGWIN_BIN_PATH,
            rapid_io_files_location=self.OUTPUT_DATA_PATH,
            lsm_data_location=os.path.join(self.LSM_INPUT_DATA_PATH, 'erai3'),
            simulation_start_datetime=datetime(1980, 1, 1),
            simulation_end_datetime=datetime(2014, 1, 31),
            generate_rapid_namelist_file=False,
            run_rapid_simulation=True,
            generate_return_periods_file=False,
            generate_seasonal_initialization_file=False,
            generate_initialization_file=True,
            use_all_processors=True,
        )

        # CHECK OUTPUT
        # m3_riv
        m3_file_name = "m3_riv_bas_erai_t511_3hr_20030121to20030122.nc"
        generated_m3_file = os.path.join(rapid_output_path, m3_file_name)
        generated_m3_file_solution = os.path.join(
            self.INFLOW_COMPARE_DATA_PATH, m3_file_name)
        self._compare_m3(generated_m3_file, generated_m3_file_solution)

        # qout file
        qout_file_name = "Qout_erai_t511_3hr_20030121to20030122.nc"
        generated_qout_file = os.path.join(rapid_output_path, qout_file_name)
        generated_qout_file_solution = os.path.join(
            self.INFLOW_COMPARE_DATA_PATH, qout_file_name)
        d1 = Dataset(generated_qout_file)
        d2 = Dataset(generated_qout_file_solution)
        assert_almost_equal(d1.variables['Qout'][:],
                            d2.variables['Qout'][:],
                            decimal=5)
        assert (d1.variables['rivid'][:] == d2.variables['rivid'][:]).all()
        assert (d1.variables['time'][:] == d2.variables['time'][:]).all()
        if 'lat' in d2.variables.keys():
            assert (d1.variables['lat'][:] == d2.variables['lat'][:]).all()
        if 'lon' in d2.variables.keys():
            assert (d1.variables['lon'][:] == d2.variables['lon'][:]).all()
        d1.close()
        d2.close()

        # check output file info
        assert output_file_info[0]['x-x']['m3_riv'] == generated_m3_file
        assert output_file_info[0]['x-x']['qout'] == generated_qout_file

        # initialization file
        qinit_file_name = "qinit_erai_t511_3hr_20030121to20030122.csv"
        generated_qinit_file = os.path.join(rapid_input_path, qinit_file_name)
        generated_qinit_file_solution = os.path.join(
            self.INFLOW_COMPARE_DATA_PATH, qinit_file_name)

        assert compare_csv_decimal_files(generated_qinit_file,
                                         generated_qinit_file_solution)

        # additional cleanup
        remove_files(generated_qinit_file)
Example #32
0
def test_gen_static_rapid_input():
    """
    Checks generating static RAPID input
    """
    print("TEST 1: TEST GENERATE STATIC RAPID INPUT DATA")
    CreateAllStaticECMWFRAPIDFiles(in_drainage_line=os.path.join(GIS_INPUT_DATA_PATH, 'flowline.shp'),
                                   river_id="COMID",
                                   length_id="LENGTHKM",
                                   slope_id="Slope",
                                   next_down_id="NextDownID",
                                   in_catchment=os.path.join(GIS_INPUT_DATA_PATH, 'catchment.shp'),
                                   catchment_river_id="FEATUREID",
                                   rapid_output_folder=OUTPUT_DATA_PATH,
                                   kfac_length_units="km",
                                   )
    
    #CHECK OUTPUT   
    #comid_lat_lon_z
    generated_comid_lat_lon_z_file = os.path.join(OUTPUT_DATA_PATH, 
                                                  "comid_lat_lon_z.csv")
    generated_comid_lat_lon_z_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                           "comid_lat_lon_z.csv")
    ok_(compare_csv_decimal_files(generated_comid_lat_lon_z_file, 
                                  generated_comid_lat_lon_z_file_solution))

    #rapid_connect
    generated_rapid_connect_file = os.path.join(OUTPUT_DATA_PATH, 
                                                "rapid_connect.csv")
    generated_rapid_connect_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                         "rapid_connect.csv")
    ok_(compare_csv_decimal_files(generated_rapid_connect_file, 
                                  generated_rapid_connect_file_solution))

    #riv_bas_id
    generated_riv_bas_id_file = os.path.join(OUTPUT_DATA_PATH, 
                                             "riv_bas_id.csv")
    generated_riv_bas_id_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                      "riv_bas_id.csv")
    ok_(compare_csv_decimal_files(generated_riv_bas_id_file, 
                                  generated_riv_bas_id_file_solution))

    #kfac
    generated_kfac_file = os.path.join(OUTPUT_DATA_PATH, 
                                       "kfac.csv")
    generated_kfac_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                "kfac.csv")
    ok_(compare_csv_decimal_files(generated_kfac_file, 
                                  generated_kfac_file_solution))
    
    #k
    generated_k_file = os.path.join(OUTPUT_DATA_PATH, 
                                    "k.csv")
    generated_k_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                             "k.csv")
    ok_(compare_csv_decimal_files(generated_k_file, 
                                  generated_k_file_solution))

    #x
    generated_x_file = os.path.join(OUTPUT_DATA_PATH, 
                                    "x.csv")
    generated_x_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                             "x.csv")
    ok_(compare_csv_decimal_files(generated_x_file, 
                                  generated_x_file_solution))

    #weight_ecmwf_t1279
    generated_weight_ecmwf_t1279_file = os.path.join(OUTPUT_DATA_PATH, 
                                                     "weight_ecmwf_t1279.csv")
    generated_weight_ecmwf_t1279_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                              "weight_ecmwf_t1279.csv")
    ok_(compare_csv_decimal_files(generated_weight_ecmwf_t1279_file, 
                                  generated_weight_ecmwf_t1279_file_solution))

    #weight_ecmwf_tco369
    generated_weight_ecmwf_tco639_file = os.path.join(OUTPUT_DATA_PATH, 
                                                      "weight_ecmwf_tco639.csv")
    generated_weight_ecmwf_tco639_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                               "weight_ecmwf_tco639.csv")
    ok_(compare_csv_decimal_files(generated_weight_ecmwf_tco639_file, 
                                  generated_weight_ecmwf_tco639_file_solution))

    #weight_era_t511
    generated_weight_era_t511_file = os.path.join(OUTPUT_DATA_PATH, 
                                                  "weight_era_t511.csv")
    generated_weight_era_t511_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                           "weight_era_t511.csv")
    ok_(compare_csv_decimal_files(generated_weight_era_t511_file, 
                                  generated_weight_era_t511_file_solution))

    remove_files(generated_comid_lat_lon_z_file,
                 generated_rapid_connect_file,
                 generated_riv_bas_id_file,
                 generated_kfac_file,
                 generated_k_file,
                 generated_x_file,
                 generated_weight_ecmwf_t1279_file,
                 generated_weight_ecmwf_tco639_file,
                 generated_weight_era_t511_file)
Example #33
0
def test_gen_static_rapid_input():
    """
    Checks generating static RAPID input
    """
    print("TEST 1: TEST GENERATE STATIC RAPID INPUT DATA")
    CreateAllStaticECMWFRAPIDFiles(
        in_drainage_line=os.path.join(GIS_INPUT_DATA_PATH, 'flowline.shp'),
        river_id="COMID",
        length_id="LENGTHKM",
        slope_id="Slope",
        next_down_id="NextDownID",
        in_catchment=os.path.join(GIS_INPUT_DATA_PATH, 'catchment.shp'),
        catchment_river_id="FEATUREID",
        rapid_output_folder=OUTPUT_DATA_PATH,
        kfac_length_units="km",
    )

    #CHECK OUTPUT
    #comid_lat_lon_z
    generated_comid_lat_lon_z_file = os.path.join(OUTPUT_DATA_PATH,
                                                  "comid_lat_lon_z.csv")
    generated_comid_lat_lon_z_file_solution = os.path.join(
        COMPARE_DATA_PATH, "x-x", "comid_lat_lon_z.csv")
    assert (compare_csv_decimal_files(generated_comid_lat_lon_z_file,
                                      generated_comid_lat_lon_z_file_solution))

    #rapid_connect
    generated_rapid_connect_file = os.path.join(OUTPUT_DATA_PATH,
                                                "rapid_connect.csv")
    generated_rapid_connect_file_solution = os.path.join(
        COMPARE_DATA_PATH, "x-x", "rapid_connect.csv")
    assert (compare_csv_decimal_files(generated_rapid_connect_file,
                                      generated_rapid_connect_file_solution))

    #riv_bas_id
    generated_riv_bas_id_file = os.path.join(OUTPUT_DATA_PATH,
                                             "riv_bas_id.csv")
    generated_riv_bas_id_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                      "riv_bas_id.csv")
    assert (compare_csv_decimal_files(generated_riv_bas_id_file,
                                      generated_riv_bas_id_file_solution))

    #kfac
    generated_kfac_file = os.path.join(OUTPUT_DATA_PATH, "kfac.csv")
    generated_kfac_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x",
                                                "kfac.csv")
    # MPG: Generated kfac appears to be higher resolution than solution.
    # To avoid failure, we assert that the two are equal up to a relative
    # tolerance of 0.01.

    kfac = np.genfromtxt(generated_kfac_file, delimiter=',')
    kfac_solution = np.genfromtxt(generated_kfac_file_solution, delimiter=',')
    np.testing.assert_allclose(kfac, kfac_solution, rtol=1.0e-2)

    generated_k_file = os.path.join(OUTPUT_DATA_PATH, "k.csv")
    generated_k_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x", "k.csv")

    k = np.genfromtxt(generated_kfac_file, delimiter=',')
    k_solution = np.genfromtxt(generated_kfac_file_solution, delimiter=',')
    np.testing.assert_allclose(k, k_solution, rtol=1.0e-2)

    #x
    generated_x_file = os.path.join(OUTPUT_DATA_PATH, "x.csv")
    generated_x_file_solution = os.path.join(COMPARE_DATA_PATH, "x-x", "x.csv")
    assert (compare_csv_decimal_files(generated_x_file,
                                      generated_x_file_solution))

    #weight_ecmwf_t1279
    generated_weight_ecmwf_t1279_file = os.path.join(OUTPUT_DATA_PATH,
                                                     "weight_ecmwf_t1279.csv")
    generated_weight_ecmwf_t1279_file_solution = os.path.join(
        COMPARE_DATA_PATH, "x-x", "weight_ecmwf_t1279.csv")
    assert (compare_csv_decimal_files(
        generated_weight_ecmwf_t1279_file,
        generated_weight_ecmwf_t1279_file_solution))

    #weight_ecmwf_tco369
    generated_weight_ecmwf_tco639_file = os.path.join(
        OUTPUT_DATA_PATH, "weight_ecmwf_tco639.csv")
    generated_weight_ecmwf_tco639_file_solution = os.path.join(
        COMPARE_DATA_PATH, "x-x", "weight_ecmwf_tco639.csv")
    assert (compare_csv_decimal_files(
        generated_weight_ecmwf_tco639_file,
        generated_weight_ecmwf_tco639_file_solution))

    #weight_era_t511
    generated_weight_era_t511_file = os.path.join(OUTPUT_DATA_PATH,
                                                  "weight_era_t511.csv")
    generated_weight_era_t511_file_solution = os.path.join(
        COMPARE_DATA_PATH, "x-x", "weight_era_t511.csv")
    assert (compare_csv_decimal_files(generated_weight_era_t511_file,
                                      generated_weight_era_t511_file_solution))

    remove_files(generated_comid_lat_lon_z_file, generated_rapid_connect_file,
                 generated_riv_bas_id_file, generated_kfac_file,
                 generated_k_file, generated_x_file,
                 generated_weight_ecmwf_t1279_file,
                 generated_weight_ecmwf_tco639_file,
                 generated_weight_era_t511_file)
Example #34
0
def test_run_era_interim_inflow():
    """
    Checks generating inflow file from ERA Interim LSM
    """
    print("TEST 1: TEST GENERATE INFLOW FILE FROM ERA INTERIM DATA")

    rapid_input_path = os.path.join(RAPID_DATA_PATH, "x-x")
    rapid_output_path = os.path.join(OUTPUT_DATA_PATH, "output", "x-x")
    #Create testing environment
    try:
        os.mkdir(RAPID_DATA_PATH)
    except OSError:
        pass
    try:
        os.mkdir(os.path.join(OUTPUT_DATA_PATH, "output"))
    except OSError:
        pass

    try:
        copytree(os.path.join(COMPARE_DATA_PATH, "gis", "x-x"),
                 rapid_input_path)
    except OSError:
        pass

    #run main process
    run_lsm_rapid_process(
        rapid_executable_location=RAPID_EXE_PATH,
        cygwin_bin_location=CYGWIN_BIN_PATH,
        rapid_io_files_location=OUTPUT_DATA_PATH,
        lsm_data_location=os.path.join(LSM_INPUT_DATA_PATH, 'erai3'),
        simulation_start_datetime=datetime(1980, 1, 1),
        simulation_end_datetime=datetime(2014, 1, 31),
        generate_rapid_namelist_file=False,
        run_rapid_simulation=True,
        generate_return_periods_file=False,
        generate_seasonal_initialization_file=False,
        generate_initialization_file=True,
        use_all_processors=True,
    )

    #CHECK OUTPUT
    #m3_riv
    m3_file_name = "m3_riv_bas_erai_t511_3hr_20030121to20030122.nc"
    generated_m3_file = os.path.join(rapid_output_path, m3_file_name)
    generated_m3_file_solution = os.path.join(INFLOW_COMPARE_DATA_PATH,
                                              m3_file_name)
    #check other info in netcdf file
    d1 = Dataset(generated_m3_file)
    d2 = Dataset(generated_m3_file_solution)
    assert_almost_equal(d1.variables['m3_riv'][:],
                        d2.variables['m3_riv'][:],
                        decimal=5)
    if 'rivid' in d2.variables.keys():
        ok_((d1.variables['rivid'][:] == d2.variables['rivid'][:]).all())
    if 'lat' in d2.variables.keys():
        ok_((d1.variables['lat'][:] == d2.variables['lat'][:]).all())
    if 'lon' in d2.variables.keys():
        ok_((d1.variables['lon'][:] == d2.variables['lon'][:]).all())
    d1.close()
    d2.close()

    #qout file
    qout_file_name = "Qout_erai_t511_3hr_20030121to20030122.nc"
    generated_qout_file = os.path.join(rapid_output_path, qout_file_name)
    generated_qout_file_solution = os.path.join(INFLOW_COMPARE_DATA_PATH,
                                                qout_file_name)
    d1 = Dataset(generated_qout_file)
    d2 = Dataset(generated_qout_file_solution)
    assert_almost_equal(d1.variables['Qout'][:],
                        d2.variables['Qout'][:],
                        decimal=5)
    ok_((d1.variables['rivid'][:] == d2.variables['rivid'][:]).all())
    ok_((d1.variables['time'][:] == d2.variables['time'][:]).all())
    if 'lat' in d2.variables.keys():
        ok_((d1.variables['lat'][:] == d2.variables['lat'][:]).all())
    if 'lon' in d2.variables.keys():
        ok_((d1.variables['lon'][:] == d2.variables['lon'][:]).all())
    d1.close()
    d2.close()

    #initialization file
    qinit_file_name = "qinit_erai_t511_3hr_20030121to20030122.csv"
    generated_qinit_file = os.path.join(rapid_input_path, qinit_file_name)
    generated_qinit_file_solution = os.path.join(INFLOW_COMPARE_DATA_PATH,
                                                 qinit_file_name)

    ok_(
        compare_csv_decimal_files(generated_qinit_file,
                                  generated_qinit_file_solution))

    #cleanup
    remove_files(generated_qinit_file)
    rmtree(os.path.join(OUTPUT_DATA_PATH, "input"))
    rmtree(os.path.join(OUTPUT_DATA_PATH, "output"))