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

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

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

In [5]:
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 [6]:
# 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 [7]:
ds.coords['time'][0]


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

In [8]:
ds.data_vars


Out[8]:
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 [9]:
ds2 = ds.data_vars['press'].loc['2014-02-07':'2014-02-10'].isel(sample=0)

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

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

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


Out[12]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f527e90ba90>

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

In [14]:
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 [15]:
coord_list = dvar.coords.keys()

In [16]:
#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 [17]:
df3.head()


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

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


Out[18]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f527be71490>

In [19]:
df3.head()


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

In [20]:
ds3


Out[20]:
<xray.Dataset>
Dimensions:  (beam: 4, beambin: 12, lat: 1, lon: 1, sample: 2048)
Coordinates:
    time     datetime64[ns] 2014-02-08T00:51:30
  * lat      (lat) float32 40.6369
  * lon      (lon) float32 -73.1484
  * sample   (sample) int32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
  * beam     (beam) int64 0 1 2 3
  * beambin  (beambin) int64 0 1 2 3 4 5 6 7 8 9 10 11
Data variables:
    time2    int32 3090000
    burst    int32 113
    strk     (sample, beam, lat, lon) int16 5000 5450 5050 5600 5600 4813 ...
    vel      (sample, beambin, lat, lon) int16 -45 -275 82 92 267 -101 217 ...
    press    (sample, lat, lon) int16 1121 1118 1115 1122 1129 1128 1134 ...

In [23]:
!git add epic_to_xray*

In [22]:
cd /usgs/data2/notebook/xray


/usgs/data2/notebook/xray

In [24]:
!git commit -m 'adding epic_to_xray'


[master 39911ad] adding epic_to_xray
 4 files changed, 894 insertions(+)
 create mode 100644 xray/epic_to_xray-Copy0.ipynb
 create mode 100644 xray/epic_to_xray-Copy0.py
 create mode 100644 xray/epic_to_xray.ipynb
 create mode 100644 xray/epic_to_xray.py

In [25]:
!git push


Counting objects: 8, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 127.70 KiB, done.
Total 7 (delta 2), reused 0 (delta 0)
To git@github.com:rsignell-usgs/notebook.git
   470d624..39911ad  master -> master

In [ ]: