1 #!/usr/bin/env python
2 """Pmag python library - Kurt's draft version
3
4 Hext - statistics for Anisotropy
5 s_Matrix - the diagonalized s matrix
6
7 Right now this just wraps k15_hext, k15_s, s_eigs, etc
8
9 """
10
11 # $Id: ams.py,v 1.6 2005/02/22 00:54:48 schwehr Exp $
12
13 ######################################################################
14 # Copyright (C) 2005 Kurt Schwehr
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 ######################################################################
30
31 import popen2,string
32 import sqlite # for DepthLookup
33
34 class S_Matrix:
35 """ k15_s wrapper """
36 def __init__(self,k15_lines):
37 """ Take 4 lines of k15 data and fill in the object"""
38 assert 4 == len(k15_lines)
39
40 # Use Lisa's k15_hext for now. FIX to use all python code
41 # FIX: make sure the lines all have \n at the end
42 fin,fout=popen2.popen2("k15_s")
43 for l in range(4):
44 fout.write(k15_lines[l])
45 fout.close()
46 line = fin.readline()
47 self.values = line.split()
48 for i in range(7):
49 self.values[i] = float(self.values[i])
50 self.sigma = self.values[6]
51 #print line, self.values
52
53 class Hext:
54 """ Handle Lisa's k15_hext output """
55 maxpos = 0
56 intpos = 1
57 minpos = 2
58 def __init__(self,k15_lines):
59 """Take 4 lines of k15 Kappabridge data and create a Hext stats object """
60 assert 4 == len(k15_lines)
61
62 # Use Lisa's k15_hext for now. FIX to use all python code
63 # FIX: make sure the lines all have \n at the end
64 fin,fout=popen2.popen2("k15_hext")
65 for l in range(4):
66 fout.write(k15_lines[l])
67 fout.close()
68 lines=[]
69 for l in range(5):
70 lines.append(fin.readline())
71 #print lines
72 # d is for dummy positions of things we want to just drop
73 self.sampleName,d,d,d,self.bulk_susc = lines[0].split()
74 #print self.sampleName, self.bulk_susc
75
76 d,d, self.F, d,d, self.F12,d,d,self.F23 = lines[1].split()
77 #print self.F,self.F12,self.F23
78 self.tau=[]; self.dec=[]; self.inc=[];
79 self.Eij=[]; self.Eij_dec=[]; self.Eij_inc=[];
80 self.Eik=[]; self.Eik_dec=[]; self.Eik_inc=[];
81
82 for i in range(3):
83 tau, dec, inc, Eij, Eij_dec, Eij_inc, Eik, Eik_dec, Eik_inc = lines[i+2].split()
84 self.tau.append(tau)
85 self.dec.append(dec)
86 self.inc.append(inc)
87
88 self.Eij.append(Eij)
89 self.Eij_dec.append(Eij_dec)
90 self.Eij_inc.append(Eij_inc)
91
92 self.Eik.append(Eik)
93 self.Eik_dec.append(Eik_dec)
94 self.Eik_inc.append(Eik_inc)
95
96
97 class SampleName:
98 """ Parse a Kurt style pmag sample cube name """
99 def __init__(self,nameString=""):
100 if 0==len(nameString):
101 # Make a blank object
102 self.cruise="None"
103 self.coreNum=-1
104 else:
105 #self.cruise,core,section,self.depthOffset=nameString.split("-")
106 lst = nameString.split("-")
107
108 self.cruise=lst[0]
109 # FIX: handle core numbers greater than 9
110 self.coreNum=int(lst[1][0])
111 self.coreType=lst[1][1]
112 self.coreHalf=lst[1][2]
113
114 self.section=int(lst[2][1])
115 self.depthOffset=float(lst[3])
116 # FIX: check for optional part
117 if 4==len(lst):
118 # There was no L,C,R tag for left, center, right
119 self.horizPart=None
120 else:
121 self.horizPart=lst[4]
122
123 def __str__(self):
124 """ Reconstruct the original sample name string """
125 tmpStr=self.cruise+"-"+("%d"%self.coreNum)+self.coreType+self.coreHalf
126 tmpStr+="-s" + ("%d" % self.section) + "-"
127 o = self.depthOffset
128 if ( (round(o)-0.01<o) & (round(o)+0.01>o) ):
129 tmpStr+="%03d" % int(o)
130 else:
131 tmpStr+="%03.1f" % o
132 if (self.horizPart):
133 tmpStr+="-"+self.horizPart
134 else:
135 pass # Then just drop it
136 return tmpStr
137
138
139 class DepthLookup:
140 def __init__(self, db_cx):
141 """ Lookup the section table and make a dictionary. Loads from the database section table """
142 cu = db_cx.cursor()
143 cu.execute ("SELECT cruise,corenum,section,sectopdepth FROM sections")
144 self.table = {}
145 for row in cu.fetchall():
146 key = row.cruise + "-" + str(row.corenum) + "-" + str(row.section)
147 #print "key=",key
148 self.table[key] = row.sectopdepth
149
150 def lookup(self, sampleNameStr):
151 """ return the depth in cm """
152 sn = SampleName(sampleNameStr)
153
154 key = sn.cruise + "-" + str(sn.coreNum) + '-' + str(sn.section)
155 depth = self.table[key] + sn.depthOffset
156 #print "Found: ",key, depth, "(",self.table[key],"+",sn.depthOffset,")"
157 return depth
158
159
160 # Test code
161 if __name__ == '__main__':
162 testname1="bp04-6gw-s1-064.5-L"
163 testname2="bp04-6gw-s1-064.5"
164 testname3="bp04-6gw-s1-064"
165 name0=SampleName()
166 name=SampleName(testname1); print name
167 name=SampleName(testname2); print name
168 name=SampleName(testname3); print name
169
170 k15_lines=[]
171 k15_lines.append( "bp04-6gw-s1-141.2 schwehr 1/3/05 4:33 PM 1716 -4.75\n")
172 k15_lines.append( "9.3800E+1 9.2950E+1 9.3550E+1 9.3550E+1 9.2950E+1\n" )
173 k15_lines.append( "8.9150E+1 8.9100E+1 9.2900E+1 8.9150E+1 8.8900E+1\n" )
174 k15_lines.append( "8.9950E+1 8.8750E+1 8.5100E+1 8.9950E+1 8.8800E+1\n" )
175
176 hext = Hext(k15_lines)
177 print "Tau 1 (max) = ", hext.tau[Hext.maxpos]
178 print "Tau 2 (int) = ", hext.tau[Hext.intpos]
179 print "Tau 3 (min) = ", hext.tau[Hext.minpos]
180
syntax highlighted by Code2HTML, v. 0.9.1