| Conditions | 4 |
| Total Lines | 68 |
| Code Lines | 29 |
| 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:
| 1 | # Copyright (C) 2019 NRL |
||
| 141 | def igrf_dipole_axis(date): |
||
| 142 | """Get Cartesian unit vector pointing at dipole pole in the north (IGRF). |
||
| 143 | |||
| 144 | Parameters |
||
| 145 | ---------- |
||
| 146 | date : dt.datetime |
||
| 147 | Date and time |
||
| 148 | |||
| 149 | Returns |
||
| 150 | ------- |
||
| 151 | m_0 : np.ndarray |
||
| 152 | Cartesian 3 element unit vector pointing at dipole pole in the north |
||
| 153 | (geocentric coords) |
||
| 154 | |||
| 155 | Notes |
||
| 156 | ----- |
||
| 157 | IGRF coefficients are read from the igrf12coeffs.txt file. It should also |
||
| 158 | work after IGRF updates. The dipole coefficients are interpolated to the |
||
| 159 | date, or extrapolated if date > latest IGRF model |
||
| 160 | |||
| 161 | """ |
||
| 162 | # Get time in years, as float |
||
| 163 | year = date.year |
||
| 164 | doy = date.timetuple().tm_yday |
||
| 165 | year_days = dt.date(date.year, 12, 31).timetuple().tm_yday |
||
| 166 | year = year + doy / year_days |
||
| 167 | |||
| 168 | # Read the IGRF coefficients |
||
| 169 | with open(aacgmv2.IGRF_COEFFS) as f_igrf: |
||
| 170 | lines = f_igrf.readlines() |
||
| 171 | |||
| 172 | years = lines[3].split()[3:][:-1] |
||
| 173 | years = np.array(years, dtype=float) # time array |
||
| 174 | |||
| 175 | g10 = lines[4].split()[3:] |
||
| 176 | g11 = lines[5].split()[3:] |
||
| 177 | h11 = lines[6].split()[3:] |
||
| 178 | |||
| 179 | # Secular variation coefficients (for extrapolation) |
||
| 180 | g10sv = np.float32(g10[-1]) |
||
| 181 | g11sv = np.float32(g11[-1]) |
||
| 182 | h11sv = np.float32(h11[-1]) |
||
| 183 | |||
| 184 | # Model coefficients: |
||
| 185 | g10 = np.array(g10[:-1], dtype=float) |
||
| 186 | g11 = np.array(g11[:-1], dtype=float) |
||
| 187 | h11 = np.array(h11[:-1], dtype=float) |
||
| 188 | |||
| 189 | # Get the gauss coefficient at given time: |
||
| 190 | if year <= years[-1] and year >= years[0]: |
||
| 191 | # Regular interpolation |
||
| 192 | g10 = np.interp(year, years, g10) |
||
| 193 | g11 = np.interp(year, years, g11) |
||
| 194 | h11 = np.interp(year, years, h11) |
||
| 195 | else: |
||
| 196 | # Extrapolation |
||
| 197 | dyear = year - years[-1] |
||
| 198 | g10 = g10[-1] + g10sv * dyear |
||
| 199 | g11 = g11[-1] + g11sv * dyear |
||
| 200 | h11 = h11[-1] + h11sv * dyear |
||
| 201 | |||
| 202 | # Calculate pole position |
||
| 203 | B_0 = np.sqrt(g10**2 + g11**2 + h11**2) |
||
| 204 | |||
| 205 | # Calculate output |
||
| 206 | m_0 = -np.array([g11, h11, g10]) / B_0 |
||
| 207 | |||
| 208 | return m_0 |
||
| 209 |