Redecorate Transient Variable

This Jupyter Notebook shows how to redecorate a transient variable that became a numpy array

The CDAT software was developed by LLNL. This tutorial was written by Charles Doutriaux. This work was performed under the auspices of the U.S. Department of Energy by Lawrence Livermore National Laboratory under Contract DE-AC52-07NA27344.

Download the Jupyter Notebook

Import Modules

Back to Top

In [1]:
# import modules
import MV2
import cdms2
import numpy
import cdat_info  # for sample data

Open Data

Back to Top

In [2]:
# open some data
f=cdms2.open(cdat_info.get_sampledata_path()+"/clt.nc")
s=f("clt")
print(type(s))
<class 'cdms2.tvariable.TransientVariable'>

Run Data

Back to Top

In [3]:
# Now run an operation on this MV2 that turns it to munpy array
fft_s = numpy.fft.fft(s)
print(type(fft_s))  # numpy array
<class 'numpy.ndarray'>

Put Back Dimensions

Back to Top

In [4]:
# now put back dimensions on it
fft_s = MV2.array(fft_s)
fft_s.setAxisList(s.getAxisList())

# Dimensions are back
print(fft_s.getAxisIds())
['time', 'latitude', 'longitude']

Put Attributes On It

Back to Top

In [5]:
# now puts the attributes on it
for a in s.attributes:
    setattr(fft_s,a,getattr(s,a))
fft_s.info()
*** Description of Slab variable_3 ***
id: variable_3
shape: (120, 46, 72)
filename: 
missing_value: (1e+20+0j)
comments: YONU_AMIP1
grid_name: YONU4X5
grid_type: gaussian
time_statistic: average
long_name: Total cloudiness
units: %
tileIndex: None
Grid has Python id 0x7fbf20fbc8d0.
Gridtype: generic
Grid shape: (46, 72)
Order: yx
** Dimension 1 **
   id: time
   Designated a time axis.
   units:  months since 1979-1-1 0
   Length: 120
   First:  0.0
   Last:   119.0
   Other axis attributes:
      axis: T
      calendar: gregorian
      realtopology: linear
   Python id:  0x7fbf589edc50
** Dimension 2 **
   id: latitude
   Designated a latitude axis.
   units:  degrees_north
   Length: 46
   First:  -90.0
   Last:   90.0
   Other axis attributes:
      axis: Y
      long_name: Latitude
      realtopology: linear
   Python id:  0x7fbf236b79e8
** Dimension 3 **
   id: longitude
   Designated a longitude axis.
   units:  degrees_east
   Length: 72
   First:  -180.0
   Last:   175.0
   Other axis attributes:
      axis: X
      modulo: 360.0
      topology: circular
      long_name: Longitude
      realtopology: circular
   Python id:  0x7fbf20fbc6a0
*** End of description for variable_3 ***