In [1]:
import warnings
warnings.filterwarnings('ignore')

import numpy
import iris
import iris.plot as iplt
from iris.experimental.regrid import regrid_weighted_curvilinear_to_rectilinear

import matplotlib.pyplot as plt

In [1]:
import sys, os

cwd = os.getcwd()
repo_dir = '/'
for directory in cwd.split('/')[1:]:
    repo_dir = os.path.join(repo_dir, directory)
    if directory == 'ocean-analysis':
        break

modules_dir = os.path.join(repo_dir, 'modules')
sys.path.append(modules_dir)

import grids


/g/data/r87/dbi599/miniconda3/envs/ocean/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)
/g/data/r87/dbi599/miniconda3/envs/ocean/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88
  return f(*args, **kwds)

In [14]:
%matplotlib inline

In [23]:
orig_ipsl_file = '/g/data/ua6/DRSv3/CMIP5/IPSL-CM5A-LR/historicalGHG/mon/ocean/r1i1p1/thetao/latest/thetao_Omon_IPSL-CM5A-LR_historicalGHG_r1i1p1_195001-199912.nc'
orig_noresm_file = '/g/data/ua6/DRSv3/CMIP5/NorESM1-M/historicalGHG/mon/ocean/r1i1p1/thetao/latest/thetao_Omon_NorESM1-M_historicalGHG_r1i1p1_185001-185312.nc'

In [25]:
orig_ipsl_cube = iris.load_cube(orig_ipsl_file)[0, 0 , ::]
print(orig_ipsl_cube)


sea_water_potential_temperature / (K) (cell index along second dimension: 149; cell index along first dimension: 182)
     Dimension coordinates:
          cell index along second dimension                             x                                      -
          cell index along first dimension                              -                                      x
     Auxiliary coordinates:
          latitude                                                      x                                      x
          longitude                                                     x                                      x
     Scalar coordinates:
          depth: 4.999938011169434 m, bound=(0.0, 10.000015258789062) m
          time: 1950-01-16 12:00:00, bound=(1950-01-01 00:00:00, 1950-02-01 00:00:00)
     Attributes:
          Conventions: CF-1.4
          associated_files: baseURL: http://cmip-pcmdi.llnl.gov/CMIP5/dataLocation gridspecFile: gridspec_ocean_fx_IPSL-CM5A-LR_historicalGHG_r0i0p0.nc...
          branch_time: 1850.0
          cmor_version: 2.7.1
          comment: This 20th century simulation include GHG forcings only.
          contact: ipsl-cmip5 _at_ ipsl.jussieu.fr Data manager : Sebastien Denvil
          creation_date: 2012-05-17T23:28:38Z
          experiment: GHG-only
          experiment_id: historicalGHG
          forcing: GHG
          frequency: mon
          history: 2012-05-17T23:28:22Z altered by CMOR: Converted units from 'degC' to 'K'....
          initialization_method: 1
          institute_id: IPSL
          institution: IPSL (Institut Pierre Simon Laplace, Paris, France)
          model_id: IPSL-CM5A-LR
          modeling_realm: ocean
          original_name: votemper
          original_units: degC
          parent_experiment: pre-industrial control
          parent_experiment_id: piControl
          parent_experiment_rip: r1i1p1
          physics_version: 1
          product: output
          project_id: CMIP5
          realization: 1
          references: Model documentation and further reference available here : http://icmc...
          source: IPSL-CM5A-LR (2010) : atmos : LMDZ4 (LMDZ4_v5, 96x95x39); ocean : ORCA2...
          table_id: Table Omon (31 January 2011) d2d6beec2b8fea5bbed33920a6e08bbe
          title: IPSL-CM5A-LR model output prepared for CMIP5 GHG-only
          tracking_id: f24e16dd-ab61-4f29-bf7c-ed186ad0109c
     Cell methods:
          mean: time (30 minutes)

In [26]:
orig_noresm_cube = iris.load_cube(orig_noresm_file)[0, 0 , ::]
print(orig_noresm_cube)


sea_water_potential_temperature / (K) (cell index along second dimension: 384; cell index along first dimension: 320)
     Dimension coordinates:
          cell index along second dimension                             x                                      -
          cell index along first dimension                              -                                      x
     Auxiliary coordinates:
          latitude                                                      x                                      x
          longitude                                                     x                                      x
     Scalar coordinates:
          depth: 0.0 m, bound=(0.0, 2.5) m
          time: 1850-01-16 12:00:00, bound=(1850-01-01 00:00:00, 1850-02-01 00:00:00)
     Attributes:
          Conventions: CF-1.4
          associated_files: baseURL: http://cmip-pcmdi.llnl.gov/CMIP5/dataLocation gridspecFile: gridspec_ocean_fx_NorESM1-M_historicalGHG_r0i0p0.nc...
          branch_time: 255135.0
          cmor_version: 2.6.0
          contact: Please send any requests or bug reports to noresm-ncc@met.no.
          creation_date: 2011-06-12T16:27:58Z
          experiment: GHG-only
          experiment_id: historicalGHG
          forcing: GHG,Oz
          frequency: mon
          history: 2011-06-12T16:27:57Z altered by CMOR: Converted units from 'degC' to 'K'....
          initialization_method: 1
          institute_id: NCC
          institution: Norwegian Climate Centre
          model_id: NorESM1-M
          modeling_realm: ocean
          original_name: templvl
          original_units: degC
          parent_experiment: pre-industrial control
          parent_experiment_id: piControl
          parent_experiment_rip: r1i1p1
          physics_version: 1
          product: output
          project_id: CMIP5
          realization: 1
          source: NorESM1-M 2011  atmosphere: CAM-Oslo (CAM4-Oslo-noresm-ver1_cmip5-r112,...
          table_id: Table Omon (27 April 2011) 340eddd4fd838d90fa9ffe1345ecbd73
          title: NorESM1-M model output prepared for CMIP5 GHG-only
          tracking_id: b2955125-1a09-4f86-a807-e47ec386dfbb
     Cell methods:
          mean: time

In [27]:
def get_dim_vals(user_input, bounds=False):
    """Get the list of values for a data dimension/axis.
    Args:
      bounds (bool): input represents the bounds, not centre points
    """

    vals = user_input
    if user_input:
        if len(user_input) == 3:
            start, stop, step = user_input
            vals = list(numpy.arange(start, stop + step, step))
        if bounds:
            vals = numpy.array(vals)
            vals = (vals[1:] + vals[:-1]) / 2
    
    return list(vals)

In [28]:
lats = get_dim_vals([-80, 80, 1.0]) 
lons = get_dim_vals([2, 360, 2])
horizontal_grid = grids.make_grid(lats, lons)

In [29]:
new_noresm_cube, coord_names, regrid_status = grids.curvilinear_to_rectilinear(orig_noresm_cube, target_grid_cube=horizontal_grid)
iplt.contourf(new_noresm_cube)


Out[29]:
<matplotlib.contour.QuadContourSet at 0x7f7d095eb978>

In [32]:
ipsl_area_cube = iris.load_cube('/g/data/ua6/DRSv3/CMIP5/IPSL-CM5A-LR/historical/fx/ocean/r0i0p0/areacello/latest/areacello_fx_IPSL-CM5A-LR_historical_r0i0p0.nc')
ipsl_area_cube


Out[32]:
Cell Area (m2) cell index along second dimension cell index along first dimension
Shape 149 182
Dimension coordinates
cell index along second dimension x -
cell index along first dimension - x
Auxiliary coordinates
latitude x x
longitude x x
Attributes
Conventions CF-1.4
associated_files baseURL: http://cmip-pcmdi.llnl.gov/CMIP5/dataLocation gridspecFile: g...
branch_time 2000.0
cmor_version 2.7.1
comment This 20th century simulation include natural and anthropogenic forcing...
contact ipsl-cmip5 _at_ ipsl.jussieu.fr Data manager : Sebastien Denvil
creation_date 2011-10-30T19:10:58Z
experiment historical
experiment_id historical
forcing Nat,Ant,GHG,SA,Oz,LU,SS,Ds,BC,MD,OC,AA
frequency fx
history 2011-10-30T19:10:58Z altered by CMOR: replaced missing value flag (-1e+34)...
initialization_method 0
institute_id IPSL
institution IPSL (Institut Pierre Simon Laplace, Paris, France)
model_id IPSL-CM5A-LR
modeling_realm ocean
original_name AREA
parent_experiment pre-industrial control
parent_experiment_id piControl
parent_experiment_rip r1i1p1
physics_version 0
product output
project_id CMIP5
realization 0
references Model documentation and further reference available here : http://icmc...
source IPSL-CM5A-LR (2010) : atmos : LMDZ4 (LMDZ4_v5, 96x95x39); ocean : ORCA2...
table_id Table fx (31 January 2011) dd5903eb1b6f5aa24d6d6dd1679cef9d
title IPSL-CM5A-LR model output prepared for CMIP5 historical
tracking_id 2e9222b4-cb0e-43f2-936a-ac0512702c58

In [33]:
new_ipsl_cube, coord_names, regrid_status = grids.curvilinear_to_rectilinear(orig_ipsl_cube, 
                                                                             weights=ipsl_area_cube.data,
                                                                             target_grid_cube=horizontal_grid)
iplt.contourf(new_ipsl_cube)


Out[33]:
<matplotlib.contour.QuadContourSet at 0x7f7d093e63c8>

Using cdo...


In [2]:
cdo_file = '/g/data/r87/dbi599/DRSv2/CMIP5/IPSL-CM5A-LR/historical/yr/ocean/r1i1p1/thetao/latest/thetao_Oyr_IPSL-CM5A-LR_historical_r1i1p1_185001-189912_susan-grid.nc'

In [3]:
cdo_cube = iris.load_cube(cdo_file, 'sea_water_potential_temperature')

In [4]:
cdo_cube


Out[4]:
Sea Water Potential Temperature (K) time depth latitude longitude
Shape 50 66 161 180
Dimension coordinates
time x - - -
depth - x - -
latitude - - x -
longitude - - - x
Attributes
CDI Climate Data Interface version 1.9.2 (http://mpimet.mpg.de/cdi)
CDO Climate Data Operators version 1.9.2 (http://mpimet.mpg.de/cdo)
Conventions CF-1.5
associated_files baseURL: http://cmip-pcmdi.llnl.gov/CMIP5/dataLocation gridspecFile: gridspec_ocean_fx_IPSL-CM5A-LR_historical_r0i0p0.nc...
branch_time 1850.0
cmor_version 2.7.1
comment This 20th century simulation include natural and anthropogenic forcing...
contact ipsl-cmip5 _at_ ipsl.jussieu.fr Data manager : Sebastien Denvil
creation_date 2011-07-04T20:11:18Z
experiment historical
experiment_id historical
forcing Nat,Ant,GHG,SA,Oz,LU,SS,Ds,BC,MD,OC,AA
frequency mon
history Fri Mar 29 11:54:16 2019: /g/data/r87/dbi599/miniconda3/envs/ocean/bin/python...
initialization_method 1
institute_id IPSL
institution IPSL (Institut Pierre Simon Laplace, Paris, France)
model_id IPSL-CM5A-LR
modeling_realm ocean
original_name votemper
original_units degC
parent_experiment pre-industrial control
parent_experiment_id piControl
parent_experiment_rip r1i1p1
physics_version 1
product output
project_id CMIP5
realization 1
references Model documentation and further reference available here : http://icmc...
source IPSL-CM5A-LR (2010) : atmos : LMDZ4 (LMDZ4_v5, 96x95x39); ocean : ORCA2...
table_id Table Omon (31 January 2011) d2d6beec2b8fea5bbed33920a6e08bbe
title IPSL-CM5A-LR model output prepared for CMIP5 historical
tracking_id a73c6f24-c8fb-46c9-a9dc-a41507a05368
Cell methods
mean time (30 minutes)
mean year

In [6]:
iplt.contourf(cdo_cube[0, 0, ::])


Out[6]:
<matplotlib.contour.QuadContourSet at 0x7f469acf4588>

In [ ]: