Jor*_*era 2 time-series netcdf python-2.7
我正在尝试使用python从netCDF文件(通过Thredds服务器访问)中创建时间序列。我使用的代码似乎正确,但是变量amb读数的值被“屏蔽”。我是python的新手,我对格式不熟悉。关于如何读取数据的任何想法?
这是我使用的代码:
import netCDF4
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
from datetime import datetime, timedelta #
dayFile = datetime.now() - timedelta(days=1)
dayFile = dayFile.strftime("%Y%m%d")
url='http://nomads.ncep.noaa.gov:9090/dods/nam/nam%s/nam1hr_00z' %(dayFile)
# NetCDF4-Python can open OPeNDAP dataset just like a local NetCDF file
nc = netCDF4.Dataset(url)
varsInFile = nc.variables.keys()
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
time_var = nc.variables['time']
dtime = netCDF4.num2date(time_var[:],time_var.units)
first = netCDF4.num2date(time_var[0],time_var.units)
last = netCDF4.num2date(time_var[-1],time_var.units)
print first.strftime('%Y-%b-%d %H:%M')
print last.strftime('%Y-%b-%d %H:%M')
# determine what longitude convention is being used
print lon.min(),lon.max()
# Specify desired station time series location
# note we add 360 because of the lon convention in this dataset
#lati = 36.605; loni = -121.85899 + 360. # west of Pacific Grove, CA
lati = 41.4; loni = -100.8 +360.0 # Georges Bank
# Function to find index to nearest point
def near(array,value):
idx=(abs(array-value)).argmin()
return idx
# Find nearest point to desired location (no interpolation)
ix = near(lon, loni)
iy = near(lat, lati)
print ix,iy
# Extract desired times.
# 1. Select -+some days around the current time:
start = netCDF4.num2date(time_var[0],time_var.units)
stop = netCDF4.num2date(time_var[-1],time_var.units)
time_var = nc.variables['time']
datetime = netCDF4.num2date(time_var[:],time_var.units)
istart = netCDF4.date2index(start,time_var,select='nearest')
istop = netCDF4.date2index(stop,time_var,select='nearest')
print istart,istop
# Get all time records of variable [vname] at indices [iy,ix]
vname = 'dswrfsfc'
var = nc.variables[vname]
hs = var[istart:istop,iy,ix]
tim = dtime[istart:istop]
# Create Pandas time series object
ts = pd.Series(hs,index=tim,name=vname)
Run Code Online (Sandbox Code Playgroud)
var数据没有按我的预期读取,显然是因为数据被屏蔽了:
>>> hs
masked_array(data = [-- -- -- ..., -- -- --],
mask = [ True True True ..., True True True],
fill_value = 9.999e+20)
Run Code Online (Sandbox Code Playgroud)
变量名称和时间序列以及脚本的其余部分均正确。唯一不起作用的是检索到的var数据。这是我得到的时间:
>>> ts
2016-10-25 00:00:00.000000 NaN
2016-10-25 01:00:00.000000 NaN
2016-10-25 02:00:00.000006 NaN
2016-10-25 03:00:00.000000 NaN
2016-10-25 04:00:00.000000 NaN
... ... ... ... ...
2016-10-26 10:00:00.000000 NaN
2016-10-26 11:00:00.000006 NaN
Name: dswrfsfc, dtype: float32
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!
嗯,这段代码看起来很熟悉。;-)
之所以得到NaN,是因为您尝试访问的NAM模型现在在范围[-180, 180]而不是范围中使用经度[0, 360]。因此,如果您请求loni = -100.8而不是loni = -100.8 +360.0,我相信您的代码将返回非NaN值。
但是,值得注意的是,使用xarray从多维网格数据中提取时间序列的任务现在要容易得多,因为您可以简单地选择最接近lon,lat点的数据集,然后绘制任何变量。仅在需要时才加载数据,而在提取数据集对象时则不会加载。所以基本上您现在只需要:
import xarray as xr
ds = xr.open_dataset(url) # NetCDF or OPeNDAP URL
lati = 41.4; loni = -100.8 # Georges Bank
# Extract a dataset closest to specified point
dsloc = ds.sel(lon=loni, lat=lati, method='nearest')
# select a variable to plot
dsloc['dswrfsfc'].plot()
Run Code Online (Sandbox Code Playgroud)
完整的笔记本在这里:http : //nbviewer.jupyter.org/gist/rsignell-usgs/d55b37c6253f27c53ef0731b610b81b4
| 归档时间: |
|
| 查看次数: |
4117 次 |
| 最近记录: |