scia_conv_hdf5_limb   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 76
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 6

1 Function

Rating   Name   Duplication   Size   Complexity  
B main() 0 78 6
1
#!/usr/bin/env python
2
# coding: utf-8
3
"""SCIAMACHY l1c hdf5 to ascii conversion
4
5
Copyright (c) 2017 Stefan Bender
6
7
This program is free software: you can redistribute it or modify
8
it under the terms of the GNU General Public License as published
9
by the Free Software Foundation, version 2.
10
See accompanying LICENSE file or http://www.gnu.org/licenses/gpl-2.0.html.
11
12
This program converts SCIAMACHY level 1c HDF5 spectra, as produced
13
by the SRON nadc_tools (https://github.com/rmvanhees/nadc_tools),
14
to plain ascii text files or to binary files ready to be used in
15
trace gas retrievals.
16
17
Usage
18
-----
19
$ python sron_hdf5_spectra.py [l1c_hdf5] [-C|--cat categories] [-c|--clus clusters]
20
21
Arguments
22
---------
23
l1c_hdf5 : filename
24
	The input HDF5 file.
25
categories : int or tuple
26
	The measurement categories to extract, can be a single number or a
27
	comma separated list of numbers, for example 26,27 for the MLT states.
28
clusters : int or tuple
29
	The spectral clusters to extract, can be a single number or a
30
	comma separated list of numbers, for example 2,3,4 for channel 1.
31
32
Example
33
-------
34
$ python sron_hdf5_spectra.py SCI_NL__1PYDPA20100203_031030_000060632086_00319_41455_0002.ch1.h5 --cat 26,27 --clus 2,3,4
35
"""
36
37
from __future__ import absolute_import, division, print_function
38
39
import argparse as ap
40
import logging
41
42
import h5py
43
import numpy as np
44
45
import sciapy.level1c as slvl1c
46
47
def main():
48
	logging.basicConfig(level=logging.WARN,
49
			format="[%(levelname)-8s] (%(asctime)s) "
50
			"%(filename)s:%(lineno)d %(message)s",
51
			datefmt="%Y-%m-%d %H:%M:%S %z")
52
	parser = ap.ArgumentParser()
53
	parser.add_argument("file", help="The input HDF5 file.",
54
			default="SCI_NL__1PYDPA20100203_031030_000060632086_00319_41455_0002.ch1.h5")
55
	parser.add_argument("-C", "--cat", help="The categories to extract, either a "
56
			"single number or a comma-separated list of numbers (default: %(default)s)",
57
			default="26,27")
58
	parser.add_argument("-c", "--clus", help="The spectral clusters to extract, either a "
59
			"single number or a comma-separated list of numbers (default: %(default)s)",
60
			default="2,3,4")
61
	parser.add_argument("-z", "--solar_id", default="D0",
62
			choices=["D0", "D1", "D2", "E0", "E1", "A0", "A1", "N1", "N2", "N3", "N4", "N5"],
63
			help="The solar reference ID to extract (default: %(default)s).")
64
	loglevels = parser.add_mutually_exclusive_group()
65
	loglevels.add_argument("-q", "--quiet", action="store_true", default=False,
66
			help="less output, same as --loglevel=ERROR (default: %(default)s)")
67
	loglevels.add_argument("-v", "--verbose", action="store_true", default=False,
68
			help="verbose output, same as --loglevel=INFO (default: %(default)s)")
69
	loglevels.add_argument("-l", "--loglevel", default="WARNING",
70
			choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
71
			help="change the loglevel (default: %(default)s)")
72
	args = parser.parse_args()
73
	if args.quiet:
74
		logging.getLogger().setLevel(logging.ERROR)
75
	elif args.verbose:
76
		logging.getLogger().setLevel(logging.INFO)
77
	else:
78
		logging.getLogger().setLevel(args.loglevel)
79
80
	cats = [n for n in map(int, args.cat.split(','))]
81
	cl_ids = [n - 1 for n in map(int, args.clus.split(','))]
82
	logging.debug("categories: %s", cats)
83
	logging.debug("cluster ids: %s", cl_ids)
84
85
	hf = h5py.File(args.file, "r")
86
87
	mlt_idxs = np.array([], dtype=int)
88
	for cat in cats:
89
		meas_cats = hf.get("/ADS/STATES")["meas_cat"]
90
		mlt_idxs = np.append(mlt_idxs, np.where(meas_cats == cat)[0])
91
	logging.info("limb state indexes: %s", mlt_idxs)
92
93
	for sid, lstate_id in enumerate(sorted(mlt_idxs)):
94
		logging.info("processing limb state nr. %s (%s)...", lstate_id, sid)
95
		slsc = slvl1c.scia_limb_scan()
96
		# read and continue to the next state if reading failed
97
		if slsc.read_from_hdf5(hf, lstate_id, sid, cl_ids):
98
			continue
99
		logging.debug("final shapes: %s (wls), %s (signal)",
100
				slsc.wls.shape, slsc.limb_data["rad"].shape)
101
		filename = "SCIA_limb_{0:04d}{1:02d}{2:02d}_{3:02d}{4:02d}{5:02d}_{6}_{7}_{8:05d}".format(
102
				slsc.date[0], slsc.date[1], slsc.date[2],
103
				slsc.date[3], slsc.date[4], slsc.date[5],
104
				slsc.orbit_state[3], slsc.orbit_state[4],
105
				slsc.orbit_state[0])
106
107
		slsc.write_to_textfile("{0}.dat".format(filename))
108
		logging.info("limb state nr. %s written to %s",
109
				lstate_id, "{0}.dat".format(filename))
110
		slsc.write_to_mpl_binary("{0}.l_mpl_binary".format(filename))
111
		logging.info("limb state nr. %s written to %s",
112
				lstate_id, "{0}.l_mpl_binary".format(filename))
113
		del slsc
114
115
	sol = slvl1c.scia_solar()
116
	sol.read_from_hdf5(hf, args.solar_id)
117
	sol_filename = ("SCIA_solar_{0:%Y%m%d}_{1:%H%M%S}_{2}_{3:05d}".format(
118
					sol.time, sol.time, sol.solar_id, sol.orbit))
119
	sol.write_to_textfile("{0}.dat".format(sol_filename))
120
	logging.info("solar reference %s written to %s",
121
			sol.solar_id, "{0}.dat".format(sol_filename))
122
	del sol
123
124
	hf.close()
125
126
127
if __name__ == "__main__":
128
	main()
129