Skip to content

Instantly share code, notes, and snippets.

@benbovy
Last active October 12, 2016 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbovy/e9163e8a649e33133c451fad963512b9 to your computer and use it in GitHub Desktop.
Save benbovy/e9163e8a649e33133c451fad963512b9 to your computer and use it in GitHub Desktop.
Iris: read data from daily data files into a single, merged cube
import datetime
import iris
import cf_units
TIME_UNITS = 'hours since 1970-01-01 00:00:00'
def read_file_as_cube(filename, prefix):
# get date from filename
date = datetime.datetime.strptime(filename, prefix + '%Y%m%d')
# set the `cube` and `hour` variables from reading the file
# just an example below, REPLACE BY YOUR CODE
lon = iris.coords.DimCoord([70, 80], standard_name='longitude')
lat = iris.coords.DimCoord([5, 10, 15], standard_name='latitude')
cube = iris.cube.Cube([[0, 1, 2], [2, 3, 4]],
dim_coords_and_dims=((lon, 0), (lat, 1)))
hour = 12
# create time scalar coordinate and add to the cube
hour = datetime.time(hour=hour)
dt = datetime.datetime.combine(date, hour)
dt_num = cf_units.date2num(dt, TIME_UNITS, cf_units.CALENDAR_STANDARD)
time_coord = iris.coords.AuxCoord(dt_num, standard_name='time',
units=TIME_UNITS)
cube.add_aux_coord(time_coord)
return cube
filenames = ['HCOOHiasi_20090727', 'HCOOHiasi_20090728', 'HCOOHiasi_20090729']
cubes = iris.cube.CubeList([read_file_as_cube(fn, 'HCOOHiasi_')
for fn in filenames])
merged_cube = cubes.merge_cube()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment