Try using Xray to convert CSV to netcdf


In [1]:
import pandas as pd
import xray
%matplotlib inline

In [2]:
url = 'http://www.cpc.ncep.noaa.gov/products/precip/CWlink/'

ao_file = url + 'daily_ao_index/monthly.ao.index.b50.current.ascii'
nao_file = url + 'pna/norm.nao.monthly.b5001.current.ascii'

kw = dict(sep='\s*', parse_dates={'dates': [0, 1]},
          header=None, index_col=0, squeeze=True, engine='python')

# read into Pandas Series
s1 = pd.read_csv(ao_file, **kw)
s2 = pd.read_csv(nao_file, **kw)

s1.name='AO'
s2.name='NAO'

# concatenate two Pandas Series into a Pandas DataFrame
df=pd.concat([s1, s2], axis=1)

In [3]:
df.plot(figsize=(16,4));



In [4]:
# create xray Dataset from Pandas DataFrame
xr = xray.Dataset.from_dataframe(df)

In [5]:
# add variable attribute metadata
xr['AO'].attrs={'units':'1', 'long_name':'Arctic Oscillation'}
xr['NAO'].attrs={'units':'1', 'long_name':'North Atlantic Oscillation'}

In [6]:
# add global attribute metadata
xr.attrs={'Conventions':'CF-1.0', 'title':'AO and NAO', 'summary':'Arctic and North Atlantic Oscillation Indices'}

In [7]:
print xr


<xray.Dataset>
Dimensions:  (dates: 782)
Coordinates:
  * dates    (dates) datetime64[ns] 1950-01-06 1950-02-06 1950-03-06 1950-04-06 ...
Data variables:
    AO       (dates) float64 -0.06031 0.6268 -0.008128 0.5551 0.07158 0.5386 -0.8025 ...
    NAO      (dates) float64 0.92 0.4 -0.36 0.73 -0.59 -0.06 -1.26 -0.05 0.25 0.85 -1.26 ...
Attributes:
    title: AO and NAO
    summary: Arctic and North Atlantic Oscillation Indices
    Conventions: CF-1.0

In [8]:
# save to netCDF
xr.to_netcdf('/usgs/data2/notebook/data/ao_and_nao.nc')