20 import math, os, unittest
21 from datetime
import date
25 def calc(self, dlat, dlon, h=0, time=date.today()):
27 time = time.year+((time - date(time.year,1,1)).days/365.0)
30 otime = oalt = olat = olon = -1000.0
32 dt = time - self.
epoch 35 rlat = math.radians(glat)
36 rlon = math.radians(glon)
37 srlon = math.sin(rlon)
38 srlat = math.sin(rlat)
39 crlon = math.cos(rlon)
40 crlat = math.cos(rlat)
47 if (alt != oalt
or glat != olat):
48 q = math.sqrt(self.
a2-self.
c2*srlat2)
50 q2 = ((q1+self.
a2)/(q1+self.
b2))*((q1+self.
a2)/(q1+self.
b2))
51 ct = srlat/math.sqrt(q2*crlat2+srlat2)
52 st = math.sqrt(1.0-(ct*ct))
53 r2 = (alt*alt)+2.0*q1+(self.
a4-self.
c4*srlat2)/(q*q)
55 d = math.sqrt(self.
a2*crlat2+self.
b2*srlat2)
57 sa = self.
c2*crlat*srlat/(r*d)
60 for m
in range(2,self.
maxord+1):
61 self.
sp[m] = self.
sp[1]*self.
cp[m-1]+self.
cp[1]*self.
sp[m-1]
62 self.
cp[m] = self.
cp[1]*self.
cp[m-1]-self.
sp[1]*self.
sp[m-1]
66 br = bt = bp = bpp = 0.0
67 for n
in range(1,self.
maxord+1):
81 if (alt != oalt
or glat != olat):
83 self.
p[m][n] = st * self.
p[m-1][n-1]
84 self.
dp[m][n] = st*self.
dp[m-1][n-1]+ct*self.
p[m-1][n-1]
86 elif (n == 1
and m == 0):
87 self.
p[m][n] = ct*self.
p[m][n-1]
88 self.
dp[m][n] = ct*self.
dp[m][n-1]-st*self.
p[m][n-1]
90 elif (n > 1
and n != m):
95 self.
p[m][n] = ct*self.
p[m][n-1]-self.
k[m][n]*self.
p[m][n-2]
96 self.
dp[m][n] = ct*self.
dp[m][n-1] - st*self.
p[m][n-1]-self.
k[m][n]*self.
dp[m][n-2]
102 self.
tc[m][n] = self.
c[m][n]+dt*self.
cd[m][n]
104 self.
tc[n][m-1] = self.
c[n][m-1]+dt*self.
cd[n][m-1]
109 par = ar*self.
p[m][n]
112 temp1 = self.
tc[m][n]*self.
cp[m]
113 temp2 = self.
tc[m][n]*self.
sp[m]
115 temp1 = self.
tc[m][n]*self.
cp[m]+self.
tc[n][m-1]*self.
sp[m]
116 temp2 = self.
tc[m][n]*self.
sp[m]-self.
tc[n][m-1]*self.
cp[m]
118 bt = bt-ar*temp1*self.
dp[m][n]
119 bp = bp + (self.
fm[m] * temp2 * par)
120 br = br + (self.
fn[n] * temp1 * par)
124 if (st == 0.0
and m == 1):
126 self.
pp[n] = self.
pp[n-1]
128 self.
pp[n] = ct*self.
pp[n-1]-self.
k[m][n]*self.
pp[n-2]
130 bpp = bpp + (self.
fm[m]*temp2*parp)
150 bh = math.sqrt((bx*bx)+(by*by))
151 ti = math.sqrt((bh*bh)+(bz*bz))
152 dec = math.degrees(math.atan2(by,bx))
153 dip = math.degrees(math.atan2(bz,bh))
162 if (math.fabs(glat) >= 55.):
163 if (glat > 0.0
and glon >= 0.0):
165 if (glat > 0.0
and glon < 0.0):
166 gv = dec+math.fabs(glon);
167 if (glat < 0.0
and glon >= 0.0):
169 if (glat < 0.0
and glon < 0.0):
170 gv = dec-math.fabs(glon)
200 wmm_filename = os.path.join(os.path.dirname(__file__),
'WMM.COF')
202 with open(wmm_filename)
as wmm_file:
203 for line
in wmm_file:
204 linevals = line.strip().split()
205 if len(linevals) == 3:
209 elif len(linevals) == 6:
210 linedict = {
'n': int(float(linevals[0])),
211 'm': int(float(linevals[1])),
212 'gnm': float(linevals[2]),
213 'hnm': float(linevals[3]),
214 'dgnm': float(linevals[4]),
215 'dhnm': float(linevals[5])}
218 z = [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
220 self.
tc = [z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13]]
226 self.
p = [z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14]]
228 self.
dp = [z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13]]
230 self.
b = 6356.7523142
239 self.
c = [z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14]]
240 self.
cd = [z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14],z[0:14]]
254 self.
cd[n][m-1] = dhnm
257 self.
snorm = [z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13]]
258 self.
snorm[0][0] = 1.0
259 self.
k = [z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13],z[0:13]]
261 self.
fn = [0.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0]
262 self.
fm = [0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0]
263 for n
in range(1,self.
maxord+1):
264 self.
snorm[0][n] = self.
snorm[0][n-1]*(2.0*n-1)/n
271 self.
k[m][n] = (((n-1)*(n-1))-(m*m))/((2.0*n-1)*(2.0*n-3.0))
273 flnmj = ((n-m+1.0)*j)/(n+m)
274 self.
snorm[m][n] = self.
snorm[m-1][n]*math.sqrt(flnmj)
276 self.
c[n][m-1] = self.
snorm[m][n]*self.
c[n][m-1]
277 self.
cd[n][m-1] = self.
snorm[m][n]*self.
cd[n][m-1]
278 self.
c[m][n] = self.
snorm[m][n]*self.
c[m][n]
279 self.
cd[m][n] = self.
snorm[m][n]*self.
cd[m][n]
290 (d1, 0, 80, 0, -6.13),
291 (d1, 0, 0, 120, 0.97),
292 (d1, 0, -80, 240, 70.21),
293 (d1, 328083.99, 80, 0, -6.57),
294 (d1, 328083.99, 0, 120, 0.94),
295 (d1, 328083.99, -80, 240, 69.62),
296 (d2, 0, 80, 0, -5.21),
297 (d2, 0, 0, 120, 0.88),
298 (d2, 0, -80, 240, 70.04),
299 (d2, 328083.99, 80, 0, -5.63),
300 (d2, 328083.99, 0, 120, 0.86),
301 (d2, 328083.99, -80, 240, 69.45),
307 calcval=gm.GeoMag(values[2], values[3], values[1], values[0])
308 self.assertAlmostEqual(values[4], calcval.dec, 2,
'Expected %s, result %s' % (values[4], calcval.dec))
310 if __name__ ==
'__main__':
def __init__(self, wmm_filename=None)
def test_declination(self)
def calc(self, dlat, dlon, h=0, time=date.today())