2017-04-05 30 views
0

사용자 지정 위도/경도 좌표 쌍에 가장 가까운 2 차원 위도/경도 배열의 인덱스를 반환하는 NCL 함수와 동일한 (존재하는 경우) 위치를 찾으려고합니다.2D lat lon 데이터의 인덱스

이것은 파이썬에서와 동등한 NCL 함수에 대한 링크입니다. 나는이 아니라는 것을이 시점에서 의심, 그래서 위도/경도에서 인덱스를 얻는 방법에 대한 팁 내 좌표 값이 .nc 파일로 저장이, 지금

https://www.ncl.ucar.edu/Document/Functions/Contributed/getind_latlon2d.shtml

을 감사 좌표 다음에 의해 읽혀집니다 :

coords='coords.nc' 
fh = Dataset(coords, mode='r') 
lons = fh.variables['g5_lon_1'][:,:] 
lats = fh.variables['g5_lat_0'][:,:] 
rot = fh.variables['g5_rot_2'][:,:] 
fh.close() 
+0

나는 매우 비슷한 것을 궁금해합니다. – CRogers

+0

[NetCDF 및 Python의 가능한 복제본 : 실제 lon/lat 값으로 주어진 가장 가까운 lon/lat 색인 찾기] (https://stackoverflow.com/questions/33789379/netcdf-and-python-finding-the-closest-lon- lat-index-given-actual-lon-lat-values) –

답변

0

나는 scipy 공간을 발견했습니다 .KDTree는 비슷한 작업을 수행 할 수 있습니다. 여기에 지금 당신이해야 할 수 있습니다 다음과 같은 솔루션을 사용하여, 파이썬에서 읽을 때 경도/위도 배열이 저장되는 방법을 잘 모르겠어요

from scipy import spatial 
from netCDF4 import Dataset 

# read in the one dimensional lat lon info from a dataset 
fname = '0k_T_ann_clim.nc' 
fid = Dataset(fname, 'r') 

lat = fid.variables['lat'][:] 
lon = fid.variables['lon'][:] 
# make them a meshgrid for later use KDTree 
lon2d, lat2d = np.meshgrid(lon, lat) 
# zip them together 
model_grid = list(zip(np.ravel(lon2d), np.ravel(lat2d))) 

#target point location : 30.5N, 56.1E 
target_pts = [30.5 56.1] 
distance, index = spatial.KDTree(model_grid).query(target_pts) 
# the nearest model location (in lat and lon) 
model_loc_coord = [coord for i, coord in enumerate(model_grid) if i==index] 
0

관찰 위치에 가장 가까운 모델 그리드를 찾는 내 코드입니다 lon/lat를 numpy 배열로 변환합니다. 함수에 abs (array-target) .argmin()을 넣을 수 있습니다.

import numpy as np 

# make a dummy longitude array, 0.5 degree resolution. 
lon=np.linspace(0.5,360,720) 

# find index of nearest longitude to 25.4 
ind=abs(lon-25.4).argmin() 

# check it works! this gives 25.5 
lon[ind]