Exemple #1
0
def get_sorted_krg_wts(vg_str, rnd_pts, abs_thresh_wt):

    ord_krg_cls = OrdinaryKriging(xi=rnd_pts[:, 0],
                                  yi=rnd_pts[:, 1],
                                  zi=np.zeros(rnd_pts.shape[0]),
                                  xk=np.array([0.0]),
                                  yk=np.array([0.0]),
                                  model=vg_str)

    ord_krg_cls.krige()

    wts = ord_krg_cls.lambdas.ravel()

    wts.sort()

    if abs_thresh_wt:
        abs_wts = np.abs(wts)

        abs_wts_sum = abs_wts.sum()

        rel_wts = abs_wts / abs_wts_sum

        zero_idxs = rel_wts <= abs_thresh_wt

        non_zero_wts = wts[~zero_idxs]

        sclr = 1 / non_zero_wts.sum()

        assert zero_idxs.sum() < zero_idxs.size

        wts[zero_idxs] = 0.0
        wts[~zero_idxs] *= sclr

    assert (wts[1:] - wts[:-1]).min() >= 0.0

    assert np.isclose(wts.sum(), 1.0)

    return wts
                if use_dwd_stns_for_kriging:
                    print('using DWD stations to find Netatmo values')
                    measured_vals = netatmo_vals
                    used_vals = dwd_vals

                    xlabel = 'Netatmo observed values'
                    ylabel = 'Netatmo interpolated values using DWD data'
                    measured_stns = 'Netatmo'
                    used_stns = 'DWD'
                    plot_title_acc = '_using_DWD_stations_to_find_Netatmo_values_'

                    ordinary_kriging = OrdinaryKriging(
                        xi=x_dwd,
                        yi=y_dwd,
                        zi=dwd_vals,
                        xk=x_netatmo,
                        yk=y_netatmo,
                        model=vgs_model)

                try:
                    ordinary_kriging.krige()
                except Exception as msg:
                    print('Error while Kriging', msg)
                    continue

                # print('\nDistances are:\n', ordinary_kriging.in_dists)
                # print('\nVariances are:\n', ordinary_kriging.in_vars)
                # print('\nRight hand sides are:\n', ordinary_kriging.rhss)
                # print('\nzks are:', ordinary_kriging.zk)
                # print('\nest_vars are:\n', ordinary_kriging.est_vars)
                        else:
                            break
                    print('**Changed Variogram model to**\n', vgs_model_dwd)

                except Exception as msg:
                    print(msg)
                    print('Only Nugget variogram for this day')

            if type(vgs_model_dwd) != np.float and len(vgs_model_dwd) > 0:

                print('+++ KRIGING +++\n')

                ordinary_kriging_dwd_netatmo_comb = OrdinaryKriging(
                    xi=dwd_netatmo_xcoords,
                    yi=dwd_netatmo_ycoords,
                    zi=dwd_netatmo_edf,
                    xk=x_dwd_interpolate,
                    yk=y_dwd_interpolate,
                    model=vgs_model_dwd)

                ordinary_kriging_dwd_only = OrdinaryKriging(
                    xi=dwd_xcoords,
                    yi=dwd_ycoords,
                    zi=edf_dwd_vals,
                    xk=x_dwd_interpolate,
                    yk=y_dwd_interpolate,
                    model=vgs_model_dwd)

                ordinary_kriging_netatmo_only = OrdinaryKriging(
                    xi=netatmo_xcoords,
                    yi=netatmo_ycoords,
Exemple #4
0
            ppt_netatmo_vals_gd = np.round(
                np.array(netatmo_df_gd.values).ravel(), 2)

            #==================================================================
            netatmo_dwd_x_coords = np.concatenate(
                [netatmo_xcoords, dwd_xcoords])
            netatmo_dwd_y_coords = np.concatenate(
                [netatmo_ycoords, dwd_ycoords])
            netatmo_dwd_ppt_vals_gd = np.round(
                np.hstack((ppt_netatmo_vals_gd, ppt_dwd_vals)), 2).ravel()

            #print('\n+*-KRIGING WITH 1st and 2nd Filter-*+')
            ordinary_kriging_dwd_netatmo_ppt = OrdinaryKriging(
                xi=netatmo_dwd_x_coords,
                yi=netatmo_dwd_y_coords,
                zi=netatmo_dwd_ppt_vals_gd,
                xk=x_dwd_interpolate,
                yk=y_dwd_interpolate,
                model=vgs_model_dwd_ppt)

            try:
                ordinary_kriging_dwd_netatmo_ppt.krige()
            except Exception as msg:
                print('ERROR BEI OK DWD-NETATMO', msg)
            interpolated_vals_dwd_netatmo = (
                ordinary_kriging_dwd_netatmo_ppt.zk.copy())

            # put negative values to 0
            interpolated_vals_dwd_netatmo[
                interpolated_vals_dwd_netatmo < 0] = 0
Exemple #5
0
        netatmo_xcoords = netatmo_in_coords_df.loc[netatmo_edf.index,
                                                   'X'].values.ravel()

        netatmo_ycoords = netatmo_in_coords_df.loc[netatmo_edf.index,
                                                   'Y'].values.ravel()

        #===============================================================
        # # apply on event filter
        #===============================================================
        #print('NETATMO 2nd Filter')

        ordinary_kriging_filter_netamto = OrdinaryKriging(
            xi=dwd_xcoords,
            yi=dwd_ycoords,
            zi=edf_dwd_vals,
            xk=netatmo_xcoords,
            yk=netatmo_ycoords,
            model=vgs_model_dwd_ppt)

        try:
            ordinary_kriging_filter_netamto.krige()
        except Exception as msg:
            print('Error while Error Kriging', msg)

        # interpolated vals
        interpolated_vals = ordinary_kriging_filter_netamto.zk

        # calcualte standard deviation of estimated values
        std_est_vals = np.sqrt(ordinary_kriging_filter_netamto.est_vars)
Exemple #6
0
                            # uncertainty for dwd is 0
                            uncert_dwd = np.zeros(
                                shape=ppt_dwd_vals_nona.values.shape)

                            # combine both uncertainty terms
                            edf_dwd_netatmo_vals_uncert = np.concatenate([
                                netatmo_ppt_vals_fr_dwd_interp_unc, uncert_dwd
                            ])
                            #==================================================
                            # Start kriging ppt at DWD
                            #==================================================

                            ordinary_kriging_dwd_netatmo_ppt = OrdinaryKriging(
                                xi=netatmo_dwd_x_coords,
                                yi=netatmo_dwd_y_coords,
                                zi=netatmo_dwd_ppt_vals,
                                xk=x_dwd_interpolate,
                                yk=y_dwd_interpolate,
                                model=vgs_model_dwd_ppt)

                            ordinary_kriging_dwd_ppt = OrdinaryKriging(
                                xi=x_dwd_all,
                                yi=y_dwd_all,
                                zi=ppt_dwd_vals_nona.values,
                                xk=x_dwd_interpolate,
                                yk=y_dwd_interpolate,
                                model=vgs_model_dwd_ppt)
                            #
                            # kriging with uncertainty
                            ordinary_kriging_dwd_netatmo_ppt_unc = OrdinaryKrigingWithUncertainty(
                                xi=netatmo_dwd_x_coords,
                        netatmo_qts = netatmo_in_vals_df.loc[
                            event_date,
                            netatmo_stns_names_evt].dropna().values.ravel()
                        dwd_netatmo_qts = np.concatenate(
                            [dwd_qts, netatmo_qts])
                        # get VG model from transformed DWD values
                        vgs_model_dwd_transf = df_vgs_transf.loc[event_date, 1]

                        #==============================================
                        # KRIGING
                        #==============================================
                        #                             print('\n+++ KRIGING +++\n')
                        ordinary_kriging_dwd_netatmo_comb = OrdinaryKriging(
                            xi=dwd_netatmo_xcoords,
                            yi=dwd_netatmo_ycoords,
                            zi=dwd_netatmo_vals,
                            xk=x_dwd_interpolate,
                            yk=y_dwd_interpolate,
                            model=vgs_model_dwd_transf)

                        # qts
                        ordinary_kriging_dwd_netatmo_comb_qt = OrdinaryKriging(
                            xi=dwd_netatmo_xcoords,
                            yi=dwd_netatmo_ycoords,
                            zi=dwd_netatmo_qts,
                            xk=x_dwd_interpolate,
                            yk=y_dwd_interpolate,
                            model=vgs_model_dwd_transf)
                        #                     ordinary_kriging_un_dwd_netatmo_comb = OrdinaryKrigingWithUncertainty(
                        #                         xi=dwd_netatmo_xcoords,
                        #                         yi=dwd_netatmo_ycoords,
Exemple #8
0
            if vg_scaling_ratio == 0:
                vg_scaling_ratio = 1

            # rescale variogram
            vgs_model_dwd_ppt = str(np.round(
                vg_scaling_ratio, 4)) + ' ' + vgs_model_dwd_ppt.split(" ")[1]
            # print(vgs_model_dwd_ppt)

            dwd_xcoords = np.array(x_dwd_all_ngbrs)
            dwd_ycoords = np.array(y_dwd_all_ngbrs)
            ppt_dwd_vals = np.array(ppt_dwd_vals_nona)

            # using DWD data
            ordinary_kriging_dwd_ppt = OrdinaryKriging(xi=dwd_xcoords,
                                                       yi=dwd_ycoords,
                                                       zi=ppt_dwd_vals,
                                                       xk=x_dwd_interpolate,
                                                       yk=y_dwd_interpolate,
                                                       model=vgs_model_dwd_ppt)
            # print('\nOK using DWD')
            ordinary_kriging_dwd_ppt.krige()

            interpolated_vals_dwd_only = ordinary_kriging_dwd_ppt.zk.copy()
            interpolated_vals_dwd_only[interpolated_vals_dwd_only < 0] = 0

            #==========================================================
            # NO FILTER USED
            #==========================================================
            # print('\n**NETATMO NOT FILTERED**')
            netatmo_ppt_vals_fr_dwd_interp = np.array(ppt_netatmo_vals_nona)

            x_netatmo_ppt_vals_fr_dwd_interp = np.array(x_netatmo_all_ngbrs)
#         plt.legend(loc=4, framealpha=0.7)
#         plt.show()
#         plt.savefig(
#             str(self._out_figs_path / f'{date_str}.png'),
#             bbox_inches='tight')
#
#         plt.close()

    if type(vgs_model) != np.float:
        print('**Changed Variogram model to**\n', vgs_model)
        print('+++ KRIGING +++\n')
        vgs_list_all.append(vgs_model)
        ordinary_kriging = OrdinaryKriging(
            xi=dwd_xcoords,
            yi=dwd_ycoords,
            zi=ppt_vals,
            xk=x_netatmo,
            yk=y_netatmo,
            model=vgs_model)

        try:
            ordinary_kriging.krige()
        except Exception as msg:
            print('Error while Kriging', msg)

        interpolated_vals = ordinary_kriging.zk.copy()

    else:
        interpolated_vals = np.nan

    print('+++ Saving result to DF +++\n')
Exemple #10
0
                    except Exception as msg:
                        print(msg)
                        print('Only Nugget variogram for this day')

                # if type(vgs_model_dwd) != np.float and len(vgs_model_dwd) >
                # 0:
                if ('Nug' in vgs_model_dwd or len(vgs_model_dwd) > 0) and (
                        'Exp' in vgs_model_dwd or 'Sph' in vgs_model_dwd):
                    print('**Changed Variogram model to**\n', vgs_model_dwd)
                    print('\n+++ KRIGING +++\n')

                    ordinary_kriging_dwd_only = OrdinaryKriging(
                        xi=dwd_xcoords,
                        yi=dwd_ycoords,
                        zi=ppt_dwd_vals,
                        xk=x_dwd_interpolate,
                        yk=y_dwd_interpolate,
                        model=vgs_model_dwd)

                    try:

                        ordinary_kriging_dwd_only.krige()

                    except Exception as msg:
                        print('Error while Kriging', msg)

                    interpolated_vals_dwd_only = ordinary_kriging_dwd_only.zk.copy(
                    )

                    print('**Interpolated DWD: ', interpolated_vals_dwd_only)
                    print(msg)
                    print('Only Nugget variogram for this day')

            # if type(vgs_model_dwd) != np.float and len(vgs_model_dwd) >
            # 0:
            if ('Nug' in vgs_model_dwd
                    or len(vgs_model_dwd) > 0) and (
                    'Exp' in vgs_model_dwd or
                    'Sph' in vgs_model_dwd):
                #                 print('**Changed Variogram model to**\n\n', vgs_model_dwd)
                print('\n+++ KRIGING +++\n')

                ordinary_kriging_dwd_only = OrdinaryKriging(
                    xi=dwd_xcoords_new,
                    yi=dwd_ycoords_new,
                    zi=ppt_vals_dwd_new_for_obsv_ppt,
                    xk=x_netatmo_interpolate,
                    yk=y_netatmo_interpolate,
                    model=vgs_model_dwd)

                try:
                    ordinary_kriging_dwd_only.krige()
                except Exception as msg:
                    print('Error while Kriging', msg)

                interpolated_vals_dwd_only = ordinary_kriging_dwd_only.zk.copy()

                if interpolated_vals_dwd_only < 0:
                    interpolated_vals_dwd_only = np.nan

                print('**Interpolated PPT by DWD recent: ',
Exemple #12
0
    def krige(self, n_krige_intervals):
        assert self._epsg_set_flag
        assert self._bck_shp_set_flag
        assert self._coords_arr_set_flag
        assert self._sel_cps_arr_set_flag
        assert self._cp_rules_arr_set_flag
        assert self._anom_arr_set_flag
        assert self._other_prms_set_flag

        assert isinstance(n_krige_intervals, int)
        assert n_krige_intervals > 0

        assert self.sel_cps_arr.shape[0] == self.anom_arr.shape[0]

        assert self.anom_arr.shape[1] == self.n_pts

        self.n_krige_intervals = n_krige_intervals

        self._prep_coords()

        self.best_cps_mean_anoms = np.empty(shape=(self.n_cps, self.n_pts),
                                           dtype=float)
        self.best_cps_std_anoms = np.empty(shape=(self.n_cps,
                                                  self.y_coords.shape[0],
                                                  self.x_coords.shape[0]),
                                           dtype=float)

        self.best_cps_min_anoms = self.best_cps_std_anoms.copy()
        self.best_cps_max_anoms = self.best_cps_std_anoms.copy()

        (self.x_coords_mesh,
         self.y_coords_mesh) = np.meshgrid(self.x_coords,
                                           self.y_coords)

        (self.x_coords_mesh_rav,
         self.y_coords_mesh_rav) = (self.x_coords_mesh.ravel(),
                                    self.y_coords_mesh.ravel())

        assert self.x_coords_mesh_rav.shape[0] == self.n_pts

        for j in range(self.n_cps):
            _ = self.sel_cps_arr == j
            _ = self.anom_arr[_]

            _1 = _.mean(axis=0)
            _2 = _.std(axis=0)
            _3 = _.min(axis=0)
            _4 = _.max(axis=0)

            self.best_cps_mean_anoms[j] = _1

            self.best_cps_std_anoms[j] = _2.reshape((self.y_coords.shape[0],
                                                     self.x_coords.shape[0]))

            self.best_cps_min_anoms[j] = _3.reshape((self.y_coords.shape[0],
                                                     self.x_coords.shape[0]))

            self.best_cps_max_anoms[j] = _4.reshape((self.y_coords.shape[0],
                                                     self.x_coords.shape[0]))

        self.krige_z_coords = np.zeros((self.n_cps, self.n_pts_krige))
        self.krige_z_coords_mesh = np.zeros((self.n_cps,
                                             *self.krige_pts_shape))

        self.cp_x_coords_list = []
        self.cp_y_coords_list = []
        self.cp_z_coords_list = []
        self.vgs_list = []

        for j in range(self.n_cps):
            if self.msgs:
                print('Kriging CP:', (j))

            curr_mean_cp = self.best_cps_mean_anoms[j]

            if self.anom_type != 'd':
                curr_false_idxs = self.cp_rules_arr[j] == self.n_fuzz_nos
            else:
                curr_false_idxs = (
                    np.zeros(self.best_cps_mean_anoms[j].shape[0], dtype=bool))

            curr_true_idxs = np.logical_not(curr_false_idxs)
            curr_mean_cp[curr_false_idxs] = np.nan

            curr_cp_vals = curr_mean_cp[curr_true_idxs]
            curr_x_coord_vals = self.x_coords_mesh_rav[curr_true_idxs]
            curr_y_coord_vals = self.y_coords_mesh_rav[curr_true_idxs]

            curr_mean_cp = curr_mean_cp.reshape(self.x_coords_mesh.shape)

            #==================================================================
            # Reproject to out_epsg
            #==================================================================

            curr_re_x_coords, curr_re_y_coords = change_pts_crs(
                curr_x_coord_vals,
                curr_y_coord_vals,
                self.anom_epsg,
                self.out_epsg)

            self.cp_x_coords_list.append(curr_re_x_coords)
            self.cp_y_coords_list.append(curr_re_y_coords)
            self.cp_z_coords_list.append(curr_cp_vals)

            #==================================================================
            # Fit Variogram
            #==================================================================
            variogram = Variogram(
                x=curr_re_x_coords,
                y=curr_re_y_coords,
                z=curr_cp_vals,
                perm_r_list=[1],
                fit_vgs=['Sph'])

            variogram.fit()
            fit_vg = variogram.vg_str_list[0]
            assert fit_vg

            self.vgs_list.append(fit_vg)

            if self.msgs:
                print('Fitted variogram is:', fit_vg)

            #==================================================================
            # Krige
            #==================================================================
            ord_krig = OrdinaryKriging(xi=curr_re_x_coords,
                                       yi=curr_re_y_coords,
                                       zi=curr_cp_vals,
                                       xk=self.krige_x_coords,
                                       yk=self.krige_y_coords,
                                       model=fit_vg)
            ord_krig.krige()

            krige_z_coords = ord_krig.zk
            self.krige_z_coords[j] = krige_z_coords

            assert check_nans_finite(krige_z_coords)

            krige_z_coords_mesh = krige_z_coords.reshape(self.krige_pts_shape)
            self.krige_z_coords_mesh[j] = krige_z_coords_mesh

        self._kriged_flag = True
        return
                                        dwd_xcoords_new.append(stn_xcoords_new)
                                        dwd_ycoords_new.append(stn_xcoords_new)
                                except Exception as msg:
                                    print(msg)
                                    continue

                        ppt_vals_dwd_new_for_obsv_ppt = np.array(
                            ppt_vals_dwd_new_for_obsv_ppt)
                        dwd_xcoords_new = np.array(dwd_xcoords_new)
                        dwd_ycoords_new = np.array(dwd_ycoords_new)

                        ordinary_kriging_dwd_netatmo_crt = OrdinaryKriging(
                            xi=dwd_xcoords_new,
                            yi=dwd_ycoords_new,
                            zi=ppt_vals_dwd_new_for_obsv_ppt,
                            xk=x_netatmo_interpolate,
                            yk=y_netatmo_interpolate,
                            model=vgs_model_dwd)

                        try:
                            ordinary_kriging_dwd_netatmo_crt.krige()
                        except Exception as msg:
                            print('Error while Kriging', msg)

                        interpolated_netatmo_prct = ordinary_kriging_dwd_netatmo_crt.zk.copy(
                        )

                        if interpolated_netatmo_prct < 0:
                            interpolated_netatmo_prct = np.nan