import eofs
from eofs.standard import Eof
import pandas as pd
import numpy as np
import glob
import datetime
from matplotlib import pyplot as plt
import copy
import re
import time
from pylab import *
import matplotlib.dates as mdate
import matplotlib.patches as patches
import matplotlib.ticker as ticker
import xarray as ax
import copy
import geopandas as gpd
from pykrige.ok import OrdinaryKriging
from pykrige.kriging_tools import write_asc_grid
import pykrige.kriging_tools as kt
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.patches import Path, PathPatch
from shapely.geometry import LineString
df = pd.read_csv(r'.6月xq表层.csv')
fg = gpd.read_file(r'.覆盖.shp')
fg1 = fg.to_crs(crs = 'EPSG:4326')
bj = gpd.read_file(r'.xinqiao1.shp')
bj1 = bj.to_crs(crs = 'EPSG:4326')
bj2 = bj1.boundary[0]
bj3 = bj2.coords[:]
bj4 = list(bj3)
x = []
y =[]
for i in range(len(bj4)):
g = bj4[i][0]
c = bj4[i][1]
x.append(g)
y.append(c)
lons = np.array(x)
lats = np.array(y)
lons_1 = df['经度']
lats_1 = df['纬度']
a = df.iloc[:,9:22].T
pd.set_option('display.max_rows', 10)
pd.set_option('display.max_columns', 4)
# print(a)
# 预处理
c = a.mean(axis=1)
td = a.std(axis=1)
# print(td)
for i in range(len(a)):
for j in range(0,34):
# print(a.iloc[i,j])
a.iloc[i,j] = (a.iloc[i,j]-c[i])/td[i] # 列标准化
# print(a)
a1 = copy.deepcopy(np.array(a))
# a2 = pd.DataFrame(a1)
# print(a2)
# a2.to_csv(r'.iaozhunc.csv',encoding='utf-8')
# eofs
solver = Eof(a1,ddof=0,center=False) # center 意味着要减去所有的平均,TMD
eofs = solver.eofs(neofs=3, eofscaling=0)
weights = solver.eigenvalues(3) / sum(solver.eigenvalues()) # 贡献量
print(weights)
pt = pd.DataFrame(eofs.T,columns=['fir','sec','thi']) # 模态
print(pt)
z1 = pt['fir']
z2 = pt['sec']
z3 = pt['thi']
pcs = solver.pcs(npcs =3)
# print(pcs)
tc = pd.DataFrame(pcs,columns=['fir','sec','thi']) # 时间、权重
tc_fir = tc['fir']
tc_sec = tc['sec']
tc_thi = tc['thi']
grid_space = 0.001
grid_lon = np.arange(np.amin(lons), np.amax(lons), grid_space)
grid_lat = np.arange(np.amin(lats-0.0001), np.amax(lats), grid_space)
OK1 = OrdinaryKriging(lons_1, lats_1,z1, variogram_model='spherical', verbose=False, enable_plotting=False,nlags=20)
OK2 = OrdinaryKriging(lons_1, lats_1,z2, variogram_model='spherical', verbose=False, enable_plotting=False,nlags=20)
OK3 = OrdinaryKriging(lons_1, lats_1,z3, variogram_model='spherical', verbose=False, enable_plotting=False,nlags=20)
z_1,sss1 =OK1.execute('grid', grid_lon, grid_lat)
z_2,sss2 =OK2.execute('grid', grid_lon, grid_lat)
z_3,sss3 =OK3.execute('grid', grid_lon, grid_lat)
X, Y = np.meshgrid(grid_lon, grid_lat)
fig = plt.figure(figsize=(16,12))
subplots_adjust(left=0.1,bottom=0.1,top=0.95,right=0.95,hspace=0.15,wspace=0.0005)
# grid1 =plt.GridSpec(3,2,wspace=0.5,hspace=0.1)
ax = fig.add_subplot(321)
ax.plot(tc.index,tc_fir,color = 'black')
ax1 = fig.add_subplot(322)
cs =ax1.contourf(X,Y,z_1,200,extend='both',cmap='jet')
cbar = fig.colorbar(cs,ax =ax1,fraction = 0.09)
fg1.geometry.plot(facecolor='white',edgecolor='white',alpha=1,ax =ax1)
ax2 = fig.add_subplot(323)
ax2.plot(tc.index,tc_sec,color = 'black')
ax3 = fig.add_subplot(324)
cs1 =ax3.contourf(X,Y,z_2,200,extend='both',cmap='jet')
cbar1 = fig.colorbar(cs,ax =ax3,fraction = 0.09)
fg1.geometry.plot(facecolor='white',edgecolor='white',alpha=1,ax =ax3)
ax4 = fig.add_subplot(325)
ax4.plot(tc.index,tc_thi,color = 'black')
ax5= fig.add_subplot(326)
cs2 =ax5.contourf(X,Y,z_3,200,extend='both',cmap='jet')
cbar2 = fig.colorbar(cs,ax =ax5,fraction = 0.09)
fg1.geometry.plot(facecolor='white',edgecolor='white',alpha=1,ax =ax5)
plt.show()