-
Notifications
You must be signed in to change notification settings - Fork 128
/
gpcp_sg.py
107 lines (80 loc) · 2.9 KB
/
gpcp_sg.py
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
104
105
106
107
"""ESMValTool CMORizer for GPCP-SG data.
Tier
Tier 2: other freely-available dataset.
Source
https://psl.noaa.gov/data/gridded/data.gpcp.html
https://downloads.psl.noaa.gov/Datasets/gpcp/precip.mon.mean.nc
Last access
20230215
Download and processing instructions
Download the file precip.mon.mean.nc
wget https://downloads.psl.noaa.gov/Datasets/gpcp/precip.mon.mean.nc
"""
import logging
import warnings
from pathlib import Path
import iris
from iris import NameConstraint
from esmvaltool.cmorizers.data import utilities as utils
logger = logging.getLogger(__name__)
def _fix_var_metadata(var_info, cmor_info, cube):
"""Fix variable metadata."""
if 'raw_units' in var_info:
cube.units = var_info['raw_units']
if cube.units == 'mm/day':
cube.units = 'kg m-2 day-1'
cube.convert_units(cmor_info.units)
utils.fix_var_metadata(cube, cmor_info)
return cube
def _fix_coords(cube, filepath):
"""Fix coordinates."""
utils.fix_dim_coordnames(cube)
# Bounds
# Time
time_bnds = iris.load_cube(filepath, NameConstraint(var_name='time_bnds'))
cube.coord('time').bounds = time_bnds.core_data()
# Latitude
lat_bnds = iris.load_cube(filepath, NameConstraint(var_name='lat_bnds'))
cube.coord('latitude').bounds = lat_bnds.core_data()
# Longitude
lon_bnds = iris.load_cube(filepath, NameConstraint(var_name='lon_bnds'))
cube.coord('longitude').bounds = lon_bnds.core_data()
def _extract_variable(var_info, cmor_info, attrs, filepath, out_dir):
"""Extract variable."""
var = cmor_info.short_name
raw_var = var_info.get('raw_name', var)
# Load data
with warnings.catch_warnings():
warnings.filterwarnings(
action='ignore',
message="Skipping global attribute 'units': 'units' is not a "
"permitted attribute",
category=UserWarning,
module='iris',
)
cube = iris.load_cube(filepath, NameConstraint(var_name=raw_var))
# Fix variable metadata
cube = _fix_var_metadata(var_info, cmor_info, cube)
# Fix coordinates
_fix_coords(cube, filepath)
# Fix global metadata
utils.set_global_atts(cube, attrs)
# Save variable
utils.save_variable(
cube,
var,
out_dir,
attrs,
unlimited_dimensions=['time'],
)
def cmorization(in_dir, out_dir, cfg, cfg_user, start_date, end_date):
"""Cmorization func call."""
cmor_table = cfg['cmor_table']
glob_attrs = cfg['attributes']
# Run the cmorization
for (var, var_info) in cfg['variables'].items():
filepath = Path(in_dir) / var_info['filename']
logger.info("CMORizing variable '%s' from file %s", var, filepath)
glob_attrs['mip'] = var_info['mip']
cmor_info = cmor_table.get_variable(var_info['mip'], var)
_extract_variable(var_info, cmor_info, glob_attrs, filepath, out_dir)