1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# vim:fileencoding=utf-8 |
3
|
|
|
# |
4
|
|
|
# Copyright (c) 2014-2017 Stefan Bender |
5
|
|
|
# |
6
|
|
|
# This file is part of sciapy. |
7
|
|
|
# sciapy is free software: you can redistribute it or modify it |
8
|
|
|
# under the terms of the GNU General Public License as published by |
9
|
|
|
# the Free Software Foundation, version 2. |
10
|
|
|
# See accompanying LICENSE file or http://www.gnu.org/licenses/gpl-2.0.html. |
11
|
1 |
|
"""SCIAMACHY level 1c limb spectra text interface |
12
|
|
|
""" |
13
|
1 |
|
from __future__ import absolute_import, division, print_function |
14
|
|
|
|
15
|
1 |
|
import numpy as np |
16
|
|
|
|
17
|
1 |
|
from ._types import _float_type, _int_type, _limb_data_dtype |
18
|
|
|
|
19
|
1 |
|
def _print_indent(fp, indent): |
20
|
1 |
|
for _ in range(indent): |
21
|
1 |
|
print(" ", end="", file=fp) |
22
|
|
|
|
23
|
1 |
|
def _print_array(fp, a, indent): |
24
|
|
|
_print_indent(fp, indent) |
25
|
|
|
print('\t'.join(map(str, a)), file=fp) |
26
|
|
|
|
27
|
|
|
# format strings taken from scia_L1C_ascii.c |
28
|
1 |
|
def _print_array1(fp, a, padding=0): |
29
|
1 |
|
if padding > 0: |
30
|
1 |
|
_print_indent(fp, padding) |
31
|
1 |
|
print(''.join(map(lambda x: str('%12.3f ' % x), a)), file=fp) |
32
|
|
|
|
33
|
1 |
|
def _print_array2(fp, a): |
34
|
|
|
# print(' '.join(map(lambda x: str('% -7.5e' % x), a)), file=fp) |
35
|
1 |
|
print(''.join(map(lambda x: str('%12.5e ' % x), a)), file=fp) |
36
|
|
|
|
37
|
|
|
|
38
|
1 |
|
def read_from_textfile(self, filename): |
39
|
|
|
"""SCIAMACHY level 1c limb scan text import |
40
|
|
|
|
41
|
|
|
Parameters |
42
|
|
|
---------- |
43
|
|
|
filename : str |
44
|
|
|
The (plain ascii) table text filename to read the data from. |
45
|
|
|
|
46
|
|
|
Returns |
47
|
|
|
------- |
48
|
|
|
nothing |
49
|
|
|
""" |
50
|
1 |
|
import numpy.lib.recfunctions as rfn |
51
|
1 |
|
if hasattr(filename, 'seek'): |
52
|
|
|
f = filename |
53
|
|
|
else: |
54
|
1 |
|
f = open(filename, 'rb') |
55
|
1 |
|
h_list = [] |
56
|
1 |
|
nh = int(f.readline()) |
57
|
1 |
|
for _ in range(nh): |
58
|
1 |
|
h_list.append(bytes(f.readline()).decode().rstrip()) |
59
|
1 |
|
self.textheader_length = nh |
60
|
1 |
|
self.textheader = '\n'.join(h_list) |
61
|
1 |
|
self.parse_textheader() |
62
|
1 |
|
self.nalt, self.npix = np.fromstring(f.readline(), dtype=_int_type, sep=' ') |
63
|
|
|
|
64
|
1 |
|
self.orbit_state = np.fromstring(f.readline(), dtype=_int_type, sep=' ') |
65
|
1 |
|
(self.orbit, self.state_in_orbit, self.state_id, |
66
|
|
|
self.profiles_per_state, self.profile_in_state) = self.orbit_state |
67
|
1 |
|
self.date = np.fromstring(f.readline(), dtype=_int_type, sep=' ') |
68
|
|
|
# pre-set the limb_data |
69
|
1 |
|
if self._limb_data_dtype is None: |
70
|
1 |
|
self._limb_data_dtype = _limb_data_dtype[:] |
71
|
1 |
|
self.limb_data = np.zeros((self.nalt), dtype=self._limb_data_dtype) |
72
|
1 |
|
if nh > 27: |
73
|
1 |
|
self.limb_data["sub_sat_lat"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
74
|
1 |
|
self.limb_data["sub_sat_lon"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
75
|
1 |
|
if nh > 29: |
76
|
1 |
|
self.orbit_phase = np.fromstring(f.readline(), dtype=_float_type, sep=' ')[0] |
77
|
1 |
|
self.cent_lat_lon = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
78
|
|
|
|
79
|
1 |
|
self.limb_data["tp_lat"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
80
|
1 |
|
self.limb_data["tp_lon"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
81
|
1 |
|
self.limb_data["tp_alt"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
82
|
1 |
|
self.limb_data["tp_sza"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
83
|
1 |
|
self.limb_data["tp_saa"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
84
|
1 |
|
self.limb_data["tp_los"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
85
|
1 |
|
self.limb_data["toa_sza"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
86
|
1 |
|
self.limb_data["toa_saa"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
87
|
1 |
|
self.limb_data["toa_los"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
88
|
1 |
|
self.limb_data["sat_sza"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
89
|
1 |
|
self.limb_data["sat_saa"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
90
|
1 |
|
self.limb_data["sat_los"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
91
|
1 |
|
self.limb_data["sat_alt"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
92
|
1 |
|
self.limb_data["earth_rad"] = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
93
|
|
|
|
94
|
1 |
|
tmp_list = [] |
95
|
1 |
|
for _ in range(self.npix): |
96
|
1 |
|
input = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
97
|
1 |
|
self.wls.append(input[0]) |
98
|
1 |
|
tmp_list.append(input[1:]) |
99
|
1 |
|
tmp_rad_arr = np.asarray(tmp_list).reshape(self.npix, self.nalt).transpose() |
100
|
1 |
|
tmp_list[:] = [] |
101
|
1 |
|
line = f.readline().strip() |
102
|
1 |
|
if bytes(line) == b"ERRORS": |
103
|
1 |
|
for _ in range(self.npix): |
104
|
1 |
|
input = np.fromstring(f.readline(), dtype=_float_type, sep=' ') |
105
|
1 |
|
tmp_list.append(input[1:]) |
106
|
|
|
else: |
107
|
|
|
for _ in range(self.npix): |
108
|
|
|
tmp_list.append(np.zeros(self.nalt)) |
109
|
1 |
|
tmp_err_arr = np.asarray(tmp_list).reshape(self.npix, self.nalt).transpose() |
110
|
|
|
|
111
|
|
|
# save to limb_data recarray |
112
|
1 |
|
rads = np.rec.fromarrays([tmp_rad_arr], |
113
|
|
|
dtype=np.dtype([("rad", 'f4', (self.npix,))])) |
114
|
1 |
|
errs = np.rec.fromarrays([tmp_err_arr], |
115
|
|
|
dtype=np.dtype([("err", 'f4', (self.npix,))])) |
116
|
1 |
|
self.limb_data = rfn.merge_arrays([self.limb_data, rads, errs], |
117
|
|
|
usemask=False, asrecarray=True, flatten=True) |
118
|
1 |
|
self._limb_data_dtype = self.limb_data.dtype |
119
|
|
|
|
120
|
1 |
|
def write_to_textfile(self, filename): |
121
|
|
|
"""SCIAMACHY level 1c limb scan text export |
122
|
|
|
|
123
|
|
|
Parameters |
124
|
|
|
---------- |
125
|
|
|
filename : str |
126
|
|
|
The (plain ascii) table text filename to write the data to. |
127
|
|
|
|
128
|
|
|
Returns |
129
|
|
|
------- |
130
|
|
|
nothing |
131
|
|
|
""" |
132
|
1 |
|
if hasattr(filename, 'seek'): |
133
|
|
|
f = filename |
134
|
|
|
else: |
135
|
1 |
|
f = open(filename, 'w') |
136
|
1 |
|
print(self.textheader_length, file=f) |
137
|
1 |
|
print(self.textheader, file=f) |
138
|
|
|
# format strings taken from scia_L1C_ascii.c |
139
|
1 |
|
print("%2d %4d" % (self.nalt, self.npix), file=f) |
140
|
1 |
|
print("%05d %2d %2d %2d %2d" % tuple(self.orbit_state), file=f) |
141
|
1 |
|
print("%4d %2d %2d %2d %2d %2d" % tuple(self.date), file=f) |
142
|
1 |
|
if self.textheader_length > 27: |
143
|
1 |
|
_print_array1(f, self.limb_data["sub_sat_lat"], 9) |
144
|
1 |
|
_print_array1(f, self.limb_data["sub_sat_lon"], 9) |
145
|
1 |
|
if self.textheader_length > 29: |
146
|
1 |
|
_print_indent(f, 9) |
147
|
1 |
|
print("%12.3f " % (self.orbit_phase,), file=f) |
148
|
1 |
|
_print_indent(f, 9) |
149
|
1 |
|
print("%8.3f %8.3f %8.3f %8.3f %8.3f %8.3f " |
150
|
|
|
"%8.3f %8.3f %8.3f %8.3f" % tuple(self.cent_lat_lon), file=f) |
151
|
|
|
# print the limb data |
152
|
1 |
|
_print_array1(f, self.limb_data["tp_lat"], 9) |
153
|
1 |
|
_print_array1(f, self.limb_data["tp_lon"], 9) |
154
|
1 |
|
_print_array1(f, self.limb_data["tp_alt"], 9) |
155
|
1 |
|
_print_array1(f, self.limb_data["tp_sza"], 9) |
156
|
1 |
|
_print_array1(f, self.limb_data["tp_saa"], 9) |
157
|
1 |
|
_print_array1(f, self.limb_data["tp_los"], 9) |
158
|
1 |
|
_print_array1(f, self.limb_data["toa_sza"], 9) |
159
|
1 |
|
_print_array1(f, self.limb_data["toa_saa"], 9) |
160
|
1 |
|
_print_array1(f, self.limb_data["toa_los"], 9) |
161
|
1 |
|
_print_array1(f, self.limb_data["sat_sza"], 9) |
162
|
1 |
|
_print_array1(f, self.limb_data["sat_saa"], 9) |
163
|
1 |
|
_print_array1(f, self.limb_data["sat_los"], 9) |
164
|
1 |
|
_print_array1(f, self.limb_data["sat_alt"], 9) |
165
|
1 |
|
_print_array1(f, self.limb_data["earth_rad"], 9) |
166
|
|
|
|
167
|
1 |
|
rads = np.asarray(self.limb_data["rad"]).reshape(self.nalt, self.npix).transpose() |
168
|
1 |
|
errs = np.asarray(self.limb_data["err"]).reshape(self.nalt, self.npix).transpose() |
169
|
|
|
|
170
|
|
|
# format strings taken from scia_L1C_ascii.c |
171
|
1 |
|
for i in range(self.npix): |
172
|
1 |
|
print("%9.4f" % self.wls[i], end=" ", file=f) |
173
|
1 |
|
_print_array2(f, rads[i]) |
174
|
|
|
|
175
|
1 |
|
print("ERRORS", file=f) |
176
|
|
|
|
177
|
1 |
|
for i in range(self.npix): |
178
|
1 |
|
print("%9.4f" % self.wls[i], end=" ", file=f) |
179
|
1 |
|
_print_array2(f, errs[i]) |
180
|
|
|
|
181
|
|
|
f.close() |
182
|
|
|
|