In [58]:
import xray
import pandas as pd
import numpy as np
%matplotlib inline
import datetime as dt

In [59]:
url='http://geoport.whoi.edu/thredds/dodsC/usgs/data2/emontgomery/stellwagen/Data/FI14/10001whr-cal.nc'

In [60]:
ds = xray.open_dataset(url)

In [61]:
def convert_epic_time(ds):
    """ convert EPIC time and time2 variables to datenum64 """
    t1 = np.array(ds.coords['time'].values - 2440000,dtype='int64')*3600*24*1000
    t2 = np.array(ds.data_vars['time2'].values, dtype='int64')
    dt64 = [np.datetime64('1968-05-23T00:00:00Z') + np.timedelta64(a,'ms') for a in t1+t2]
    ds.coords['time'] = dt64

In [62]:
# if we find a time2 variable, convert EPIC time and time2 variables to datetime64 object
if 'time2' in ds.data_vars.keys():
    convert_epic_time(ds)

In [63]:
ds.coords['time'][0]


Out[63]:
<xray.DataArray 'time' ()>
numpy.datetime64('2014-02-07T09:51:29.999000000-0500')
Coordinates:
    time     datetime64[ns] 2014-02-07T14:51:29.999000

In [64]:
ds.data_vars


Out[64]:
Data variables:
    time2    (time) int32 53489999 57090000 60690000 64289999 67890000 ...
    burst    (time) int32 ...
    strk     (time, sample, beam, lat, lon) int16 ...
    vel      (time, sample, beambin, lat, lon) int16 ...
    press    (time, sample, lat, lon) int16 ...

In [65]:
ds2 = ds.data_vars['press'].loc['2014-02-07':'2014-02-10'].isel(sample=0)

In [66]:
df = ds2.to_dataframe()

In [67]:
df2 = df.reset_index().set_index('time').drop(['lon', 'lat', 'sample'], axis=1)

In [68]:
df2.plot(figsize=(12,4))


Out[68]:
<matplotlib.axes._subplots.AxesSubplot at 0x7ff7a5c97190>

In [116]:
ds3 = ds.sel(time=dt.datetime(2014,2,8,1),method='nearest')

In [122]:
dvar = ds3.data_vars['vel'].sel(beambin=0)
dvar = ds3.data_vars['press']
# why does this return 10 values?
#ds3 = ds.data_vars['press'].isel(time=0)
df3 = dvar.to_dataframe()

In [123]:
coord_list = dvar.coords.keys()

In [124]:
#create a drop list (by removing the dimension we want to keep)
idx = 'sample'
coord_list.remove(idx)
df3 = df3.reset_index().set_index(idx).drop(coord_list, axis=1)

In [126]:
df3.head()


Out[126]:
press
sample
1 1121
2 1118
3 1115
4 1122
5 1129

In [78]:
df3.plot(figsize=(12,4))


Out[78]:
<matplotlib.axes._subplots.AxesSubplot at 0x7ff7a7dae290>

In [72]:
df3.head()


Out[72]:
press
sample
1 1145
2 1146
3 1149
4 1149
5 1145

In [73]:
ds3


Out[73]:
<xray.DataArray 'press' (time: 1, sample: 2048, lat: 1, lon: 1)>
array([[[[1145]],

        [[1146]],

        [[1149]],

        ..., 
        [[1143]],

        [[1146]],

        [[1149]]]], dtype=int16)
Coordinates:
  * sample   (sample) int32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
  * time     (time) datetime64[ns] 2014-02-08T01:51:30
  * lat      (lat) float32 40.6369
  * lon      (lon) float32 -73.1484

In [73]:


In [73]: