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