pyarea 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyarea/__init__.py ADDED
@@ -0,0 +1,4 @@
1
+ from pyarea import calibration, directory, file, nav, navigation, utils
2
+ from pyarea.file import AreaFile
3
+
4
+ __all__ = ['AreaFile', 'calibration', 'directory', 'file', 'nav', 'navigation', 'utils']
pyarea/calibration.py ADDED
@@ -0,0 +1,644 @@
1
+ __author__ = 'jano'
2
+
3
+ import numpy as np
4
+
5
+ from pyarea import utils
6
+ from pyarea.sensors import get_sensor
7
+
8
+
9
+ class Calibrator(object):
10
+
11
+ CAL_NONE = -1
12
+ CAL_MIN = 1
13
+ CAL_RAW = 1
14
+ CAL_RAD = 2
15
+ CAL_ALB = 3
16
+ CAL_TEMP = 4
17
+ CAL_BRIT = 5
18
+ CAL_MAX = 5
19
+ def setCalType(self, calType):
20
+ raise Exception( 'Function selCalType is abstract')
21
+
22
+ def calibrate_pixel(self, inputPixel, band, calTypeOut):
23
+ raise Exception('Function calibrate_pixel is abstract')
24
+
25
+ def calibrate_array(self, input_array, band, calTypeOut):
26
+ raise Exception( 'Function calibrate_array is abstract')
27
+
28
+
29
+ class DefaultCalibrator(Calibrator):
30
+ """The calibrator for sensors without one of their own: values pass through unchanged.
31
+
32
+ Like ``edu.wisc.ssec.mcidas.CalibratorDefault`` every value is returned as it is, pre-calibrated or not, whatever
33
+ ``band`` and ``calTypeOut`` ask for, so a file that already holds TEMP gives its own pixels. One deliberate
34
+ difference: VisAD gives 0 until ``setCalType`` has been called (its calibration type is an unset static field),
35
+ here the values pass through then as well. ``isPreCalibrated`` is only kept for the callers that read it.
36
+ """
37
+ curCalType = 0
38
+ isPreCalibrated = False
39
+
40
+ def setCalType(self, calType):
41
+ """Set the calibration type of the input data; returns 0, or -1 for a type that does not exist."""
42
+ if calType < Calibrator.CAL_MIN or calType > Calibrator.CAL_MAX:
43
+ return -1
44
+ self.curCalType = calType
45
+ return 0
46
+
47
+ def calibrate_pixel(self, inputPixel, band, calTypeOut):
48
+ """
49
+ Calibrate one pixel
50
+ """
51
+ return inputPixel
52
+
53
+ def calibrate_array(self, input_array, band, calTypeOut):
54
+ """:meth:`calibrate_pixel` for an array; returns a float32 array of the same shape."""
55
+ return np.array(input_array, dtype=np.float32)
56
+
57
+ def calibratedList(self, band, isPreCal=None):
58
+ """The calibration types on offer: raw and brightness, however ``isPreCal`` is set."""
59
+ return [self.CAL_RAW, self.CAL_BRIT]
60
+
61
+ def calibratedUnit(self, calType):
62
+ """There is no unit for any type."""
63
+ return None
64
+
65
+
66
+ # Planck constants of the GOES-8 to 15 imagers (5 bands, band 1 is the visible and unused) and sounders (18 bands),
67
+ # keyed by satellite number and then instrument. FK1, FK2, TC1 and TC2 are the McIDAS/VisAD constants
68
+ # (edu.wisc.ssec.mcidas.CalibratorGvarG*) for GOES-12 to 15. VisAD has no GOES-11 class, so GOES-11 comes from SSEC's
69
+ # McIDAS file sndimg11.pfc (BC1 = TC1, BC2 = TC2; sounder channels 1-18, imager channels 22-25 = bands 2-5).
70
+ GVAR_PLANCK_COEFFS = {
71
+ 8: {
72
+ "imager": {
73
+ "FK1": (0.0, 199986.2, 38792.39, 9737.93, 6944.64),
74
+ "FK2": (0.0, 3684.27, 2132.72, 1345.37, 1201.99),
75
+ "TC1": (0.0, 0.6357, 0.606, 0.3735, 0.2217),
76
+ "TC2": (0.0, 0.9991, 0.9986, 0.9987, 0.9992),
77
+ },
78
+ "sounder": {
79
+ "FK1": (
80
+ 3756.81, 4011.1, 4296.87, 4681.13, 4975.25, 5881.41, 6787.44, 8873.71, 12997.94, 28629.32, 34248.3,
81
+ 43114.3, 124235.3, 128123.5, 135148.2, 169167.1, 188235.0, 225794.4
82
+ ),
83
+ "FK2": (
84
+ 3765.12, 3981.16, 4281.88, 4678.91, 4962.59, 5860.42, 6770.32, 8958.91, 12965.93, 28398.28,
85
+ 34201.34, 42525.14, 124057.4, 128011.4, 134849.7, 167814.2, 188801.2, 225856.5
86
+ ),
87
+ "TC1": (
88
+ 0.0123, 0.0133, 0.0186, 0.015, 0.0165, 0.0474, 0.1318, 0.12, 0.0426, 0.1505, 0.2743, 0.1447, 0.0224,
89
+ 0.022, 0.0217, 0.0579, 0.0623, 0.3675
90
+ ),
91
+ "TC2": (
92
+ 0.9999, 0.9999, 0.9999, 0.9999, 0.9999, 0.9998, 0.9995, 0.9996, 0.9999, 0.9996, 0.9993, 0.9997, 1.0,
93
+ 1.0, 1.0, 0.9999, 0.9999, 0.9995
94
+ ),
95
+ },
96
+ },
97
+ 9: {
98
+ "imager": {
99
+ "FK1": (0.0, 198807.8, 38732.41, 9717.21, 6899.47),
100
+ "FK2": (0.0, 3677.02, 2131.62, 1344.41, 1199.38),
101
+ "TC1": (0.0, 0.5864, 0.4841, 0.3622, 0.2014),
102
+ "TC2": (0.0, 0.9992, 0.9989, 0.9988, 0.9992),
103
+ },
104
+ "sounder": {
105
+ "FK1": (
106
+ 3765.12, 3981.16, 4281.88, 4678.91, 4962.59, 5860.42, 6770.32, 8958.91, 12965.93, 28398.28,
107
+ 34201.34, 42525.14, 124057.4, 128011.4, 134849.7, 167814.2, 188801.2, 225856.5
108
+ ),
109
+ "FK2": (
110
+ 980.12, 998.52, 1023.05, 1053.74, 1074.62, 1135.87, 1191.85, 1308.49, 1480.08, 1922.13, 2045.03,
111
+ 2199.04, 3142.14, 3175.18, 3230.74, 3475.05, 3614.26, 3836.74
112
+ ),
113
+ "TC1": (
114
+ 0.0099, 0.0119, 0.0122, 0.0119, 0.0135, 0.044, 0.1345, 0.1193, 0.0407, 0.1438, 0.2762, 0.137,
115
+ 0.0189, 0.0198, 0.0191, 0.0531, 0.0612, 0.3042
116
+ ),
117
+ "TC2": (
118
+ 1.0, 0.9999, 0.9999, 0.9999, 0.9999, 0.9998, 0.9995, 0.9996, 0.9999, 0.9996, 0.9993, 0.9997, 1.0,
119
+ 1.0, 1.0, 0.9999, 0.9999, 0.9996
120
+ ),
121
+ },
122
+ },
123
+ 10: {
124
+ "imager": {
125
+ "FK1": (0.0, 198410.0, 39086.0, 9774.4, 6828.6),
126
+ "FK2": (0.0, 3674.5, 2138.1, 1347.0, 1195.3),
127
+ "TC1": (0.0, 0.62226, 0.61438, 0.27791, 0.21145),
128
+ "TC2": (0.0, 0.99912, 0.99857, 0.99905, 0.99919),
129
+ },
130
+ "sounder": {
131
+ "FK1": (
132
+ 3730.5, 4003.9, 4312.4, 4661.6, 4973.4, 5869.8, 6816.1, 8940.4, 12973.0, 28708.0, 34401.0, 43086.0,
133
+ 124680.0, 128820.0, 135320.0, 168530.0, 188620.0, 224870.0
134
+ ),
135
+ "FK2": (
136
+ 977.1, 1000.4, 1025.5, 1052.4, 1075.4, 1136.5, 1194.5, 1307.6, 1480.4, 1929.1, 2049.0, 2208.7,
137
+ 3147.4, 3181.8, 3234.5, 3480.0, 3613.1, 3831.1
138
+ ),
139
+ "TC1": (
140
+ 0.00988, 0.01196, 0.01245, 0.01245, 0.01366, 0.04311, 0.13973, 0.11707, 0.03979, 0.14968, 0.27603,
141
+ 0.13049, 0.02008, 0.01834, 0.02017, 0.05292, 0.0533, 0.28683
142
+ ),
143
+ "TC2": (
144
+ 0.99995, 0.99994, 0.99994, 0.99995, 0.99994, 0.99983, 0.99947, 0.99959, 0.99988, 0.99962, 0.99933,
145
+ 0.9997, 0.99997, 0.99997, 0.99997, 0.99992, 0.99992, 0.99961
146
+ ),
147
+ },
148
+ },
149
+ 11: {
150
+ "imager": {
151
+ "FK1": (0.0, 200180.0, 38789.0, 9653.4, 6877.8),
152
+ "FK2": (0.0, 3685.4, 2132.7, 1341.5, 1198.1),
153
+ "TC1": (0.0, 0.62864, 0.59339, 0.38284, 0.20258),
154
+ "TC2": (0.0, 0.99911, 0.99861, 0.99869, 0.99923),
155
+ },
156
+ "sounder": {
157
+ "FK1": (
158
+ 3765.6, 3992.8, 4303.9, 4680.6, 4956.3, 5858.9, 6866.8, 8939.5, 13027.0, 28611.0, 34527.0, 43237.0,
159
+ 124990.0, 128850.0, 134490.0, 169500.0, 188730.0, 225760.0
160
+ ),
161
+ "FK2": (
162
+ 980.16, 999.49, 1024.8, 1053.9, 1074.2, 1135.8, 1197.5, 1307.5, 1482.4, 1926.9, 2051.5, 2211.2,
163
+ 3150.0, 3182.1, 3227.8, 3486.7, 3613.8, 3836.2
164
+ ),
165
+ "TC1": (
166
+ 0.00955, 0.01206, 0.0123, 0.01189, 0.01317, 0.04492, 0.1299, 0.118, 0.03855, 0.15092, 0.27383,
167
+ 0.13448, 0.019, 0.01939, 0.02005, 0.04879, 0.05423, 0.29165
168
+ ),
169
+ "TC2": (
170
+ 0.99996, 0.99994, 0.99994, 0.99995, 0.99994, 0.99982, 0.99951, 0.99959, 0.99988, 0.99962, 0.99934,
171
+ 0.99969, 0.99997, 0.99997, 0.99997, 0.99993, 0.99992, 0.9996
172
+ ),
173
+ },
174
+ },
175
+ 12: {
176
+ "imager": {
177
+ "FK1": (0.0, 200960.0, 43702.0, 9685.9, 5047.1),
178
+ "FK2": (0.0, 3690.2, 2219.1, 1343.0, 1080.7),
179
+ "TC1": (0.0, 0.69703, 5.08315, 0.37554, 0.09537),
180
+ "TC2": (0.0, 0.99902, 0.98872, 0.99872, 0.9996),
181
+ },
182
+ "sounder": {
183
+ "FK1": (
184
+ 3777.8, 4008.6, 4308.5, 4704.1, 5013.4, 5864.5, 6907.1, 9038.8, 12972.0, 28931.0, 34531.0, 43340.0,
185
+ 124920.0, 128220.0, 135350.0, 169810.0, 189540.0, 225380.0
186
+ ),
187
+ "FK2": (
188
+ 981.21, 1000.8, 1025.2, 1055.6, 1078.3, 1136.1, 1199.8, 1312.4, 1480.3, 1934.0, 2051.6, 2213.0,
189
+ 3149.4, 3176.9, 3234.7, 3488.8, 3618.9, 3834.0
190
+ ),
191
+ "TC1": (
192
+ 0.0101, 0.01252, 0.01229, 0.01189, 0.01264, 0.04189, 0.13474, 0.12341, 0.03844, 0.15764, 0.2742,
193
+ 0.13683, 0.02124, 0.0178, 0.02037, 0.04933, 0.05386, 0.28872
194
+ ),
195
+ "TC2": (
196
+ 0.99995, 0.99994, 0.99994, 0.99995, 0.99995, 0.99983, 0.99949, 0.99957, 0.99988, 0.9996, 0.99934,
197
+ 0.99969, 0.99996, 0.99997, 0.99997, 0.99993, 0.99992, 0.99961
198
+ ),
199
+ },
200
+ },
201
+ 13: {
202
+ "imager": {
203
+ "FK1": (0.0, 200750.0, 42508.0, 9805.0, 5017.3),
204
+ "FK2": (0.0, 3689.0, 2198.7, 1348.4, 1078.6),
205
+ "TC1": (0.0, 1.4795, 3.96964, 0.3635, 0.09502),
206
+ "TC2": (0.0, 0.99794, 0.99112, 0.99876, 0.9996),
207
+ },
208
+ "sounder": {
209
+ "FK1": (
210
+ 3733.0, 4035.1, 4266.1, 4692.0, 4995.3, 5802.1, 6833.8, 8942.0, 13042.0, 28772.0, 34368.0, 42901.0,
211
+ 125130.0, 128120.0, 134820.0, 168920.0, 189380.0, 226080.0
212
+ ),
213
+ "FK2": (
214
+ 977.32, 1003.0, 1021.8, 1054.7, 1077.0, 1132.1, 1195.6, 1307.7, 1483.0, 1930.5, 2048.3, 2205.5,
215
+ 3151.2, 3176.1, 3230.5, 3482.6, 3617.9, 3838.0
216
+ ),
217
+ "TC1": (
218
+ 0.00944, 0.01022, 0.01011, 0.01291, 0.01353, 0.04272, 0.12493, 0.12033, 0.03838, 0.15609, 0.28,
219
+ 0.18057, 0.01799, 0.01809, 0.02012, 0.05092, 0.0574, 0.29874
220
+ ),
221
+ "TC2": (
222
+ 0.99996, 0.99995, 0.99995, 0.99994, 0.99994, 0.99983, 0.99952, 0.99958, 0.99988, 0.99961, 0.99932,
223
+ 0.99959, 0.99997, 0.99997, 0.99997, 0.99992, 0.99992, 0.99959
224
+ ),
225
+ },
226
+ },
227
+ 14: {
228
+ "imager": {
229
+ "FK1": (0.0, 204320.0, 42187.0, 9721.1, 5070.1),
230
+ "FK2": (0.0, 3710.7, 2192.9, 1344.6, 1082.3),
231
+ "TC1": (0.0, 1.56149, 3.75637, 0.3797, 0.08482),
232
+ "TC2": (0.0, 0.99783, 0.99154, 0.99869, 0.99964),
233
+ },
234
+ "sounder": {
235
+ "FK1": (
236
+ 3748.9, 4008.3, 4269.6, 4695.2, 4978.0, 5807.4, 6839.8, 8972.5, 13085.0, 28920.0, 34362.0, 42901.0,
237
+ 125040.0, 128340.0, 135210.0, 168900.0, 189000.0, 227200.0
238
+ ),
239
+ "FK2": (
240
+ 978.71, 1000.8, 1022.1, 1055.0, 1075.7, 1132.4, 1195.9, 1309.2, 1484.6, 1933.8, 2048.2, 2205.5,
241
+ 3150.4, 3177.9, 3233.6, 3482.5, 3615.5, 3844.3
242
+ ),
243
+ "TC1": (
244
+ 0.00949, 0.01039, 0.01018, 0.01299, 0.01335, 0.04179, 0.12271, 0.11528, 0.0387, 0.14343, 0.27532,
245
+ 0.18229, 0.01783, 0.01744, 0.01906, 0.05207, 0.05324, 0.2968
246
+ ),
247
+ "TC2": (
248
+ 0.99996, 0.99995, 0.99995, 0.99994, 0.99994, 0.99983, 0.99953, 0.9996, 0.99988, 0.99964, 0.99933,
249
+ 0.99958, 0.99997, 0.99997, 0.99997, 0.99992, 0.99992, 0.9996
250
+ ),
251
+ },
252
+ },
253
+ 15: {
254
+ "imager": {
255
+ "FK1": (0.0, 200750.0, 42330.0, 9766.8, 5088.2),
256
+ "FK2": (0.0, 3689.0, 2195.7, 1346.6, 1083.6),
257
+ "TC1": (0.0, 1.59091, 3.75399, 0.38176, 0.08671),
258
+ "TC2": (0.0, 0.99777, 0.99156, 0.9987, 0.99963),
259
+ },
260
+ "sounder": {
261
+ "FK1": (
262
+ 3731.8, 4002.2, 4265.6, 4684.9, 4981.7, 5833.2, 6816.8, 8993.7, 13075.0, 28886.0, 34356.0, 42758.0,
263
+ 124880.0, 128320.0, 135200.0, 169620.0, 188730.0, 227910.0
264
+ ),
265
+ "FK2": (
266
+ 977.22, 1000.3, 1021.8, 1054.2, 1076.0, 1134.1, 1194.6, 1310.2, 1484.2, 1933.1, 2048.1, 2203.0,
267
+ 3149.1, 3177.7, 3233.5, 3487.5, 3613.8, 3848.3
268
+ ),
269
+ "TC1": (
270
+ 0.00909, 0.00998, 0.01021, 0.01327, 0.01318, 0.04219, 0.11811, 0.11003, 0.0389, 0.13744, 0.27731,
271
+ 0.17079, 0.01809, 0.01782, 0.01976, 0.05217, 0.05275, 0.29972
272
+ ),
273
+ "TC2": (
274
+ 0.99996, 0.99995, 0.99995, 0.99994, 0.99994, 0.99983, 0.99955, 0.99962, 0.99988, 0.99965, 0.99933,
275
+ 0.99961, 0.99997, 0.99997, 0.99997, 0.99992, 0.99992, 0.99959
276
+ ),
277
+ },
278
+ },
279
+ }
280
+
281
+ def gvar_sensor(sensor_number):
282
+ """``(satellite number, "imager" or "sounder")`` of a GVAR sensor source number, e.g. ``(13, "imager")``.
283
+
284
+ Taken from :mod:`pyarea.sensors`, so it follows the same sensor table as the directory.
285
+
286
+ Raises:
287
+ ValueError: if the number is not a GOES imager or sounder.
288
+ """
289
+ sensor = get_sensor(sensor_number)
290
+ if (sensor is None or not sensor.platform.startswith("GOES-")
291
+ or sensor.instrument not in ("imager", "sounder")):
292
+ raise ValueError(f"Sensor {sensor_number} is not a GOES imager or sounder.")
293
+ return int(sensor.platform.split("-")[1]), sensor.instrument
294
+
295
+
296
+ _UNITS = {"RAW": Calibrator.CAL_RAW, "RAD": Calibrator.CAL_RAD, "ALB": Calibrator.CAL_ALB,
297
+ "TEMP": Calibrator.CAL_TEMP, "BRIT": Calibrator.CAL_BRIT}
298
+
299
+
300
+ def cal_type_from_name(name):
301
+ """The ``Calibrator.CAL_*`` number for ``RAW``, ``RAD``, ``ALB``, ``TEMP`` or ``BRIT``."""
302
+ try:
303
+ return _UNITS[name.strip().upper()]
304
+ except KeyError:
305
+ raise ValueError(f"Unknown calibration type {name!r}; use one of {', '.join(_UNITS)}.") from None
306
+
307
+
308
+ def calibrated_unit(cal_type):
309
+ """The unit of a calibrated value, or None where there isn't one (counts and brightness)."""
310
+ return {Calibrator.CAL_RAD: "mW/m^2/sr/cm-1", Calibrator.CAL_ALB: "%", Calibrator.CAL_TEMP: "K"}.get(cal_type)
311
+
312
+
313
+ def brit_to_temperature(brit):
314
+ """Brightness (0-255) to temperature in K, the inverse of the IR brightness formula.
315
+
316
+ Like ``CalibratorDefault.convertBritToTemp`` in VisAD the brightness is truncated to an integer, and
317
+ ``(660 - brit) / 2`` is an integer division, so odd brightnesses below 176 lose half a kelvin (1 gives 329 K).
318
+ """
319
+ brit = np.trunc(np.asarray(brit, dtype=np.float32))
320
+ return np.where(brit > 176, 418.0 - brit, np.trunc((660.0 - brit) / 2.0))
321
+
322
+
323
+ def temperature_to_brit(temperature):
324
+ """Temperature in K to brightness: ``660 - 2T`` from 242 K up, ``418 - T`` below; NaN stays NaN."""
325
+ t = np.asarray(temperature, dtype=np.float64)
326
+ return np.where(t >= 242.0, np.maximum(660.0 - np.trunc(t * 2.0), 0.0), np.minimum(418.0 - np.trunc(t), 255.0))
327
+
328
+
329
+ class GVARCalibrator(Calibrator):
330
+ """Calibrate GVAR (GOES-8 onward) counts from the coefficients in the AREA calibration block.
331
+
332
+ A numpy port of ``edu.wisc.ssec.mcidas.CalibratorGvar`` (VisAD): bias and gain coefficients are read
333
+ from the block in Gould floating point format; the detector coefficients are averaged for the visible,
334
+ and the first detector's are used for the IR bands.
335
+
336
+ Args:
337
+ sensorId: The sensor source number; its satellite and instrument come from :mod:`pyarea.sensors`.
338
+ calBlock: The calibration block as 32-bit words, or None for data that is already calibrated.
339
+ """
340
+
341
+ NUM_BANDS_IMAGER = 5
342
+ NUM_BANDS_SOUNDER = 18
343
+
344
+ NUM_VIS_DETECTORS = 8
345
+ NUM_IR_DETECTORS = 2
346
+ NUM_IR_BANDS = 4
347
+
348
+ def __init__(self, sensorId=None, calBlock=None):
349
+ self.sid = sensorId
350
+ self.satellite, instrument = gvar_sensor(sensorId)
351
+ self.imager = instrument == "imager"
352
+ self.curCalType = self.CAL_RAW
353
+ self.isPreCalibrated = calBlock is None
354
+ self.scale = 32 if self.imager else 2
355
+ if calBlock is not None:
356
+ self._read_cal_block([utils.gould2native(int(word) & 0xFFFFFFFF) for word in calBlock])
357
+
358
+ def _read_cal_block(self, values):
359
+ """Split the block's coefficients like ``CalibratorGvar.initGvar``."""
360
+ n_vis = self.NUM_VIS_DETECTORS if self.imager else self.NUM_VIS_DETECTORS // 2
361
+ need = 3 * n_vis + 1 + (2 * self.NUM_IR_DETECTORS * self.NUM_IR_BANDS if self.imager
362
+ else 2 * self.NUM_BANDS_SOUNDER)
363
+ if len(values) < need:
364
+ raise ValueError(f"The GVAR calibration block has {len(values)} words, but {need} are needed.")
365
+ values = np.asarray(values, dtype=np.float64)
366
+ self.visBiasCoef = values[0:n_vis]
367
+ self.visGain1Coef = values[n_vis:2 * n_vis]
368
+ self.visGain2Coef = values[2 * n_vis:3 * n_vis]
369
+ self.visBiasCoefAVG = self.visBiasCoef.mean()
370
+ self.visGain1CoefAVG = self.visGain1Coef.mean()
371
+ self.visGain2CoefAVG = self.visGain2Coef.mean()
372
+ self.visRadToAlb = values[3 * n_vis]
373
+ rest = values[3 * n_vis + 1:]
374
+ if self.imager:
375
+ # the IR bands are stored rotated by two for satellites before GOES-12
376
+ ir_offset = 0 if self.satellite >= 12 else 2
377
+ order = [(i + ir_offset) % self.NUM_IR_BANDS for i in range(self.NUM_IR_BANDS)]
378
+ ir = rest[:4 * self.NUM_IR_BANDS].reshape(4, self.NUM_IR_BANDS)
379
+ self.irBiasCoef = np.empty((self.NUM_IR_DETECTORS, self.NUM_IR_BANDS))
380
+ self.irGainCoef = np.empty((self.NUM_IR_DETECTORS, self.NUM_IR_BANDS))
381
+ for det in range(self.NUM_IR_DETECTORS):
382
+ self.irBiasCoef[det, order] = ir[det]
383
+ self.irGainCoef[det, order] = ir[self.NUM_IR_DETECTORS + det]
384
+ else:
385
+ self.sBiasCoef = rest[:self.NUM_BANDS_SOUNDER]
386
+ self.sGainCoef = rest[self.NUM_BANDS_SOUNDER:2 * self.NUM_BANDS_SOUNDER]
387
+
388
+ def setCalType(self, calType):
389
+ """Set the calibration type of the input data; anything but RAW is passed through unchanged."""
390
+ if calType < Calibrator.CAL_MIN or calType > Calibrator.CAL_MAX:
391
+ return -1
392
+ self.curCalType = calType
393
+ return 0
394
+
395
+ def _gain_bias(self, band):
396
+ if self.imager:
397
+ if band == 6: # the 13 micron band of GOES-12 onward uses band 5's coefficients
398
+ band = 5
399
+ return self.irGainCoef[0, band - 2], self.irBiasCoef[0, band - 2]
400
+ return self.sGainCoef[band - 1], self.sBiasCoef[band - 1]
401
+
402
+ def _planck(self, band):
403
+ satellite = self.satellite
404
+ if satellite not in GVAR_PLANCK_COEFFS:
405
+ raise NotImplementedError(f"No Planck constants for GOES-{satellite}; only GOES-8 to 15 are available.")
406
+ instrument = "imager" if self.imager else "sounder"
407
+ if instrument not in GVAR_PLANCK_COEFFS[satellite]:
408
+ raise NotImplementedError(f"No {instrument} Planck constants for GOES-{satellite}.")
409
+ table = GVAR_PLANCK_COEFFS[satellite][instrument]
410
+ index = (5 if band == 6 else band) - 1 if self.imager else band - 1
411
+ return tuple(table[name][index] for name in ("FK1", "FK2", "TC1", "TC2"))
412
+
413
+ def radToTemp(self, inval=None, band=None, sId=None):
414
+ """Radiance to temperature in K: ``T = (FK2 / ln(FK1 / R + 1) - TC1) / TC2``."""
415
+ fk1, fk2, tc1, tc2 = self._planck(band)
416
+ radiance = np.asarray(inval, dtype=np.float64)
417
+ with np.errstate(invalid="ignore", divide="ignore"):
418
+ expn = fk1 / radiance + 1.0
419
+ temperature = np.where(expn > 0, (fk2 / np.log(expn) - tc1) / tc2, np.nan)
420
+ return temperature
421
+
422
+ def _is_visible(self, band):
423
+ return band == (1 if self.imager else 19)
424
+
425
+ def _albedo(self, counts):
426
+ """Visible albedo as a fraction, from counts."""
427
+ f = counts / self.scale
428
+ g2 = f ** self.visGain2CoefAVG if abs(self.visGain2CoefAVG) > 0.0001 else 1.0
429
+ return np.maximum((g2 + f * self.visGain1CoefAVG + self.visBiasCoefAVG) * self.visRadToAlb, 0.0)
430
+
431
+ def calibrate_array(self, input_array, band, calTypeOut):
432
+ """Convert counts to ``calTypeOut`` (a ``Calibrator.CAL_*`` number) for ``band``; float32 array.
433
+
434
+ Visible bands can give RAW, ALB (percent) or BRIT; IR bands RAW, RAD, TEMP (K) or BRIT.
435
+ Input that is not RAW is passed through, as is everything when no calibration block is present.
436
+ """
437
+ counts = np.asarray(input_array, dtype=np.float64)
438
+ if self.curCalType != self.CAL_RAW or self.isPreCalibrated or calTypeOut == self.CAL_RAW:
439
+ return counts.astype(np.float32)
440
+ if self._is_visible(band):
441
+ if calTypeOut not in (self.CAL_ALB, self.CAL_BRIT):
442
+ raise ValueError(f"Band {band} is visible; it can be calibrated to ALB or BRIT, not {calTypeOut}.")
443
+ albedo = self._albedo(counts)
444
+ if calTypeOut == self.CAL_ALB:
445
+ return (albedo * 100.0).astype(np.float32)
446
+ return np.minimum(np.floor(np.sqrt(100.0 * albedo) * 25.5 + 0.5), 255.0).astype(np.float32)
447
+ if calTypeOut == self.CAL_ALB:
448
+ raise ValueError(f"Band {band} is infrared; it can be calibrated to RAD, TEMP or BRIT, not ALB.")
449
+ gain, bias = self._gain_bias(band)
450
+ radiance = ((counts / self.scale if self.imager else counts) - bias) / gain
451
+ if calTypeOut == self.CAL_RAD:
452
+ return radiance.astype(np.float32)
453
+ temperature = self.radToTemp(radiance, band, self.sid)
454
+ if calTypeOut == self.CAL_TEMP:
455
+ return temperature.astype(np.float32)
456
+ return temperature_to_brit(temperature).astype(np.float32)
457
+
458
+ def calibrate_pixel(self, inputPixel, band, calTypeOut):
459
+ """:meth:`calibrate_array` for a single value."""
460
+ return float(self.calibrate_array(np.asarray([inputPixel]), band, calTypeOut)[0])
461
+
462
+ def calibratedList(self, band, isPreCal=None):
463
+ """The calibration types available for ``band``."""
464
+ isPreCal = self.isPreCalibrated if isPreCal is None else isPreCal
465
+ visible = band in (1, 12)
466
+ if isPreCal:
467
+ return [self.CAL_RAW, self.CAL_BRIT] if visible else [self.CAL_RAW, self.CAL_TEMP, self.CAL_BRIT]
468
+ if visible:
469
+ return [self.CAL_RAW, self.CAL_ALB, self.CAL_BRIT]
470
+ return [self.CAL_RAW, self.CAL_TEMP, self.CAL_RAD, self.CAL_BRIT]
471
+
472
+
473
+ class G8GVARCalibrator(GVARCalibrator):
474
+ """GOES-8 (sensors 70 and 71). The constants for every GVAR satellite are in ``GVAR_PLANCK_COEFFS``."""
475
+
476
+
477
+ class G9GVARCalibrator(GVARCalibrator):
478
+ """GOES-9 (sensors 72 and 73)."""
479
+
480
+
481
+ class G10GVARCalibrator(GVARCalibrator):
482
+ """GOES-10 (sensors 74 and 75)."""
483
+
484
+
485
+ # ---------------------------------------------------------------------------
486
+ # Pre-GVAR (SMS/GOES-1..7 VISSR/VAS) calibration.
487
+
488
+ class GOESCalibrator(Calibrator):
489
+ """Calibration of the pre-GVAR GOES (SMS/GOES-1 to 7) VISSR and VAS data, like :class:`GVARCalibrator` for GVAR.
490
+
491
+ Three kinds of file need three calibrations, and each needs different parts of the file, so unlike
492
+ :class:`GVARCalibrator` this class has no per-file state: the calibrations are static methods that take the
493
+ counts and whatever coefficients the file holds (``AreaFile.read_*`` supplies them).
494
+
495
+ * Mode A (source type VISR): 8-bit counts, brightness temperature from the documented BRIT formula
496
+ (:meth:`mode_a_ir_temperature`) and a relative visible reflectance (:meth:`vis_unnormalized_reflectance`).
497
+ * Mode AA (VAS, GOES-4/5/6, 1980-86): per-line offset and gain entries in the line prefix, Planck-inverted
498
+ with the McIDAS vastbl constants (:meth:`mode_aa_temperature`).
499
+ * Mode AAA (GOES-6/7, 1987-94): per-channel coefficients in the CAL block (:meth:`aaa_radiance`,
500
+ :meth:`aaa_temperature`).
501
+
502
+ Constants courtesy of SSEC via NOAA CLASS.
503
+ """
504
+
505
+ # channel code (1-38) -> IR band (1-12); index 0 is code 1
506
+ MODE_AA_CHANNEL_BANDS = tuple(range(1, 13)) * 2 + (3, 4, 5, 7, 8, 9, 10) * 2
507
+ # deltaF prefix code (0-15) -> actual deltaF (-5..+5)
508
+ MODE_AA_DELTA_F = (0, -1, -2, -3, -4, -5, 0, 0, 0, 1, 2, 3, 4, 5, 0, 0)
509
+ # band -> (F, FK1, FK2, TC1, TC2)
510
+ MODE_AA_COEFFS = {
511
+ 1: (7, 3723, 976.5, -0.0003905001, 1.0),
512
+ 2: (7, 3910, 992.5, 0.002225, 1.0),
513
+ 3: (7, 4097, 1008, 0.00236, 1.0),
514
+ 4: (7, 4352, 1029, 0.004170999, 0.9999),
515
+ 5: (7, 5049, 1081, 0.004202001, 0.9999),
516
+ 6: (11, 128100, 3176, 0.06761003, 0.9998),
517
+ 7: (7, 5874, 1137, 0.004214, 0.9999),
518
+ 8: (7, 8512, 1286, 0.3212, 0.9974),
519
+ 9: (8, 30940, 1978, 0.06927001, 0.9997),
520
+ 10: (8, 38880, 2134, 0.7513, 0.9973),
521
+ 11: (11, 136000, 3240, 0.05738, 0.9999),
522
+ 12: (13, 195900, 3659, 0.4237, 0.9991),
523
+ }
524
+
525
+ # Mode AAA channel -> (FK1, FK2, TC1, TC2). Only channels 1-12 are confirmed;
526
+ # channel 13 borrows channel 8's constants.
527
+ PLANCK_COEFFS_GOES6 = {
528
+ 1: (3716.0, 975.8, -0.0002859, 1.0),
529
+ 2: (3936.0, 994.8, 0.002472, 1.0),
530
+ 3: (4089.0, 1007.0, 0.002334, 1.0),
531
+ 4: (4336.0, 1027.0, 0.003948, 0.9999),
532
+ 5: (5019.0, 1079.0, 0.004031, 0.9999),
533
+ 6: (128000.0, 3175.0, 0.06857, 0.9998),
534
+ 7: (5855.0, 1136.0, 0.004256, 0.9999),
535
+ 8: (8438.0, 1283.0, 0.3263, 0.9973),
536
+ 9: (31030.0, 1980.0, 0.06822, 0.9997),
537
+ 10: (38790.0, 2133.0, 0.736, 0.9973),
538
+ 11: (136100.0, 3241.0, 0.05787, 0.9999),
539
+ 12: (195000.0, 3653.0, 0.4213, 0.9991),
540
+ }
541
+ PLANCK_COEFFS_GOES7 = {
542
+ 1: (3767.0, 980.3, -0.001267, 1.0),
543
+ 2: (3940.0, 995.1, 0.00318, 0.9999),
544
+ 3: (4167.0, 1014.0, 0.002383, 1.0),
545
+ 4: (4364.0, 1030.0, 0.002739, 0.9999),
546
+ 5: (5025.0, 1079.0, 0.00536, 0.9999),
547
+ 6: (129100.0, 3185.0, 0.07353, 0.9998),
548
+ 7: (5851.0, 1135.0, 0.004447, 0.9999),
549
+ 8: (8525.0, 1287.0, 0.3408, 0.9973),
550
+ 9: (31070.0, 1981.0, 0.06478, 0.9998),
551
+ 10: (39200.0, 2140.0, 0.6448, 0.9977),
552
+ 11: (132500.0, 3212.0, 0.0583, 0.9999),
553
+ 12: (193200.0, 3642.0, 0.415, 0.9991),
554
+ }
555
+ PLANCK_COEFFS_BY_SATELLITE = {
556
+ "GOES-6": PLANCK_COEFFS_GOES6,
557
+ "GOES-7": PLANCK_COEFFS_GOES7,
558
+ }
559
+
560
+ @classmethod
561
+ def vas_planck_coeffs(cls, satellite, channel):
562
+ """Return ``(FK1, FK2, TC1, TC2)`` for a Mode AAA channel of ``satellite``."""
563
+ table = cls.PLANCK_COEFFS_BY_SATELLITE.get(satellite)
564
+ if table is None:
565
+ raise NotImplementedError(
566
+ f"No verified VAS Planck-inversion constants for satellite {satellite!r} "
567
+ f"(only GOES-6 and GOES-7 are confirmed)."
568
+ )
569
+ planck_channel = 8 if channel == 13 else channel
570
+ if planck_channel not in table:
571
+ raise NotImplementedError(
572
+ f"No verified Planck-inversion constants for channel {channel} on {satellite} "
573
+ f"(only channels 1-12 have been checked against real data)."
574
+ )
575
+ return table[planck_channel]
576
+
577
+ @staticmethod
578
+ def mode_a_ir_temperature(counts):
579
+ """Mode A (VISR) IR counts to brightness temperature in K: ``418 - B`` for B >= 176, else ``330 - B/2``."""
580
+ counts = np.asarray(counts, dtype=np.float32)
581
+ return np.where(counts >= 176, 418.0 - counts, 330.0 - counts / 2.0)
582
+
583
+ @staticmethod
584
+ def vis_unnormalized_reflectance(counts):
585
+ """VIS counts to a relative reflectance in percent: ``(counts / 255) ** 2 * 100``, clipped to 0-100.
586
+
587
+ The visible counts are 6 bits shifted left by two, so they run from 0 to 252 on the 0-255 brightness scale
588
+ of McIDAS. The VAS visible channel does not output counts proportional to radiance but to its square root,
589
+ so that the photomultiplier noise, which grows with the square root of the radiance, is about the same in
590
+ counts at every level (Kidder and Vonder Haar, 1995; Frouin and Gautier, 1986).
591
+ Squaring the counts undoes that, in the form of the GOES-R fire algorithm's "visible brightness"
592
+ (``int(sqrt(reflectivity) * 255)``, its ATBD): the largest count, 252, gives 97.7 %.
593
+
594
+ The result is relative, not an absolute reflectance; the gain and intercept are not implemented and the
595
+ solar zenith angle is not corrected for.
596
+ """
597
+ fraction = np.clip(np.asarray(counts, dtype=np.float32) / 255.0, 0.0, 1.0)
598
+ return (fraction ** 2 * 100.0).astype(np.float32)
599
+
600
+ @classmethod
601
+ def mode_aa_code_to_band(cls, codes):
602
+ """Map Mode AA channel codes (1-38) to IR bands (1-12); anything else (e.g. unused 0) maps to 0."""
603
+ codes = np.asarray(codes)
604
+ lookup = np.array((0,) + cls.MODE_AA_CHANNEL_BANDS)
605
+ out = np.zeros_like(codes)
606
+ valid = (codes >= 1) & (codes <= 38)
607
+ out[valid] = lookup[codes[valid]]
608
+ return out
609
+
610
+ @classmethod
611
+ def mode_aa_temperature(cls, raw, delta_f, ysubz, band):
612
+ """Mode AA brightness temperature (K) for IR ``band`` from raw counts and per-line offset/gain.
613
+
614
+ Args:
615
+ raw: Raw counts, shape (lines, elements).
616
+ delta_f: Per-line deltaF, shape (lines,).
617
+ ysubz: Per-line zero offset, shape (lines,).
618
+ band: IR band 1-12.
619
+
620
+ Returns:
621
+ float32 array, NaN where the raw count is 0 or the corrected count rounds down to 0.
622
+ """
623
+ f, fk1, fk2, tc1, tc2 = cls.MODE_AA_COEFFS[band]
624
+ corrected = np.maximum(0.0, raw - ysubz[:, None])
625
+ corrected = 8.0 * np.floor(corrected / 8.0)
626
+ radiance = corrected * (2.0 ** (delta_f[:, None] - f))
627
+ with np.errstate(invalid="ignore", divide="ignore"):
628
+ t = (fk2 / np.log(fk1 / radiance + 1.0) - tc1) / tc2
629
+ return np.where((raw == 0) | (corrected == 0), np.nan, t).astype(np.float32)
630
+
631
+ @staticmethod
632
+ def aaa_radiance(counts, ab1, ab2, ifab):
633
+ """Mode AAA counts to radiance using the CAL block's AB1, AB2 and IFAB for the channel."""
634
+ return ((ab2 * np.asarray(counts, dtype=np.float64) / 32.0 - ab1) / 2.0 ** (15 - ifab)).astype(np.float32)
635
+
636
+ @staticmethod
637
+ def aaa_temperature(radiance, coeffs):
638
+ """Planck-invert Mode AAA radiance with ``(FK1, FK2, TC1, TC2)``; NaN where the inversion is undefined."""
639
+ fk1, fk2, tc1, tc2 = coeffs
640
+ r = np.asarray(radiance, dtype=np.float64)
641
+ with np.errstate(invalid="ignore", divide="ignore"):
642
+ expn = fk1 / r + 1.0
643
+ t = fk2 / np.log(expn)
644
+ return np.where((r > -fk1) & (expn > 0), (t - tc1) / tc2, np.nan).astype(np.float32)