Passed
Push — master ( bdae2e...f0f30c )
by Stefan
03:19
created

sciapy.level1c.scia_limb_nc.write_to_netcdf()   B

Complexity

Conditions 1

Size

Total Lines 88
Code Lines 70

Duplication

Lines 88
Ratio 100 %

Code Coverage

Tests 1
CRAP Score 1.9565

Importance

Changes 0
Metric Value
cc 1
eloc 70
nop 2
dl 88
loc 88
ccs 1
cts 68
cp 0.0147
crap 1.9565
rs 7.9818
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 netcdf interface
12
"""
13
14 1
from __future__ import absolute_import, division, print_function
15
16 1
import numpy as np
17 1
try:
18 1
	from netCDF4 import Dataset as netcdf_file
19 1
	_fmtargs = {"format": "NETCDF4"}
20
except ImportError:
21
	try:
22
		from scipy.io.netcdf import netcdf_file
23
		_fmtargs = {"version": 1}
24
	except ImportError:
25
		from pupynere import netcdf_file
26
		_fmtargs = {"version": 1}
27
28 1
from ._types import _limb_data_dtype
29
30 1 View Code Duplication
def read_from_netcdf(self, filename):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
31
	"""SCIAMACHY level 1c limb scan netcdf import
32
33
	Parameters
34
	----------
35
	filename : str
36
		The netcdf filename to read the data from.
37
38
	Returns
39
	-------
40
	nothing
41
	"""
42 1
	import numpy.lib.recfunctions as rfn
43 1
	ncf = netcdf_file(filename, 'r')
44
45
	self.textheader_length = ncf.textheader_length
46
	self.textheader = ncf.textheader
47
48
	self.orbit_state = ncf.orbit_state
49
	(self.orbit, self.state_in_orbit, self.state_id,
50
		self.profiles_per_state, self.profile_in_state) = self.orbit_state
51
	self.date = ncf.date
52
	self.cent_lat_lon = ncf.cent_lat_lon
53
	self.orbit_phase = ncf.orbit_phase
54
55
	try:
56
		self.nalt = ncf.dimensions['limb'].size
57
		self.npix = ncf.dimensions['wavelength'].size
58
	except:
59
		self.nalt = ncf.dimensions['limb']
60
		self.npix = ncf.dimensions['wavelength']
61
62
	self.wls = ncf.variables['wavelength'][:]
63
64
	# pre-set the limb_data
65
	if self._limb_data_dtype is None:
66
		self._limb_data_dtype = _limb_data_dtype.copy()
67
	self.limb_data = np.zeros((self.nalt), dtype=self._limb_data_dtype)
68
69
	self.limb_data["sub_sat_lat"] = ncf.variables['sub_sat_lat'][:]
70
	self.limb_data["sub_sat_lon"] = ncf.variables['sub_sat_lon'][:]
71
	self.limb_data["tp_lat"] = ncf.variables['TP latitude'][:]
72
	self.limb_data["tp_lon"] = ncf.variables['TP longitude'][:]
73
	self.limb_data["tp_alt"] = ncf.variables['TP altitude'][:]
74
	self.limb_data["tp_sza"] = ncf.variables['TP SZA'][:]
75
	self.limb_data["tp_saa"] = ncf.variables['TP SAA'][:]
76
	self.limb_data["tp_los"] = ncf.variables['TP LOS Zenith'][:]
77
	self.limb_data["toa_sza"] = ncf.variables['TOA SZA'][:]
78
	self.limb_data["toa_saa"] = ncf.variables['TOA SAA'][:]
79
	self.limb_data["toa_los"] = ncf.variables['TOA LOS Zenith'][:]
80
	self.limb_data["sat_sza"] = ncf.variables['SAT SZA'][:]
81
	self.limb_data["sat_saa"] = ncf.variables['SAT SAA'][:]
82
	self.limb_data["sat_los"] = ncf.variables['SAT LOS Zenith'][:]
83
	self.limb_data["sat_alt"] = ncf.variables['SAT altitude'][:]
84
	self.limb_data["earth_rad"] = ncf.variables['earthradius'][:]
85
86
	tmp_rad_arr = list(ncf.variables['radiance'][:])
87
	tmp_err_arr = list(ncf.variables['radiance errors'][:])
88
89
	# save to limb_data recarray
90
	rads = np.rec.fromarrays([tmp_rad_arr],
91
				dtype=np.dtype([("rad", 'f4', (self.npix,))]))
92
	errs = np.rec.fromarrays([tmp_err_arr],
93
				dtype=np.dtype([("err", 'f4', (self.npix,))]))
94
	self.limb_data = rfn.merge_arrays([self.limb_data, rads, errs],
95
			usemask=False, asrecarray=True, flatten=True)
96
	self._limb_data_dtype = self.limb_data.dtype
97
98
	ncf.close()
99
100 1 View Code Duplication
def write_to_netcdf(self, filename):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
101
	"""SCIAMACHY level 1c limb scan netcdf export
102
103
	Parameters
104
	----------
105
	filename : str
106
		The netcdf filename to write the data to.
107
108
	Returns
109
	-------
110
	nothing
111
	"""
112
	ncf = netcdf_file(filename, 'w', **_fmtargs)
113
114
	ncf.textheader_length = self.textheader_length
115
	ncf.textheader = self.textheader
116
117
	ncf.orbit_state = self.orbit_state
118
	ncf.date = self.date
119
	ncf.cent_lat_lon = self.cent_lat_lon
120
	ncf.orbit_phase = self.orbit_phase
121
122
	ncf.createDimension('limb', self.nalt)
123
	ncf.createDimension('wavelength', self.npix)
124
125
	wavs = ncf.createVariable('wavelength', np.dtype('float32').char, ('wavelength',))
126
	wavs.units = 'nm'
127
	wavs[:] = np.asarray(self.wls)
128
129
	sslat = ncf.createVariable('sub_sat_lat', np.dtype('float32').char, ('limb',))
130
	sslat.units = 'deg'
131
	sslat[:] = np.asarray(self.limb_data["sub_sat_lat"])
132
	sslon = ncf.createVariable('sub_sat_lon', np.dtype('float32').char, ('limb',))
133
	sslon.units = 'deg'
134
	sslon[:] = np.asarray(self.limb_data["sub_sat_lon"])
135
	tp_lats = ncf.createVariable('TP latitude', np.dtype('float32').char, ('limb',))
136
	tp_lats.units = 'deg'
137
	tp_lats[:] = np.asarray(self.limb_data["tp_lat"])
138
	tp_lons = ncf.createVariable('TP longitude', np.dtype('float32').char, ('limb',))
139
	tp_lons.units = 'deg'
140
	tp_lons[:] = np.asarray(self.limb_data["tp_lon"])
141
	tp_alts = ncf.createVariable('TP altitude', np.dtype('float32').char, ('limb',))
142
	tp_alts.units = 'km'
143
	tp_alts[:] = np.asarray(self.limb_data["tp_alt"])
144
	tp_szas = ncf.createVariable('TP SZA', np.dtype('float32').char, ('limb',))
145
	tp_szas.units = 'deg'
146
	tp_szas[:] = np.asarray(self.limb_data["tp_sza"])
147
	tp_saas = ncf.createVariable('TP SAA', np.dtype('float32').char, ('limb',))
148
	tp_saas.units = 'deg'
149
	tp_saas[:] = np.asarray(self.limb_data["tp_saa"])
150
	tp_los_zeniths = ncf.createVariable('TP LOS Zenith', np.dtype('float32').char, ('limb',))
151
	tp_los_zeniths.units = 'deg'
152
	tp_los_zeniths[:] = np.asarray(self.limb_data["tp_los"])
153
	toa_szas = ncf.createVariable('TOA SZA', np.dtype('float32').char, ('limb',))
154
	toa_szas.units = 'deg'
155
	toa_szas[:] = np.asarray(self.limb_data["toa_sza"])
156
	toa_saas = ncf.createVariable('TOA SAA', np.dtype('float32').char, ('limb',))
157
	toa_saas.units = 'deg'
158
	toa_saas[:] = np.asarray(self.limb_data["toa_saa"])
159
	toa_los_zeniths = ncf.createVariable('TOA LOS Zenith', np.dtype('float32').char, ('limb',))
160
	toa_los_zeniths.units = 'deg'
161
	toa_los_zeniths[:] = np.asarray(self.limb_data["toa_los"])
162
	sat_szas = ncf.createVariable('SAT SZA', np.dtype('float32').char, ('limb',))
163
	sat_szas.units = 'deg'
164
	sat_szas[:] = np.asarray(self.limb_data["sat_sza"])
165
	sat_saas = ncf.createVariable('SAT SAA', np.dtype('float32').char, ('limb',))
166
	sat_saas.units = 'deg'
167
	sat_saas[:] = np.asarray(self.limb_data["sat_saa"])
168
	sat_los_zeniths = ncf.createVariable('SAT LOS Zenith', np.dtype('float32').char, ('limb',))
169
	sat_los_zeniths.units = 'deg'
170
	sat_los_zeniths[:] = np.asarray(self.limb_data["sat_los"])
171
	sat_alts = ncf.createVariable('SAT altitude', np.dtype('float32').char, ('limb',))
172
	sat_alts.units = 'km'
173
	sat_alts[:] = np.asarray(self.limb_data["sat_alt"])
174
	eradii_alts = ncf.createVariable('earthradius', np.dtype('float32').char, ('limb',))
175
	eradii_alts.units = 'km'
176
	eradii_alts[:] = np.asarray(self.limb_data["earth_rad"])
177
178
	rads = ncf.createVariable('radiance', np.dtype('float32').char,
179
			('limb', 'wavelength'), zlib=True, complevel=1)
180
	errs = ncf.createVariable('radiance errors', np.dtype('float32').char,
181
			('limb', 'wavelength'), zlib=True, complevel=1)
182
	rads.units = 'ph / s / cm^2 / nm'
183
	errs.units = 'ph / s / cm^2 / nm'
184
	rads[:] = np.asarray(self.limb_data["rad"]).reshape(self.nalt, self.npix)
185
	errs[:] = np.asarray(self.limb_data["err"]).reshape(self.nalt, self.npix)
186
187
	ncf.close()
188