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