/
utils.py
335 lines (263 loc) · 10.5 KB
/
utils.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2009-2019 Satpy developers
#
# This file is part of satpy.
#
# satpy is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# satpy is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with satpy. If not, see <http://www.gnu.org/licenses/>.
"""Module defining various utilities."""
import logging
import os
import sys
import re
import warnings
import numpy as np
try:
import configparser
except ImportError:
from six.moves import configparser
_is_logging_on = False
TRACE_LEVEL = 5
class OrderedConfigParser(object):
"""Intercepts read and stores ordered section names.
Cannot use inheritance and super as ConfigParser use old style classes.
"""
def __init__(self, *args, **kwargs):
"""Initialize the instance."""
self.config_parser = configparser.ConfigParser(*args, **kwargs)
def __getattr__(self, name):
"""Get the attribute."""
return getattr(self.config_parser, name)
def read(self, filename):
"""Read config file."""
try:
conf_file = open(filename, 'r')
config = conf_file.read()
config_keys = re.findall(r'\[.*\]', config)
self.section_keys = [key[1:-1] for key in config_keys]
except IOError as e:
# Pass if file not found
if e.errno != 2:
raise
return self.config_parser.read(filename)
def sections(self):
"""Get sections from config file."""
try:
return self.section_keys
except: # noqa: E722
return self.config_parser.sections()
def ensure_dir(filename):
"""Check if the dir of f exists, otherwise create it."""
directory = os.path.dirname(filename)
if directory and not os.path.isdir(directory):
os.makedirs(directory)
def debug_on():
"""Turn debugging logging on."""
logging_on(logging.DEBUG)
def trace_on():
"""Turn trace logging on."""
logging_on(TRACE_LEVEL)
def logging_on(level=logging.WARNING):
"""Turn logging on."""
global _is_logging_on
if not _is_logging_on:
console = logging.StreamHandler()
console.setFormatter(logging.Formatter("[%(levelname)s: %(asctime)s :"
" %(name)s] %(message)s",
'%Y-%m-%d %H:%M:%S'))
console.setLevel(level)
logging.getLogger('').addHandler(console)
_is_logging_on = True
log = logging.getLogger('')
log.setLevel(level)
for h in log.handlers:
h.setLevel(level)
def logging_off():
"""Turn logging off."""
logging.getLogger('').handlers = [logging.NullHandler()]
def get_logger(name):
"""Return logger with null handler added if needed."""
if not hasattr(logging.Logger, 'trace'):
logging.addLevelName(TRACE_LEVEL, 'TRACE')
def trace(self, message, *args, **kwargs):
if self.isEnabledFor(TRACE_LEVEL):
# Yes, logger takes its '*args' as 'args'.
self._log(TRACE_LEVEL, message, args, **kwargs)
logging.Logger.trace = trace
log = logging.getLogger(name)
if not log.handlers and sys.version_info[0] < 3:
log.addHandler(logging.NullHandler())
return log
def in_ipynb():
"""Check if we are in a jupyter notebook."""
try:
return 'ZMQ' in get_ipython().__class__.__name__
except NameError:
return False
# Spherical conversions
def lonlat2xyz(lon, lat):
"""Convert lon lat to cartesian."""
lat = np.deg2rad(lat)
lon = np.deg2rad(lon)
x = np.cos(lat) * np.cos(lon)
y = np.cos(lat) * np.sin(lon)
z = np.sin(lat)
return x, y, z
def xyz2lonlat(x, y, z, asin=False):
"""Convert cartesian to lon lat."""
lon = np.rad2deg(np.arctan2(y, x))
if asin:
lat = np.rad2deg(np.arcsin(z))
else:
lat = np.rad2deg(np.arctan2(z, np.sqrt(x ** 2 + y ** 2)))
return lon, lat
def angle2xyz(azi, zen):
"""Convert azimuth and zenith to cartesian."""
azi = np.deg2rad(azi)
zen = np.deg2rad(zen)
x = np.sin(zen) * np.sin(azi)
y = np.sin(zen) * np.cos(azi)
z = np.cos(zen)
return x, y, z
def xyz2angle(x, y, z, acos=False):
"""Convert cartesian to azimuth and zenith."""
azi = np.rad2deg(np.arctan2(x, y))
if acos:
zen = np.rad2deg(np.arccos(z))
else:
zen = 90 - np.rad2deg(np.arctan2(z, np.sqrt(x ** 2 + y ** 2)))
return azi, zen
def proj_units_to_meters(proj_str):
"""Convert projection units from kilometers to meters."""
proj_parts = proj_str.split()
new_parts = []
for itm in proj_parts:
key, val = itm.split('=')
key = key.strip('+')
if key in ['a', 'b', 'h']:
val = float(val)
if val < 6e6:
val *= 1000.
val = '%.3f' % val
if key == 'units' and val == 'km':
continue
new_parts.append('+%s=%s' % (key, val))
return ' '.join(new_parts)
def _get_sunz_corr_li_and_shibata(cos_zen):
return 24.35 / (2. * cos_zen + np.sqrt(498.5225 * cos_zen**2 + 1))
def sunzen_corr_cos(data, cos_zen, limit=88., max_sza=95.):
"""Perform Sun zenith angle correction.
The correction is based on the provided cosine of the zenith
angle (``cos_zen``). The correction is limited
to ``limit`` degrees (default: 88.0 degrees). For larger zenith
angles, the correction is the same as at the ``limit`` if ``max_sza``
is `None`. The default behavior is to gradually reduce the correction
past ``limit`` degrees up to ``max_sza`` where the correction becomes
0. Both ``data`` and ``cos_zen`` should be 2D arrays of the same shape.
"""
# Convert the zenith angle limit to cosine of zenith angle
limit_rad = np.deg2rad(limit)
limit_cos = np.cos(limit_rad)
max_sza_rad = np.deg2rad(max_sza) if max_sza is not None else max_sza
# Cosine correction
corr = 1. / cos_zen
if max_sza is not None:
# gradually fall off for larger zenith angle
grad_factor = (np.arccos(cos_zen) - limit_rad) / (max_sza_rad - limit_rad)
# invert the factor so maximum correction is done at `limit` and falls off later
grad_factor = 1. - np.log(grad_factor + 1) / np.log(2)
# make sure we don't make anything negative
grad_factor = grad_factor.clip(0.)
else:
# Use constant value (the limit) for larger zenith angles
grad_factor = 1.
corr = corr.where(cos_zen > limit_cos, grad_factor / limit_cos)
# Force "night" pixels to 0 (where SZA is invalid)
corr = corr.where(cos_zen.notnull(), 0)
return data * corr
def atmospheric_path_length_correction(data, cos_zen, limit=88., max_sza=95.):
"""Perform Sun zenith angle correction.
This function uses the correction method proposed by
Li and Shibata (2006): https://doi.org/10.1175/JAS3682.1
The correction is limited to ``limit`` degrees (default: 88.0 degrees). For
larger zenith angles, the correction is the same as at the ``limit`` if
``max_sza`` is `None`. The default behavior is to gradually reduce the
correction past ``limit`` degrees up to ``max_sza`` where the correction
becomes 0. Both ``data`` and ``cos_zen`` should be 2D arrays of the same
shape.
"""
# Convert the zenith angle limit to cosine of zenith angle
limit_rad = np.deg2rad(limit)
limit_cos = np.cos(limit_rad)
max_sza_rad = np.deg2rad(max_sza) if max_sza is not None else max_sza
# Cosine correction
corr = _get_sunz_corr_li_and_shibata(cos_zen)
# Use constant value (the limit) for larger zenith angles
corr_lim = _get_sunz_corr_li_and_shibata(limit_cos)
if max_sza is not None:
# gradually fall off for larger zenith angle
grad_factor = (np.arccos(cos_zen) - limit_rad) / (max_sza_rad - limit_rad)
# invert the factor so maximum correction is done at `limit` and falls off later
grad_factor = 1. - np.log(grad_factor + 1) / np.log(2)
# make sure we don't make anything negative
grad_factor = grad_factor.clip(0.)
else:
# Use constant value (the limit) for larger zenith angles
grad_factor = 1.
corr = corr.where(cos_zen > limit_cos, grad_factor * corr_lim)
# Force "night" pixels to 0 (where SZA is invalid)
corr = corr.where(cos_zen.notnull(), 0)
return data * corr
def get_satpos(dataset):
"""Get satellite position from dataset attributes.
Preferences are:
* Longitude & Latitude: Nadir, actual, nominal, projection
* Altitude: Actual, nominal, projection
A warning is issued when projection values have to be used because nothing else is available.
Returns:
Geodetic longitude, latitude, altitude
"""
try:
orb_params = dataset.attrs['orbital_parameters']
# Altitude
try:
alt = orb_params['satellite_actual_altitude']
except KeyError:
try:
alt = orb_params['satellite_nominal_altitude']
except KeyError:
alt = orb_params['projection_altitude']
warnings.warn('Actual satellite altitude not available, using projection altitude instead.')
# Longitude & Latitude
try:
lon = orb_params['nadir_longitude']
lat = orb_params['nadir_latitude']
except KeyError:
try:
lon = orb_params['satellite_actual_longitude']
lat = orb_params['satellite_actual_latitude']
except KeyError:
try:
lon = orb_params['satellite_nominal_longitude']
lat = orb_params['satellite_nominal_latitude']
except KeyError:
lon = orb_params['projection_longitude']
lat = orb_params['projection_latitude']
warnings.warn('Actual satellite lon/lat not available, using projection centre instead.')
except KeyError:
# Legacy
lon = dataset.attrs['satellite_longitude']
lat = dataset.attrs['satellite_latitude']
alt = dataset.attrs['satellite_altitude']
return lon, lat, alt