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 +4 -0
- pyarea/calibration.py +644 -0
- pyarea/directory.py +403 -0
- pyarea/file.py +752 -0
- pyarea/nav.py +2837 -0
- pyarea/navigation/__init__.py +3 -0
- pyarea/navigation/area_navigation.py +183 -0
- pyarea/navigation/hrit_navigation.py +475 -0
- pyarea/navigation/proj4_navigation.py +107 -0
- pyarea/sensors.py +249 -0
- pyarea/utils.py +316 -0
- pyarea-0.2.0.dist-info/METADATA +156 -0
- pyarea-0.2.0.dist-info/RECORD +16 -0
- pyarea-0.2.0.dist-info/WHEEL +5 -0
- pyarea-0.2.0.dist-info/licenses/LICENSE +165 -0
- pyarea-0.2.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
__author__ = 'jano'
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
This module contains navigation functions and constants to reproject
|
|
5
|
+
ata using Proj4 library form geostationary projection GEOS(SSP) to LATLON
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import math
|
|
12
|
+
|
|
13
|
+
import numpy as np
|
|
14
|
+
import pyproj
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def lc2YX(l=None,c=None,gt=None):
|
|
18
|
+
|
|
19
|
+
xmin, xres, _, ymax, __, yres = gt
|
|
20
|
+
return ymax + l*yres+yres/2., xmin + c*xres + xres/2.
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def YX2lc(Y=None, X=None, gt=None):
|
|
24
|
+
"""
|
|
25
|
+
Given a GDAL GeoTransform http://www.gdal.org/gdal_datamodel.html and an image of certain size (nl, nc)
|
|
26
|
+
converts Geostationary coordinates Y and X into raw|pixel coordinates (lc)
|
|
27
|
+
|
|
28
|
+
:param Y: number, float, froy axis geostationary coordinate
|
|
29
|
+
:param X: number, float, x axis geostationary coordinate
|
|
30
|
+
:param gt: iterable, GDAL geotransform
|
|
31
|
+
|
|
32
|
+
:return: tuple of ints representing the line&column coordinates corresponding to Y and X
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
xmin, xres, _, ymax, __, yres = gt
|
|
36
|
+
ty = ((Y-ymax)/yres) + .5
|
|
37
|
+
tx = ((X-xmin)/xres) + .5
|
|
38
|
+
try:
|
|
39
|
+
rc = int(math.floor(ty)), int(math.floor(tx))
|
|
40
|
+
except TypeError:
|
|
41
|
+
rc = np.floor(ty).astype(np.uint32), np.floor(tx).astype(np.uint32)
|
|
42
|
+
return rc
|
|
43
|
+
|
|
44
|
+
def lc2ll(l=None, c=None, ssp=None, gt=None):
|
|
45
|
+
Y,X = lc2YX(l=l, c=c, gt=gt)
|
|
46
|
+
return YX2ll(Y=Y, X=X, ssp=ssp)
|
|
47
|
+
|
|
48
|
+
def YX2ll(Y=None, X=None, ssp=None):
|
|
49
|
+
proj4_str = '+proj=geos +a=6378169.0 +b=6356583.8 +h=35785831.0 +lon_0=%.2f +units=m' % ssp
|
|
50
|
+
geosp = pyproj.Proj(proj4_str)
|
|
51
|
+
lon, lat = geosp(X,Y,inverse=True)
|
|
52
|
+
lat = np.where(lat==1e30,np.nan, lat)
|
|
53
|
+
lon = np.where(lon==1e30,np.nan, lon)
|
|
54
|
+
return lat, lon
|
|
55
|
+
|
|
56
|
+
def ll2YX(lat=None, lon=None, ssp=None):
|
|
57
|
+
"""
|
|
58
|
+
COnverts lat, lon coordinates to GEOS(ssp) projection using Proj4 library
|
|
59
|
+
|
|
60
|
+
:param lat: number or numpy array, input latitude,
|
|
61
|
+
:param lon:, number or numpy array, input longitude
|
|
62
|
+
:param ssp: number, float, sub-satellite point
|
|
63
|
+
:return:
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
proj4_str = '+proj=geos +a=6378169.0 +b=6356583.8 +h=35785831.0 +lon_0=%.2f +units=m' % ssp
|
|
67
|
+
geosp = pyproj.Proj(proj4_str)
|
|
68
|
+
try: # fix the bug in Proj4 that Tomas run into. The bug is related to not
|
|
69
|
+
shp=lon.shape
|
|
70
|
+
X, Y = geosp(list(lon.flatten()), list(lat.flatten()))
|
|
71
|
+
X = np.array(X).reshape(shp)
|
|
72
|
+
Y = np.array(Y).reshape(shp)
|
|
73
|
+
return Y, X
|
|
74
|
+
except AttributeError:
|
|
75
|
+
X, Y = geosp(lon, lat)
|
|
76
|
+
return Y,X
|
|
77
|
+
|
|
78
|
+
def ll2lc(lat=None, lon=None, gt=None, ssp=None):
|
|
79
|
+
"""
|
|
80
|
+
Converts LATLON coordinates into line/column (RAW) coordinates using Proj4 lib
|
|
81
|
+
:param lat: number of numpy array, input latitude
|
|
82
|
+
:param lon: number or numpy array, input longitude
|
|
83
|
+
:param gt:, iter, GDAL geotransform in the source (GEOS) projection
|
|
84
|
+
:param ssp: number , float, sub-satellite point
|
|
85
|
+
|
|
86
|
+
:return:
|
|
87
|
+
"""
|
|
88
|
+
Y,X = ll2YX(lat=lat, lon=lon,ssp=ssp)
|
|
89
|
+
return YX2lc(Y=Y, X=X,gt=gt)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def reproject_geos_2_latlon(array_in=None, bbox=None, ssp=None, gt=None):
|
|
93
|
+
"""
|
|
94
|
+
Reprojects a geostationary image from GEOS(SSP) to LATLON using PROJ4
|
|
95
|
+
|
|
96
|
+
:param array_in: 2D numpy array, input image
|
|
97
|
+
:param bbox: instance of bounding_box object, the pyarea in geographic coordinates that will be reprojected from the input image
|
|
98
|
+
:param ssp: number, float, input image sub-satellite point in degrees
|
|
99
|
+
:param gt: inter, GDAL geotransform
|
|
100
|
+
:return: 2D numpy array representing the reprojected input image
|
|
101
|
+
"""
|
|
102
|
+
lats = bbox.latitudes(array2d=True)
|
|
103
|
+
lons = bbox.longitudes(array2d=True)
|
|
104
|
+
|
|
105
|
+
l, c = ll2lc(lat=lats, lon=lons, gt=gt, ssp=ssp)
|
|
106
|
+
|
|
107
|
+
return array_in[l,c]
|
pyarea/sensors.py
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
"""The AREA sensor source numbers (word 3 of the directory) and what they stand for.
|
|
2
|
+
|
|
3
|
+
The numbers and names are those of ``AreaDirectory.java`` in VisAD/McIDAS-V, brought up to date with Appendix C
|
|
4
|
+
("Sensor Source Numbers") of the McIDAS Users Guide, which also lists the newer satellites; see also Appendix A of the
|
|
5
|
+
`SSEC AREA format <https://www.ssec.wisc.edu/mcidas/doc/misc_doc/area2.html>`_ document. The names are
|
|
6
|
+
parsed once into a platform and an instrument/band, so the rest of the package never has to look at text.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import re
|
|
10
|
+
from typing import NamedTuple
|
|
11
|
+
|
|
12
|
+
SENSOR_NAMES = {
|
|
13
|
+
0: "Derived Data",
|
|
14
|
+
1: "Test Patterns",
|
|
15
|
+
2: "Graphics",
|
|
16
|
+
3: "Miscellaneous",
|
|
17
|
+
4: "PDUS Meteosat visible",
|
|
18
|
+
5: "PDUS Meteosat infrared",
|
|
19
|
+
6: "PDUS Meteosat water vapor",
|
|
20
|
+
7: "Radar",
|
|
21
|
+
8: "Miscellaneous Aircraft Data",
|
|
22
|
+
9: "Raw Meteosat",
|
|
23
|
+
10: "Composite image",
|
|
24
|
+
11: "Topography image",
|
|
25
|
+
12: "GMS visible",
|
|
26
|
+
13: "GMS infrared",
|
|
27
|
+
14: "ATS 6 visible",
|
|
28
|
+
15: "ATS 6 infrared",
|
|
29
|
+
16: "SMS-1 visible",
|
|
30
|
+
17: "SMS-1 infrared",
|
|
31
|
+
18: "SMS-2 visible",
|
|
32
|
+
19: "SMS-2 infrared",
|
|
33
|
+
20: "GOES-1 visible",
|
|
34
|
+
21: "GOES-1 infrared",
|
|
35
|
+
22: "GOES-2 visible",
|
|
36
|
+
23: "GOES-2 infrared",
|
|
37
|
+
24: "GOES-3 visible",
|
|
38
|
+
25: "GOES-3 infrared",
|
|
39
|
+
26: "GOES-4 visible (VAS)",
|
|
40
|
+
27: "GOES-4 infrared and water vapor (VAS)",
|
|
41
|
+
28: "GOES-5 visible (VAS)",
|
|
42
|
+
29: "GOES-5 infrared and water vapor (VAS)",
|
|
43
|
+
30: "GOES-6 visible",
|
|
44
|
+
31: "GOES-6 infrared",
|
|
45
|
+
32: "GOES-7 visible, Block 1 Auxiliary Data",
|
|
46
|
+
33: "GOES-7 infrared",
|
|
47
|
+
34: "FY-2B",
|
|
48
|
+
35: "FY-2C",
|
|
49
|
+
36: "FY-2D",
|
|
50
|
+
37: "FY-2E",
|
|
51
|
+
38: "FY-2F",
|
|
52
|
+
39: "FY-2G",
|
|
53
|
+
40: "FY-2H",
|
|
54
|
+
41: "TIROS-N (POES)",
|
|
55
|
+
42: "NOAA-6",
|
|
56
|
+
43: "NOAA-7",
|
|
57
|
+
44: "NOAA-8",
|
|
58
|
+
45: "NOAA-9",
|
|
59
|
+
46: "Venus",
|
|
60
|
+
47: "Voyager 1",
|
|
61
|
+
48: "Voyager 2",
|
|
62
|
+
49: "Galileo",
|
|
63
|
+
50: "Hubble space telescope",
|
|
64
|
+
51: "Meteosat-8 (MSG-1)",
|
|
65
|
+
52: "Meteosat-9 (MSG-2)",
|
|
66
|
+
53: "Meteosat-10 (MSG-3)",
|
|
67
|
+
54: "Meteosat-3",
|
|
68
|
+
55: "Meteosat-4",
|
|
69
|
+
56: "Meteosat-5",
|
|
70
|
+
57: "Meteosat-6",
|
|
71
|
+
58: "Meteosat-7",
|
|
72
|
+
60: "NOAA-10",
|
|
73
|
+
61: "NOAA-11",
|
|
74
|
+
62: "NOAA-12",
|
|
75
|
+
63: "NOAA-13",
|
|
76
|
+
64: "NOAA-14",
|
|
77
|
+
65: "NOAA-15",
|
|
78
|
+
66: "NOAA-16",
|
|
79
|
+
67: "NOAA-17",
|
|
80
|
+
68: "NOAA-18",
|
|
81
|
+
69: "NOAA-19",
|
|
82
|
+
70: "GOES 8 imager",
|
|
83
|
+
71: "GOES 8 sounder",
|
|
84
|
+
72: "GOES 9 imager",
|
|
85
|
+
73: "GOES 9 sounder",
|
|
86
|
+
74: "GOES 10 imager",
|
|
87
|
+
75: "GOES 10 sounder",
|
|
88
|
+
76: "GOES 11 imager",
|
|
89
|
+
77: "GOES 11 sounder",
|
|
90
|
+
78: "GOES 12 imager",
|
|
91
|
+
79: "GOES 12 sounder",
|
|
92
|
+
80: "ERBE",
|
|
93
|
+
82: "GMS-4",
|
|
94
|
+
83: "GMS-5",
|
|
95
|
+
84: "MTSAT-1R",
|
|
96
|
+
85: "MTSAT-2",
|
|
97
|
+
86: "Himawari-8",
|
|
98
|
+
87: "DMSP F-8",
|
|
99
|
+
88: "DMSP F-9",
|
|
100
|
+
89: "DMSP F-10",
|
|
101
|
+
90: "DMSP F-11",
|
|
102
|
+
91: "DMSP F-12",
|
|
103
|
+
92: "DMSP F-13",
|
|
104
|
+
93: "DMSP F-14",
|
|
105
|
+
94: "DMSP F-15",
|
|
106
|
+
95: "FY-1B",
|
|
107
|
+
96: "FY-1C",
|
|
108
|
+
97: "FY-1D",
|
|
109
|
+
101: "TERRA L1B",
|
|
110
|
+
102: "TERRA CLD",
|
|
111
|
+
103: "TERRA GEO",
|
|
112
|
+
104: "TERRA-AER",
|
|
113
|
+
106: "TERRA TOP",
|
|
114
|
+
107: "TERRA ATM",
|
|
115
|
+
108: "TERRA GUS",
|
|
116
|
+
109: "TERRA RET",
|
|
117
|
+
111: "AQUA L1B",
|
|
118
|
+
112: "AQUA CLD",
|
|
119
|
+
113: "AQUA GEO",
|
|
120
|
+
114: "AQUA AER",
|
|
121
|
+
116: "AQUA TOP",
|
|
122
|
+
117: "AQUA ATM",
|
|
123
|
+
118: "AQUA GUS",
|
|
124
|
+
119: "AQUA RET",
|
|
125
|
+
128: "TERRA-SST",
|
|
126
|
+
129: "TERRA-LST",
|
|
127
|
+
138: "AQUA-SST",
|
|
128
|
+
139: "AQUA-LST",
|
|
129
|
+
160: "TERRA NDVI",
|
|
130
|
+
161: "TERRA CREF",
|
|
131
|
+
170: "AQUA NDVI",
|
|
132
|
+
171: "AQUA CREF",
|
|
133
|
+
174: "EWS-G1 imager",
|
|
134
|
+
175: "EWS-G1 sounder",
|
|
135
|
+
176: "EWS-G2 imager",
|
|
136
|
+
177: "EWS-G2 sounder",
|
|
137
|
+
178: "EWS-G3 imager",
|
|
138
|
+
179: "EWS-G3 sounder",
|
|
139
|
+
180: "GOES 13 imager",
|
|
140
|
+
181: "GOES 13 sounder",
|
|
141
|
+
182: "GOES 14 imager",
|
|
142
|
+
183: "GOES 14 sounder",
|
|
143
|
+
184: "GOES 15 imager",
|
|
144
|
+
185: "GOES 15 sounder",
|
|
145
|
+
186: "GOES 16 imager",
|
|
146
|
+
187: "GOES-16 Level 2 Products",
|
|
147
|
+
188: "GOES 17 imager",
|
|
148
|
+
189: "GOES-17 Level 2 Products",
|
|
149
|
+
190: "GOES 18 imager",
|
|
150
|
+
191: "GOES-18 Level 2 Products",
|
|
151
|
+
192: "GOES 19 imager",
|
|
152
|
+
193: "GOES-19 Level 2 Products",
|
|
153
|
+
195: "DMSP F-16",
|
|
154
|
+
196: "DMSP F-17",
|
|
155
|
+
200: "AIRS L1B",
|
|
156
|
+
210: "AMSR-E L1B",
|
|
157
|
+
211: "AMSR-E RAIN",
|
|
158
|
+
216: "AMSU-A LWP",
|
|
159
|
+
220: "TRMM",
|
|
160
|
+
221: "GMS-1",
|
|
161
|
+
222: "GMS-2",
|
|
162
|
+
223: "GMS-3",
|
|
163
|
+
230: "Kalpana-1",
|
|
164
|
+
231: "INSAT-3D Imager",
|
|
165
|
+
232: "INSAT-3D Sounder",
|
|
166
|
+
240: "MetOp-A",
|
|
167
|
+
241: "MetOp-B",
|
|
168
|
+
242: "MetOp-C",
|
|
169
|
+
250: "COMS-1",
|
|
170
|
+
251: "KOMPSAT-2",
|
|
171
|
+
261: "Landsat 1",
|
|
172
|
+
262: "Landsat 2",
|
|
173
|
+
263: "Landsat 3",
|
|
174
|
+
264: "Landsat 4",
|
|
175
|
+
265: "Landsat 5",
|
|
176
|
+
266: "Landsat 6",
|
|
177
|
+
267: "Landsat 7",
|
|
178
|
+
268: "Landsat 8",
|
|
179
|
+
275: "FY-3A",
|
|
180
|
+
276: "FY-3B",
|
|
181
|
+
277: "FY-3C",
|
|
182
|
+
286: "HimawariCast-8",
|
|
183
|
+
287: "Himawari-9",
|
|
184
|
+
288: "HimawariCast-9",
|
|
185
|
+
289: "HimawariCast-8/9",
|
|
186
|
+
300: "Suomi NPP-VIIRS",
|
|
187
|
+
301: "NOAA-20 (JPSS-1)",
|
|
188
|
+
302: "NOAA-21 (JPSS-2)",
|
|
189
|
+
303: "NOAA-22 (JPSS-3)",
|
|
190
|
+
304: "NOAA-23 (JPSS-4)",
|
|
191
|
+
320: "SNPP SDR",
|
|
192
|
+
321: "NOAA-20 SDR",
|
|
193
|
+
322: "NOAA-21 SDR",
|
|
194
|
+
323: "NOAA-22 SDR",
|
|
195
|
+
324: "NOAA-23 SDR",
|
|
196
|
+
325: "SNPP EDR",
|
|
197
|
+
326: "NOAA-20 EDR",
|
|
198
|
+
327: "NOAA-21 EDR",
|
|
199
|
+
328: "NOAA-22 EDR",
|
|
200
|
+
329: "NOAA-23 EDR",
|
|
201
|
+
354: "Meteosat-11 (MSG-4)",
|
|
202
|
+
355: "Meteosat-12 (MTG-I1)",
|
|
203
|
+
356: "Meteosat-13 (MTG-I2)",
|
|
204
|
+
357: "Meteosat-14 (MTG-I3)",
|
|
205
|
+
365: "MTG-S1 sounder",
|
|
206
|
+
366: "MTG-S2 sounder",
|
|
207
|
+
400: "South Pole Composites",
|
|
208
|
+
401: "North Pole Composites",
|
|
209
|
+
410: "Megha-Tropiques",
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
class Sensor(NamedTuple):
|
|
213
|
+
"""One sensor source.
|
|
214
|
+
|
|
215
|
+
Attributes:
|
|
216
|
+
name: The full name, e.g. ``GOES-3 infrared``.
|
|
217
|
+
platform: The satellite or product, e.g. ``GOES-3``; ``GOES 8`` style names are written ``GOES-8``.
|
|
218
|
+
instrument: What follows the platform in the name (``infrared``, ``imager``, ``sounder``...), or None.
|
|
219
|
+
band: ``VIS`` or ``IR`` for visible and infrared (including water vapor) sensors, otherwise None.
|
|
220
|
+
"""
|
|
221
|
+
|
|
222
|
+
name: str
|
|
223
|
+
platform: str
|
|
224
|
+
instrument: str | None
|
|
225
|
+
band: str | None
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
_NAME = re.compile(
|
|
229
|
+
r"^(?P<platform>.+?) (?P<instrument>visible|infrared|water vapor|imager|sounder|level 2 products)\b(?P<rest>.*)$",
|
|
230
|
+
re.IGNORECASE)
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _parse(name):
|
|
234
|
+
match = _NAME.match(name)
|
|
235
|
+
if match is None:
|
|
236
|
+
return Sensor(name, name, None, None)
|
|
237
|
+
platform = re.sub(r"^GOES (\d+)$", r"GOES-\1", match["platform"])
|
|
238
|
+
instrument = (match["instrument"] + match["rest"]).strip()
|
|
239
|
+
kind = match["instrument"].lower()
|
|
240
|
+
band = "VIS" if kind == "visible" else "IR" if kind in ("infrared", "water vapor") else None
|
|
241
|
+
return Sensor(name, platform, instrument, band)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
SENSORS = {number: _parse(name) for number, name in SENSOR_NAMES.items()}
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def get_sensor(number):
|
|
248
|
+
"""The :class:`Sensor` for a sensor source number, or None if the number is not in the table."""
|
|
249
|
+
return SENSORS.get(number)
|
pyarea/utils.py
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import ctypes as C
|
|
2
|
+
import datetime
|
|
3
|
+
import struct
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
#binary values representing a missing value in McIDAS
|
|
8
|
+
MCMISSING = int('0x80808080', 16)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def is_mcmissing(word):
|
|
12
|
+
"""True if a 32-bit directory or navigation word is the McIDAS "missing" value.
|
|
13
|
+
|
|
14
|
+
``MCMISSING`` is the unsigned bit pattern, but the words of a block are usually read as signed integers, where
|
|
15
|
+
the same pattern is -2139062144; both are recognised.
|
|
16
|
+
"""
|
|
17
|
+
return (int(word) & 0xFFFFFFFF) == MCMISSING
|
|
18
|
+
INT32SIZE = np.int32().itemsize
|
|
19
|
+
|
|
20
|
+
def yyyddd_hhmmss2datetime(yyyddd, hhmmss ):
|
|
21
|
+
"""
|
|
22
|
+
Convert year_day (yyyddd) and time (hhmmss) to datetime
|
|
23
|
+
@:args
|
|
24
|
+
@yyyddd, int, represents the year and day-of-year. The yyy values are the actual year modulo 1900
|
|
25
|
+
@hhmmss, int, represents the time in hhmmss
|
|
26
|
+
@returns
|
|
27
|
+
@datetime, the corresponding datetime object
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
shhmmss = str(hhmmss)
|
|
31
|
+
ls = len(shhmmss)
|
|
32
|
+
if ls != 6: #use custom logic
|
|
33
|
+
if ls <= 2: #exceptionally, only seconds are present
|
|
34
|
+
hh = 0
|
|
35
|
+
mm = 0
|
|
36
|
+
ss = int(shhmmss)
|
|
37
|
+
|
|
38
|
+
elif ls == 3:
|
|
39
|
+
hh = 0
|
|
40
|
+
mm = int(shhmmss[0])
|
|
41
|
+
ss = int(shhmmss[1:])
|
|
42
|
+
|
|
43
|
+
elif ls == 4:
|
|
44
|
+
hh = 0
|
|
45
|
+
mm = int(shhmmss[:2])
|
|
46
|
+
ss = int(shhmmss[2:])
|
|
47
|
+
|
|
48
|
+
elif ls == 5:
|
|
49
|
+
hh = int(shhmmss[0])
|
|
50
|
+
mm = int(shhmmss[1:3])
|
|
51
|
+
ss = int(shhmmss[3:])
|
|
52
|
+
|
|
53
|
+
else :
|
|
54
|
+
raise ValueError(f'Unable to convert integer argument {hhmmss} into hour, minute, second')
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
else:
|
|
59
|
+
hh = int(shhmmss[:2])
|
|
60
|
+
mm = int(shhmmss[2:4])
|
|
61
|
+
ss = int(shhmmss[4:])
|
|
62
|
+
|
|
63
|
+
year = ((yyyddd // 1000) % 1900) + 1900
|
|
64
|
+
yday = yyyddd % 1000
|
|
65
|
+
month, day = get_month_day(year, yday, True)
|
|
66
|
+
return datetime.datetime(year, month, day,hour=hh,minute=mm,second=ss)
|
|
67
|
+
|
|
68
|
+
def timdif(yrday1, hms1, yrday2, hms2):
|
|
69
|
+
"""Minutes from the first date and time to the second, as McIDASUtil.timdif.
|
|
70
|
+
|
|
71
|
+
The days are YYDDD or YYYYDDD, counted from 1900 as in a directory (see ``yyyddd_hhmmss2datetime``),
|
|
72
|
+
the times HHMMSS.
|
|
73
|
+
"""
|
|
74
|
+
first = yyyddd_hhmmss2datetime(yrday1, hms1)
|
|
75
|
+
second = yyyddd_hhmmss2datetime(yrday2, hms2)
|
|
76
|
+
return (second - first).total_seconds() / 60.0
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def get_month_day(year, day, one_based=False):
|
|
80
|
+
if one_based: # if Jan 1st is 1 instead of 0
|
|
81
|
+
day -= 1
|
|
82
|
+
dt = datetime.datetime(year, 1, 1) + datetime.timedelta(days=day)
|
|
83
|
+
return dt.month, dt.day
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def gould2native(number):
|
|
87
|
+
"""
|
|
88
|
+
Converts a number from Gould floating point format to native floating point
|
|
89
|
+
an example conversion:
|
|
90
|
+
input integer 4 bytes = 3201977607
|
|
91
|
+
input value (hex): BD DA 4D 07
|
|
92
|
+
a) convert to binary:
|
|
93
|
+
1011 1110 1101 1010 0100 1101 0000 0111
|
|
94
|
+
b) sign bit is set, so take twos complement:
|
|
95
|
+
0100 0001 0010 0101 1011 0010 1111 1001
|
|
96
|
+
c) convert this back to hex: 41 25 B2 F9
|
|
97
|
+
d) mantissa = 2470649
|
|
98
|
+
e) exponent = 65
|
|
99
|
+
f) tempVal = mantissa / 16 exp (70 - 65)
|
|
100
|
+
g) outputVal = tempVal * sign = -2.3561944
|
|
101
|
+
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
#the input is an 4 byte int of a hexadecimal float in Gould format
|
|
105
|
+
#we need to prepare byte masks in order to extract the most significant byte (byte 4)
|
|
106
|
+
# as well the first three bytes. I am not sure but my feeling is that OGE doc implies Gould is
|
|
107
|
+
#big endian byte order and thus the section 3.5.4 labels bytes in an opposite way
|
|
108
|
+
#(byte1 is the most significant byte)
|
|
109
|
+
#however the nav header is in Little endian and I use its labeling for bytes
|
|
110
|
+
#prepare masks
|
|
111
|
+
byte012_mask = int('0xffffff',16) # 0000 0000 1111 1111 1111 1111 1111 1111
|
|
112
|
+
byte3_mask = int('0xFFFFFFFF', 16) # 1111 1111 0000 0000 0000 0000 0000 0000
|
|
113
|
+
sign_bit_mask = int('0x80000000',16) # 10000000000000000000000000000000
|
|
114
|
+
exponent_mask = int('0x7F000000', 16) # '01111111000000000000000000000000
|
|
115
|
+
#compute sign
|
|
116
|
+
sign = -1 if number & sign_bit_mask else 1
|
|
117
|
+
if sign < 0:
|
|
118
|
+
#two's complement for negative numbers
|
|
119
|
+
number = (number ^ byte3_mask) + 1
|
|
120
|
+
#compute exonent
|
|
121
|
+
exponent = (number & exponent_mask) >> 24
|
|
122
|
+
if exponent == 0: #account for bias
|
|
123
|
+
exponent = 64
|
|
124
|
+
#compute matissa
|
|
125
|
+
mantissa = number & byte012_mask
|
|
126
|
+
tempVal = 16 ** float(70 - exponent)
|
|
127
|
+
nativeVal = mantissa / tempVal
|
|
128
|
+
nativeVal = nativeVal * sign
|
|
129
|
+
|
|
130
|
+
return nativeVal
|
|
131
|
+
|
|
132
|
+
def mcPI2F(value):
|
|
133
|
+
"""Packed integer (SDDDMMSS for degrees, HHMMSS for hours) to a float, as McIDASUtil.mcPackedIntegerToDouble."""
|
|
134
|
+
val = abs(int(value))
|
|
135
|
+
fvalue = val // 10000 + (val // 100 % 100) / 60. + (val % 100) / 3600.
|
|
136
|
+
return -fvalue if value < 0 else fvalue
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def mcF2PI(value):
|
|
140
|
+
"""Float degrees (or hours) to a packed integer, rounded to whole seconds, as McIDASUtil.mcDoubleToPackedInteger."""
|
|
141
|
+
j = int(3600 * abs(value) + .5)
|
|
142
|
+
val = 10000 * (j // 3600) + 100 * (j // 60 % 60) + j % 60
|
|
143
|
+
return -val if value < 0 else val
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def leapyear(year):
|
|
147
|
+
"""Days in a year, with the McIDAS rule of a leap year every fourth year (right from 1901 to 2099)."""
|
|
148
|
+
return 366 - ((year % 4) + 3) // 4
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
_field_names_ = names = ('year_1000', 'year_100', 'year_10', 'year_1', 'day_100', 'day_10', 'day_1', 'hour_10',
|
|
152
|
+
'hour_1', 'min_10', 'min_1', 'sec_10', 'sec_1', 'msec_100', 'msec_10', 'msec_1')
|
|
153
|
+
|
|
154
|
+
_field_types_ = [C.c_uint8]* len(_field_names_)
|
|
155
|
+
class BCDDateTime(C.BigEndianStructure, ):
|
|
156
|
+
|
|
157
|
+
_fields_ = tuple([(_field_names_[i], _field_types_[i], 4) for i in range(len(_field_names_))])
|
|
158
|
+
|
|
159
|
+
@staticmethod
|
|
160
|
+
def get_month_day(year, day, one_based=False):
|
|
161
|
+
if one_based: # if Jan 1st is 1 instead of 0
|
|
162
|
+
day -= 1
|
|
163
|
+
dt = datetime.datetime(year, 1, 1) + datetime.timedelta(days=day)
|
|
164
|
+
return dt.month, dt.day
|
|
165
|
+
|
|
166
|
+
def python_value(self):
|
|
167
|
+
# compose year
|
|
168
|
+
d = self.fields
|
|
169
|
+
year = d['year_1000'] * 1000 + d['year_100'] * 100 + d['year_10'] * 10 + d['year_1']
|
|
170
|
+
yday = d['day_100'] * 100 + d['day_10'] * 10 + d['day_1']
|
|
171
|
+
month, day = self.get_month_day(year, yday, True)
|
|
172
|
+
hour = d['hour_10'] * 10 + d['hour_1']
|
|
173
|
+
minute = d['min_10'] * 10 + d['min_1']
|
|
174
|
+
second = d['sec_10'] * 10 + d['sec_1']
|
|
175
|
+
milisecond = d['msec_100'] * 100 + d['msec_10'] * 10 + d['msec_1']
|
|
176
|
+
return datetime.datetime(year, month, day, hour, minute, second, milisecond*1000)
|
|
177
|
+
|
|
178
|
+
@property
|
|
179
|
+
def fields(self):
|
|
180
|
+
d = {}
|
|
181
|
+
|
|
182
|
+
for fdef in self._fields_:
|
|
183
|
+
v = getattr(self, fdef[0])
|
|
184
|
+
try:
|
|
185
|
+
d[fdef[0]] = v.fields
|
|
186
|
+
except AttributeError:
|
|
187
|
+
cls_name = fdef[1].__class__.__name__
|
|
188
|
+
if cls_name == 'PyCArrayType':
|
|
189
|
+
return dict([('%s[%d]' % (fdef[0], i), repr(e)) for i, e in enumerate(v)])
|
|
190
|
+
else:
|
|
191
|
+
d[fdef[0]] = v
|
|
192
|
+
|
|
193
|
+
return d
|
|
194
|
+
|
|
195
|
+
_BCD_NAMES = ('year_1000', 'year_100', 'year_10', 'year_1', 'day_100', 'day_10', 'day_1', 'hour_10', 'hour_1',
|
|
196
|
+
'min_10', 'min_1', 'sec_10', 'sec_1', 'msec_100', 'msec_10', 'msec_1')
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def bcd_words_to_datetime(word1, word2):
|
|
200
|
+
"""Decode a BCD time stored in two 32-bit words, as ``YYYYDDDH`` and ``HMMSSmmm`` hexadecimal digits.
|
|
201
|
+
|
|
202
|
+
The words are integers, i.e. already decoded in the byte order of the file they came from, so this does not
|
|
203
|
+
depend on that byte order. ``0x19962562, 0x14515000`` is 1996 day 256 (September 12), 21:45:15.000.
|
|
204
|
+
"""
|
|
205
|
+
chars = struct.pack('>2I', int(word1) & 0xFFFFFFFF, int(word2) & 0xFFFFFFFF)
|
|
206
|
+
nibbles = [value for byte in chars for value in (byte >> 4, byte & 15)]
|
|
207
|
+
d = dict(zip(_BCD_NAMES, nibbles))
|
|
208
|
+
year = d['year_1000'] * 1000 + d['year_100'] * 100 + d['year_10'] * 10 + d['year_1']
|
|
209
|
+
yday = d['day_100'] * 100 + d['day_10'] * 10 + d['day_1']
|
|
210
|
+
month, day = get_month_day(year, yday, True)
|
|
211
|
+
hour = d['hour_10'] * 10 + d['hour_1']
|
|
212
|
+
minute = d['min_10'] * 10 + d['min_1']
|
|
213
|
+
second = d['sec_10'] * 10 + d['sec_1']
|
|
214
|
+
millisecond = d['msec_100'] * 100 + d['msec_10'] * 10 + d['msec_1']
|
|
215
|
+
return datetime.datetime(year, month, day, hour, minute, second, millisecond * 1000) # microseconds
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def bcdtime2datetime(bytes=None, byte_order=None):
|
|
219
|
+
"""Convert a BCD time given as the 8 bytes it occupies in a file (two 32-bit words) to a datetime.
|
|
220
|
+
|
|
221
|
+
``byte_order`` is the byte order of the file's 32-bit words, ``'>'`` or ``'<'``. The bytes of each word are in
|
|
222
|
+
that order, so in a little-endian file the digits of each word appear reversed.
|
|
223
|
+
"""
|
|
224
|
+
assert byte_order in ('>', '<'), 'byte_order has to be supplied, as > or <'
|
|
225
|
+
word1, word2 = struct.unpack(f'{byte_order}2I', bytearray(bytes))
|
|
226
|
+
return bcd_words_to_datetime(word1, word2)
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def julday(month, day, year, hour=0, minute=0, second=0):
|
|
230
|
+
"""
|
|
231
|
+
Julian date from month, day, and year (can be scalars or arrays)
|
|
232
|
+
|
|
233
|
+
Parameters
|
|
234
|
+
----------
|
|
235
|
+
month : numpy.ndarray or int32
|
|
236
|
+
Month.
|
|
237
|
+
day : numpy.ndarray or int32
|
|
238
|
+
Day.
|
|
239
|
+
year : numpy.ndarray or int32
|
|
240
|
+
Year.
|
|
241
|
+
hour : numpy.ndarray or int32, optional
|
|
242
|
+
Hour.
|
|
243
|
+
minute : numpy.ndarray or int32, optional
|
|
244
|
+
Minute.
|
|
245
|
+
second : numpy.ndarray or int32, optional
|
|
246
|
+
Second.
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
Returns
|
|
251
|
+
-------
|
|
252
|
+
jul : numpy.ndarray or double
|
|
253
|
+
Julian day.
|
|
254
|
+
"""
|
|
255
|
+
month = np.array(month)
|
|
256
|
+
day = np.array(day)
|
|
257
|
+
inJanFeb = month <= 2
|
|
258
|
+
jy = year - inJanFeb
|
|
259
|
+
jm = month + 1 + inJanFeb * 12
|
|
260
|
+
|
|
261
|
+
jul = np.int32(np.floor(365.25 * jy) +
|
|
262
|
+
np.floor(30.6001 * jm) + (day + 1720995.0))
|
|
263
|
+
ja = np.int32(0.01 * jy)
|
|
264
|
+
jul += 2 - ja + np.int32(0.25 * ja)
|
|
265
|
+
|
|
266
|
+
jul = jul + hour / 24.0 - 0.5 + minute / 1440.0 + second / 86400.0
|
|
267
|
+
|
|
268
|
+
return jul
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def caldat(julian):
|
|
272
|
+
"""
|
|
273
|
+
Calendar date (month, day, year) from julian date, inverse of 'julday()'
|
|
274
|
+
Return value: month, day, and year in the Gregorian
|
|
275
|
+
Works only for years past 1582!
|
|
276
|
+
|
|
277
|
+
Parameters
|
|
278
|
+
----------
|
|
279
|
+
julian : numpy.ndarray or double
|
|
280
|
+
Julian day.
|
|
281
|
+
|
|
282
|
+
Returns
|
|
283
|
+
-------
|
|
284
|
+
month : numpy.ndarray or int32
|
|
285
|
+
Month.
|
|
286
|
+
day : numpy.ndarray or int32
|
|
287
|
+
Day.
|
|
288
|
+
year : numpy.ndarray or int32
|
|
289
|
+
Year.
|
|
290
|
+
"""
|
|
291
|
+
jn = np.int32(((np.array(julian) + 0.000001).round()))
|
|
292
|
+
|
|
293
|
+
jalpha = np.int32(((jn - 1867216) - 0.25) / 36524.25)
|
|
294
|
+
ja = jn + 1 + jalpha - (np.int32(0.25 * jalpha))
|
|
295
|
+
jb = ja + 1524
|
|
296
|
+
jc = np.int32(6680.0 + ((jb - 2439870.0) - 122.1) / 365.25)
|
|
297
|
+
jd = np.int32(365.0 * jc + (0.25 * jc))
|
|
298
|
+
je = np.int32((jb - jd) / 30.6001)
|
|
299
|
+
|
|
300
|
+
day = jb - jd - np.int32(30.6001 * je)
|
|
301
|
+
month = je - 1
|
|
302
|
+
month = (month - 1) % 12 + 1
|
|
303
|
+
year = jc - 4715
|
|
304
|
+
year = year - (month > 2)
|
|
305
|
+
|
|
306
|
+
return month, day, year
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def compose_area_name(adir=None,coverage=None, ):
|
|
310
|
+
|
|
311
|
+
band = adir.bands[0]
|
|
312
|
+
nom_dt = adir.nominal_time
|
|
313
|
+
date_str = nom_dt.date().strftime('%Y%m%d')
|
|
314
|
+
time_str = nom_dt.time().strftime('%H%M')
|
|
315
|
+
cov = '_%s' % coverage if coverage else ''
|
|
316
|
+
return '%s_B%s_%s%s%s.pyarea' % (adir.sensor, band, date_str, time_str, cov)
|