def KrigeDems(dems,ncp):
    cpDems=[]
    krigeDemsTensor =torch.FloatTensor(opt.batchSize,nc,opt.imageSize,opt.imageSize).zero_()# all ncp control points in one layer
    krigeDemsTensor =Variable(krigeDemsTensor)
    if(opt.cuda):
        krigeDemsTensor = krigeDemsTensor.cuda()
    x_index=[]
    y_index=[]
    step=float((opt.imageSize-1)/(np.sqrt(ncp)-1))   
    #print step,pad
    for i in range(0,int(np.floor(np.sqrt(ncp)))):
        x_index.append(i*step)
        y_index.append(i*step)
    for n in range(0,opt.batchSize):
        cpDems.append([])
        for i in x_index:
            for j in y_index:
                cpDems[n].append([int(round(j)),int(round(i)),dems.data[n,0,int(round(j)),int(round(i))]])# extract dem control point function
        cpDems[n]=np.array(cpDems[n])
        
    gridx = np.arange(0.0, float(opt.imageSize), 1)
    gridy = np.arange(0.0, float(opt.imageSize), 1)
    for n in range(0,opt.batchSize):
        OK = OrdinaryKriging(cpDems[n][:, 0], cpDems[n][:, 1], cpDems[n][:, 2], variogram_model='spherical',
                         verbose=False, enable_plotting=False, nlags=100)
        z,ss=OK.execute('grid',gridx,gridy,backend='loop')
        for i in gridx:
            for j in gridy:
                    krigeDemsTensor[n,0,i,j]=z[int(j)][int(i)]# different transpose for the z and dems
        #kt.write_asc_grid(gridx, gridy, z, filename="output"+str(n)+".asc")
    return krigeDemsTensor
Beispiel #2
0
    def modelo(latt, longg, ncbz):
        glo, gla = fazer_grid(latt, longg)
        Long = []
        Lat = []
        Dac = []
        Data = []
        for i in range(0, len(gla), 1):
            for j in range(0, len(gla), 1):
                val = encontrar(gla[i][j], glo[i][j], ncbz)
                Data.append(val[0])
                Lat.append(val[1])
                Long.append(val[2])
                Dac.append(val[3])

        # Quando os quatro nós apresentam o mesmo valor, a Krigagem não ocorre ----------------------------------------

        if Dac[0] == Dac[1] and Dac[0] == Dac[2] and Dac[0] == Dac[3]:
            z_dac = Dac[0]
        else:
            OK_re = OrdinaryKriging(Long,
                                    Lat,
                                    Dac,
                                    variogram_model='linear',
                                    verbose=False,
                                    enable_plotting=False,
                                    coordinates_type='geographic')
            z_dac = OK_re.execute('points', longg, latt)[0][0]
        return latt, longg, z_dac
def KrigeDems_random(dems, ncp):
    cpDems = []
    krigeDemsTensor = torch.FloatTensor(
        opt.batchSize, nc, opt.imageSize,
        opt.imageSize).zero_()  # all ncp control points in one layer
    krigeDemsTensor = Variable(krigeDemsTensor)
    if (opt.cuda):
        krigeDemsTensor = krigeDemsTensor.cuda()
    for n in range(0, opt.batchSize):
        cpDems.append([])
        for ite in range(0, ncp):
            i = random.randint(0, opt.imageSize - 1)
            j = random.randint(0, opt.imageSize - 1)
            cpDems[n].append([j, i, dems.data[n, 0, j, i]
                              ])  # extract dem control point function
        cpDems[n] = np.array(cpDems[n])

    gridx = np.arange(0.0, float(opt.imageSize), 1)
    gridy = np.arange(0.0, float(opt.imageSize), 1)
    for n in range(0, opt.batchSize):
        OK = OrdinaryKriging(cpDems[n][:, 0],
                             cpDems[n][:, 1],
                             cpDems[n][:, 2],
                             variogram_model='power',
                             verbose=False,
                             enable_plotting=False,
                             nlags=100)
        z, ss = OK.execute('grid', gridx, gridy, backend='loop')
        for i in gridx:
            for j in gridy:
                krigeDemsTensor[n, 0, i, j] = z[int(j)][int(
                    i)]  # different transpose for the z and dems
        #kt.write_asc_grid(gridx, gridy, z, filename="output"+str(n)+".asc")
    return krigeDemsTensor
Beispiel #4
0
def update_status_one_s_one_t(z, wind_gens, loc_wind_gens):
    np.random.seed(89239413)

    N = 25
    lon = np.array(RTS_MISOgrid.station_locations_grid)[:, 0]
    lat = np.array(RTS_MISOgrid.station_locations_grid)[:, 1]

    grid_lon = xs[0]
    grid_lat = ys[:, 0]

    OK = OrdinaryKriging(lon,
                         lat,
                         z,
                         variogram_model='linear',
                         verbose=True,
                         enable_plotting=False)

    # Execute on grid:
    z2, ss2 = OK.execute("grid", grid_lon, grid_lat)

    loc_wind_gens = np.array(loc_wind_gens)
    t_gen, ss2 = OK.execute("points", loc_wind_gens[:, 0], loc_wind_gens[:, 1])
    for i in range(len(t_gen)):
        wind_gens[i]["temperature"].append(t_gen[i])
        wind_gens[i]["status"].append(windturbine_shutoff(t_gen[i]))

    return wind_gens
Beispiel #5
0
def test_uk_ok_produce_same_result(data):

    print "Testing OK/UK equivalence..."

    gridx = np.linspace(1067000.0, 1072000.0, 100)
    gridy = np.linspace(241500.0, 244000.0, 100)
    ok = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2], variogram_model='linear',
                         verbose=False, enable_plotting=False)
    z_ok, ss_ok = ok.execute(gridx, gridy)
    uk = UniversalKriging(data[:, 0], data[:, 1], data[:, 2], variogram_model='linear',
                          verbose=False, enable_plotting=False)
    z_uk, ss_uk = uk.execute(gridx, gridy)

    if np.array_equal(z_ok, z_uk):
        z = True
    else:
        z = False
    if np.array_equal(ss_ok, ss_uk):
        ss = True
    else:
        ss = False
    if z:
        print "Kriged grid from UK matches that from OK."
    else:
        print "Kriged grids DO NOT match!"
    if ss:
        print "SS from UK matches that from OK."
    else:
        print "Errors DO NOT match!"
    print '\n'
def Ordinary_Kriging(xvalue, yvalue, zvalue, xi, yi):
    #data = np.column_stack((xvalue, yvalue, zvalue))

    # Create the ordinary kriging object. Required inputs are the X-coordinates of the data points, the Y-coordinates of
    # the data points, and the Z-values of the data points. Variogram is handled as in the ordinary kriging case.
    # drift_terms is a list of the drift terms to include; currently supported terms are 'regional_linear', 'point_log',
    # and 'external_Z'.  Refer to UniversalKriging.__doc__ for more information.

    # cov_model = Gaussian(dim=2, len_scale=4, anis=.2, angles=-.5, var=.5, nugget=.1)
    # cov_model = 'gaussian'
    cov_model = Stable(dim=2,
                       len_scale=1,
                       anis=0.2,
                       angles=-0.5,
                       var=0.5,
                       nugget=0.1)

    UK = OrdinaryKriging(xvalue, yvalue, zvalue, cov_model)

    # UK = OrdinaryKriging(xvalue, yvalue, zvalue, variogram_model='exponential', verbose=True, enable_plotting=False)
    #UK = UniversalKriging(xvalue, yvalue, zvalue, variogram_model='linear',drift_terms=['regional_linear'])

    # Creates the kriged grid and the variance grid. Allows for kriging on a rectangular grid of points, on a masked
    # rectangular grid of points, or with arbitrary points. (See UniversalKriging.__doc__ for more information.)
    z, var = UK.execute('grid', xi, yi)
    return z, var
Beispiel #7
0
    def _fit(self, X, y):
        """This method of the Kriging Class is used to fit Kriging interpolation model to
        the train data. This function shouldn't be called directly."""
        if self.type == "Ordinary":
            self.ok = OrdinaryKriging(X[:, 0],
                                      X[:, 1],
                                      y,
                                      variogram_model=self.variogram_model,
                                      enable_plotting=self.plotting,
                                      coordinates_type=self.coordinate_type,
                                      nlags=self.nlags)

        elif self.type == "Universal":
            self.uk = UniversalKriging(
                X[:, 0],
                X[:, 1],
                y,
                variogram_model=self.variogram_model,
                enable_plotting=self.plotting,
            )

        else:
            raise ValueError(
                "Choose either Universal or Ordinary - Given argument is neither"
            )

        return self
Beispiel #8
0
def interp_precipitation(df_fit):
    fit_cities = df_fit["Estacao"].tolist()
    df_coordinates_cities = pd.read_csv('original_data/coordinates_cities.csv',
                                        delimiter=',',
                                        encoding='utf-8')
    df_predict_cities = df_coordinates_cities.loc[~df_coordinates_cities.
                                                  Estacao.isin(fit_cities)]
    lon_fit = df_fit["long"]
    lat_fit = df_fit["lat"]
    if not (df_fit['Precipitacao'] == 0).all():
        z_fit = df_fit["Precipitacao"]
        OK = OrdinaryKriging(lon_fit,
                             lat_fit,
                             z_fit,
                             variogram_model='linear',
                             verbose=False,
                             enable_plotting=False,
                             coordinates_type='geographic')
        lon_predict = df_predict_cities["long"]
        lat_predict = df_predict_cities["lat"]
        z_predict, ss_predict = OK.execute('points', lon_predict, lat_predict)
    else:
        z_predict = None
    df_fit_cleaned = clean_df_fit(df_fit)
    return build_df_predict(df_predict_cities, df_fit_cleaned, z_predict)
Beispiel #9
0
def krige(means, min_val):
    means_f = means.to_frame().apply(lambda x: x - min_val)
    in_frame_bgt = in_frame.merge(means_f, left_on='BBL', right_index=True)
    in_frame_bgt['brightness'] = in_frame_bgt[means_f.columns[0]]
    in_frame_bgt['scaled_bgt'] = in_frame_bgt['brightness'] * in_frame_bgt['surface_area']
    medians = []
    for i, indices in enumerate(out_reset["nearest_ix"]):
        cbbl = in_frame.loc[indices].BBL
        median = means_f.loc[cbbl].median()
        medians.append((i, median))
    medians = np.array(medians, dtype=[('ix', int), ('median', np.float64)])
    med_ix = pd.Series(medians['median'], index=out_reset.index)
    out_bgt = med_ix
    out_scaled_bgt = out_bgt * out_reset['surface_area']
    in_considered = in_frame_bgt[get_filt(in_frame_bgt)]
    out_considered = out_scaled_bgt[get_filt(out_reset)]
    out_reset_considered = out_reset[get_filt(out_reset)]
    full_x = np.concatenate((in_considered.geometry.centroid.x.values, out_reset_considered.geometry.centroid.x.values))
    full_y = np.concatenate((in_considered.geometry.centroid.y.values, out_reset_considered.geometry.centroid.y.values))
    full_z = np.concatenate((in_considered.scaled_bgt.values, out_considered.values))
    full_z = np.log(full_z + 1)

    OK = OrdinaryKriging(full_x, full_y,  full_z, variogram_model='gaussian', nlags=25,
                         verbose=True)
    z, ss = OK.execute('points', interp_x.ravel(), interp_y.ravel(), n_closest_points=50, backend='C')
    return z
Beispiel #10
0
    def ordinary_kriging(self, x, y, z, xDim, yDim):

        items = ("linear", "power", "gaussian", "spherical", "exponential",
                 "hole-effect")

        item, ok = QtGui.QInputDialog.getItem(self, "Kriging",
                                              "Select a Model", items, 0,
                                              False)

        if (ok):
            data = np.vstack((x, y))
            data = np.vstack((data, z)).T

            OK = OrdinaryKriging(data[:, 0],
                                 data[:, 1],
                                 data[:, 2],
                                 variogram_function=str(item),
                                 verbose=False,
                                 enable_plotting=False)

            gridx = np.linspace(np.min(x), np.max(x), xDim)
            gridy = np.linspace(np.min(y), np.max(y), yDim)

            zVal, ss = OK.execute('grid', gridx, gridy)
            self.Z = zVal.data

            self._display_variogram_model(OK)
Beispiel #11
0
def Spatial_Map(df, filename):
    lons = np.array(df['x']) #lons =longitude
    lats = np.array(df['y']) #lats=lattitude
    data = np.array(df['Difference'])
    
    plt.figure()
    plt.scatter(lons, lats, 15, data, cmap = plt.cm.Blues)
    plt.xlabel("East")
    plt.ylabel("North")
    plt.title("Data Visualization")
    cbar = plt.colorbar()
    cbar.set_label("Data", labelpad=+1) #Why "labelpad+1" #minus, for example -100 will set the label "Data" more to the left side. If you set += then the label "Data" will be set more to the right. If you set = python wil read it like you would type +=
    plt.show()

    OK = OrdinaryKriging(lons, lats, data, variogram_model='spherical', nlags=25, verbose=True, enable_plotting=True) #To check spatial correlation #What does the verbose=True do?
    # z1, ss1 = OK.execute('grid', lons, lats)
    # xintrp, yintrp = np.meshgrid(grid_lon, grid_lat)
    # xintrp, yintrp = np.meshgrid(lons, lats)

    grid_x = np.linspace(150000, 250000, num = 100, endpoint = False)
    grid_y = np.linspace(160000, 220000, num = 100, endpoint = False)

    # Creates the kriged grid and the variance grid. Allows for kriging on a rectangular
    # grid of points, on a masked rectangular grid of points, or with arbitrary points.
    # (See OrdinaryKriging.__doc__ for more information.):

    z, ss = OK.execute('grid', grid_x, grid_y) # data: z and variance:ss 

    # Writes the kriged grid to an ASCII grid file:

    kt.write_asc_grid(grid_x, grid_y, z, filename="kriging_ordinary.asc") #Ordinary Kriging in matrix form

    kt.write_asc_grid(grid_x, grid_y, ss, filename="kriging_ordinary_var.asc") #Estimation variance in matrix form

    asc = pd.read_csv("kriging_ordinary.asc", header=None, skiprows=7, sep="\s+") 
    asc.shape #check that they are 70 rows and 80 columns #Why 70 rows and 80 columns?

    cu2 = np.array(asc) #pandas dataframe to ndarray conversion

    #Krigging map plot
    from descartes import PolygonPatch #PolygonPatch: Constructs a matplotlib patch from a geometric object, but what is a "matplotlibpatch"??

    gpo = gdp.read_file("C:\\Users\\Julio\\Desktop\\IUPWARE\\ENVIRONMENTAL PROGRAMMING\\Project Groundwater\\Data\\Groundwaterbodies_Oligocene\\Groundwaterbodies_Oligocene.shp")

    fig = plt.figure(figsize = (12,12))
    ax = fig.gca()

    a = plt.imshow(cu2, cmap=plt.cm.YlOrBr, extent=[150000,250000,160000,220000]) #gist_rainbow
    plt.grid(True)
    cbar = fig.colorbar(a, orientation='horizontal', pad=0.05)
    cbar.set_label("Anomaly in mTAW", labelpad=+1)
    plt.title('Anomaly of groundwater level - Kriging interpolation')
    plt.xlabel('East')
    plt.ylabel('North')
    ax.scatter(lons, lats, c='blue')
    for i in gpo['geometry']:
        ax.add_patch(PolygonPatch(i, fill=False, alpha=0.5, zorder=2, hatch='////')) #What does this line of code do?
    plt.show()
    #Save as PDF document
    fig.savefig(filename+".pdf", bbox_inches='tight')
    def _Kriging_sp(self, attrName, skip_nodata=1, skip_nodata_col='ABS_SHIFT', outGridRes=None,
                    fName_out=None, tilepos=None):
        GDF = self.CoRegPoints_table
        GDF2pass = GDF if not skip_nodata else GDF[GDF[skip_nodata_col] != self.outFillVal]

        X_coords, Y_coords, ABS_SHIFT = GDF2pass['X_UTM'], GDF2pass['Y_UTM'], GDF2pass[attrName]

        xmin, ymin, xmax, ymax = GDF2pass.total_bounds

        grid_res = outGridRes if outGridRes else int(min(xmax - xmin, ymax - ymin) / 250)
        grid_x, grid_y = np.arange(xmin, xmax + grid_res, grid_res), np.arange(ymax, ymin - grid_res, -grid_res)

        # Reference: P.K. Kitanidis, Introduction to Geostatistcs: Applications in Hydrogeology,
        #            (Cambridge University Press, 1997) 272 p.
        from pykrige.ok import OrdinaryKriging
        OK = OrdinaryKriging(X_coords, Y_coords, ABS_SHIFT, variogram_model='spherical', verbose=False)
        zvalues, sigmasq = OK.execute('grid', grid_x, grid_y, backend='C', n_closest_points=12)

        if self.CPUs is None or self.CPUs > 1:
            fName_out = fName_out if fName_out else \
                "Kriging__%s__grid%s_ws%s_%s.tif" % (attrName, self.grid_res, self.COREG_obj.win_size_XY, tilepos)
        else:
            fName_out = fName_out if fName_out else \
                "Kriging__%s__grid%s_ws%s.tif" % (attrName, self.grid_res, self.COREG_obj.win_size_XY)
        path_out = get_generic_outpath(dir_out=self.dir_out, fName_out=fName_out)
        # add a half pixel grid points are centered on the output pixels
        xmin, ymin, xmax, ymax = xmin - grid_res / 2, ymin - grid_res / 2, xmax + grid_res / 2, ymax + grid_res / 2

        GeoArray(zvalues,
                 geotransform=(xmin, grid_res, 0, ymax, 0, -grid_res),
                 projection=self.COREG_obj.shift.prj).save(path_out)

        return zvalues
Beispiel #13
0
def kriging_interpolation_model_residuals(DF,f,MODEL_POLLUTANT):   
    """
    funkcia, ktora odcita hodnoty z modelu CMAQ a interpoluje rezidua pomocou Kriging interpolacie 
    """
    predpoved_modelu=[]
     # predpoved_modelu= hodnoty z modelu CMAQ
    for k, row in DF.iterrows():
        ix, iy = getclosest_ij(f.variables['LAT'][0,0,:,:],f.variables['LON'][0,0,:,:],row['lat_x'],row['lon_x'])
        hodnota=(MODEL_POLLUTANT[ix, iy])
        predpoved_modelu.append(hodnota)
    predpoved_modelu=np.array(predpoved_modelu)    
    residuals=DF['pollutant']-predpoved_modelu    
     #residuals = namerane hodnoty-hodnoty z CMAQ
    x,y,z = list(DF['lon_x']),list(DF['lat_x']),list(residuals)
    #xi,yi are new grid coordinates
    xi=np.linspace(np.min(f.variables['LON'][0,0,:,:].flatten()), np.max(f.variables['LON'][0,0,:,:].flatten()), 271)
    yi=np.linspace(np.min(f.variables['LAT'][0,0,:,:].flatten()), np.max(f.variables['LAT'][0,0,:,:].flatten()), 142)
    
    # OK is kriging funkction 
    OK = OrdinaryKriging(x, y, z, variogram_model='spherical')
    # z1 is kriging interpolation array 
    z1, ss1 = OK.execute('grid', xi, yi)
   
    KRIGING_RESIDUALS=z1
   
    
    return predpoved_modelu, residuals, KRIGING_RESIDUALS, MODEL_POLLUTANT+KRIGING_RESIDUALS    
    
    
    
    
Beispiel #14
0
def ok_map():
    fig = plt.Figure(figsize=(12, 7))
    # Compute the ordinary kriging
    df_heatmap = df.groupby("station").mean()
    OK = OrdinaryKriging(df_heatmap['latitude'],
                         df_heatmap['longitude'],
                         df_heatmap['iptcc'],
                         variogram_model='spherical',
                         verbose=False,
                         enable_plotting=False)
    z, ss = OK.execute('points', df_grid['Y'], df_grid['X'])
    df_gradient = df_grid[['Y', 'X']]
    df_gradient['iptcc'] = pd.Series(z)
    mark_size = [100 for i in df_gradient.index]
    fig = px.scatter_mapbox(df_gradient,
                            lat="Y",
                            lon="X",
                            hover_data=["iptcc"],
                            color="iptcc",
                            color_continuous_scale=[
                                '#7BD150', '#F6E626', '#F6E626', '#FC9129',
                                '#FF1B00', '#6E1E80'
                            ],
                            range_color=[0, 100],
                            size=mark_size,
                            size_max=4,
                            zoom=4,
                            height=450)
    fig.update_layout(mapbox_style="open-street-map")
    fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0})
    return fig
Beispiel #15
0
def krigingmodel(DF, dic_polia, dic_latlon):
    #start_time_of_gmodel = time.time()
    # MODELWI is model without interpolation of shape, it is an array of shape (142,271)
    MODELWI = regresia.model_reg(DF, dic_polia, dic_latlon)[0]
    # ERROR is caculated as pollutant minus PREDICT, it has shape same as measured pollutant (32)
    ERROR_stanice = regresia.reg_funkcia(DF, dic_polia, dic_latlon)[4]

    x, y, z = list(DF['lon_x']), list(DF['lat_x']), ERROR_stanice
    #xi,yi are new grid coordinates
    xi = np.linspace(np.min(dic_latlon['LON'].flatten()),
                     np.max(dic_latlon['LON'].flatten()), 271)
    yi = np.linspace(np.min(dic_latlon['LAT'].flatten()),
                     np.max(dic_latlon['LAT'].flatten()), 142)

    # OK is kriging funkction
    OK = OrdinaryKriging(x, y, z, variogram_model='spherical')
    """
    OK: is kriging funkction form pykrige package 
    x, y: are latitude and lontitude of measured concentrations from DF(dataframe)
    z : error concentrations of pollutant, from linear regresion model
    variogram model: is tzpe of theoretical variogram
    """
    # z1 is kriging interpolation array
    z1, ss1 = OK.execute('grid', xi, yi)
    """
    z1: is interpolated array realised on grid 
    xi, yi: are new grid coordinates
    """
    KRIGING_ERROR = z1
    #KOMPLET_MODEL is linear reggresion predict MODELWI plus kriging of residuals
    KOMPLET_MODEL = KRIGING_ERROR + MODELWI
    return KOMPLET_MODEL, KRIGING_ERROR, np.exp(KOMPLET_MODEL), np.exp(
        KRIGING_ERROR)
Beispiel #16
0
def ordinary_kriging_evaluation(year):
    """
    Under leave-one-out setting, use only crime rate.
    """
    from pykrige.ok import OrdinaryKriging
    from sklearn.model_selection import LeaveOneOut

    y_cnt = retrieve_crime_count(year)
    demo = generate_corina_features()
    population = demo[1][:,0].reshape(demo[1].shape[0], 1)
    Y = y_cnt / population * 10000
    
    coords = get_centroid_ca()
    
    data = np.concatenate((coords, Y), axis=1)
    loo = LeaveOneOut()
    
    errors = []
    for train_idx, test_idx in loo.split(data):
        x_train = data[train_idx,:]
        coords_test = data[test_idx, [0,1]]
        y_test = data[test_idx, 2]
        
        OK = OrdinaryKriging(x_train[:,0], x_train[:,1], x_train[:,2], variogram_model="linear")
        z, var = OK.execute("points", coords_test[0], coords_test[1])
        errors.append(abs(z[0] - y_test[0]))
    print np.mean(errors), np.mean(errors) / np.mean(Y)
    return errors
Beispiel #17
0
def krige_interp(df):
    #插值成0.25*0.25
    grid_lon = np.arange(float(math.floor(df.lon.min())),
                         float(math.ceil(df.lon.max())), 0.25)
    grid_lat = np.arange(float(math.floor(df.lat.min())),
                         float(math.ceil(df.lat.max())), 0.25)
    lons = df["lon"].values
    lats = df["lat"].values
    data = df["data"].values

    #调用克里金插值函数
    OK = OrdinaryKriging(
        lons,
        lats,
        data,
        variogram_model='gaussian',
        coordinates_type="geographic",
        nlags=6,
    )
    z, ss = OK.execute('grid', grid_lon, grid_lat)
    #转换成网格
    xgrid, ygrid = np.meshgrid(grid_lon, grid_lat)
    #将插值网格数据整理
    df_grid = pd.DataFrame(dict(lon=xgrid.flatten(), lat=ygrid.flatten()))
    #添加插值结果
    df_grid['data'] = z.flatten()
    df_grid = df_grid.set_index(['lat', 'lon'])
    ds = xr.Dataset.from_dataframe(df_grid).data
    return ds
Beispiel #18
0
def interpolate(lonlist,
                latlist,
                valuelist,
                extent,
                gridSize,
                variogram_model='spherical',
                verbose=False,
                enable_plotting=False,
                n_closest_points=None):
    minXY = [extent[0], extent[2]]
    maxXY = [extent[1], extent[3]]
    interval = (float)(maxXY[0] - minXY[0]) / gridSize
    gridx = np.arange(minXY[0], maxXY[0], interval)
    gridy = np.arange(minXY[1], maxXY[1], interval)
    OK_rh = OrdinaryKriging(lonlist,
                            latlist,
                            valuelist,
                            variogram_model=variogram_model,
                            verbose=verbose,
                            enable_plotting=enable_plotting)
    if (n_closest_points is not None):
        print('n_closest_points=', n_closest_points)
        z_rh, ss_rh = OK_rh.execute('grid',
                                    gridx,
                                    gridy,
                                    backend='loop',
                                    n_closest_points=n_closest_points)
    else:
        z_rh, ss_rh = OK_rh.execute('grid', gridx, gridy)
    return z_rh
def surfkriging(xvec, yvec, zvec, dof_surf, **kwargs):
    """Interpolates surface z value data by kriging.

		INPUTS:
			[x,y,z] = coordinates of degrees of freedom onto which the z value is mapped
			[dof_surf] = the degrees of freedom of the surface of the mesh

			OrdinaryKriging function takes variogram parameters as:
				linear - [slope, nugget]
				power - [scale, exponent, nugget]
			`   gaussian - [sill, range, nugget]
				spherical - [sill, range, nugget]
				exponential - [sill, range, nugget]
				hole-effect - [sill, range, nugget]

		OUPUTS:
			z_surf - surface height on all mesh nodes as a numpy array
	"""

    x_surf, y_surf, z_surf = xvec[dof_surf], yvec[dof_surf], zvec[dof_surf]
    x_all, y_all = xvec[:], yvec[:]

    # PyKrige interpolation
    OK_surf = OrdinaryKriging(x_surf,
                              y_surf,
                              z_surf,
                              variogram_model='gaussian',
                              variogram_parameters=[5e5, 5e4, 1e2])
    z_surf, ss = OK_surf.execute('points', x_all, y_all)

    return z_surf.compressed()
def rain_ordinary_kriging(radar_center_x, radar_center_y,
                          radar_radius_in_graph, radar_resolution_x,
                          radar_resolution_y, x_measure, y_measure,
                          z_measure_data):
    """rainfall ordinary kriging.
    Parameters
    ----------
    center_grid_num_x: in radar map, the num in x-axis of center
    center_grid_num_y: in radar map, the num in y-axis of center
    x_measure: the x num of the points we have measured
    y_measure: the y num of the points we have measured
    z_measure_data: z_keige=z_keige(x,y) is the measurement value at point (x,y)
    Returns
    ----------
    z_krige: ndarray, Z-values of all specified grid
    ss_krige: ndarray,  Variance at specified grid points or at the specified set of points
    """
    # show the range of interpolation
    # plus '1' for the open interval
    # the last parameter "1.0" means one grid is "1", the type has to be float, or it will cast error
    gridx = np.arange(
        radar_center_x - radar_radius_in_graph,
        radar_center_x - radar_radius_in_graph + radar_radius_in_graph * 2 + 1,
        1.0)
    gridy = np.arange(
        radar_center_y - radar_radius_in_graph,
        radar_center_y - radar_radius_in_graph + radar_radius_in_graph * 2 + 1,
        1.0)
    '''krige the rain gauge observations. Create the ordinary kriging object. Required inputs are the X-coordinates 
    of the data points, the Y-coordinates of the data points, and the Z-values of the data points. If no variogram 
    model is specified, defaults to a linear variogram model. If no variogram model parameters are specified, 
    then the code automatically calculates the parameters by fitting the variogram model to the binned experimental 
    semivariogram. The verbose kwarg controls code talk-back, and the enable_plotting kwarg controls the display of 
    the semivariogram. '''
    temp_zero = np.zeros(1)
    if (z_measure_data <= temp_zero).all():
        z_krige = np.zeros((len(gridx), len(gridy)))
        ss_krige = np.zeros((len(gridx), len(gridy)))
    else:
        OK = OrdinaryKriging(x_measure,
                             y_measure,
                             z_measure_data,
                             variogram_model='linear',
                             verbose=False,
                             enable_plotting=True)
        '''the enable-plotting controls the display of semivariogram, so we can check it to choose the best r(d) fitting 
            curve '''
        # Creates the kriged grid and the variance grid. Allows for kriging on a rectangular
        # grid of points, on a masked rectangular grid of points, or with arbitrary points.
        # (See OrdinaryKriging.__doc__ for more information.)
        z_krige, ss_krige = OK.execute('grid', gridx, gridy)
        '''z_keige is the result, and ss_krige is the variance. So, next ,we can visualize the interpolation data'''
        kt.write_asc_grid(gridx, gridy, z_krige, filename="output.asc")
        X, Y = np.meshgrid(gridx, gridy)
        C = plt.contour(X, Y, z_krige, 8, colors='black')  # 生成等值线图
        plt.contourf(X, Y, z_krige, 8)
        plt.clabel(C, inline=1, fontsize=10)
        plt.show()
    return z_krige, ss_krige
Beispiel #21
0
def Krige_model(gridx, gridy, data, fitness):
    ok3d = OrdinaryKriging(data[:, 0],
                           data[:, 1],
                           fitness,
                           variogram_model='exponential')  # 模型
    # pykrige提供 linear, power, gaussian, spherical, exponential, hole-effect几种variogram_model可供选择,默认的为linear模型。
    k3d1, ss3d = ok3d.execute("grid", gridx, gridy)
    return k3d1
Beispiel #22
0
    def kriging(self, progress_bar=False, t_start=None, t_stop=None):
        fields = []

        if t_start is None:
            t_start = self.df_cmls_R.index[0]
        if t_stop is None:
            t_stop = self.df_cmls_R.index[-1]

        if progress_bar:
            pbar = tqdm(total=len(self.df_cmls_R[t_start:t_stop].index))

        for t, row in self.df_cmls_R[t_start:t_stop].iterrows():
            values = row.values
            i_not_nan = ~pd.isnull(values)

            if values[i_not_nan].sum() == 0:
                print("Skipping %s" % t)
                zi = np.zeros_like(self.xgrid)

            else:
                try:
                    ok = OrdinaryKriging(
                        x=self.lons[i_not_nan],
                        y=self.lats[i_not_nan],
                        z=values[i_not_nan],
                        nlags=30,
                        variogram_model="spherical",
                        weight=True,
                    )

                    zi, sigma = ok.execute(
                        "points",
                        self.xgrid.flatten(),
                        self.ygrid.flatten(),
                        n_closest_points=10,
                        backend="C",
                    )
                    zi = np.reshape(zi, self.xgrid.shape)
                except:
                    # if 'Singular matrix' in err.message:
                    #    print 'Singular matrix encountered while doing ' \
                    #          'moving window kriging.'
                    print("Error while doing kriging for %s" % t)
                    zi = np.zeros_like(self.xgrid)
                    # else:
                    #    raise

            fields.append(zi)

            if progress_bar:
                pbar.update(1)

        # Close progress bar
        if progress_bar:
            pbar.close()

        self.gridded_data = self._fields_to_dataset(fields)
        return self.gridded_data
Beispiel #23
0
 def usekrige(self):
     Locations=self.get_sensor_locations()
     Prx=self.get_sensor_powers()
     data = np.hstack((Locations, Prx.T))
     gridx = np.arange(0.0, myglobals.area_size[0], 0.5)
     gridy = np.arange(0.0, myglobals.area_size[1], 0.5)
     OK = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2], variogram_model='spherical', verbose=False, enable_plotting=False)
     z, ss = OK.execute('grid', gridx, gridy)
     self.Img=z
Beispiel #24
0
def plot_ei(df,path_to_rep,species,date):
    df = df.dropna(axis = 0, how="any")
    lons = np.array(df.long)
    lats = np.array(df.lat)
    data = np.array(df.EI)
    grid_space = 0.1
    grid_lon = np.arange(lons.min()-0.05, lons.max()+0.1, grid_space) 
    grid_lat = np.arange(lats.min()-0.05, lats.max()+0.1, grid_space)
    OK = OrdinaryKriging(lons, lats, data, variogram_model='gaussian', verbose=True, enable_plotting=False,nlags=20)
    z1, ss1 = OK.execute('grid', grid_lon, grid_lat)
    xintrp, yintrp = np.meshgrid(grid_lon, grid_lat)
    
    ind = (z1>1)
    z1[ind] = 1
    ind = (z1<0)
    z1[ind] = 0
    
    fig, ax = plt.subplots(figsize=(10,10))
    m = Basemap(llcrnrlon=119.5,llcrnrlat=21.5,
                urcrnrlon=122.5,urcrnrlat=26, projection='merc', 
                resolution='h',area_thresh=1000.,ax=ax)
    m.drawcoastlines() #draw coastlines on the map
    
    x,y=m(xintrp,yintrp)
    lo,la = m(lons,lats)
    cs = ax.contourf(x,y,z1,
                       cmap="rainbow",
                       levels = np.linspace(0,1,10))
    #cs = ax.scatter(lo,la,c=data,cmap="rainbow")
    ax.scatter(lo,la,c=data,cmap="rainbow",vmin=0,vmax=1,edgecolor="k")
    cbar=m.colorbar(cs,location='right') #plot the colorbar on the map
    parallels = np.arange(21.5,26.0,1)
    m.drawparallels(parallels,labels=[1,0,0,0],fontsize=14, linewidth=0.0) #Draw the latitude labels on the map
    meridians = np.arange(119.5,123.5,1)
    m.drawmeridians(meridians,labels=[0,0,0,1],fontsize=14, linewidth=0.0)

    x0,x1 = ax.get_xlim()
    y0,y1 = ax.get_ylim()
    map_edges = np.array([[x0,y0],[x1,y0],[x1,y1],[x0,y1]])
    polys = [p.boundary for p in m.landpolygons]
    polys = [map_edges]+polys[:]
    codes = [
    [Path.MOVETO]+[Path.LINETO for p in p[1:]]
    for p in polys
    ]

    polys_lin = [v for p in polys for v in p]
    codes_lin = [c for cs in codes for c in cs]

    path = Path(polys_lin, codes_lin)
    patch = PathPatch(path,facecolor='white', lw=0)
    ax.add_patch(patch)
#    plt.title("EI at {}".format(date))
    plt.savefig(path_to_rep  + "result/{}/{}_{}.png".format(species,date,species),bbox_inches="tight",dpi=300)
Beispiel #25
0
    def kriging(self, data, extrapolation_spots):
        gridx = np.arange(1.0, self.front_num, self.end_num)
        gridy = np.arange(1.0, self.front_num, self.end_num)
        OK = OrdinaryKriging(data[:, 0],
                             data[:, 1],
                             data[:, 2],
                             variogram_model='spherical',
                             verbose=False,
                             nlags=100)

        z, ss = OK.execute('grid', gridx, gridy)
        return gridx, gridy, z, ss
Beispiel #26
0
	def interpolate(self, interpdf, output_path, generate_plots= True, format= 'csv'):
		"""
		Function that makes Kringe interpolation on the preprocessed wl data.
		Inputs:
		   format: 'csv' : generates csv files in folder output_path/csv_files
				   'tif': generates tif files in folder output_path/tif_files

		"""
		# Get raster of Wadden Sea
		extent, dims, crs, ws = utils.get_raster_wadden_sea()

		# grid to make interpolation on
		x= np.linspace(extent[0], extent[2], dims[0])
		y = np.linspace(extent[1],extent[3], dims[1])

		# For each date, make an interpolation using data, on grid x,y
		#for row in range(interpdf.shape[0]):
		for row in [0]:
			print('Interpolating data at index %d ...'%row)
			date= interpdf.loc[row,'date']
			no_obs = int(len(interpdf.iloc[row, 1:].dropna().values)/3.)
			data = interpdf.iloc[row, 1:].dropna().values.reshape(no_obs, 3)
			# Do the linear interpolation
			UK = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2],
								variogram_model='power', coordinates_type= 'euclidean',
                    			verbose= False, enable_plotting= False)
			# kriged points and the variance.
			z, ss = UK.execute('grid',x,y)
			watlev = z.data*ws
			watlev1 = np.where(watlev !=0, watlev, np.nan)


			if (format == 'tif'):
				#Convert array to raster.
				opath= os.path.join(output_path,'tif_files/')
				if not os.path.exists(opath):
					os.makedirs(opath)
				utils.array2raster(463, extent[0], extent[3], crs, watlev, opath, raster_name= 'wl_%d-%02d-%02d.tif'%(date.year,date.month, date.day) )

			if (format== 'csv'):
				opath= os.path.join(output_path,'csv_files/')
				if not os.path.exists(opath):
					os.makedirs(opath)
				table= utils.arraytocsv(watlev1, dims[0], dims[1])
				table.rename(columns={'value': 'water_level'}, inplace= True)
				table.to_csv(opath+'wl_%d-%02d-%02d.csv'%(date.year,date.month, date.day), index= False)

			#Create plot if true:
			if (generate_plots):
				path= os.path.join(output_path, 'plots/')
				if not os.path.exists(path):
					os.makedirs(path)
				self.plotting(path, watlev1, interpdf.loc[row,'date'] , ofilename= '%05d.png'%row)
Beispiel #27
0
def make_benchark(n_train, n_test, n_dim=2):
    """Compute the benchmarks for Ordianry Kriging.

    Parameters
    ----------
    n_train : int
      number of points in the training set
    n_test : int
      number of points in the test set
    n_dim : int
      number of dimensions (default=2)

    Returns
    -------
    res : dict
      a dictionary with the timing results
    """
    X_train = np.random.rand(n_train, n_dim)
    y_train = np.random.rand(n_train)
    X_test = np.random.rand(n_test, n_dim)

    res = {}

    for variogram_model in VARIOGRAM_MODELS:
        tic = time()
        OK = OrdinaryKriging(
            X_train[:, 0],
            X_train[:, 1],
            y_train,
            variogram_model="linear",
            verbose=False,
            enable_plotting=False,
        )
        res["t_train_{}".format(variogram_model)] = time() - tic

    # All the following tests are performed with the linear variogram model
    for backend in BACKENDS:
        for n_closest_points in N_MOVING_WINDOW:

            if backend == "vectorized" and n_closest_points is not None:
                continue  # this is not supported

            tic = time()
            OK.execute(
                "points",
                X_test[:, 0],
                X_test[:, 1],
                backend=backend,
                n_closest_points=n_closest_points,
            )
            res["t_test_{}_{}".format(backend, n_closest_points)] = time() - tic

    return res
Beispiel #28
0
    def correct_pws_inner_loop(pws_edf):

        if pws_edf == 1:
            pws_edf = 0.999999999
        # print(pws_edf)

        prim_netw_ppt_pws_edf = prim_netw_ppt_neigbrs.apply(
            find_prim_netw_ppt_pws_edf, axis=0,
            args=pws_edf, raw=False)

        prim_netw_ppt_pws_edf.dropna(how='all', inplace=True)
        prim_netw_stns = prim_netw_ppt_pws_edf.reset_index()['level_0'].values
        prim_netw_xcoords = np.array(
            prim_netw_in_coords_df.loc[prim_netw_stns, 'X'])
        prim_netw_ycoords = np.array(
            prim_netw_in_coords_df.loc[prim_netw_stns, 'Y'])

        # sacle variogram based on prim_netw ppt
#         vgs_model_dwd_ppt = scale_vg_based_on_prim_netw_ppt(
#             prim_netw_ppt_pws_edf.values, vg_sill_b4_scale)

        # start kriging pws location
#         OK_prim_netw_pws_crt = OKpy(xi=prim_netw_xcoords,
#                                     yi=prim_netw_ycoords,
#                                     zi=prim_netw_ppt_pws_edf.values,
#                                     xk=np.array([xpws]),
#                                     yk=np.array([ypws]),
#                                     model=vgs_model_dwd_ppt)

        dwd_vals_var = np.var(prim_netw_ppt_pws_edf.values)
        vg_scaling_ratio = dwd_vals_var / vg_sill_b4_scale
        OK_prim_netw_pws_crt = OKpy(
            prim_netw_xcoords, prim_netw_ycoords, prim_netw_ppt_pws_edf.values,
            variogram_model=vg_model_str,
            variogram_parameters={
                'sill': vg_scaling_ratio,
                'range': vg_range,
                'nugget': 0})

        try:
            zvalues, _ = OK_prim_netw_pws_crt.execute(
                'points', np.array([xpws]),  np.array([ypws]))
#             OK_prim_netw_pws_crt.krige()
#             zvalues = OK_prim_netw_pws_crt.zk.copy()
        except Exception:
            print('error kriging pws location')

        # to avoid memory leaks
        gc.collect()
        del gc.garbage[:]

        return np.round(zvalues[0], 3)
Beispiel #29
0
    def calculate(self, data):
        """
        Estimate the variogram, calculate the model, then apply to the grid

        Arg:
            data: numpy array same length as m*
            config: configuration for dk

        Returns:
            v: Z-values of specified grid or at thespecified set of points.
               If style was specified as 'masked', zvalues will
               be a numpy masked array.
            sigmasq: Variance at specified grid points or
                     at the specified set of points. If style was specified as
                     'masked', sigmasq will be a numpy masked array.
        """

        nan_val = pd.isnull(data)

        if self.config['detrend']:
            d = self.detrendData(data)
        else:
            d = data.copy()

        OK = OrdinaryKriging(self.mx[~nan_val],
                             self.my[~nan_val],
                             d[~nan_val],
                             variogram_model=self.variogram_model,
                             variogram_parameters=self.variogram_parameters,
                             variogram_function=self.variogram_function,
                             nlags=self.nlags,
                             weight=self.weight,
                             anisotropy_scaling=self.anisotropy_scaling,
                             anisotropy_angle=self.anisotropy_angle,
                             verbose=self.verbose,
                             enable_plotting=self.enable_plotting,
                             enable_statistics=self.enable_statistics)

        v, ss1 = OK.execute('grid',
                            self.GridX[0, :],
                            self.GridY[:, 0],
                            backend=self.backend,
                            n_closest_points=self.n_closest_points)

        if self.config['detrend']:
            # retrend the residuals
            v = self.retrendData(v)

        return v, ss1
    def kriging(self,
                type='Ordinary',
                variogram_model='gaussian',
                intertype="points"):
        """https://buildmedia.readthedocs.org/media/pdf/pykrige/latest/pykrige.pdf"""
        mtx = self.topDF.values
        OK = OrdinaryKriging(mtx[:, 0],
                             mtx[:, 1],
                             mtx[:, 2],
                             variogram_model=variogram_model,
                             verbose=False,
                             enable_plotting=False)
        z, _ = OK.execute(intertype, self.query_point[0], self.query_point[1])

        return round(float(z), 2)
 def perform_kriging(self, data, grid_x, grid_y):
     startTime = time.time()
     OK = OrdinaryKriging(data[:, 0],
                          data[:, 1],
                          data[:, 2],
                          variogram_model='linear',
                          verbose=False,
                          enable_plotting=False)
     z, ss = OK.execute('grid', grid_x, grid_y)
     endTime = time.time()
     if (self.st):
         print(
             "performKriging Method took {:.2f} seconds".format(endTime -
                                                                startTime))
     return z
Beispiel #32
0
def make_benchark(n_train, n_test, n_dim=2):
    """ Compute the benchmarks for Ordianry Kriging 

    Parameters
    ----------
    n_train : int
      number of points in the training set
    n_test : int
      number of points in the test set
    n_dim : int
      number of dimensions (default=2)

    Returns
    -------
    res : dict
      a dictionary with the timing results
    """
    X_train = np.random.rand(n_train, n_dim)
    y_train = np.random.rand(n_train)
    X_test = np.random.rand(n_test, n_dim)

    res = {}

    for variogram_model in VARIOGRAM_MODELS:
        tic = time()
        OK = OrdinaryKriging(X_train[:, 0], X_train[:, 1], y_train,
                             variogram_model='linear',
                             verbose=False, enable_plotting=False)
        res['t_train_{}'.format(variogram_model)] = time() - tic

    # All the following tests are performed with the linear variogram model
    for backend in BACKENDS:
        for n_closest_points in N_MOVING_WINDOW:

            if backend == 'vectorized' and n_closest_points is not None:
                continue  # this is not supported

            tic = time()
            OK.execute('points', X_test[:, 0], X_test[:, 1],
                       backend=backend,
                       n_closest_points=n_closest_points)
            res['t_test_{}_{}'.format(backend, n_closest_points)] = time() - tic

    return res
Beispiel #33
0
def test1(data):
    """
    Test to compare OK results to those obtained using KT3D_H2O.
    (M. Karanovic, M. Tonkin, and D. Wilson, 2009, Groundwater, vol. 47, no. 4, 580-586.)
    """

    print "Comparing OK to KT3D_H2O..."

    ok = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2],
                         variogram_model='exponential',
                         variogram_parameters=[500.0, 3000.0, 0.0],
                         anisotropy_scaling=1.0, anisotropy_angle=0.0)
    answer, gridx, gridy, cellsize, no_data = kt.read_asc_grid("test1_answer.asc", footer=2)
    z, ss = ok.execute(gridx, gridy)
    diff = 100.0*np.absolute(z - answer)/answer
    mean_diff = np.mean(diff)
    max_diff = np.amax(diff)
    print "Mean percent difference is %.4f%%" % mean_diff
    print "Max percent difference is %.4f%%" % max_diff
    if mean_diff <= 0.1 and max_diff <= 0.1:
        print "Acceptable."
    else:
        print "Difference unacceptable!"
    print '\n'
Beispiel #34
0
np.random.seed(89239413)

# Generate random data following a uniform spatial distribution
# of nodes and a uniform distribution of values in the interval
# [2.0, 5.5]:
N = 7
lon = 360.0*np.random.random(N)
lat = 180.0/np.pi*np.arcsin(2*np.random.random(N)-1)
z   = 3.5*np.random.rand(N) + 2.0

# Generate a regular grid with 60° longitude and 30° latitude steps:
grid_lon = np.linspace(0.0, 360.0, 7)
grid_lat = np.linspace(-90.0, 90.0, 7)

# Create ordinary kriging object:
OK = OrdinaryKriging(lon, lat, z, variogram_model='linear', verbose=False,
                     enable_plotting=False, coordinates_type='geographic')

# Execute on grid:
z1, ss1 = OK.execute('grid', grid_lon, grid_lat)

# Create ordinary kriging object ignoring curvature:
OK = OrdinaryKriging(lon, lat, z, variogram_model='linear', verbose=False,
                     enable_plotting=False)

# Execute on grid:
z2, ss2 = OK.execute('grid', grid_lon, grid_lat)

# Print data at equator (last longitude index will show periodicity):
print("Original data:")
print("Longitude:",lon.astype(int))
print("Latitude: ",lat.astype(int))