-
Notifications
You must be signed in to change notification settings - Fork 39
/
seviri_rsr.py
272 lines (224 loc) · 10.2 KB
/
seviri_rsr.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2013-2018 Adam.Dybbroe
# Author(s):
# Adam.Dybbroe <adam.dybbroe@smhi.se>
# Panu Lahtinen <panu.lahtinen@fmi.fi>
# This program 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.
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
"""Interface to the SEVIRI spectral response functions for all four MSG
satellites
"""
import os
from xlrd import open_workbook
import numpy as np
from pyspectral.config import get_config
import pkg_resources
import logging
LOG = logging.getLogger(__name__)
DATA_PATH = pkg_resources.resource_filename('pyspectral', 'data/')
class Seviri(object):
"""Class for Seviri RSR"""
def __init__(self, wavespace='wavelength'):
"""
Read the seviri relative spectral responses for all channels and all
MSG satellites.
Optional input: 'wavespace'. Equals 'wavelength' (units of micron's) on
default. Can be 'wavenumber' in which case the unit is in cm-1.
"""
options = get_config()
self.seviri_path = options['seviri'].get('path')
if not os.path.exists(self.seviri_path):
self.seviri_path = os.path.join(
DATA_PATH, options['seviri'].get('filename'))
LOG.debug("Original RSR file from EUMETSAT: {}".format(self.seviri_path))
self.output_dir = options.get('rsr_dir', './')
self.rsr = None
self._load()
self.wavespace = wavespace
if wavespace not in ['wavelength', 'wavenumber']:
raise AttributeError("wavespace has to be either " +
"'wavelength' or 'wavenumber'!")
self.unit = 'micrometer'
if wavespace == 'wavenumber':
# Convert to wavenumber:
self.convert2wavenumber()
self.central_wavenumber = None
self.central_wavelength = None
self.get_centrals()
def _load(self, filename=None):
"""Read the SEVIRI rsr data"""
if not filename:
filename = self.seviri_path
wb_ = open_workbook(filename)
self.rsr = {}
sheet_names = []
for sheet in wb_.sheets():
if sheet.name in ['Info', 'Requirements']:
continue
ch_name = sheet.name.strip()
sheet_names.append(sheet.name.strip())
self.rsr[ch_name] = {'wavelength': None,
'Meteosat-8': None,
'Meteosat-9': None,
'Meteosat-10': None,
'Meteosat-11': None}
if ch_name.startswith('HRV'):
wvl = np.array(
sheet.col_values(0, start_rowx=37, end_rowx=137))
# TODO: Add the 'extended' responses as well!
met8 = np.array(
sheet.col_values(1, start_rowx=37, end_rowx=137))
met9 = np.array(
sheet.col_values(3, start_rowx=37, end_rowx=137))
met10 = np.array(
sheet.col_values(5, start_rowx=37, end_rowx=137))
met11 = np.array(
sheet.col_values(6, start_rowx=37, end_rowx=137))
self.rsr[ch_name]['Meteosat-8'] = met8
self.rsr[ch_name]['Meteosat-9'] = met9
self.rsr[ch_name]['Meteosat-10'] = met10
self.rsr[ch_name]['Meteosat-11'] = met11
elif ch_name.startswith('IR'):
wvl = np.array(
sheet.col_values(0, start_rowx=13, end_rowx=113))
met8_95 = np.array(
sheet.col_values(1, start_rowx=13, end_rowx=113))
met9_95 = np.array(
sheet.col_values(3, start_rowx=13, end_rowx=113))
met10_95 = np.array(
sheet.col_values(5, start_rowx=13, end_rowx=113))
met11_95 = np.array(
sheet.col_values(7, start_rowx=13, end_rowx=113))
met8_85 = np.array(
sheet.col_values(2, start_rowx=13, end_rowx=113))
met9_85 = np.array(
sheet.col_values(4, start_rowx=13, end_rowx=113))
met10_85 = np.array(
sheet.col_values(6, start_rowx=13, end_rowx=113))
met11_85 = np.array(
sheet.col_values(8, start_rowx=13, end_rowx=113))
self.rsr[ch_name]['Meteosat-8'] = {'95': met8_95,
'85': met8_85}
self.rsr[ch_name]['Meteosat-9'] = {'95': met9_95,
'85': met9_85}
self.rsr[ch_name]['Meteosat-10'] = {'95': met10_95,
'85': met10_85}
self.rsr[ch_name]['Meteosat-11'] = {'95': met11_95,
'85': met11_85}
else:
wvl = np.array(
sheet.col_values(0, start_rowx=12, end_rowx=112))
met8 = np.array(
sheet.col_values(1, start_rowx=12, end_rowx=112))
met9 = np.array(
sheet.col_values(2, start_rowx=12, end_rowx=112))
met10 = np.array(
sheet.col_values(3, start_rowx=12, end_rowx=112))
met11 = np.array(
sheet.col_values(4, start_rowx=12, end_rowx=112))
self.rsr[ch_name]['Meteosat-8'] = met8
self.rsr[ch_name]['Meteosat-9'] = met9
self.rsr[ch_name]['Meteosat-10'] = met10
self.rsr[ch_name]['Meteosat-11'] = met11
self.rsr[ch_name]['wavelength'] = wvl
def convert2wavenumber(self):
"""Convert from wavelengths to wavenumber"""
for chname in self.rsr.keys():
elems = [k for k in self.rsr[chname].keys()]
for sat in elems:
if sat == "wavelength":
LOG.debug("Get the wavenumber from the wavelength: sat=%s chname=%s", sat, chname)
wnum = 1. / (1e-4 * self.rsr[chname][sat][:]) # microns to cm
self.rsr[chname]['wavenumber'] = wnum[::-1]
else:
if type(self.rsr[chname][sat]) is dict:
for name in self.rsr[chname][sat].keys():
resp = self.rsr[chname][sat][name][:]
self.rsr[chname][sat][name] = resp[::-1]
else:
resp = self.rsr[chname][sat][:]
self.rsr[chname][sat] = resp[::-1]
for chname in self.rsr.keys():
del self.rsr[chname]['wavelength']
self.unit = 'cm-1'
def get_centrals(self):
"""Get the central wavenumbers or central wavelengths of all channels,
depending on the given 'wavespace'
"""
result = {}
for chname in self.rsr.keys():
result[chname] = {}
if self.wavespace == "wavelength":
x__ = self.rsr[chname]["wavelength"]
else:
x__ = self.rsr[chname]["wavenumber"]
for sat in self.rsr[chname].keys():
if sat in ["wavelength", "wavenumber"]:
continue
if type(self.rsr[chname][sat]) is dict:
result[chname][sat] = {}
for name in self.rsr[chname][sat].keys():
resp = self.rsr[chname][sat][name]
result[chname][sat][name] = get_central_wave(x__, resp)
else:
resp = self.rsr[chname][sat]
result[chname][sat] = get_central_wave(x__, resp)
if self.wavespace == "wavelength":
self.central_wavelength = result
else:
self.central_wavenumber = result
def get_central_wave(wavl, resp):
"""Calculate the central wavelength or the central wavenumber,
depending on what is input
"""
return np.trapz(resp * wavl, wavl) / np.trapz(resp, wavl)
def generate_seviri_file(seviri, platform_name):
"""Generate the pyspectral internal common format relative response
function file for one SEVIRI
"""
import h5py
filename = os.path.join(seviri.output_dir,
"rsr_seviri_{0}.h5".format(platform_name))
sat_name = platform_name
with h5py.File(filename, "w") as h5f:
h5f.attrs['description'] = 'Relative Spectral Responses for SEVIRI'
h5f.attrs['platform_name'] = platform_name
bandlist = [str(key) for key in seviri.rsr.keys()]
h5f.attrs['band_names'] = bandlist
for key in seviri.rsr.keys():
grp = h5f.create_group(key)
if isinstance(seviri.central_wavelength[key][sat_name], dict):
grp.attrs['central_wavelength'] = \
seviri.central_wavelength[key][sat_name]['95']
else:
grp.attrs['central_wavelength'] = \
seviri.central_wavelength[key][sat_name]
arr = seviri.rsr[key]['wavelength']
dset = grp.create_dataset('wavelength', arr.shape, dtype='f')
dset.attrs['unit'] = 'm'
dset.attrs['scale'] = 1e-06
dset[...] = arr
try:
arr = seviri.rsr[key][sat_name]['95']
except ValueError:
arr = seviri.rsr[key][sat_name]
dset = grp.create_dataset('response', arr.shape, dtype='f')
dset[...] = arr
return
def main():
"""Main"""
sev_obj = Seviri()
for satnum in [8, 9, 10, 11]:
generate_seviri_file(sev_obj, 'Meteosat-{0:d}'.format(satnum))
print("Meteosat-{0:d} done...".format(satnum))
if __name__ == "__main__":
main()