forked from PmagPy/PmagPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
igrf.py
executable file
·153 lines (146 loc) · 5.3 KB
/
igrf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python
import pmag,sys,numpy,exceptions
#
def main():
"""
NAME
igrf.py
DESCRIPTION
This program calculates igrf field values
using the routine of Malin and Barraclough (1981)
based on d/igrfs from 1900 to 2010.
between 1900 and 1000BCE, it uses CALS3K.4 or ARCH3K.1
Prior to 1000BCE, it uses CALS10k-4b
Calculates reference field vector at specified location and time.
SYNTAX
igrf.py [-h] [-i] -f FILE [< filename]
OPTIONS:
-h prints help message and quits
-i for interactive data entry
-f FILE specify file name with input data
-F FILE specify output file name
-ages MIN MAX INCR: specify age minimum in years (+/- AD), maximum and increment, default is line by line
-loc LAT LON; specify location, default is line by line
-alt ALT; specify altitude in km, default is sealevel (0)
-plt; make a plot of the time series
-fmt [pdf,jpg,eps,svg] specify format for output figure (default is svg)
-mod [arch3k,cals3k] specify model for 3ka to 1900 AD, default is cals3k.4b
INPUT FORMAT
interactive entry:
date: decimal year
alt: altitude in km
lat: positive north
lon: positive east
for file entry:
space delimited string: date alt lat long
OUTPUT FORMAT
Declination Inclination Intensity (nT) date alt lat long
"""
plt,fmt=0,'svg'
if '-fmt' in sys.argv:
ind=sys.argv.index('-fmt')
fmt=sys.argv[ind+1]
if len(sys.argv)!=0 and '-h' in sys.argv:
print main.__doc__
sys.exit()
if '-mod' in sys.argv:
ind=sys.argv.index('-mod')
mod3k=sys.argv[ind+1]
else: mod3k=''
if '-f' in sys.argv:
ind=sys.argv.index('-f')
file=sys.argv[ind+1]
input=numpy.loadtxt(file)
elif '-i' in sys.argv:
while 1:
try:
line=[]
line.append(float(raw_input("Decimal year: <cntrl-D to quit> ")))
alt=raw_input("Elevation in km [0] ")
if alt=="":alt="0"
line.append(float(alt))
line.append(float(raw_input("Latitude (positive north) ")))
line.append(float(raw_input("Longitude (positive east) ")))
if mod3k=='':
x,y,z,f=pmag.doigrf(line[3]%360.,line[2],line[1],line[0])
else:
x,y,z,f=pmag.doigrf(line[3]%360.,line[2],line[1],line[0],mod3k=mod3k)
Dir=pmag.cart2dir((x,y,z))
print '%7.1f %7.1f %8.0f'%(Dir[0],Dir[1],f)
except EOFError:
print "\n Good-bye\n"
sys.exit()
elif '-ages' in sys.argv:
ind=sys.argv.index('-ages')
agemin=float(sys.argv[ind+1])
agemax=float(sys.argv[ind+2])
ageincr=float(sys.argv[ind+3])
if '-loc' in sys.argv:
ind=sys.argv.index('-loc')
lat=float(sys.argv[ind+1])
lon=float(sys.argv[ind+2])
else:
print "must specify lat/lon if using age range option"
sys.exit()
if '-alt' in sys.argv:
ind=sys.argv.index('-alt')
alt=float(sys.argv[ind+1])
else: alt=0
ages=numpy.arange(agemin,agemax,ageincr)
lats=numpy.ones(len(ages))*lat
lons=numpy.ones(len(ages))*lon
alts=numpy.ones(len(ages))*alt
input=numpy.array([ages,alts,lats,lons]).transpose()
else:
input=numpy.loadtxt(sys.stdin,dtype=numpy.float)
if '-F' in sys.argv:
ind=sys.argv.index('-F')
outfile=sys.argv[ind+1]
out=open(outfile,'w')
else:outfile=""
if '-plt' in sys.argv:
plt=1
import matplotlib
matplotlib.use("TkAgg")
import pylab
pylab.ion()
Ages,Decs,Incs,Ints,VADMs=[],[],[],[],[]
for line in input:
if mod3k=='':
x,y,z,f=pmag.doigrf(line[3]%360.,line[2],line[1],line[0])
else:
x,y,z,f=pmag.doigrf(line[3]%360.,line[2],line[1],line[0],mod3k=mod3k)
Dir=pmag.cart2dir((x,y,z))
if outfile!="":
out.write('%7.1f %7.1f %8.0f %7.1f %7.1f %7.1f %7.1f\n'%(Dir[0],Dir[1],f,line[0],line[1],line[2],line[3]))
elif plt:
Ages.append(line[0])
if Dir[0]>180: Dir[0]=Dir[0]-360.0
Decs.append(Dir[0])
Incs.append(Dir[1])
Ints.append(f*1e-3)
VADMs.append(pmag.b_vdm(f*1e-9,line[2])*1e-21)
else:
print '%7.1f %7.1f %8.0f %7.1f %7.1f %7.1f %7.1f'%(Dir[0],Dir[1],f,line[0],line[1],line[2],line[3])
if plt:
fig=pylab.figure(num=1,figsize=(7,9))
fig.add_subplot(411)
pylab.plot(Ages,Decs)
pylab.ylabel('Declination ($^{\circ}$)')
fig.add_subplot(412)
pylab.plot(Ages,Incs)
pylab.ylabel('Inclination ($^{\circ}$)')
fig.add_subplot(413)
pylab.plot(Ages,Ints)
pylab.ylabel('Intensity ($\mu$T)')
fig.add_subplot(414)
pylab.plot(Ages,VADMs)
pylab.ylabel('VADMs (ZAm$^2$)')
pylab.xlabel('Ages')
pylab.draw()
ans=raw_input("S[a]ve to save figure, <Return> to quit ")
if ans=='a':
pylab.savefig('igrf.'+fmt)
print 'Figure saved as: ','igrf.'+fmt
sys.exit()
main()