forked from pytroll/satpy
/
abi_base.py
284 lines (239 loc) · 10.8 KB
/
abi_base.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2016-2018 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/>.
"""Advance Baseline Imager reader base class for the Level 1b and l2+ reader."""
import logging
from datetime import datetime
import numpy as np
import xarray as xr
from pyresample import geometry
from satpy import CHUNK_SIZE
from satpy.readers import open_file_or_filename
from satpy.readers.file_handlers import BaseFileHandler
logger = logging.getLogger(__name__)
PLATFORM_NAMES = {
'G16': 'GOES-16',
'G17': 'GOES-17',
}
class NC_ABI_BASE(BaseFileHandler):
"""Base reader for ABI L1B L2+ NetCDF4 files."""
def __init__(self, filename, filename_info, filetype_info):
"""Open the NetCDF file with xarray and prepare the Dataset for reading."""
super(NC_ABI_BASE, self).__init__(filename, filename_info, filetype_info)
f_obj = open_file_or_filename(self.filename)
try:
self.nc = xr.open_dataset(f_obj,
decode_cf=True,
mask_and_scale=False,
chunks={'x': CHUNK_SIZE, 'y': CHUNK_SIZE}, )
except ValueError:
self.nc = xr.open_dataset(f_obj,
decode_cf=True,
mask_and_scale=False,
chunks={'lon': CHUNK_SIZE, 'lat': CHUNK_SIZE}, )
if 't' in self.nc.dims or 't' in self.nc.coords:
self.nc = self.nc.rename({'t': 'time'})
platform_shortname = filename_info['platform_shortname']
self.platform_name = PLATFORM_NAMES.get(platform_shortname)
if 'goes_imager_projection' in self.nc:
self.nlines = self.nc['y'].size
self.ncols = self.nc['x'].size
elif 'goes_lat_lon_projection' in self.nc:
self.nlines = self.nc['lat'].size
self.ncols = self.nc['lon'].size
self.nc = self.nc.rename({'lon': 'x', 'lat': 'y'})
self.coords = {}
@property
def sensor(self):
"""Get sensor name for current file handler."""
return 'abi'
def __getitem__(self, item):
"""Wrap `self.nc[item]` for better floating point precision.
Some datasets use a 32-bit float scaling factor like the 'x' and 'y'
variables which causes inaccurate unscaled data values. This method
forces the scale factor to a 64-bit float first.
"""
data = self.nc[item]
attrs = data.attrs
data = self._adjust_data(data, item)
data.attrs = attrs
data = self._adjust_coords(data, item)
return data
def _adjust_data(self, data, item):
"""Adjust data with typing, scaling and filling."""
factor = data.attrs.get('scale_factor', 1)
offset = data.attrs.get('add_offset', 0)
fill = data.attrs.get('_FillValue')
unsigned = data.attrs.get('_Unsigned', None)
def is_int(val):
return np.issubdtype(val.dtype, np.integer) if hasattr(val, 'dtype') else isinstance(val, int)
# Ref. GOESR PUG-L1B-vol3, section 5.0.2 Unsigned Integer Processing
if unsigned is not None and unsigned.lower() == 'true':
# cast the data from int to uint
data = data.astype('u%s' % data.dtype.itemsize)
if fill is not None:
fill = fill.astype('u%s' % fill.dtype.itemsize)
if fill is not None:
# Some backends (h5netcdf) may return attributes as shape (1,)
# arrays rather than shape () scalars, which according to the netcdf
# documentation at <URL:https://www.unidata.ucar.edu
# /software/netcdf/docs/netcdf_data_set_components.html#attributes>
# is correct.
if np.ndim(fill) > 0:
fill = fill.item()
if is_int(data) and is_int(factor) and is_int(offset):
new_fill = fill
else:
new_fill = np.nan
data = data.where(data != fill, new_fill)
if factor != 1 and item in ('x', 'y'):
# be more precise with x/y coordinates
# see get_area_def for more information
data = data * np.round(float(factor), 6) + np.round(float(offset), 6)
elif factor != 1:
# make sure the factor is a 64-bit float
# can't do this in place since data is most likely uint16
# and we are making it a 64-bit float
if not is_int(factor):
factor = float(factor)
data = data * factor + offset
return data
def _adjust_coords(self, data, item):
"""Handle coordinates (and recursive fun)."""
new_coords = {}
# 'time' dimension causes issues in other processing
# 'x_image' and 'y_image' are confusing to some users and unnecessary
# 'x' and 'y' will be overwritten by base class AreaDefinition
for coord_name in ('x_image', 'y_image', 'time', 'x', 'y'):
if coord_name in data.coords:
data = data.drop_vars(coord_name)
if item in data.coords:
self.coords[item] = data
for coord_name in data.coords.keys():
if coord_name not in self.coords:
self.coords[coord_name] = self[coord_name]
new_coords[coord_name] = self.coords[coord_name]
data.coords.update(new_coords)
return data
def get_dataset(self, key, info):
"""Load a dataset."""
raise NotImplementedError("Reader {} has not implemented get_dataset".format(self.name))
def get_area_def(self, key):
"""Get the area definition of the data at hand."""
if 'goes_imager_projection' in self.nc:
return self._get_areadef_fixedgrid(key)
elif 'goes_lat_lon_projection' in self.nc:
return self._get_areadef_latlon(key)
else:
raise ValueError('Unsupported projection found in the dataset')
def _get_areadef_latlon(self, key):
"""Get the area definition of the data at hand."""
projection = self.nc["goes_lat_lon_projection"]
a = projection.attrs['semi_major_axis']
b = projection.attrs['semi_minor_axis']
fi = projection.attrs['inverse_flattening']
pm = projection.attrs['longitude_of_prime_meridian']
proj_ext = self.nc["geospatial_lat_lon_extent"]
w_lon = proj_ext.attrs['geospatial_westbound_longitude']
e_lon = proj_ext.attrs['geospatial_eastbound_longitude']
n_lat = proj_ext.attrs['geospatial_northbound_latitude']
s_lat = proj_ext.attrs['geospatial_southbound_latitude']
lat_0 = proj_ext.attrs['geospatial_lat_center']
lon_0 = proj_ext.attrs['geospatial_lon_center']
area_extent = (w_lon, s_lat, e_lon, n_lat)
proj_dict = {'proj': 'latlong',
'lon_0': float(lon_0),
'lat_0': float(lat_0),
'a': float(a),
'b': float(b),
'fi': float(fi),
'pm': float(pm)}
ll_area_def = geometry.AreaDefinition(
self.nc.attrs.get('orbital_slot', 'abi_geos'),
self.nc.attrs.get('spatial_resolution', 'ABI file area'),
'abi_latlon',
proj_dict,
self.ncols,
self.nlines,
np.asarray(area_extent))
return ll_area_def
def _get_areadef_fixedgrid(self, key):
"""Get the area definition of the data at hand.
Note this method takes special care to round and cast numbers to new
data types so that the area definitions for different resolutions
(different bands) should be equal. Without the special rounding in
`__getitem__` and this method the area extents can be 0 to 1.0 meters
off depending on how the calculations are done.
"""
projection = self.nc["goes_imager_projection"]
a = projection.attrs['semi_major_axis']
b = projection.attrs['semi_minor_axis']
h = projection.attrs['perspective_point_height']
lon_0 = projection.attrs['longitude_of_projection_origin']
sweep_axis = projection.attrs['sweep_angle_axis'][0]
# compute x and y extents in m
h = np.float64(h)
x = self['x']
y = self['y']
x_l = x[0].values
x_r = x[-1].values
y_l = y[-1].values
y_u = y[0].values
x_half = (x_r - x_l) / (self.ncols - 1) / 2.
y_half = (y_u - y_l) / (self.nlines - 1) / 2.
area_extent = (x_l - x_half, y_l - y_half, x_r + x_half, y_u + y_half)
area_extent = tuple(np.round(h * val, 6) for val in area_extent)
proj_dict = {'proj': 'geos',
'lon_0': float(lon_0),
'a': float(a),
'b': float(b),
'h': h,
'units': 'm',
'sweep': sweep_axis}
fg_area_def = geometry.AreaDefinition(
self.nc.attrs.get('orbital_slot', 'abi_geos'),
self.nc.attrs.get('spatial_resolution', 'ABI file area'),
'abi_fixed_grid',
proj_dict,
self.ncols,
self.nlines,
np.asarray(area_extent))
return fg_area_def
@property
def start_time(self):
"""Start time of the current file's observations."""
return datetime.strptime(self.nc.attrs['time_coverage_start'], '%Y-%m-%dT%H:%M:%S.%fZ')
@property
def end_time(self):
"""End time of the current file's observations."""
return datetime.strptime(self.nc.attrs['time_coverage_end'], '%Y-%m-%dT%H:%M:%S.%fZ')
def spatial_resolution_to_number(self):
"""Convert the 'spatial_resolution' global attribute to meters."""
res = self.nc.attrs['spatial_resolution'].split(' ')[0]
if res.endswith('km'):
res = int(float(res[:-2]) * 1000)
elif res.endswith('m'):
res = int(res[:-1])
else:
raise ValueError("Unexpected 'spatial_resolution' attribute '{}'".format(res))
return res
def __del__(self):
"""Close the NetCDF file that may still be open."""
try:
self.nc.close()
except (IOError, OSError, AttributeError):
pass