| Conditions | 3 |
| Total Lines | 70 |
| Code Lines | 30 |
| 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 |
||
| 139 | def igrf_dipole_axis(date): |
||
| 140 | """Get Cartesian unit vector pointing at dipole pole in the north, |
||
| 141 | according to IGRF |
||
| 142 | |||
| 143 | Parameters |
||
| 144 | ------------- |
||
| 145 | date : (dt.datetime) |
||
| 146 | Date and time |
||
| 147 | |||
| 148 | Returns |
||
| 149 | ---------- |
||
| 150 | m_0: (np.ndarray) |
||
| 151 | Cartesian 3 element unit vector pointing at dipole pole in the north |
||
| 152 | (geocentric coords) |
||
| 153 | |||
| 154 | Notes |
||
| 155 | ---------- |
||
| 156 | IGRF coefficients are read from the igrf12coeffs.txt file. It should also |
||
| 157 | work after IGRF updates. The dipole coefficients are interpolated to the |
||
| 158 | date, or extrapolated if date > latest IGRF model |
||
| 159 | """ |
||
| 160 | warnings.warn(dep_str, category=FutureWarning) |
||
| 161 | |||
| 162 | # get time in years, as float: |
||
| 163 | year = date.year |
||
| 164 | doy = date.timetuple().tm_yday |
||
| 165 | year_days = int(dt.date(date.year, 12, 31).strftime("%j")) |
||
| 166 | year = year + doy / year_days |
||
| 167 | |||
| 168 | # read the IGRF coefficients |
||
| 169 | with open(aacgmv2.IGRF_COEFFS, 'r') 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]: |
||
| 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 |