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