2017-05-09 8 views
2

저는 두 가지 월간 글로벌 그리드 데이터 세트 (시간, 라트, 론) 형식의 액체 물 상당 두께가 있습니다. 둘 다 공간적, 시간적 해상도가 동일합니다. 상관 관계를 원하지만 numpy.corrcoef()은 3D가 아닌 2D 배열에서만 작동합니다. 그래서 전체 시계열에 대해 두 변수의 동일한 격자 점 (x, y)을 상관 시키려고합니다. 사실 상관 계수 격자로 새로운 nc 파일을 원합니다.파이썬에서 그리드 된 데이터 세트를 연결합니다.

import numpy as np 
from netCDF4 import Dataset 

wdir = '.../Data/' 

# read GRACE NCs 
GRACE_GFZ = Dataset(wdir+'GRACE/GRCTellus.GFZ.200204_201607.nc','r') 
GRACE_JPL = Dataset(wdir+'GRACE/GRCTellus.JPL.200204_201607.nc','r') 

두 변수 (GFZ 및 JPL)가 동일한 위치에서 누락 값을 동일한 양을 갖는다.

GRACE_GFZ.variables['lwe_thickness'] 
    <type 'netCDF4._netCDF4.Variable'> 
    float32 lwe_thickness(time, lat, lon) 
     long_name: Liquid_Water_Equivalent_Thickness 
     units: cm 
     _FillValue: 32767.0 
     missing_value: 32767.0 
    unlimited dimensions: time 
    current shape = (155, 72, 144) 
    filling off 

GRACE_JPL.variables['lwe_thickness'] 
    <type 'netCDF4._netCDF4.Variable'> 
    float32 lwe_thickness(time, lat, lon) 
     long_name: Liquid_Water_Equivalent_Thickness 
     units: cm 
     _FillValue: 32767.0 
     missing_value: 32767.0 
    unlimited dimensions: time 
    current shape = (155, 72, 144) 
    filling off 

들은 하나 모두에 사용될 수로부터 동일한 공간적 해상도, 시간, 위도 및 경도를 가지고있다.

time = GRACE_GFZ.variables['time'][:] 
lons = GRACE_GFZ.variables['lon'][:] 
lats = GRACE_GFZ.variables['lat'][:] 
gfz = GRACE_GFZ.variables['lwe_thickness'][:] 
jpl = GRACE_JPL.variables['lwe_thickness'][:] 

여기 그리드를 통과하여 corrcoef를 배열에 넣고 싶습니다. 이것은 나에게 2x2 행렬의 무리를 준다.

test = [] 
for x in range(len(lats)): 
    for y in range(len(lons)): 
     print(np.corrcoef(gfz[:,x,y],jpl[:,x,y])) 

어떻게 각 지점의 corrcoef를 올바른 위치에 새로운 배열에 넣을 수 있습니까?

+1

질문을 [최소, 완전하며 검증 가능한 예] (http://stackoverflow.com/help/mcve)로 업데이트 할 수 있습니까? – Kasramvd

+0

@ Kasramvd가 필요한 정보입니까? –

답변

0

나는 다음에 내 문제를 극복 :

temp =[] 
corrcoefMatrix_gfzjpl = [[0 for i in range(len(lons))] for j in range(len(lats))] 
for x in range(len(lats)): 
    for y in range(len(lons)): 
     temp = np.corrcoef(gfz[:,x,y],jpl[:,x,y]) 
     corrcoefMatrix_gfzjpl[x][y] = temp[0,1] 

corrcoefMatrix_gfzjpl = np.squeeze(np.asarray(corrcoefMatrix_gfzjpl)) 

은 기본적으로 내가 0을 포함하는 매트릭스를 제작하고 corrcoef 매트릭스의 상관 계수 값을 대체했다. 나는 각각을위한 for 루프를 사용하여 lats와 lons로 가면서 각 그리드 셀에 대해 이것을 수행했다. 나중에 모든 치수와 변수를 정의한 새로운 netcdf 파일을 만들었습니다.

nc_data.createDimension('lat', len(lats)) 
nc_data.createDimension('lon', len(lons)) 
nc_data.createDimension('data', 1) 

latitudes = nc_data.createVariable('latitude', 'f4', ('lat',)) 
longitudes = nc_data.createVariable('longitude', 'f4', ('lon',)) 
corrcoef_gfzjpl = nc_data.createVariable('corrcoef_gfzjpl', 'f4', ('lat', 'lon', 'data'), fill_value=-999.0) 

latitudes.units = 'degree_north' 
longitudes.units = 'degree_east' 
latitudes[:] = np.arange(-90, 90, 180./len(lats)) 
longitudes[:] = np.arange(0, 360, 360./len(lons)) 
corrcoef_gfzjpl[:,:] = corrcoefMatrix_gfzjpl[:,:] 

개선을위한 제안은 대단히 환영합니다!

+0

또는, 계수 행렬을'corrcoefMatrix_gfzjpl = np.zeros ([len (lats), len (lons)])'로 초기화 한 다음 데이터를 double for 루프에'corrcoefMatrix_gfzjpl [x, y] = 임시 [0,1]'. – N1B4