In [15]:
import iris
import numpy

In [3]:
areacello_file = '/g/data/ua6/DRSv2/CMIP5/CSIRO-Mk3-6-0/historical/fx/ocean/r0i0p0/areacello/latest/areacello_fx_CSIRO-Mk3-6-0_historical_r0i0p0.nc'
data_file = '/g/data/ua6/DRSv2/CMIP5/CSIRO-Mk3-6-0/historical/mon/ocean/r1i1p1/hfds/latest/hfds_Omon_CSIRO-Mk3-6-0_historical_r1i1p1_185001-200512.nc'

In [4]:
area_cube = iris.load_cube(areacello_file)


/g/data/r87/dbi599/miniconda3/envs/ocean/lib/python3.6/site-packages/iris/fileformats/cf.py:1143: IrisDeprecation: NetCDF default loading behaviour currently does not expose variables which define reference surfaces for dimensionless vertical coordinates as independent Cubes. This behaviour is deprecated in favour of automatic promotion to Cubes. To switch to the new behaviour, set iris.FUTURE.netcdf_promote to True.
  warn_deprecated(msg)

In [6]:
print(area_cube)


cell_area / (m2)                    (latitude: 189; longitude: 192)
     Dimension coordinates:
          latitude                           x               -
          longitude                          -               x
     Attributes:
          Conventions: CF-1.4
          associated_files: baseURL: http://cmip-pcmdi.llnl.gov/CMIP5/dataLocation gridspecFile: g...
          branch_time: 0.0
          cmor_version: 2.6.0
          comment: Data is stored on the native ocean T-grid on which the data was generated....
          contact: Project leaders: Stephen Jeffrey (Stephen.Jeffrey@qld.gov.au) & Leon Rotstayn...
          creation_date: 2011-08-02T01:11:26Z
          experiment: historical
          experiment_id: historical
          forcing: N/A
          frequency: fx
          history: 2011-08-02T01:11:26Z CMOR rewrote data to comply with CF standards and...
          initialization_method: 0
          institute_id: CSIRO-QCCCE
          institution: Australian Commonwealth Scientific and Industrial Research Organization...
          model_id: CSIRO-Mk3-6-0
          modeling_realm: ocean
          original_name: areacello
          parent_experiment: N/A
          parent_experiment_id: N/A
          parent_experiment_rip: N/A
          physics_version: 0
          product: output
          project_id: CMIP5
          realization: 0
          references: a) Rotstayn, L., Collier, M., Dix, M., Feng, Y., Gordon, H., O\'Farrell,...
          source: CSIRO-Mk3-6-0 2010 atmosphere: AGCM v7.3.n (T63 spectral, 1.875 degrees...
          table_id: Table fx (27 April 2011) f984543f4b43fbadc314e59cafbac627
          title: CSIRO-Mk3-6-0 model output prepared for CMIP5 historical
          tracking_id: 39caddbf-1bfc-4d36-8498-7b656251d2c7
          version_number: v20110518

In [7]:
data_cube = iris.load_cube(data_file)


/g/data/r87/dbi599/miniconda3/envs/ocean/lib/python3.6/site-packages/iris/fileformats/cf.py:798: UserWarning: Missing CF-netCDF measure variable 'areacello', referenced by netCDF variable 'hfds'
  warnings.warn(message % (variable_name, nc_var_name))
/g/data/r87/dbi599/miniconda3/envs/ocean/lib/python3.6/site-packages/iris/fileformats/cf.py:1143: IrisDeprecation: NetCDF default loading behaviour currently does not expose variables which define reference surfaces for dimensionless vertical coordinates as independent Cubes. This behaviour is deprecated in favour of automatic promotion to Cubes. To switch to the new behaviour, set iris.FUTURE.netcdf_promote to True.
  warn_deprecated(msg)

In [8]:
print(data_cube)


surface_downward_heat_flux_in_sea_water / (W m-2) (time: 1872; latitude: 189; longitude: 192)
     Dimension coordinates:
          time                                         x               -               -
          latitude                                     -               x               -
          longitude                                    -               -               x
     Attributes:
          Conventions: CF-1.4
          associated_files: baseURL: http://cmip-pcmdi.llnl.gov/CMIP5/dataLocation gridspecFile: gridspec_ocean_fx_CSIRO-Mk3-6-0_historical_r0i0p0.nc...
          branch_time: 29200.0
          cmor_version: 2.6.0
          comment: Data is stored on the native ocean T-grid on which the data was generated....
          contact: Project leaders: Stephen Jeffrey (Stephen.Jeffrey@qld.gov.au) & Leon Rotstayn...
          creation_date: 2011-07-27T01:48:46Z
          experiment: historical
          experiment_id: historical
          forcing: Ant,Nat (all forcings)
          frequency: mon
          history: 2011-07-27T01:48:46Z altered by CMOR: replaced missing value flag (-7.77778e+06)...
          initialization_method: 1
          institute_id: CSIRO-QCCCE
          institution: Australian Commonwealth Scientific and Industrial Research Organization...
          model_id: CSIRO-Mk3-6-0
          modeling_realm: ocean
          original_name: hflx
          parent_experiment: piControl
          parent_experiment_id: piControl
          parent_experiment_rip: r1i1p1
          physics_version: 1
          product: output
          project_id: CMIP5
          realization: 1
          references: a) Rotstayn, L., Collier, M., Dix, M., Feng, Y., Gordon, H., O\'Farrell,...
          source: CSIRO-Mk3-6-0 2010 atmosphere: AGCM v7.3.8 (T63 spectral, 1.875 degrees...
          table_id: Table Omon (27 April 2011) 9e1a53e4873bf6f26879903e165fe4a0
          title: CSIRO-Mk3-6-0 model output prepared for CMIP5 historical
          tracking_id: 6167d9ac-7434-4be1-867b-de6714f6ba82
          version_number: v20110518
     Cell methods:
          mean: time
          mean where sea: area

In [12]:
if not data_cube.coord('latitude').has_bounds():
    data_cube.coord('latitude').guess_bounds()

if not data_cube.coord('longitude').has_bounds():
    data_cube.coord('longitude').guess_bounds()

area_weights = iris.analysis.cartography.area_weights(data_cube)

print(area_weights.shape)
print(type(area_weights))


(1872, 189, 192)
<class 'numpy.ndarray'>
/g/data/r87/dbi599/miniconda3/envs/ocean/lib/python3.6/site-packages/iris/analysis/cartography.py:376: UserWarning: Using DEFAULT_SPHERICAL_EARTH_RADIUS.
  warnings.warn("Using DEFAULT_SPHERICAL_EARTH_RADIUS.")

In [16]:
area_weights = numpy.ma.masked_where(numpy.ma.getmask(data_cube.data), area_weights)

In [17]:
print(area_weights[0,50, 0:50])


[16175296506.375238 16175296506.37524 16175296506.37524 16175296506.37524
 16175296506.375235 16175296506.375235 16175296506.375248
 16175296506.375235 16175296506.375248 16175296506.375221
 16175296506.375248 16175296506.375248 16175296506.375221
 16175296506.375248 16175296506.375248 16175296506.375221
 16175296506.375277 16175296506.375221 16175296506.375221
 16175296506.375277 16175296506.375221 16175296506.375221
 16175296506.375277 16175296506.375221 16175296506.375221
 16175296506.375277 16175296506.375221 16175296506.375221
 16175296506.375277 16175296506.375221 16175296506.375221
 16175296506.375277 16175296506.375221 16175296506.375221
 16175296506.375221 16175296506.375332 16175296506.375221
 16175296506.375221 16175296506.375221 16175296506.375221
 16175296506.375221 16175296506.375332 16175296506.375221
 16175296506.375221 16175296506.375221 16175296506.375221
 16175296506.375221 16175296506.375332 16175296506.375221
 16175296506.375221]

In [18]:
print(area_cube.data[50, 0:50])


[16194533376.0 16194533376.0 16194533376.0 16194533376.0 16194533376.0
 16194533376.0 16194533376.0 16194533376.0 16194533376.0 16194533376.0
 16194533376.0 16194533376.0 16194533376.0 16194533376.0 16194533376.0
 16194533376.0 16194533376.0 16194533376.0 16194533376.0 16194533376.0
 16194533376.0 16194533376.0 16194533376.0 16194533376.0 16194533376.0
 16194533376.0 16194533376.0 16194533376.0 16194533376.0 16194533376.0
 16194533376.0 16194533376.0 16194533376.0 16194533376.0 16194533376.0
 16194533376.0 16194533376.0 16194533376.0 16194533376.0 16194533376.0
 16194533376.0 16194533376.0 16194533376.0 16194533376.0 16194533376.0
 16194533376.0 16194533376.0 16194533376.0 16194533376.0 16194533376.0]

In [22]:
print(area_weights[0, 50, :].mean())
print(area_cube.data[50, 0:50].mean())

print(area_weights[0, 50, :].sum())
print(area_cube.data[50, :].sum())


16175296506.4
16194534113.3
2.96007926067e+12
2.9636e+12

In [23]:
def create_areacello_cube(cube):
    """Create an area cube."""

    dim_coord_names = [coord.name() for coord in cube.dim_coords]
    assert 'latitude' in dim_coord_names
    assert 'longitude' in dim_coord_names

    if not cube.coord('latitude').has_bounds():
        cube.coord('latitude').guess_bounds()

    if not cube.coord('longitude').has_bounds():
        cube.coord('longitude').guess_bounds()

    area_weights = iris.analysis.cartography.area_weights(cube)

    out_dims = []
    for index, name in enumerate(dim_coord_names):
        out_dims.append((cube.coord(name), index))

    area_cube = iris.cube.Cube(area_weights,
                               standard_name='cell_area',
                               long_name='Ocean Grid-Cell Area',
                               var_name='areacello',
                               units='m2',
                               dim_coords_and_dims=out_dims)

    area_cube.data = numpy.ma.masked_where(numpy.ma.getmask(cube.data), area_cube.data)

    if 'time' in dim_coord_names:
        first_time = area_cube.coord('time').points[0]
        area_cube = area_cube.extract(iris.Constraint(time=first_time))

    return area_cube

In [24]:
new_area_cube = create_areacello_cube(data_cube)


/g/data/r87/dbi599/miniconda3/envs/ocean/lib/python3.6/site-packages/iris/analysis/cartography.py:376: UserWarning: Using DEFAULT_SPHERICAL_EARTH_RADIUS.
  warnings.warn("Using DEFAULT_SPHERICAL_EARTH_RADIUS.")

In [25]:
print(new_area_cube.data[0, 50, :].mean())
print(area_cube.data[50, 0:50].mean())

print(new_area_cube.data[0, 50, :].sum())
print(area_cube.data[50, :].sum())


16175296506.4
16194534113.3
2.96007926067e+12
2.9636e+12

In [31]:
total_zonal_area = area_cube.collapsed('longitude', iris.analysis.SUM)
new_total_zonal_area = new_area_cube.collapsed('longitude', iris.analysis.SUM)

print(total_zonal_area.data[0:10])
print(new_total_zonal_area.data[0, 0:10])


[-- -- -- -- -- -- -- 41036279808.0 45557096448.0 61619191808.0]
[-- -- -- -- -- -- -- 40986528376.58848 45501844701.91126 61545718673.75773]

In [ ]: