def Krige_Interpolate(
    X, Y, new_X, variogram_parameters={"slope": 1e-4, "nugget": 1e-5}
):
    # X, Y, new_X, variogram_parameters={"sill": 1e3, "range": 1e2, "nugget": 0.0001}

    uk = OrdinaryKriging(
        X,
        np.zeros(X.shape),
        Y,
        pseudo_inv=True,
        # weight=True,
        # nlags=2,
        # variogram_model="gaussian",
        # exact_values = False,
        variogram_model="linear",
        variogram_parameters=variogram_parameters
        # variogram_model="gaussian",
        # variogram_parameters=variogram_parameters,
    )

    y_pred, y_std = uk.execute("grid", new_X, np.array([0.0]))
    y_pred = np.squeeze(y_pred)
    y_std = np.squeeze(y_std)

    return new_X, y_pred, y_std
示例#2
0
def do_krigin_for_one_sigma(sig):
    T = get_T_from_metric_and_sigma(Metric, sig)
    x = np.arange(min, max, T)
    y = np.copy(x)
    for i in range(len(x)):
        y[i] = np.random.normal(fonction_to_follow(x[i]), sig)
    uk = OrdinaryKriging(x, np.zeros(x.shape), y, variogram_model="gaussian", variogram_parameters={'nugget': sig, 'psill': 0.1, 'range' : 3})
    y_pred, y_std = uk.execute("grid", inputs, np.array([0.0]))
    y_pred = np.squeeze(y_pred)
    y_std = np.squeeze(y_std)

    fig, ax = plt.subplots(1, 1, figsize=(10, 4))
    ax.scatter(x, y, s=40, label="Input data")

    ax.plot(inputs, y_pred, label="Predicted values")
    ax.fill_between(
        inputs,
        y_pred - 3 * y_std,
        y_pred + 3 * y_std,
        alpha=0.3,
        label="Confidence interval",
    )
    ax.legend(loc=9)
    ax.set_xlabel("x")
    ax.set_ylabel("y")
    plt.show()
    return y_pred, y_std
示例#3
0
def krige_greenery(green_res, lat_grid, long_grid, init_kwargs={}, **kwargs):

    coor, green = _stack_green_res(green_res)
    OK = OrdinaryKriging(coor[:, 0], coor[:, 1], green,
                         **init_kwargs)

    lat_grid, long_grid = _lat_long_to_metric(lat_grid, long_grid)
    z, _ = OK.execute('grid', long_grid, lat_grid, backend='loop',
                      **kwargs)
    return z
示例#4
0
def create_kriged_overlay(green_res, grid=[200, 200], cmap="gist_rainbow",
                          overlay_fp="krige_map.json", name=None,
                          n_closest_points=None):
    """
    Create an overlay for openstreetmap using kriging as interpolation.

    Arguments
    ---------
    green_res: dict
        Contains greenery values with coordinates.
    n_grid: int
        Number of grid points.
    cmap: str
        Matplotlib color map.
    overlay_fp: str
        Filepath to store kriged map. Use as cache if available.
    name: str
        Name to give to the overlay.
    n_closest_points: int
        Used in kriging procedure to limit memory/compute time.
    """
#     Load overlay from file if available.
#     try:
#         overlay = MapImageOverlay(overlay_fp)
#         return overlay
#     except FileNotFoundError:
#         pass

    if name is None:
        name = overlay_fp

    lat = np.array(green_res['lat'])
    long = np.array(green_res['long'])

    lat_grid = np.linspace(lat.min(), lat.max(), num=grid[1])
    long_grid = np.linspace(long.min(), long.max(), num=grid[0])

    green = np.zeros((len(green_res["green"]), 3))
    for i in range(len(green_res["green"])):
        green[i][0] = green_res["long"][i]
        green[i][1] = green_res["lat"][i]
        green[i][2] = green_res["green"][i]

    OK = OrdinaryKriging(green[:, 0], green[:, 1], green[:, 2],
                         variogram_model='spherical')
    z, _ = OK.execute('grid', long_grid, lat_grid, backend='loop',
                      n_closest_points=n_closest_points)

    alpha_map = _alpha_from_coordinates(lat, long, grid)
    overlay = MapImageOverlay(z, lat_grid=lat_grid, long_grid=long_grid,
                              alpha_map=alpha_map,
                              cmap=cmap, name=name)
    overlay.save(overlay_fp)
    return overlay
示例#5
0
def krige_greenery(greenery_dict, krige_tiles, tile, init_kwargs={},
                   dots_per_tile=10):

    coor, green = _compile_greenery(greenery_dict, krige_tiles)
    OK = OrdinaryKriging(coor[:, 0], coor[:, 1], green,
                         **init_kwargs)

    bbox = tile["bbox"]
    lat_grid_degree = np.linspace(bbox[0][0], bbox[1][0], dots_per_tile, endpoint=False)
    long_grid_degree = np.linspace(bbox[0][1], bbox[1][1], dots_per_tile, endpoint=False)
    lat_grid, long_grid = _lat_long_to_metric(lat_grid_degree, long_grid_degree)
    z, _ = OK.execute('grid', long_grid, lat_grid, backend='loop')
    return z
示例#6
0
    def fit(self, X, y):
        if self.interpolation_type == 'kriging':
            # pykrige doesn't support 1D data for now, only 2D or 3D
            # adapting the 1D input to 2D
            self.model = OrdinaryKriging(X,
                                         np.zeros(X.shape),
                                         y,
                                         variogram_model='gaussian')

        if self.interpolation_type == 'linear':
            self.model = scipy.interpolate.interp1d(X,
                                                    y,
                                                    fill_value="extrapolate")

        if self.interpolation_type == 'gmm':
            self.model = cluster_utils.gmr(X, y)
示例#7
0
    def _interpol_func(self, x, y, z, xi, yi):
        ok = OrdinaryKriging(x,
                             y,
                             z,
                             nlags=self.nlags,
                             variogram_model=self.variogram_model,
                             weight=self.weight)
        # coordinates_type=self.coordinates_type)

        zi, sigma = ok.execute(style='points',
                               xpoints=xi,
                               ypoints=yi,
                               n_closest_points=self.n_closest_points,
                               backend=self.backend)

        self.sigma = sigma
        return zi
示例#8
0
def Krige_Interpolate(
    X, Y, new_X, variogram_parameters={"slope": 1e-4, "nugget": 1e-5}
):
   
    uk = OrdinaryKriging(
        X,
        np.zeros(X.shape),
        Y,
        pseudo_inv=True,
        variogram_model="linear",
        variogram_parameters=variogram_parameters
    )

    y_pred, y_std = uk.execute("grid", new_X, np.array([0.0]))
    y_pred = np.squeeze(y_pred)
    y_std = np.squeeze(y_std)

    return new_X, y_pred, y_std
示例#9
0
class Interpolator1D(BaseEstimator):
    def __init__(self, interpolation_type):
        self.interpolation_type = interpolation_type

    def fit_density(self, X):
        if frequency_interpolation_type == 'linear_density':
            self.model = scipy.interpolate.interp1d(list(Counter(X).keys()),
                                                    list(Counter(X).values()),
                                                    fill_value="extrapolate")

        if self.interpolation_type == 'kde':
            self.model = mlab.GaussianKDE(X, 'scott')

    def fit(self, X, y):
        if self.interpolation_type == 'kriging':
            # pykrige doesn't support 1D data for now, only 2D or 3D
            # adapting the 1D input to 2D
            self.model = OrdinaryKriging(X,
                                         np.zeros(X.shape),
                                         y,
                                         variogram_model='gaussian')

        if self.interpolation_type == 'linear':
            self.model = scipy.interpolate.interp1d(X,
                                                    y,
                                                    fill_value="extrapolate")

        if self.interpolation_type == 'gmm':
            self.model = cluster_utils.gmr(X, y)

    def predict(self, X_pred):
        if self.interpolation_type == 'kriging':
            y_pred, y_std = self.model.execute('grid', X_pred, np.array([0.]))
            return np.squeeze(y_pred)

        if self.interpolation_type in ['linear', 'linear_density']:
            return self.model(X_pred)

        if self.interpolation_type == 'kde':
            return self.model.evaluate(X_pred)

        if self.interpolation_type == 'gmm':
            return self.model.predict(np.array([0]),
                                      X_pred[:, np.newaxis]).ravel()
def Krige_Interpolate(X, Y, new_X):
    uk = OrdinaryKriging(
        X,
        np.zeros(X.shape),
        Y,
        pseudo_inv=True,
        weight=True,
        # nlags=3,
        # exact_values=False,
        variogram_model="linear",
        variogram_parameters={"slope": 3e-5, "nugget": 0.0002}
        # variogram_model="gaussian",
        # variogram_parameters={"sill": 1e2, "range": 1e2, "nugget": 0.0006},
    )

    y_pred, y_std = uk.execute("grid", new_X, np.array([0.0]), backend="loop")
    y_pred = np.squeeze(y_pred)
    y_std = np.squeeze(y_std)

    return new_X, y_pred, y_std
示例#11
0
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from mpl_toolkits.basemap import Basemap
import fiona
from pykrige import OrdinaryKriging
from shapely.geometry import Polygon, Point

workbook = pd.read_excel("pmdata.xlsx")
lon, lat, pm = workbook['lon'], workbook['lat'], workbook['PM2.5']

#116.362   30.7578 121.9752  35.1245
un_lon = np.linspace(116.362, 121.9752, 400)
un_lat = np.linspace(30.7578, 35.1245, 400)

OK = OrdinaryKriging(lon, lat, pm, variogram_model='gaussian', nlags=6)
zgrid, ss = OK.execute('grid', un_lon, un_lat)

xgrid, ygrid = np.meshgrid(un_lon, un_lat)

shp = fiona.open('shp/江苏省_行政边界.shp')
pol = shp.next()
polygon = Polygon(pol['geometry']['coordinates'][0][0])

#np.nan
for i in range(xgrid.shape[0]):
    for j in range(xgrid.shape[1]):
        plon = xgrid[i][j]
        plat = ygrid[i][j]
        if not polygon.contains(Point(plon, plat)):
            zgrid[i][j] = np.nan
def draw_function():
    if markclick == 0:  # mclick 如果不存在,则鼠标未点击色阶级数,则给mark1和mnum赋初始值
        mark1 = 0
        mnum = None
    else:
        mark1 = mark1_1
        mnum = mnum_1
    if btn_legendmin.get() == '默认':
        mark2 = 0
        mmin = None
    else:
        mark2 = 1
        mmin = float(btn_legendmin.get())
    if btn_legendmax.get() == '默认':
        mark3 = 0
        mmax = None
    else:
        mark3 = 1
        mmax = float(btn_legendmax.get())
        if mmax <= mmin:
            tm.showwarning('警告', '图例最大值应大于最小值!')

    # ------------警告提示框---------
    if (mark1 == 0 or mark1 == 1) and mark2 == 0 and mark3 == 0:
        pass
    elif color_mark == 6:
        pass
    elif mark1 == 0 and (mark2 == 1 or mark3 == 1):
        tm.showwarning('警告', '色阶级数默认情况下,图例最小值和图例最大值均应为“默认”。')
        return
    elif mark1 == 1 and mark2 == 1 and mark3 == 1:
        pass
    else:
        tm.showinfo('提示', '色阶级数非默认情况下,需同时自定义设置图例最小值和图例最大值;否则图例最大值和最小值以默认值绘出。')
        pass

    path0 = 'DTool/dishi.shp'
    file = shapefile.Reader(path0)
    rec = file.shapeRecords()
    polygon = list()
    for r in rec:
        polygon.append(Polygon(r.shape.points))
    poly = cascaded_union(polygon)  # 并集
    ext = list(poly.exterior.coords)  # 外部点
    codes = [Path.MOVETO] + [Path.LINETO] * (len(ext) - 1) + [Path.CLOSEPOLY]
    #    codes += [Path.CLOSEPOLY]
    ext.append(ext[0])  # 起始点
    path = Path(np.array(ext), codes)
    patch = PathPatch(path, facecolor='None')

    x, y = df['经度'], df['纬度']
    xi = np.arange(113, 118.5, 0.01)
    yi = np.arange(24, 31, 0.01)
    olon, olat = np.meshgrid(xi, yi)

    # Rbf空间插值
    # func = Rbf(x, y, z, function='linear')
    # oz = func(olon, olat)

    # 克里金插值
    ok = OrdinaryKriging(x, y, z, variogram_model='linear')
    oz, ss = ok.execute('grid', xi, yi)

    ax = plt.axes(projection=ccrs.PlateCarree())
    box = [113.4, 118.7, 24.1, 30.4]
    ax.set_extent(box, crs=ccrs.PlateCarree())
    ax.add_patch(patch)
    shp = list(shpreader.Reader(path0).geometries())
    ax.add_geometries(shp,
                      ccrs.PlateCarree(),
                      edgecolor='black',
                      facecolor='none',
                      alpha=0.3,
                      linewidth=0.5)  # 加底图
    if mark1 == 1 and mark2 == 1 and mark3 == 1 and btn_style.get(
    ) != 'CMA_Rain':
        v = np.linspace(mmin, mmax, num=mnum, endpoint=True)  # 设置显示数值范围和级数
        if color_mark == 1:
            pic = plt.contourf(olon, olat, oz, v, cmap=plt.cm.jet)
        elif color_mark == 2:
            pic = plt.contourf(olon, olat, oz, v, cmap=plt.cm.rainbow)
        elif color_mark == 3:
            pic = plt.contourf(olon, olat, oz, v, cmap=plt.cm.gist_rainbow)
        elif color_mark == 4:
            pic = plt.contourf(olon, olat, oz, v, cmap=plt.cm.OrRd)
    elif btn_style.get() == 'CMA_Rain':
        # 应加入超出levels的提示
        try:
            rain_levels = [0, 0.1, 10, 25, 50, 100, 250, 2500]
            rain_colors = [
                '#FFFFFF', '#A6F28F', '#38A800', '#61B8FF', '#0000FF',
                '#FA00FA', '#730000', '#400000'
            ]
            pic = plt.contourf(olon,
                               olat,
                               oz,
                               levels=rain_levels,
                               colors=rain_colors)
            # cbar = plt.colorbar(pic, ticks=[0, 0.1, 10, 25, 50, 100, 250])
            # position = fig.add_axes([0.65, 0.15, 0.03, 0.3])  # 位置
            # plt.colorbar(pic, ticks=[0, 0.1, 10, 25, 50, 100, 250], cax=position, orientation='vertical')
            # cbar.set_label(btn9.get(), fontproperties='SimHei')  # 图例label在右边
        except:
            if np.min(z) < 0:
                tm.showinfo(message='存在负数,超出降水图例范围!请换其他颜色样式。')
            elif np.max(z) > 2500:
                tm.showinfo(message='降水量过大,请何查数据!或请换其他颜色样式。')
                # cbar.make_axes(locations='top')
        # cbar.ax.set_xlabel(btn9.get(),fontproperties='SimHei')
    else:
        if color_mark == 1:
            if mark1 == 0:  # 未设置级数
                pic = plt.contourf(olon, olat, oz, cmap=plt.cm.jet)
            else:
                v = np.linspace(np.min(oz),
                                np.max(oz),
                                num=mnum,
                                endpoint=True)  # 设置显示数值范围和级数
                pic = plt.contourf(olon, olat, oz, v, cmap=plt.cm.jet)
        elif color_mark == 2:
            if mark1 == 0:
                pic = plt.contourf(olon, olat, oz, cmap=plt.cm.rainbow)
            else:
                v = np.linspace(np.min(oz),
                                np.max(oz),
                                num=mnum,
                                endpoint=True)
                pic = plt.contourf(olon, olat, oz, v, cmap=plt.cm.rainbow)
        elif color_mark == 3:
            if mark1 == 0:
                pic = plt.contourf(olon, olat, oz, cmap=plt.cm.gist_rainbow)
            else:
                v = np.linspace(np.min(oz),
                                np.max(oz),
                                num=mnum,
                                endpoint=True)
                pic = plt.contourf(olon, olat, oz, v, cmap=plt.cm.gist_rainbow)
        elif color_mark == 4:
            if mark1 == 0:
                pic = plt.contourf(olon, olat, oz, cmap=plt.cm.OrRd)
            else:
                v = np.linspace(np.min(oz),
                                np.max(oz),
                                num=mnum,
                                endpoint=True)
                pic = plt.contourf(olon, olat, oz, v, cmap=plt.cm.OrRd)
        elif color_mark == 6:  # 自定义颜色
            # 判断出自定义颜色级数
            jishu_colors = []
            for index, r in enumerate(rgb):
                if r == None:
                    num_color = index
                    break
                else:
                    jishu_colors.append(r)

            if mark2 == 1 and mark3 == 1:  # mark2最小值;mark3最大值
                v = np.linspace(mmin, mmax, num=num_color + 1,
                                endpoint=True)  # 设置显示数值范围和级数
                pic = plt.contourf(olon, olat, oz, v, colors=jishu_colors)
            else:
                v = np.linspace(np.min(oz),
                                np.max(oz),
                                num=len(jishu_colors) + 1,
                                endpoint=True)
                pic = plt.contourf(olon, olat, oz, v, colors=jishu_colors)

    for collection in pic.collections:
        collection.set_clip_path(patch)  # 设置显示区域

    plt.scatter(x, y, marker='.', c='k', s=10)  # 绘制站点

    # 添加显示站名、数值等标签
    for i in range(len(z)):
        plt.text(x[i], y[i] + 0.05, df['站名'][i], size=5.5, weight=2, wrap=True)
    # 添加单位标注
    plt.text(117.75, 27.1, btn_legendunit.get(), size=8, weight=2)
    fig = plt.gcf()
    fig.set_size_inches(6, 4)  # 设置图片大小
    plt.axis('off')  # 去除四边框框
    if btn_style.get() == 'CMA_Rain':
        position = fig.add_axes([0.65, 0.15, 0.03, 0.3])  # 位置
        plt.colorbar(pic,
                     ticks=[0, 0.1, 10, 25, 50, 100, 250],
                     cax=position,
                     orientation='vertical')
    else:
        position = fig.add_axes([0.65, 0.15, 0.03, 0.3])  # 位置
        plt.colorbar(pic, cax=position, orientation='vertical')

    plt.savefig('pics_dpi100.png', dpi=100, bbox_inches='tight')
    plt.savefig('pics_dpi300.png', dpi=300, bbox_inches='tight')
    plt.close()
    wifi_img = Image.open('pics_dpi100.png')
    img = ImageTk.PhotoImage(wifi_img)
    window.img = img  # to prevent the image garbage collected.
    canvas.create_image(200, 180, anchor='center', image=img)  # 设置生成的图片位置
示例#13
0
def main(argv):

    inps = cmdLineParse()
    gps_file = inps.gps_file
    geom_file = inps.geo_file
    date = inps.date

    datasetNames = ut.get_dataNames(gps_file)

    meta0 = read_attr(gps_file)
    meta = read_attr(geom_file)
    WIDTH = int(meta['WIDTH'])
    LENGTH = int(meta['LENGTH'])
    date_list = read_hdf5(gps_file, datasetName='date')[0]
    date_list = date_list.astype('U13')
    date_list = list(date_list)

    root_path = os.getcwd()
    gig_dir = root_path + '/gigpy'
    gig_atm_dir = gig_dir + '/atm'
    gig_atm_raw_dir = gig_dir + '/atm/raw'
    gig_atm_sar_raw_dir = gig_dir + '/atm/sar_raw'

    gig_atm_sar_tzd_dir = gig_dir + '/atm/sar_tzd'
    gig_atm_sar_wzd_dir = gig_dir + '/atm/sar_wzd'

    if not os.path.isdir(gig_atm_sar_tzd_dir):
        os.mkdir(gig_atm_sar_tzd_dir)

    if not os.path.isdir(gig_atm_sar_wzd_dir):
        os.mkdir(gig_atm_sar_wzd_dir)

    if inps.out_file: OUT = inps.out_file
    else: OUT = date + '_' + inps.type + '.h5'

    if inps.type == 'tzd': OUT = gig_atm_sar_tzd_dir + '/' + OUT
    elif inps.type == 'wzd': OUT = gig_atm_sar_wzd_dir + '/' + OUT

    dem = read_hdf5(geom_file, datasetName='height')[0]
    #inc = read_hdf5(geom_file,datasetName='incidenceAngle')[0]

    dataNames = get_dataNames(geom_file)
    if 'latitude' in dataNames:
        grid_lat = read_hdf5(geom_file, datasetName='latitude')[0]
        grid_lon = read_hdf5(geom_file, datasetName='longitude')[0]
    else:
        grid_lat, grid_lon = get_lat_lon(meta)

    lats = grid_lat.flatten()
    lons = grid_lon.flatten()

    where_are_NaNs = np.isnan(lats)
    lats[where_are_NaNs] = 0

    where_are_NaNs = np.isnan(lons)
    lons[where_are_NaNs] = 0
    #print(grid_lat.shape)
    # TWD, ZWD, TURB_Kriging, Turb_ITD, Trend, HgtCor,
    k0 = date_list.index(date)

    if 'wzd_turb' in datasetNames:
        tzd_turb, wzd_turb, lat, lon = adjust_aps_lat_lon_unavco(gps_file,
                                                                 epoch=k0)
    else:
        tzd_turb, lat, lon = adjust_aps_lat_lon_unr(gps_file, epoch=k0)

    #print(inps.type)
    if inps.type == 'tzd':
        S0 = 'tzd'
        turb = tzd_turb
    elif inps.type == 'wzd':
        S0 = 'wzd'
        turb = wzd_turb

    lat0, lon0, turb0 = remove_numb(lat, lon, turb, int(meta0['remove_numb']))

    mean_lons = np.mean(lons)
    lons0 = round((np.mean(lon0) - mean_lons) /
                  360) * 360 + lons  # adjust to the same trend system
    #lons0 = lons
    lats0 = lats
    #print(lats)
    #print(lons)
    if np.mean(lon0) > 180:
        lon0 = lon0 - 360
    #print(lat0)
    #print(lon0)
    variogram_para = read_hdf5(gps_file,
                               datasetName=S0 + '_variogram_parameter')[0]
    trend_para = read_hdf5(gps_file, datasetName=S0 + '_trend_parameter')[0]
    elevation_para = read_hdf5(gps_file,
                               datasetName=S0 + '_elevation_parameter')[0]

    variogram_para0 = variogram_para[k0, :]
    trend_para0 = trend_para[k0, :]
    elevation_para0 = elevation_para[k0, :]

    Trend0 = function_trend(lats0, lons0, trend_para0)
    Trend0 = Trend0.reshape(LENGTH, WIDTH)

    hei = dem.flatten()
    elevation_function = model_dict[meta0['elevation_model']]
    Dem_cor0 = elevation_function(elevation_para0, hei)
    Dem_cor0 = Dem_cor0.reshape(LENGTH, WIDTH)

    OK = OrdinaryKriging(lon0,
                         lat0,
                         turb0,
                         variogram_model=meta0['variogram_model'],
                         verbose=False,
                         enable_plotting=False)
    para = variogram_para0[0:3]
    #print(para)
    para[1] = para[1] / 6371 / np.pi * 180
    #print(para)
    OK.variogram_model_parameters = para

    Numb = inps.parallelNumb
    zz = np.zeros((len(lats), ))
    zz_sigma = np.zeros((len(lats), ))
    #print(len(lats))
    n_jobs = inps.parallelNumb

    # split dataset into multiple subdata
    split_numb = 1000
    idx_list = split_lat_lon_kriging(len(lats), processors=split_numb)
    #print(idx_list[split_numb-1])

    print(
        '------------------------------------------------------------------------------'
    )
    if inps.type == 'tzd':
        print(
            'Start to interpolate the high-resolution tropospheric delay for SAR acquisition: '
            + date)
        print('Total number of the available GPS stations: %s' % str(len(lat)))
    elif inps.type == 'wzd':
        print(
            'Start to interpolate the high-resolution atmospheric water vapor for SAR acquisition: '
            + date)
        print('Total number of the available GPS stations: %s' % str(len(lat)))

    if inps.method == 'kriging':
        np0 = inps.kriging_points_numb
        data = []
        for i in range(split_numb):
            data0 = (OK, lats[idx_list[i]], lons[idx_list[i]], np0)
            data.append(data0)
        future = ut.parallel_process(data,
                                     OK_function,
                                     n_jobs=Numb,
                                     use_kwargs=False)

    elif inps.method == 'weight_distance':
        data = []
        for i in range(split_numb):
            data0 = (lat0, lon0, turb0, lats[idx_list[i]], lons[idx_list[i]])
            data.append(data0)
        future = ut.parallel_process(data,
                                     dist_weight_interp,
                                     n_jobs=Numb,
                                     use_kwargs=False)

    for i in range(split_numb):
        id0 = idx_list[i]
        gg = future[i]
        zz[id0] = gg[0]

    turb = zz.reshape(LENGTH, WIDTH)
    datasetDict = dict()

    aps = Dem_cor0 + Trend0 + turb
    datasetDict['hgt_sar'] = Dem_cor0
    datasetDict['trend_sar'] = Trend0
    datasetDict['turb_sar'] = turb
    datasetDict['aps_sar'] = aps

    if inps.method == 'kriging':
        for i in range(split_numb):
            id0 = idx_list[i]
            gg = future[i]
            zz_sigma[id0] = gg[1]
        sigma = zz_sigma.reshape(LENGTH, WIDTH)
        datasetDict['turb_sar_sigma'] = sigma

    meta['DATA_TYPE'] = inps.type
    meta['interp_method'] = inps.method

    for key, value in meta0.items():
        meta[key] = str(value)

    ut.write_h5(datasetDict,
                OUT,
                metadata=meta,
                ref_file=None,
                compression=None)

    sys.exit(1)
示例#14
0
                 [1.80, 0.76], [1.93, 0.68], [2.03, 0.03], [2.12, 0.31],
                 [2.23, -0.14], [2.31, -0.88], [2.40, -1.25], [2.50, -1.62],
                 [2.63, -1.37], [2.72, -0.99], [2.80, -1.92], [2.83, -1.94],
                 [2.91, -1.32], [3.00, -1.69], [3.13, -1.84], [3.21, -2.05],
                 [3.30, -1.69], [3.41, -0.53], [3.52, -0.55], [3.63, -0.92],
                 [3.72, -0.76], [3.80, -0.41], [3.91, 0.12], [4.04, 0.25],
                 [4.13, 0.16], [4.24, 0.26], [4.32, 0.62], [4.44, 1.69],
                 [4.52, 1.11], [4.65, 0.36], [4.74, 0.79], [4.84, 0.87],
                 [4.93, 1.01], [5.02, 0.55]]).T
# fmt: on

X_pred = np.linspace(-6, 6, 200)

# pykrige doesn't support 1D data for now, only 2D or 3D
# adapting the 1D input to 2D
uk = OrdinaryKriging(X, np.zeros(X.shape), y, variogram_model="gaussian")

y_pred, y_std = uk.execute("grid", X_pred, np.array([0.0]))

y_pred = np.squeeze(y_pred)
y_std = np.squeeze(y_std)

fig, ax = plt.subplots(1, 1, figsize=(10, 4))
ax.scatter(X, y, s=40, label="Input data")

ax.plot(X_pred, y_pred, label="Predicted values")
ax.fill_between(
    X_pred,
    y_pred - 3 * y_std,
    y_pred + 3 * y_std,
    alpha=0.3,
示例#15
0
def main(argv):

    inps = cmdLineParse()
    gps_h5 = inps.gps_file
    FILE = gps_h5
    R = 6371  # Radius of the Earth (km)

    if inps.out: OUT = inps.out
    else: OUT = 'gps_delay_variogram.h5'

    print('')
    print('Start to estimate the variogram (i.e. variance) samples ...')
    print('Output file name: %s' % OUT)
    print('Remove outliers number: %s' % inps.remove_numb)
    print('')
    print('       Date     Stations')

    BIN_NUMB = inps.bin_numb

    datasetNames = ut.get_dataNames(FILE)
    if 'wzd_turb' in datasetNames:
        wzd_turb = read_hdf5(FILE, datasetName='wzd_turb')[0]
        wzd_turb_trend = read_hdf5(FILE, datasetName='wzd_turb_trend')[0]

    date_list, meta = read_hdf5(FILE, datasetName='date')
    tzd_turb = read_hdf5(FILE, datasetName='tzd_turb')[0]
    tzd_turb_trend = read_hdf5(FILE, datasetName='tzd_turb_trend')[0]

    Lag = np.zeros((len(date_list), BIN_NUMB), dtype=np.float32)
    Variance = np.zeros((len(date_list), BIN_NUMB), dtype=np.float32)
    Variance_trend = np.zeros((len(date_list), BIN_NUMB), dtype=np.float32)

    Variance_wzd = np.zeros((len(date_list), BIN_NUMB), dtype=np.float32)
    Variance_wzd_trend = np.zeros((len(date_list), BIN_NUMB), dtype=np.float32)

    for i in range(len(date_list)):
        #print(date_list[i])
        if 'wzd_turb' in datasetNames:
            tzd_turb, wzd_turb, tzd_turb_trend, wzd_turb_trend, lat, lon = adjust_aps_lat_lon_unavco(
                FILE, epoch=i)
        else:
            tzd_turb, tzd_turb_trend, lat, lon = adjust_aps_lat_lon_unr(
                FILE, epoch=i)

        lat = np.asarray(lat, dtype=np.float32)
        lon = np.asarray(lon, dtype=np.float32)

        lat0, lon0, tzd_turb0, fg0 = remove_numb(lat,
                                                 lon,
                                                 tzd_turb,
                                                 numb=inps.remove_numb)
        #print(tzd_turb0*1000)
        print('     ' + date_list[i].astype(str) + '     ' + str(len(lat0[0])))
        #uk = OrdinaryKriging(lon, lat, tzd_turb, coordinates_type = 'geographic', nlags=BIN_NUMB)
        uk = OrdinaryKriging(lon0,
                             lat0,
                             tzd_turb0,
                             coordinates_type='geographic',
                             nlags=BIN_NUMB)
        #print(len((uk.lags)))
        Lags = (uk.lags) / 180 * np.pi * R
        Semivariance = 2 * (uk.semivariance)
        #print(Semivariance)
        y0 = np.asarray(tzd_turb_trend, dtype=np.float32)
        tzd_turb_trend0 = y0[fg0]
        #lat0, lon0, tzd_turb_trend0 = remove_numb(lat,lon,tzd_turb_trend,numb=inps.remove_numb)
        uk = OrdinaryKriging(lon0,
                             lat0,
                             tzd_turb_trend0,
                             coordinates_type='geographic',
                             nlags=BIN_NUMB)
        Semivariance_trend = 2 * (uk.semivariance)

        Lag[i, 0:len(Lags)] = Lags
        Variance[i, 0:len(Lags)] = Semivariance
        Variance_trend[i, 0:len(Lags)] = Semivariance_trend

        if 'wzd_turb' in datasetNames:
            lat0, lon0, wzd_turb0, fg0 = remove_numb(lat,
                                                     lon,
                                                     wzd_turb,
                                                     numb=inps.remove_numb)
            uk = OrdinaryKriging(lon0,
                                 lat0,
                                 wzd_turb0,
                                 coordinates_type='geographic',
                                 nlags=BIN_NUMB)
            Semivariance_wzd = 2 * (uk.semivariance)

            #lat0, lon0, wzd_turb_trend0, fg0 = remove_numb(lat,lon,wzd_turb_trend,numb=inps.remove_numb)
            y0 = np.asarray(wzd_turb_trend, dtype=np.float32)
            wzd_turb_trend0 = y0[fg0]
            uk = OrdinaryKriging(lon0,
                                 lat0,
                                 wzd_turb_trend0,
                                 coordinates_type='geographic',
                                 nlags=BIN_NUMB)
            Semivariance_wzd_trend = 2 * (uk.semivariance)

            Variance_wzd[i, :] = Semivariance
            Variance_wzd_trend[i, :] = Semivariance_trend

    #datasetNames = ['date','gps_name','gps_lat','gps_lon','gps_height','hzd','wzd','tzd','wzd_turb_trend','tzd_turb_trend','wzd_turb','tzd_turb','station','tzd_elevation_parameter', 'wzd_elevation_parameter','tzd_trend_parameter', 'wzd_trend_parameter']

    datasetDict = dict()
    meta['remove_numb'] = inps.remove_numb

    for dataName in datasetNames:
        datasetDict[dataName] = read_hdf5(FILE, datasetName=dataName)[0]

    datasetDict['Lags'] = Lag
    datasetDict['Semivariance'] = Variance
    datasetDict['Semivariance_trend'] = Variance_trend

    if 'wzd_turb' in datasetNames:
        datasetDict['Semivariance_wzd'] = Variance_wzd
        datasetDict['Semivariance_wzd_trend'] = Variance_wzd_trend

    ut.write_h5(datasetDict,
                OUT,
                metadata=meta,
                ref_file=None,
                compression=None)

    sys.exit(1)
示例#16
0
lista = []

for x in range(1, 101, 1):
    print('Archivo ', x)
    file = '..\\database\\dataSet60_' + str(x) + '.csv'
    data = np.array(np.loadtxt(file, delimiter=','))
    dataOld = np.array(
        pd.read_csv('..\\database\\ptr.water.csv', delimiter=','))
    dataInt = open('..\\database\\dataSetInterpolatedKringing.csv', 'w')

    gridx = np.arange(0.0, 72, 0.5)
    gridy = np.arange(0.0, 37, 0.5)

    OK = OrdinaryKriging(data[:, 0],
                         data[:, 1],
                         data[:, 2],
                         variogram_model='linear',
                         verbose=False,
                         enable_plotting=False)
    z, ss = OK.execute('grid', gridx, gridy)
    kt.write_asc_grid(gridx, gridy, z, filename="output.asc")

    valor = 0
    suma = 0
    for k in range(1, 2, 1):
        print('Iteracion ', k)
        for i in range(0, 72, 1):
            for j in range(0, 144, 1):
                valor = round(data[i][j], 4)
                valorOld = round(dataOld[i][j], 4)
                if (i == 0 and j < 143) or (j == 0 and j < 143):
                    dataInt.write(str(valor) + ',')
示例#17
0
                 [2.91, -1.32], [3.00, -1.69], [3.13, -1.84], [3.21, -2.05],
                 [3.30, -1.69], [3.41, -0.53], [3.52, -0.55], [3.63, -0.92],
                 [3.72, -0.76], [3.80, -0.41], [3.91, 0.12], [4.04, 0.25],
                 [4.13, 0.16], [4.24, 0.26], [4.32, 0.62], [4.44, 1.69],
                 [4.52, 1.11], [4.65, 0.36], [4.74, 0.79], [4.84, 0.87],
                 [4.93, 1.01], [5.02, 0.55]]).T

from pykrige import OrdinaryKriging

X_pred = np.linspace(-6, 6, 200)

# pykrige doesn't support 1D data for now, only 2D or 3D
# adapting the 1D input to 2D
uk = OrdinaryKriging(
    X,
    np.zeros(X.shape),
    y,
    variogram_model='gaussian',
)

y_pred, y_std = uk.execute('grid', X_pred, np.array([0.]))

y_pred = np.squeeze(y_pred)
y_std = np.squeeze(y_std)

fig, ax = plt.subplots(1, 1, figsize=(10, 4))
ax.scatter(X, y, s=40, label='Input data')

ax.plot(X_pred, y_pred, label='Predicted values')
ax.fill_between(X_pred,
                y_pred - 3 * y_std,
                y_pred + 3 * y_std,
co2 = (10**6) * co2
co2 = co2.flatten()
#co2=co2.transpose()
print(co2)

gridx = np.linspace(70.0, 75.0, 737)
#print(gridx)
gridy = np.linspace(22.0, 26.0, 448)
#print(gridy)

coords = np.transpose(
    [np.tile(gridx, len(gridy)),
     np.repeat(gridy, len(gridx))])
model = OrdinaryKriging(lon,
                        lat,
                        co2,
                        variogram_model='linear',
                        enable_plotting=True)
z, ss = model.execute('grid', gridx, gridy)
#
##read data from tif images for fpar values
fpar = rasterio.open('2010_01_01.tif')
#print(fpar.width)
fpar = fpar.read(1)
fpar = fpar.flatten()
print(len(fpar))

#
ndvi = rasterio.open('2010_01_01-737x448-ndvi.tif')
ndvi = ndvi.read(1)
ndvi = ndvi.flatten()
示例#19
0
     [2.12 , 0.31], [2.23 ,-0.14], [2.31 ,-0.88], [2.40 ,-1.25], [2.50 ,-1.62],
     [2.63 ,-1.37], [2.72 ,-0.99], [2.80 ,-1.92], [2.83 ,-1.94], [2.91 ,-1.32],
     [3.00 ,-1.69], [3.13 ,-1.84], [3.21 ,-2.05], [3.30 ,-1.69], [3.41 ,-0.53],
     [3.52 ,-0.55], [3.63 ,-0.92], [3.72 ,-0.76], [3.80 ,-0.41], [3.91 , 0.12],
     [4.04 , 0.25], [4.13 , 0.16], [4.24 , 0.26], [4.32 , 0.62], [4.44 , 1.69],
     [4.52 , 1.11], [4.65 , 0.36], [4.74 , 0.79], [4.84 , 0.87], [4.93 , 1.01],
     [5.02 , 0.55]]).T


from pykrige import OrdinaryKriging

X_pred = np.linspace(-6, 6, 200)

# pykrige doesn't support 1D data for now, only 2D or 3D
# adapting the 1D input to 2D
uk = OrdinaryKriging(X, np.zeros(X.shape), y, variogram_model='gaussian',)

y_pred, y_std = uk.execute('grid', X_pred, np.array([0.]))

y_pred = np.squeeze(y_pred)
y_std = np.squeeze(y_std)

fig, ax = plt.subplots(1, 1, figsize=(10, 4))
ax.scatter(X, y, s=40, label='Input data')


ax.plot(X_pred, y_pred, label='Predicted values')
ax.fill_between(X_pred, y_pred - 3*y_std, y_pred + 3*y_std, alpha=0.3, label='Confidence interval')
ax.legend(loc=9)
ax.set_xlabel('x')
ax.set_ylabel('y')
示例#20
0
datetime = df_TAHMO.index.values
x = lon_u
y = lat_u
nx = len(x)
ny = len(y)

# Interpolation of (TAHMO) stations on the regular grid
v = np.zeros((nt, 37, 37))
for k in range(nt):
    OK = OrdinaryKriging(lon_v,
                         lat_v,
                         np.sqrt(v_station[k, :]),
                         variogram_model='exponential',
                         variogram_parameters={
                             'sill': 1.0,
                             'range': 2,
                             'nugget': 0.01
                         },
                         nlags=50,
                         verbose=False,
                         enable_plotting=False,
                         weight=True,
                         coordinates_type='geographic')
    z, ss = OK.execute('grid', lon_u, lat_u)
    v[k, :, :] = (z**2).T

u_input = u.copy()
v_input = v.copy()

#==================================================================
# Pre-processing
print('\nPre-processing')
nc.variables.keys()

lat = nc.variables['latitude'][:].data
print(lat[:8], type(lat[:8]))
lon = nc.variables['longitude'][:].data
print((((68 < lon) & (lon < 75)) & (20 < lat) & (lat < 25)).sum())
print(lon[(68 < lon) & (lon < 75) & (20 < lat) & (lat < 25)])

co2 = nc.variables['xco2'][:].data
print(co2)

import pandas as pd
import numpy as np
from pykrige import OrdinaryKriging

model = OrdinaryKriging(lon, lat, co2, variogram_model='gaussian')
#print(model)

# grid around gujarat
gridx = np.arange(68.1862489922972372, 74.4766299192354495, 0.1)
print(gridx.ndim)
print(gridx.shape)
print(len(gridx))
gridy = np.arange(20.1209430201043347, 24.7084824408120198, 0.1)
print(gridy.shape)
coords = np.transpose(
    [np.tile(gridx, len(gridy)),
     np.repeat(gridy,
               len(gridx))])  #for every combination of lat and long together.
#array([[1, 5],
#       [2, 5],