Tmean_Means.py 4.24 KB
Newer Older
Klaus Zimmermann's avatar
Klaus Zimmermann committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# import pprint

import iris
import iris.coords
import iris.coord_categorisation

from cf_units import Unit

# import check_cubes

var = 'tas'
indir = '/home/rossby/imports/obs/UERRA/SURFEX-MESCAN/input/mon/'
inroot = '_EUR-055_UERRA-HARMONIE_RegRean_v1d1-v1d2_SURFEX-MESCAN_v1_mon_*.nc'
filename = '{}{}{}'.format(indir, var, inroot)

cubelist = iris.load(filename)

creation_date = ''
tracking_id = ''
for cube in cubelist:
    creation_date += cube.attributes.pop('creation_date', None) + ' '
    tracking_id += cube.attributes.pop('tracking_id', None) + ' '

# pprint.pprint(check_cubes(.....))

datacube = cubelist.concatenate()
if len(datacube) > 1:
    raise(TypeError)
else:
    datacube = datacube[0]

datacube.convert_units(Unit('degree_Celsius'))
datacube.attributes['creation_date'] = creation_date
datacube.attributes['tracking_id'] = tracking_id

iris.coord_categorisation.add_month(datacube, 'time', name='month')
iris.coord_categorisation.add_year(datacube, 'time', name='year')

Tmean_ANN = datacube.aggregated_by('year', iris.analysis.MEAN)
Tmean_ANN.long_name = u'Annual Mean Near-Surface Air Temperature'
Tmean_ANN.var_name = u'Tmean_ANN'
Tmean_ANN.remove_coord('month')
c = Tmean_ANN.extract(iris.Constraint(
    coord_values={'year': lambda cell: 1960 < cell < 1991}))
Tmean_ANN_6190 = c.collapsed('year', iris.analysis.MEAN)

Tmean_JAN = datacube.extract(iris.Constraint(coord_values={u'month': 'Jan'}))
Tmean_JAN.long_name = u'January Mean Near-Surface Air Temperature'
Tmean_JAN.var_name = u'Tmean_JAN'
Tmean_JAN.remove_coord('month')
c = Tmean_JAN.extract(iris.Constraint(
    coord_values={'year': lambda cell: 1960 < cell < 1991}))
Tmean_JAN_6190 = c.collapsed('year', iris.analysis.MEAN)

Tmean_FEB = datacube.extract(iris.Constraint(coord_values={u'month': 'Feb'}))
Tmean_FEB.long_name = u'February Mean Near-Surface Air Temperature'
Tmean_FEB.var_name = u'Tmean_FEB'
Tmean_FEB.remove_coord('month')
c = Tmean_FEB.extract(iris.Constraint(
    coord_values={'year': lambda cell: 1960 < cell < 1991}))
Tmean_FEB_6190 = c.collapsed('year', iris.analysis.MEAN)

Tmean_JUL = datacube.extract(iris.Constraint(coord_values={u'month': 'Jul'}))
Tmean_JUL.long_name = u'July Mean Near-Surface Air Temperature'
Tmean_JUL.var_name = u'Tmean_JUL'
Tmean_JUL.remove_coord('month')
c = Tmean_JUL.extract(iris.Constraint(
    coord_values={'year': lambda cell: 1960 < cell < 1991}))
Tmean_JUL_6190 = c.collapsed('year', iris.analysis.MEAN)

Tmean_max = datacube.aggregated_by('year', iris.analysis.MAX)
Tmean_min = datacube.aggregated_by('year', iris.analysis.MIN)
Tmean_RANGE = Tmean_max - Tmean_min
Tmean_RANGE.attributes = Tmean_ANN.attributes
Tmean_RANGE.standard_name = u'air_temperature'
Tmean_RANGE.long_name = u'Near-Surface Air Temperature Annual Range'
Tmean_RANGE.var_name = u'Tmean_RANGE'
Tmean_RANGE.units = Unit('degree_Celsius')
Tmean_RANGE.remove_coord('month')
Tmean_RANGE.cell_methods = (iris.coords.CellMethod(method='mean',
                                                   coords='time',
                                                   intervals='within month'),
                            iris.coords.CellMethod(method='range',
                                                   coords='time',
                                                   intervals='within year'))
c = Tmean_RANGE.extract(iris.Constraint(
    coord_values={'year': lambda cell: 1960 < cell < 1991}))
Tmean_RANGE_6190 = c.collapsed('year', iris.analysis.MEAN)

outdir = '/home/sm_lbarr/PROJ/B4EST/'
outroot = '_EUR-055_UERRA-HARMONIE_RegRean_v1d1-v1d2_SURFEX-MESCAN_v1_ann_1961-2015.nc'

iris.save(Tmean_ANN, '{}{}{}'.format(outdir, 'Tmean_ANN', outroot))
iris.save(Tmean_JAN, '{}{}{}'.format(outdir, 'Tmean_JAN', outroot))
iris.save(Tmean_FEB, '{}{}{}'.format(outdir, 'Tmean_FEB', outroot))
iris.save(Tmean_JUL, '{}{}{}'.format(outdir, 'Tmean_JUL', outroot))
iris.save(Tmean_RANGE, '{}{}{}'.format(outdir, 'Tmean_RANGE', outroot))

iris.save(Tmean_ANN_6190, '{}{}{}'.format(outdir, 'Tmean_ANN_6190', outroot))
iris.save(Tmean_JAN_6190, '{}{}{}'.format(outdir, 'Tmean_JAN_6190', outroot))
iris.save(Tmean_FEB_6190, '{}{}{}'.format(outdir, 'Tmean_FEB_6190', outroot))
iris.save(Tmean_JUL_6190, '{}{}{}'.format(outdir, 'Tmean_JUL_6190', outroot))
iris.save(Tmean_RANGE_6190, '{}{}{}'.format(outdir, 'Tmean_RANGE_6190', outroot))