| 
 
	积分17567贡献 精华在线时间 小时注册时间2013-6-7最后登录1970-1-1 
 | 
 
| 
本帖最后由 洗雨 于 2021-10-20 08:48 编辑
x
登录后查看更多精彩内容~您需要 登录 才可以下载或查看,没有帐号?立即注册 
  
 纯属瞎折腾,希望得到指点
 复制代码# -*- coding: utf-8 -*-
"""
Created on Wed Jan 13 16:56:43 2021
@author: gaoyong
"""
import xarray as xr
import numpy as np
import pandas as pd
from scipy import stats
import calendar as cal
def time_series(filename,yearstart,monthstart,yearend,monthend):
    file2 = pd.read_csv(filename,sep='\s+')
    ser   = []
  
    for j in range(yearstart,yearend+1):
        for i in range(monthstart,monthend+1):
            imonth = cal.month_abbr[i]
            ser.append(file2[imonth][j])
    return ser
def grid_series_corr(ncfilename,ser):
    data = xr.open_dataset(ncfilename)
    ser  = np.array(ser)
    yl,ml,latl,lonl = data.pre.shape
    data_new        = np.array((data.pre.values).reshape(yl*ml,latl*lonl))
    latmin          = data.lat.min()
    latmax          = data.lat.max()
    lonmin          = data.lon.min()
    lonmax          = data.lon.max()
    lats            = np.linspace(latmax,latmin,latl)
    lons            = np.linspace(lonmin,lonmax,lonl)
    rr              = []
    for i in range(latl*lonl):
        r = np.corrcoef(np.array(data_new[:,i]),ser)[0,1]
        rr.append(r)
    lons,lats = np.meshgrid(lons,lats)
    rr        = np.array(rr).reshape(lonl,latl)
    return rr,lons,lats
def spacc_corr(data1,data2):
    '''data1和data2为2维数组,数据维度相同,还没写完,慎用'''
    data1 = np.array(data1)
    data2 = np.array(data2)
    x,y  = data1.shape
    co = []
    for i in range(y):
        r  = np.corrcoef(data1[:,i],data2[:,i])[0,1]
        co.append(r)
    co = np.array(co).reshape(x,y)
    return co
if __name__ == '__main__':
    file   = r'E:\tem_test\tw\lqx\pre.mon.1961-2018.nc'
    file2  = r'E:\tem_test\tw\lqx\AOI.txt'
    ser = time_series(file2,1961,1,2018,12)
    a,lons,lats   = grid_series_corr(file,ser)
    print(a)
 
 | 
 |