21
21
22
22
import datetime
23
23
from enum import Enum
24
+ import json
25
+ import logging
26
+ import os
27
+
28
+ logger = logging .getLogger ("calibration" )
24
29
25
30
26
31
class CalibrationData (Enum ):
@@ -55,7 +60,7 @@ class CalibrationData(Enum):
55
60
SATPY_CALIB_MODE = 'Nominal'
56
61
57
62
58
- def get_calibration (platform , time , clip = False ):
63
+ def get_calibration (platform , time , clip = False , calib_ir_path = None ):
59
64
"""Get MODIS-intercalibrated gain and offset for specific time.
60
65
61
66
Args:
@@ -74,6 +79,15 @@ def get_calibration(platform, time, clip=False):
74
79
time = time ,
75
80
clip = clip
76
81
)
82
+ if calib_ir_path is not None :
83
+ for channel in ('IR_039' , 'IR_087' , 'IR_108' , 'IR_120' ,
84
+ 'IR_134' , 'IR_097' , 'WV_062' , 'WV_073' ):
85
+ coefs [channel ] = get_ir_calibration_coeffs (
86
+ calib_ir_path ,
87
+ platform = platform ,
88
+ channel = channel ,
89
+ time = time ,
90
+ )
77
91
return coefs
78
92
79
93
@@ -106,11 +120,13 @@ def _convert_to_datetime(date_or_time):
106
120
107
121
108
122
def _is_date (date_or_time ):
123
+ """Check that we have a datetime date object."""
109
124
# datetime is a subclass of date, therefore we cannot use isinstance here
110
- return type (date_or_time ) == datetime .date
125
+ return type (date_or_time ) == datetime .date # noqa E721
111
126
112
127
113
128
def _check_is_valid_time (time ):
129
+ """Check that we have a valid time."""
114
130
ref_time = CalibrationData .REF_TIME .value
115
131
if time < ref_time :
116
132
raise ValueError ('Given time ({0}) is < reference time ({1})' .format (
@@ -154,6 +170,22 @@ def _microwatts_to_milliwatts(microwatts):
154
170
return microwatts / 1000.0
155
171
156
172
173
+ def get_ir_calibration_coeffs (ir_calib_path , platform = "MSG2" , channel = "IR_039" ,
174
+ time = datetime .datetime (2048 , 1 , 18 , 12 , 0 )):
175
+ """Get IR calibration from EUMETSAT, modified by CMSAF."""
176
+ filename = os .path .join (ir_calib_path , f"TIR_calib_{ platform } _{ channel } .json" )
177
+ logger .info (f'Using IR calibration from { filename } ' )
178
+ with open (filename , 'r' ) as fhand :
179
+ data = json .load (fhand )
180
+ for item in data :
181
+ date_s = item [0 ]
182
+ date_i = datetime .datetime .strptime (date_s , "%Y-%m-%dT%H:%M:%S.%f" )
183
+ if date_i > time :
184
+ break
185
+ gain , offset = item [1 :]
186
+ return {'gain' : gain , 'offset' : offset }
187
+
188
+
157
189
if __name__ == '__main__' :
158
190
time = datetime .datetime (2018 , 1 , 18 , 12 , 0 )
159
191
platform = 'MSG3'
@@ -164,4 +196,9 @@ def _microwatts_to_milliwatts(microwatts):
164
196
time = time )
165
197
coefs [channel ] = {'gain' : gain , 'offset' : offset }
166
198
199
+ for channel in ('IR_039' , 'IR_087' , 'IR_108' , 'IR_120' ,
200
+ 'IR_134' , 'IR_097' , 'WV_062' , 'WV_073' ):
201
+ gain , offset = get_ir_calibration_coeffs (platform = platform , channel = channel ,
202
+ time = time )
203
+ coefs [channel ] = {'gain' : gain , 'offset' : offset }
167
204
print (coefs )
0 commit comments