본문 바로가기

Programing Language/Python

[visualization]netCDF 관련 지구 시각화

반응형

NetCDF

netCDF는 3차원 혹은 4차원 데이터, 특히 위성 등으로 측정된 지구, 국가단위의 map data를 구성하는 형태이다.
비교적 자세한 정보들을 담고있어, 그 자체에서 필요한 정보들을 다 담고있다.
최신은 netCDF4이며, python의 경우 netCDF3와 netCDF4의 경우 읽어오는 패키지가 다를 수도 있다.
보통 netCDF4 패키지로 읽어온다.

Data Import

아래의 데이터는 NASA의 위성데이터중 하나인 CCAR 데이터이다. 시공간마다의 각 cite에서 해수표면 높이를 담고있다.
읽어온 f_nc를 실행하면 데이터에 대한 description이 나온다. 이 데이터의 경우 가장 아래에 제일 중요한 data의 dimension에 대한 정보가 담겨져 있다.

from netCDF4 import Dataset
f_nc = Dataset('./CCAR_recon_sea_level_19500103_19591227_v1.nc')

Data extract to numpy array

별다른 어려움 없이 아래와 같은 예제처럼 불러올 수 있다.

lons = f_nc.variables['lon'][:]
lats = f_nc.variables['lat'][:]
times = f_nc.variables['time'][:]
ssha = f_nc.variables['ssha'][:]
timesshapes = f_nc.variables['time'].units
sshaunit = f_nc.variables['ssha'].units

Visualization

matplotlib와 그 toolkits인 mpl_toolkits에서 Basemap을 이용한다. ?Basemap을 이용해 document를 보면 width, height는 meter단위이고, lat_0, lon_0은 위경도의 center를 뜻한다. 더 자세한 내용은 document를 참고
drawparallels에서 labels의 경우에 각 옵션이 의미하는 바는 더 자세한 실험을 해보지 않아서 큰 차이는 모르겠음

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

fig = plt.figure(figsize=[12,15])
m = Basemap(width=1500000,height=1000000,
            resolution='l',projection='stere',
            lat_0=35.5,
            lon_0=128.
           )


# Plot Data
cs = m.pcolor(xi,yi,np.squeeze(ssha[0,:,:]))

# Add Grid Lines
m.drawparallels(np.arange(-80., 81., .5), labels=[5,0,0,0], fontsize=10) # from, to, diff
m.drawmeridians(np.arange(-180., 181., .5), labels=[0,0,0,5], fontsize=10)

# Add Coastlines, States, and Country Boundaries
m.drawcoastlines()
m.drawstates()
m.drawcountries()

# Add Colorbar
cbar = m.colorbar(cs, location='bottom', pad="10%")
cbar.set_label(sshaunit)

# Add Title
plt.title('ssha')

plt.show()

Reference

더 다양한 netCDF데이터를 이용한 시각화기법들을 보여주고 있다.

https://nordicesmhub.github.io/deep_python/14-publish/index.html

반응형