Conditions | 5 |
Total Lines | 89 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | # -*- coding: utf-8 -*- |
||
89 | def msise_model(time, alt, lat, lon, f107a, f107, ap, |
||
90 | lst=None, ap_a=None, flags=None, method="gtd7"): |
||
91 | """Interface to `gtd7()` [1]_ and `gtd7d()` [2]_ |
||
92 | |||
93 | Calls the C model function using a :class:`datetime.datetime` |
||
94 | instance to calculate the day of year, seconds and so on. |
||
95 | The `method` keyword decides which version to call, the difference |
||
96 | being that `gtd7d()` includes anomalous oxygen in the total |
||
97 | mass density (`d[5]`), `gtd7()` does not. |
||
98 | |||
99 | .. [1] https://git.linta.de/?p=~brodo/nrlmsise-00.git;a=blob;f=nrlmsise-00.c#l916 |
||
100 | .. [2] https://git.linta.de/?p=~brodo/nrlmsise-00.git;a=blob;f=nrlmsise-00.c#l1044 |
||
101 | |||
102 | Parameters |
||
103 | ---------- |
||
104 | time: datetime.datetime |
||
105 | Date and time as a `datetime.dateime`. |
||
106 | alt: float |
||
107 | Altitude in km. |
||
108 | lat: float |
||
109 | Latitude in degrees north. |
||
110 | lon: float |
||
111 | Longitude in degrees east. |
||
112 | f107a: float |
||
113 | The observed f107a (81-day running mean of f107) centred at date. |
||
114 | f107: float |
||
115 | The observed f107 value on the previous day. |
||
116 | ap: float |
||
117 | The ap value at date. |
||
118 | lst: float, optional |
||
119 | The local solar time, can be different from the calculated one. |
||
120 | ap_a: list, optional |
||
121 | List of length 7 containing ap values to be used when flags[9] is set |
||
122 | to -1, otherwise no effect. |
||
123 | flags: list, optional |
||
124 | List of length 24 setting the NRLMSIS switches explicitly. |
||
125 | method: string, optional |
||
126 | Set to "gtd7d" to use `gtd7d()` (which includes anomalous oxygen |
||
127 | in the total mass density) instead of the "standard" `gtd7()` function |
||
128 | without it. |
||
129 | |||
130 | Returns |
||
131 | ------- |
||
132 | densities: list of floats |
||
133 | 0. He number density [cm^-3] |
||
134 | 1. O number density [cm^-3] |
||
135 | 2. N2 number density [cm^-3] |
||
136 | 3. O2 number density [cm^-3] |
||
137 | 4. AR number density [cm^-3] |
||
138 | 5. total mass density [g cm^-3] (includes d[8] in gtd7d) |
||
139 | 6. H number density [cm^-3] |
||
140 | 7. N number density [cm^-3] |
||
141 | 8. Anomalous oxygen number density [cm^-3] |
||
142 | temperatures: list of floats |
||
143 | 0. Exospheric temperature [K] |
||
144 | 1. Temperature at `alt` [K] |
||
145 | |||
146 | Note |
||
147 | ---- |
||
148 | No date and time conversion will be attempted, the input needs to be |
||
149 | converted before, `dateutil` or `pandas` provide convenient functions. |
||
150 | For example to convert :class:`pandas.DatetimeIndex` |
||
151 | use ``<index>.to_pydatetime()``, |
||
152 | for :class:`astropy.time.Time` use ``<Time>.to_datetime()``. |
||
153 | |||
154 | The local solar time is calculated from time and longitude, except when |
||
155 | `lst` is set, then that value is used. |
||
156 | |||
157 | The solar and geomagnetic indices have to be provided, so far the values |
||
158 | are not included in the module. |
||
159 | """ |
||
160 | year = time.year |
||
161 | doy = int(time.strftime("%j")) |
||
162 | sec = (time.hour * 3600. |
||
163 | + time.minute * 60. |
||
164 | + time.second |
||
165 | + time.microsecond * 1e-6) |
||
166 | if lst is None: |
||
167 | lst = sec / 3600. + lon / 15.0 |
||
168 | |||
169 | kwargs = {} |
||
170 | if ap_a is not None: |
||
171 | kwargs.update({"ap_a": ap_a}) |
||
172 | if flags is not None: |
||
173 | kwargs.update({"flags": flags}) |
||
174 | |||
175 | if method == "gtd7d": |
||
176 | return gtd7d(year, doy, sec, alt, lat, lon, lst, f107a, f107, ap, **kwargs) |
||
177 | return gtd7(year, doy, sec, alt, lat, lon, lst, f107a, f107, ap, **kwargs) |
||
178 | |||
243 |