In [1]:
%matplotlib inline

In [2]:
import numpy as np
import xarray as xr

In [3]:
import datetime as dt
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)
print(url)


http://nomads.ncep.noaa.gov:9090/dods/nam/nam20161025/nam1hr_00z

In [4]:
ds = xr.open_dataset(url)

In [5]:
# Specify desired station time series location
# note we add 360 because of the lon convention in this dataset
#lati = 36.605; loni = -121.85899   # west of Pacific Grove, CA
lati = 41.4; loni = -100.8  # Georges Bank

In [6]:
# extract a dataset closeste to specified point
dsloc = ds.sel(lon=loni, lat=lati, method='nearest')

In [7]:
# select a variable to plot
dsloc['dswrfsfc'].plot()


Out[7]:
[<matplotlib.lines.Line2D at 0x7f473404ca50>]

In [13]:
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
lati = 41.4; loni = -100.8   # 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)


2016-Oct-25 00:00
2016-Oct-26 12:00
-152.878623 -49.5860145742
459 263
0 36

In [14]:
ts


Out[14]:
2016-10-25 00:00:00.000000      0.000000
2016-10-25 01:00:00.000000      0.000000
2016-10-25 02:00:00.000006      0.000000
2016-10-25 03:00:00.000000      0.000000
2016-10-25 04:00:00.000000      0.000000
2016-10-25 05:00:00.000006      0.000000
2016-10-25 06:00:00.000000      0.000000
2016-10-25 07:00:00.000000      0.000000
2016-10-25 08:00:00.000006      0.000000
2016-10-25 09:00:00.000000      0.000000
2016-10-25 10:00:00.000000      0.000000
2016-10-25 11:00:00.000006      0.000000
2016-10-25 12:00:00.000000      0.000000
2016-10-25 13:00:00.000000      0.000000
2016-10-25 14:00:00.000006     34.571644
2016-10-25 15:00:00.000000     84.320755
2016-10-25 16:00:00.000000    239.692627
2016-10-25 17:00:00.000006    541.701721
2016-10-25 18:00:00.000000    597.381653
2016-10-25 19:00:00.000000    571.218872
2016-10-25 20:00:00.000006    324.944275
2016-10-25 21:00:00.000000    371.187897
2016-10-25 22:00:00.000000    212.613312
2016-10-25 23:00:00.000006     56.342171
2016-10-26 00:00:00.000000      0.000000
2016-10-26 01:00:00.000000      0.000000
2016-10-26 02:00:00.000006      0.000000
2016-10-26 03:00:00.000000      0.000000
2016-10-26 04:00:00.000000      0.000000
2016-10-26 05:00:00.000006      0.000000
2016-10-26 06:00:00.000000      0.000000
2016-10-26 07:00:00.000000      0.000000
2016-10-26 08:00:00.000006      0.000000
2016-10-26 09:00:00.000000      0.000000
2016-10-26 10:00:00.000000      0.000000
2016-10-26 11:00:00.000006      0.000000
Name: dswrfsfc, dtype: float32

In [ ]: