In [23]:
import xarray as xr

In [24]:
import matplotlib.pyplot as plt
%matplotlib inline

In [25]:
import numpy as np
import timeit as it

In [26]:
pwd


Out[26]:
'C:\\Users\\Norman\\Dropbox\\Notebooks'

In [38]:
fn_chl = 'D:/EOData/CCI-TBX/occci-v2.0/data/geographic/netcdf/monthly/chlor_a/2010/*.nc'
chl =  xr.open_mfdataset(fn_chl, chunks={time:12, lat:1, lon:1})


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-38-3aac17cc63a6> in <module>()
      1 fn_chl = 'D:/EOData/CCI-TBX/occci-v2.0/data/geographic/netcdf/monthly/chlor_a/2010/*.nc'
----> 2 chl =  xr.open_mfdataset(fn_chl, chunks={time:12, lat:1, lon:1})

NameError: name 'time' is not defined

In [28]:
chla  = chl.chlor_a

In [29]:
print(chl)
print(chla)


<xarray.Dataset>
Dimensions:             (lat: 4320, lon: 8640, time: 12)
Coordinates:
  * lon                 (lon) float32 -179.979 -179.938 -179.896 -179.854 ...
  * lat                 (lat) float32 89.9792 89.9375 89.8958 89.8542 ...
  * time                (time) datetime64[ns] 2010-01-01 2010-02-01 ...
Data variables:
    MODISA_nobs_sum     (time, lat, lon) float64 nan nan nan nan nan nan nan ...
    total_nobs_sum      (time, lat, lon) float64 nan nan nan nan nan nan nan ...
    crs                 (time) int32 0 0 0 0 0 0 0 0 0 0 0 0
    chlor_a_log10_rmsd  (time, lat, lon) float64 nan nan nan nan nan nan nan ...
    chlor_a_log10_bias  (time, lat, lon) float64 nan nan nan nan nan nan nan ...
    SeaWiFS_nobs_sum    (time, lat, lon) float64 nan nan nan nan nan nan nan ...
    chlor_a             (time, lat, lon) float64 nan nan nan nan nan nan nan ...
    MERIS_nobs_sum      (time, lat, lon) float64 nan nan nan nan nan nan nan ...
Attributes:
    Conventions: CF-1.6
    Metadata_Conventions: Unidata Dataset Discovery v1.0
    cdm_data_type: Grid
    comment: See summary attribute
    creator_email: rsghelp@pml.ac.uk
    creator_name: Plymouth Marine Laboratory
    creator_url: http://rsg.pml.ac.uk
    geospatial_lat_max: 90.0
    geospatial_lat_min: -90.0
    geospatial_lat_resolution: .04166666666666666666
    geospatial_lat_units: decimal degrees north
    geospatial_lon_max: 180.0
    geospatial_lon_min: -180.0
    geospatial_lon_resolution: .04166666666666666666
    geospatial_lon_units: decimal degrees east
    geospatial_vertical_max: 0.0
    geospatial_vertical_min: 0.0
    institution: Plymouth Marine Laboratory
    keywords: satellite,observation,ocean,ocean colour
    keywords_vocabulary: none
    license: ESA CCI Data Policy: free and open access
    naming_authority: uk.ac.pml
    number_of_optical_water_types: 14
    platform: Orbview-2/SeaStar,Aqua,Envisat
    processing_level: Level-3
    product_version: 2.0
    project: Climate Change Initiative - European Space Agency
    references: http://www.esa-oceancolour-cci.org/
    sensor: SeaWiFS,MODIS,MERIS
    source: SeaWiFS R2010.0, MODISA R2013.1, MERIS L1B 3rd reprocessing + OCL corrections
    spatial_resolution: 4km nominal at equator
    standard_name_vocabulary: NetCDF Climate and Forecast (CF) Metadata Conventions Version 1.6
    title: ESA CCI Ocean Colour Product
    number_of_files_composited: 31
    summary: Data products generated by the Ocean Colour component of the European Space Agency's Climate Change Initiative project.  These files are monthly composites of merged sensor (MERIS, MODIS, SeaWiFS) products, where MODIS and MERIS wereband-shifted and bias-corrected to SeaWiFS bands and values.  MODIS and SeaWiFS RRS products were derived from 4km resolution level-3 binned standard NASA products; MERIS was derived from a 4km level-3 binning of the output of the HYGEOS POLYMER algorithm.  Derive...
    tracking_id: c2c8a3eb-a083-44fa-93dc-acaec149c0bc
    id: ESACCI-OC-L3S-OC_PRODUCTS-MERGED-1M_MONTHLY_4km_GEO_PML_OC4v6_QAA-201001-fv2.0.nc
    creation_date: Fri Apr 24 01:22:56 2015
    date_created: Fri Apr 24 01:22:56 2015
    time_coverage_resolution: P1M
    time_coverage_duration: P1M
    start_date: 01-JAN-2010 00:00:00.000000
    stop_date: 31-JAN-2010 23:59:00.000000
    time_coverage_start: 201001010000Z
    time_coverage_end: 201001312359Z
    history: Fri Apr 24 19:32:48 2015: ncks --variable lat,lon --exclude -C --overwrite --chunk_dimension lat,256 --chunk_dimension lon,256 /data/caspian2/scratch/cci/v2.0-production/stage11-composites/geographic/monthly/chlor_a/2010/ESACCI-OC-L3S-CHLOR_A-MERGED-1M_MONTHLY_4km_GEO_PML_OC4v6-201001-fv2.0.nc ESACCI-OC-L3S-CHLOR_A-MERGED-1M_MONTHLY_4km_GEO_PML_OC4v6-201001-fv2.0.nc
Source data were: ESACCI-OC-L3S-OC_PRODUCTS-MERGED-1D_DAILY_4km_GEO_PML_OC4v6_QAA-20100101-fv2.0.nc, ESACCI-OC-L3S-OC_PRODUCTS-M...
    netcdf_file_type: NETCDF4_CLASSIC
    NCO: 4.3.0
<xarray.DataArray 'chlor_a' (time: 12, lat: 4320, lon: 8640)>
dask.array<concate..., shape=(12, 4320, 8640), dtype=float64, chunksize=(1, 4320, 8640)>
Coordinates:
  * lon      (lon) float32 -179.979 -179.938 -179.896 -179.854 -179.813 ...
  * lat      (lat) float32 89.9792 89.9375 89.8958 89.8542 89.8125 89.7708 ...
  * time     (time) datetime64[ns] 2010-01-01 2010-02-01 2010-03-01 ...
Attributes:
    long_name: Chlorophyll-a concentration in seawater (not log-transformed), generated by SeaDAS using OC4v6 for SeaWiFS
    units: milligram m-3
    ancillary_variables: chlor_a_log10_rmsd,chlor_a_log10_bias
    grid_mapping: crs
    parameter_vocab_uri: http://vocab.ndg.nerc.ac.uk/term/P011/current/CHLTVOLU
    standard_name: mass_concentration_of_chlorophyll_a_in_sea_water
    units_nonstandard: mg m^-3

In [30]:
def myfunc(x, axis=None):
    return np.nanmean(x, axis=axis)

In [31]:
#dd = chla.reduce(myfunc,['lon','time'])

In [36]:
def myfunc2(x, axis=None,lag=1):
    print(x.shape,axis)
    g = lambda a, lag: 1
    res = np.apply_along_axis(g,axis[0],x,lag)
    print(res.shape)
    return res

In [37]:
dd = chla.reduce(myfunc2,['time'])


(12, 4320, 8640) (0,)
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-37-adfdc57a4c9a> in <module>()
----> 1 dd = chla.reduce(myfunc2,['time'])

C:\Users\Norman\Miniconda3\lib\site-packages\xarray\core\dataarray.py in reduce(self, func, dim, axis, keep_attrs, **kwargs)
    989             summarized data and the indicated dimension(s) removed.
    990         """
--> 991         var = self.variable.reduce(func, dim, axis, keep_attrs, **kwargs)
    992         return self._replace_maybe_drop_dims(var)
    993 

C:\Users\Norman\Miniconda3\lib\site-packages\xarray\core\variable.py in reduce(self, func, dim, axis, keep_attrs, allow_lazy, **kwargs)
    869             axis = self.get_axis_num(dim)
    870         data = func(self.data if allow_lazy else self.values,
--> 871                     axis=axis, **kwargs)
    872 
    873         removed_axes = (range(self.ndim) if axis is None

<ipython-input-36-964061879834> in myfunc2(x, axis, lag)
      2     print(x.shape,axis)
      3     g = lambda a, lag: 1
----> 4     res = np.apply_along_axis(g,axis[0],x,lag)
      5     print(res.shape)
      6     return res

C:\Users\Norman\Miniconda3\lib\site-packages\numpy\lib\shape_base.py in apply_along_axis(func1d, axis, arr, *args, **kwargs)
    106             i.put(indlist, ind)
    107             res = func1d(arr[tuple(i.tolist())], *args, **kwargs)
--> 108             outarr[tuple(ind)] = res
    109             k += 1
    110         return outarr

KeyboardInterrupt: 

In [ ]:
plt.imshow(dd)

In [ ]:


In [ ]: