Precip_Sums.py 2.53 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
import numpy as np

import iris
import iris.coords
import iris.coord_categorisation

from cf_units import Unit

# from change_pr_units import change_pr_units

# import pprint
# import check_cubes

var = 'pr'
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]

f = np.diff(datacube.coords('time')[0].bounds) * 24 * 60 * 60
datacube *= f[:, :, np.newaxis]
datacube.units = Unit('mm')
datacube.standard_name = 'lwe_thickness_of_precipitation_amount'
datacube.attributes['creation_date'] = creation_date
datacube.attributes['tracking_id'] = tracking_id

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

Precip_sum_ANN = datacube.aggregated_by('year', iris.analysis.SUM)
Precip_sum_ANN.long_name = u'Annual Total Precipitation'
Precip_sum_ANN.var_name = u'Precip_sum_ANN'
Precip_sum_ANN.remove_coord('month')

c = Precip_sum_ANN.extract(iris.Constraint(
    coord_values={'year': lambda cell: 1960 < cell < 1991}))
Precip_sum_ANN_6190 = c.collapsed('year', iris.analysis.MEAN)

c = datacube.extract(iris.Constraint(
    coord_values={u'month': lambda cell: 3 < cell < 11}))
Precip_sum_AMJJASO = c.aggregated_by('year', iris.analysis.SUM)
Precip_sum_AMJJASO.long_name = u'April-October Total Precipitation'
Precip_sum_AMJJASO.var_name = u'Precip_sum_AMJJASO'
Precip_sum_AMJJASO.remove_coord('month')
e = Precip_sum_AMJJASO.extract(iris.Constraint(
    coord_values={'year': lambda cell: 1960 < cell < 1991}))
Precip_sum_AMJJASO_6190 = e.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(Precip_sum_ANN,
          '{}{}{}'.format(outdir, 'Precip_sum_ANN', outroot))
iris.save(Precip_sum_AMJJASO,
          '{}{}{}'.format(outdir, 'Precip_sum_AMJJASO', outroot))

iris.save(Precip_sum_ANN_6190,
          '{}{}{}'.format(outdir, 'Precip_sum_ANN_6190', outroot))
iris.save(Precip_sum_AMJJASO_6190,
          '{}{}{}'.format(outdir, 'Precip_sum_AMJJASO_6190', outroot))