Example #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
                    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)
                # print('\nlambdas are:\n', ordinary_kriging.lambdas)
                # print('\nmus are:\n', ordinary_kriging.mus)
                # print('\n\n')

                # interpolated vals
                interpolated_vals = ordinary_kriging.zk
                    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,
                    zi=edf_netatmo_vals,
                    xk=x_dwd_interpolate,
                    yk=y_dwd_interpolate,
                    model=vgs_model_dwd)

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

                interpolated_vals_dwd_netatmo = ordinary_kriging_dwd_netatmo_comb.zk.copy(
                )
                interpolated_vals_dwd_only = ordinary_kriging_dwd_only.zk.copy(
                )
                interpolated_vals_netatmo_only = ordinary_kriging_netatmo_only.zk.copy(
                )

                if interpolated_vals_dwd_netatmo < 0:
                    interpolated_vals_dwd_netatmo = np.nan
Example #4
0
            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

            #======================================================
            # # SAVING PPT
            #======================================================

            df_interpolated_dwd_netatmos_comb.loc[
                event_date, stn_comb] = np.round(interpolated_vals_dwd_netatmo,
Example #5
0
        #===============================================================
        # # 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)

        # calculate difference observed and estimated  # values
        try:
            diff_obsv_interp = np.abs(netatmo_edf.values - interpolated_vals)
        except Exception:
            print('ERROR 2nd FILTER')
Example #6
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)

                    if interpolated_vals_dwd_only < 0:
                        interpolated_vals_dwd_only = np.nan

                else:
                    print('no good variogram found, adding nans to df')
                    interpolated_vals_dwd_only = np.nan
Example #7
0
                    if use_temporal_filter_after_kriging:

                        print('using DWD stations to find Netatmo values')
                        print('apllying on event 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)

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

                        # 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)
                        # calculate difference observed and estimated values
                        diff_obsv_interp = np.abs(
                            edf_netatmo_vals - interpolated_vals)

                        #======================================================
Example #8
0
File: cps.py Project: faizan90/wcpc
    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