Exemplo n.º 1
0
def main():

    main_dir = Path(
        r'P:\Synchronize\IWS\Testings\fourtrans_practice\multisite_phs_spec_corr\5min'
    )
    os.chdir(main_dir)

    path_to_shp = '../precipitation_validation_stns.shp'
    label_field = 'id'

    var = 'ifted'

    path_to_ras = Path(f'{var}/{var}.nc')

    nc_x_crds_label = 'X'
    nc_y_crds_label = 'Y'
    nc_variable_labels = ['OK']
    nc_time_label = 'time'

    sep = ';'
    float_fmt = '%0.2f'

    out_dir = path_to_ras.parents[0]

    verbose = True

    EP = ExtractPoints(verbose=verbose)

    EP.set_input(path_to_shp, label_field)

    EP.extract_points()

    GCII = GeomAndCrdsItsctIdxs(verbose=verbose)

    nc_crds_cls = ExtractNetCDFCoords(verbose=verbose)

    nc_crds_cls.set_input(path_to_ras, nc_x_crds_label, nc_y_crds_label)
    nc_crds_cls.extract_coordinates()

    GCII.set_geometries(EP.get_points(), EP._geom_type)

    GCII.set_coordinates(nc_crds_cls.get_x_coordinates(),
                         nc_crds_cls.get_y_coordinates(),
                         nc_crds_cls._raster_type_lab)

    GCII.verify()
    GCII.compute_intersect_indices()

    out_crds = GCII.get_intersect_indices()

    cols = list(out_crds.keys())
    out_crds_df = pd.DataFrame(columns=['X', 'Y'], dtype=float)
    for col in cols:
        out_crds_df.loc[col, ['X', 'Y']] = (out_crds[col]['x_crds'][0],
                                            out_crds[col]['y_crds'][0])

    out_crds_df.to_csv(out_dir / f'ts_crds.csv',
                       sep=sep,
                       float_format=float_fmt)

    for nc_variable_label in nc_variable_labels:
        nc_vals_cls = ExtractNetCDFValues(verbose=verbose)

        nc_vals_cls.set_input(path_to_ras, nc_variable_label, nc_time_label)
        nc_vals_cls.set_output(None)

        nc_vals_cls.extract_values(GCII.get_intersect_indices())

        out_vals = nc_vals_cls.get_values()

        cols = list(out_vals.keys())
        out_df = None
        for col in cols:
            out_ser = pd.Series(index=pd.to_datetime(
                list(out_vals[col].keys()), format=nc_vals_cls._time_strs_fmt),
                                data=list(out_vals[col].values()),
                                dtype=float)

            if out_df is None:
                out_df = pd.DataFrame(data={col: out_ser})

            else:
                out_df[col] = out_ser

        out_df.to_csv(out_dir / f'ts_{nc_variable_label}.csv',
                      sep=sep,
                      float_format=float_fmt)

    return
Exemplo n.º 2
0
def main():

    #     main_dir = Path(r'P:\Downloads\spinterp_pt_extract')
    main_dir = Path(
        r'X:\hiwi\ElHachem\Prof_Bardossy\Handook_Water_resources_Management')
    os.chdir(main_dir)

    #     path_to_shp = 'neckar_46_stns_20180624.shp'
    path_to_shp = r"X:\hiwi\ElHachem\Prof_Bardossy\Handook_Water_resources_Management\Nile_qgis\nile_basin\nile_basin.shp"
    label_field = 'field_1'

    #     path_to_ras = r'tem_spinterp.nc'
    #     input_ras_type = 'nc'

    path_to_ras = (
        r"C:\Users\hachem\Downloads\7504448\global-et0_monthly.tif\et0_month\et0_01.tif"
    )
    input_ras_type = 'gtiff'

    nc_x_crds_label = 'X'
    nc_y_crds_label = 'Y'
    nc_variable_labels = ['OK', 'SK']
    nc_time_label = 'time'

    #     path_to_output = 'test.h5'
    path_to_output = ''
    verbose = True

    EP = ExtractPoints(verbose=verbose)

    EP.set_input(path_to_shp, label_field)

    EP.extract_points()

    #     print(EP.get_labels())
    #     print(EP.get_points())
    #     print(EP.get_x_coordinates())
    #     print(EP.get_y_coordinates())

    GCII = GeomAndCrdsItsctIdxs(verbose=verbose)

    if input_ras_type == 'gtiff':
        gtiff_crds_cls = ExtractGTiffCoords(verbose=verbose)

        gtiff_crds_cls.set_input(path_to_ras)
        gtiff_crds_cls.extract_coordinates()

        GCII.set_geometries(EP.get_points(), EP._geom_type)

        GCII.set_coordinates(gtiff_crds_cls.get_x_coordinates(),
                             gtiff_crds_cls.get_y_coordinates(),
                             gtiff_crds_cls._raster_type_lab)

        GCII.verify()
        GCII.compute_intersect_indices()

        gtiff_vals_cls = ExtractGTiffValues(verbose=verbose)

        gtiff_vals_cls.set_input(path_to_ras)
        gtiff_vals_cls.set_output(path_to_output)

        gtiff_vals_cls.extract_values(GCII.get_intersect_indices())

    elif input_ras_type == 'nc':
        nc_crds_cls = ExtractNetCDFCoords(verbose=verbose)

        nc_crds_cls.set_input(path_to_ras, nc_x_crds_label, nc_y_crds_label)
        nc_crds_cls.extract_coordinates()

        GCII.set_geometries(EP.get_points(), EP._geom_type)

        GCII.set_coordinates(nc_crds_cls.get_x_coordinates(),
                             nc_crds_cls.get_y_coordinates(),
                             nc_crds_cls._raster_type_lab)

        GCII.verify()
        GCII.compute_intersect_indices()

        for nc_variable_label in nc_variable_labels:
            nc_vals_cls = ExtractNetCDFValues(verbose=verbose)

            nc_vals_cls.set_input(path_to_ras, nc_variable_label,
                                  nc_time_label)
            nc_vals_cls.set_output(path_to_output)

            nc_vals_cls.extract_values(GCII.get_intersect_indices())

    else:
        raise NotImplementedError

    return
Exemplo n.º 3
0
def main():

    main_dir = Path(
        r'P:\Synchronize\IWS\Testings\fourtrans_practice\ft_spatio_temporal_interps'
    )
    os.chdir(main_dir)

    path_to_shp = 'temperature_validation_stns.shp'
    label_field = 'field_1'

    path_to_ras = r'temperature_interpolation_validation\obs\temperature_kriging_1989-01-01_to_1992-12-30_1km_obs.nc'
    input_ras_type = 'nc'

    #     path_to_ras = (
    #         r'lower_de_gauss_z3_2km_atkis_19_extended_hydmod_lulc_ratios.tif')
    #     input_ras_type = 'gtiff'

    nc_x_crds_label = 'X'
    nc_y_crds_label = 'Y'
    nc_variable_labels = ['OK']
    nc_time_label = 'time'

    path_to_output = r'temperature_interpolation_validation\obs\ts_valid.h5'

    verbose = True

    EP = ExtractPoints(verbose=verbose)

    EP.set_input(path_to_shp, label_field)

    EP.extract_points()

    #     print(EP.get_labels())
    #     print(EP.get_points())
    #     print(EP.get_x_coordinates())
    #     print(EP.get_y_coordinates())

    GCII = GeomAndCrdsItsctIdxs(verbose=verbose)

    if input_ras_type == 'gtiff':
        gtiff_crds_cls = ExtractGTiffCoords(verbose=verbose)

        gtiff_crds_cls.set_input(path_to_ras)
        gtiff_crds_cls.extract_coordinates()

        GCII.set_geometries(EP.get_points(), EP._geom_type)

        GCII.set_coordinates(gtiff_crds_cls.get_x_coordinates(),
                             gtiff_crds_cls.get_y_coordinates(),
                             gtiff_crds_cls._raster_type_lab)

        GCII.verify()
        GCII.compute_intersect_indices()

        gtiff_vals_cls = ExtractGTiffValues(verbose=verbose)

        gtiff_vals_cls.set_input(path_to_ras)
        gtiff_vals_cls.set_output(path_to_output)

        gtiff_vals_cls.extract_values(GCII.get_intersect_indices())

    elif input_ras_type == 'nc':
        nc_crds_cls = ExtractNetCDFCoords(verbose=verbose)

        nc_crds_cls.set_input(path_to_ras, nc_x_crds_label, nc_y_crds_label)
        nc_crds_cls.extract_coordinates()

        GCII.set_geometries(EP.get_points(), EP._geom_type)

        GCII.set_coordinates(nc_crds_cls.get_x_coordinates(),
                             nc_crds_cls.get_y_coordinates(),
                             nc_crds_cls._raster_type_lab)

        GCII.verify()
        GCII.compute_intersect_indices()

        for nc_variable_label in nc_variable_labels:
            nc_vals_cls = ExtractNetCDFValues(verbose=verbose)

            nc_vals_cls.set_input(path_to_ras, nc_variable_label,
                                  nc_time_label)
            nc_vals_cls.set_output(path_to_output)

            nc_vals_cls.extract_values(GCII.get_intersect_indices())

    else:
        raise NotImplementedError

    return
def main():

    main_dir = Path(r'P:\Downloads\spinterp_nc_gtiff_test')
    os.chdir(main_dir)

    path_to_shp = r'watersheds.shp'

    label_field = r'DN'

    path_to_nc = r'tem_spinterp.nc'

    path_to_ras = (
        r'lower_de_gauss_z3_2km_atkis_19_extended_hydmod_lulc_ratios.tif')

    nc_x_crds_label = 'X'
    nc_y_crds_label = 'Y'
    nc_variable_labels = ['OK', 'SK']
    nc_time_label = 'time'

    reorder_sequence = 'nc_gtiff'
    #     reorder_sequence = 'gtiff_nc'

    maximum_reorder_threshold_distance = 100

    path_to_h5_output = 'test.h5'

    save_xy_crds_txt_flag = True

    verbose = True

    ##########################################################################
    poly_cls = ExtractPolygons(verbose=verbose)

    poly_cls.set_input(path_to_shp, label_field)

    poly_cls.extract_polygons()

    assert isinstance(nc_variable_labels, (list, tuple)), (
        'variable_labels can only be a list or tuple having strings!')

    assert all([
        isinstance(x, str) for x in nc_variable_labels
    ]), ('variable_labels can only be a list or tuple having strings!')

    ##########################################################################

    if reorder_sequence == 'nc_gtiff':
        nc_crds_cls = ExtractNetCDFCoords(verbose=verbose)

        nc_crds_cls.set_input(path_to_nc, nc_x_crds_label, nc_y_crds_label)

        nc_crds_cls.extract_coordinates()

        nc_itsct_cls = GeomAndCrdsItsctIdxs(verbose=verbose)

        nc_itsct_cls.set_geometries(poly_cls.get_polygons(),
                                    poly_cls._geom_type)

        nc_itsct_cls.set_coordinates(nc_crds_cls.get_x_coordinates(),
                                     nc_crds_cls.get_y_coordinates(),
                                     nc_crds_cls._raster_type_lab)

        nc_itsct_cls.verify()

        nc_itsct_cls.compute_intersect_indices()

        if save_xy_crds_txt_flag:
            for label in nc_itsct_cls.get_intersect_indices():
                x = nc_itsct_cls.get_intersect_indices()[label]['x_cen_crds']
                y = nc_itsct_cls.get_intersect_indices()[label]['y_cen_crds']

                pd.DataFrame(data={
                    'x': x,
                    'y': y
                }).to_csv(f'{label}.csv', index=False, sep=';')

        for nc_variable_label in nc_variable_labels:
            nc_vals_cls = ExtractNetCDFValues(verbose=verbose)

            nc_vals_cls.set_input(path_to_nc, nc_variable_label, nc_time_label)
            nc_vals_cls.set_output(path_to_h5_output)

            nc_vals_cls.extract_values(nc_itsct_cls.get_intersect_indices())

        gtiff_crds_cls = ExtractGTiffCoords(verbose=verbose)

        gtiff_crds_cls.set_input(path_to_ras)

        gtiff_crds_cls.extract_coordinates()

        ras_itsct_cls = GeomAndCrdsItsctIdxs()

        ras_itsct_cls.set_geometries(poly_cls.get_polygons(),
                                     poly_cls._geom_type)

        ras_itsct_cls.set_coordinates(gtiff_crds_cls.get_x_coordinates(),
                                      gtiff_crds_cls.get_y_coordinates(),
                                      gtiff_crds_cls._raster_type_lab)

        ras_itsct_cls.verify()

        ras_itsct_cls.compute_intersect_indices()

        reorder_idxs_cls = ReOrderIdxs(verbose=verbose)

        reorder_idxs_cls.set_reference(nc_itsct_cls.get_intersect_indices())
        reorder_idxs_cls.set_destination(ras_itsct_cls.get_intersect_indices())

        reorder_idxs_cls.reorder(maximum_reorder_threshold_distance)

        # without rordering
        #         ras_idxs = ras_itsct_cls.get_intersect_indices()
        #         ignore_rows_cols_equality = False

        # with reordering
        ras_idxs = reorder_idxs_cls.get_reordered_destination()
        ignore_rows_cols_equality = True

        gtiff_vals_cls = ExtractGTiffValues(verbose=verbose)

        gtiff_vals_cls.set_input(path_to_ras)
        gtiff_vals_cls.set_output(path_to_h5_output)

        gtiff_vals_cls.extract_values(
            ras_idxs, ignore_rows_cols_equality=ignore_rows_cols_equality)

    ##########################################################################

    elif reorder_sequence == 'gtiff_nc':
        gtiff_crds_cls = ExtractGTiffCoords(verbose=verbose)

        gtiff_crds_cls.set_input(path_to_ras)

        gtiff_crds_cls.extract_coordinates()

        ras_itsct_cls = GeomAndCrdsItsctIdxs()

        ras_itsct_cls.set_geometries(poly_cls.get_polygons(),
                                     poly_cls._geom_type)

        ras_itsct_cls.set_coordinates(gtiff_crds_cls.get_x_coordinates(),
                                      gtiff_crds_cls.get_y_coordinates(),
                                      gtiff_crds_cls._raster_type_lab)

        ras_itsct_cls.verify()

        ras_itsct_cls.compute_intersect_indices()

        if save_xy_crds_txt_flag:
            for label in ras_itsct_cls.get_intersect_indices():
                x = ras_itsct_cls.get_intersect_indices()[label]['x_cen_crds']
                y = ras_itsct_cls.get_intersect_indices()[label]['y_cen_crds']

                pd.DataFrame(data={
                    'x': x,
                    'y': y
                }).to_csv(f'{label}.csv', index=False, sep=';')

        gtiff_vals_cls = ExtractGTiffValues(verbose=verbose)

        gtiff_vals_cls.set_input(path_to_ras)
        gtiff_vals_cls.set_output(path_to_h5_output)

        gtiff_vals_cls.extract_values(ras_itsct_cls.get_intersect_indices())

        nc_crds_cls = ExtractNetCDFCoords(verbose=verbose)

        nc_crds_cls.set_input(path_to_nc, nc_x_crds_label, nc_y_crds_label)

        nc_crds_cls.extract_coordinates()

        nc_itsct_cls = GeomAndCrdsItsctIdxs(verbose=verbose)

        nc_itsct_cls.set_geometries(poly_cls.get_polygons(),
                                    poly_cls._geom_type)

        nc_itsct_cls.set_coordinates(nc_crds_cls.get_x_coordinates(),
                                     nc_crds_cls.get_y_coordinates(),
                                     nc_crds_cls._raster_type_lab)

        nc_itsct_cls.verify()

        nc_itsct_cls.compute_intersect_indices()

        reorder_idxs_cls = ReOrderIdxs(verbose=verbose)

        reorder_idxs_cls.set_reference(ras_itsct_cls.get_intersect_indices())
        reorder_idxs_cls.set_destination(nc_itsct_cls.get_intersect_indices())

        reorder_idxs_cls.reorder(maximum_reorder_threshold_distance)

        # without rordering
        #         ras_idxs = ras_itsct_cls.get_intersect_indices()
        #         ignore_rows_cols_equality = False

        # with reordering
        nc_idxs = reorder_idxs_cls.get_reordered_destination()
        ignore_rows_cols_equality = True

        for nc_variable_label in nc_variable_labels:
            nc_vals_cls = ExtractNetCDFValues(verbose=verbose)

            nc_vals_cls.set_input(path_to_nc, nc_variable_label, nc_time_label)
            nc_vals_cls.set_output(path_to_h5_output)

            nc_vals_cls.extract_values(
                nc_idxs, ignore_rows_cols_equality=ignore_rows_cols_equality)

    else:
        raise NotImplementedError

    return