In [1]:
%matplotlib inline
from matplotlib import pyplot as plt

In [2]:
import os, sys
import numpy as np
from numpy import ma
import pandas as pd
import xray
from scipy.signal import detrend

In [3]:
dset = xray.open_dataset('/Users/nicolasf/data/GPCP/precip.mon.mean.nc')

In [4]:
dset


Out[4]:
<xray.Dataset>
Dimensions:  (lat: 72, lon: 144, time: 437)
Coordinates:
  * lat      (lat) float32 88.75 86.25 83.75 81.25 78.75 76.25 73.75 71.25 ...
  * lon      (lon) float32 1.25 3.75 6.25 8.75 11.25 13.75 16.25 18.75 21.25 ...
  * time     (time) datetime64[ns] 1979-01-01 1979-02-01 1979-03-01 ...
Data variables:
    precip   (time, lat, lon) float64 0.6615 0.5773 0.6175 0.6579 0.5897 ...
Attributes:
    Conventions: COARDS
    title: GPCP Version 2.2 Combined Precipitation Dataset (Final)
    platform: Observation
    source: GPCP Polar Satellite Precipitation Data Centre - Emission (SSM/I emission estimates).
 GPCP Polar Satellite Precipitation Data Centre - Scattering (SSM/I scattering estimates).
GPCP Geostationary Satellite Precipitation Data Centre (GPI and OPI estimates and rain gauge analyses).
NASA/GSFC Satellite Applications Office (TOVS estimates
GPCP Global Precipitation Climatology Centre (rain gauge analyses)
NASA ftp://precip.gsfc.nasa.gov/pub/gpcp-v2.2/
    documentation: http://www.esrl.noaa.gov/psd/data/gridded/data.gpcp.html
    version: V2.2
    references: http://www.esrl.noaa.gov/psd/data/gridded/data.gpcp.html
    comments: Please redownload if you obtained this file before Aug 1 2012
    Acknowledgement: 
,Please cite the original source of the data.
Please email the citation to george.j.huffman@nasa.gov or david.t.bolvin@nasa.gov

    history: Converted from netCDF3 to chunked, deflated NetCDF4 Aug 2014

In [4]:
dset['lon']


Out[4]:
<xray.DataArray 'lon' (lon: 144)>
array([   1.25,    3.75,    6.25,    8.75,   11.25,   13.75,   16.25,
         18.75,   21.25,   23.75,   26.25,   28.75,   31.25,   33.75,
         36.25,   38.75,   41.25,   43.75,   46.25,   48.75,   51.25,
         53.75,   56.25,   58.75,   61.25,   63.75,   66.25,   68.75,
         71.25,   73.75,   76.25,   78.75,   81.25,   83.75,   86.25,
         88.75,   91.25,   93.75,   96.25,   98.75,  101.25,  103.75,
        106.25,  108.75,  111.25,  113.75,  116.25,  118.75,  121.25,
        123.75,  126.25,  128.75,  131.25,  133.75,  136.25,  138.75,
        141.25,  143.75,  146.25,  148.75,  151.25,  153.75,  156.25,
        158.75,  161.25,  163.75,  166.25,  168.75,  171.25,  173.75,
        176.25,  178.75,  181.25,  183.75,  186.25,  188.75,  191.25,
        193.75,  196.25,  198.75,  201.25,  203.75,  206.25,  208.75,
        211.25,  213.75,  216.25,  218.75,  221.25,  223.75,  226.25,
        228.75,  231.25,  233.75,  236.25,  238.75,  241.25,  243.75,
        246.25,  248.75,  251.25,  253.75,  256.25,  258.75,  261.25,
        263.75,  266.25,  268.75,  271.25,  273.75,  276.25,  278.75,
        281.25,  283.75,  286.25,  288.75,  291.25,  293.75,  296.25,
        298.75,  301.25,  303.75,  306.25,  308.75,  311.25,  313.75,
        316.25,  318.75,  321.25,  323.75,  326.25,  328.75,  331.25,
        333.75,  336.25,  338.75,  341.25,  343.75,  346.25,  348.75,
        351.25,  353.75,  356.25,  358.75], dtype=float32)
Coordinates:
  * lon      (lon) float32 1.25 3.75 6.25 8.75 11.25 13.75 16.25 18.75 21.25 ...
Attributes:
    units: degrees_east
    long_name: Longitude
    actual_range: [   1.25  358.75]
    standard_name: longitude
    axis: X

In [6]:
dset['lat']


Out[6]:
<xray.DataArray 'lat' (lat: 72)>
array([ 88.75,  86.25,  83.75,  81.25,  78.75,  76.25,  73.75,  71.25,
        68.75,  66.25,  63.75,  61.25,  58.75,  56.25,  53.75,  51.25,
        48.75,  46.25,  43.75,  41.25,  38.75,  36.25,  33.75,  31.25,
        28.75,  26.25,  23.75,  21.25,  18.75,  16.25,  13.75,  11.25,
         8.75,   6.25,   3.75,   1.25,  -1.25,  -3.75,  -6.25,  -8.75,
       -11.25, -13.75, -16.25, -18.75, -21.25, -23.75, -26.25, -28.75,
       -31.25, -33.75, -36.25, -38.75, -41.25, -43.75, -46.25, -48.75,
       -51.25, -53.75, -56.25, -58.75, -61.25, -63.75, -66.25, -68.75,
       -71.25, -73.75, -76.25, -78.75, -81.25, -83.75, -86.25, -88.75], dtype=float32)
Coordinates:
  * lat      (lat) float32 88.75 86.25 83.75 81.25 78.75 76.25 73.75 71.25 ...
Attributes:
    units: degrees_north
    actual_range: [ 88.75 -88.75]
    long_name: Latitude
    standard_name: latitude
    axis: Y

In [7]:
lat = dset['lat'].data
lon = dset['lon'].data

In [12]:
d = {}
d['time'] = ('time',dset['time'])
d['latitudes'] = ('latitudes',lat)
d['longitudes'] = ('longitudes', lon)
d['Rain'] = (['time','latitudes','longitudes'], dset['precip'].data)

In [13]:
dset_out = xray.Dataset(d)

In [14]:
dset_out


Out[14]:
<xray.Dataset>
Dimensions:     (latitudes: 72, longitudes: 144, time: 437)
Coordinates:
  * longitudes  (longitudes) float32 1.25 3.75 6.25 8.75 11.25 13.75 16.25 ...
  * time        (time) datetime64[ns] 1979-01-01 1979-02-01 1979-03-01 ...
  * latitudes   (latitudes) float32 88.75 86.25 83.75 81.25 78.75 76.25 ...
Data variables:
    Rain        (time, latitudes, longitudes) float64 0.6615 0.5773 0.6175 ...

In [122]:
!rm /Users/nicolasf/research/NIWA/paleo/pict/data/ERSST_1972_2014.nc

In [15]:
dset_out.to_netcdf('../data/GPCP_monthly_Rain_1979_2014.nc')

In [ ]: