forked from Caoimhinmg/PmagPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_IODP_jr6_magic.py
246 lines (229 loc) · 9.65 KB
/
old_IODP_jr6_magic.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env python
import pandas as pd
import string,sys,pmag
import numpy as np
import os
def main(command_line=True, **kwargs):
"""
NAME
IODP_jr6_magic.py
DESCRIPTION
converts shipboard .jr6 format files to magic_measurements format files
SYNTAX
IODP_jr6_magic.py [command line options]
OPTIONS
-h: prints the help message and quits.
-f FILE: specify input file, or
-F FILE: specify output file, default is magic_measurements.txt
-fsa FILE: specify er_samples.txt file for sample name lookup ,
default is 'er_samples.txt'
-loc HOLE : specify hole name (U1456A)
-A: don't average replicate measurements
INPUT
JR6 .jr6 format file
"""
def fix_separation(filename, new_filename):
old_file = open(filename, 'rU')
data = old_file.readlines()
new_data = []
for line in data:
new_line = line.replace('-', ' -')
new_line = new_line.replace(' ', ' ')
new_data.append(new_line)
new_file = open(new_filename, 'w')
for s in new_data:
new_file.write(s)
old_file.close()
new_file.close()
return new_filename
def old_fix_separation(filename, new_filename):
old_file = open(filename, 'rU')
data = old_file.readlines()
new_data = []
for line in data:
new_line = []
for i in line.split():
if '-' in i[1:]:
lead_char = '-' if i[0] == '-' else ''
if lead_char:
v = i[1:].split('-')
else:
v = i.split('-')
new_line.append(lead_char + v[0])
new_line.append('-' + v[1])
else:
new_line.append(i)
new_line = (' '.join(new_line)) + '\n'
new_data.append(new_line)
new_file = open(new_filename, 'w')
for s in new_data:
new_file.write(s)
new_file.close()
old_file.close()
return new_filename
# initialize some stuff
noave=0
volume=2.5**3 #default volume is a 2.5cm cube
inst=""
samp_con,Z='5',""
missing=1
demag="N"
er_location_name="unknown"
citation='This study'
args=sys.argv
meth_code="LP-NO"
version_num=pmag.get_version()
dir_path='.'
MagRecs=[]
samp_file = 'er_samples.txt'
meas_file = 'magic_measurements.txt'
mag_file = ''
#
# get command line arguments
#
if command_line:
if '-WD' in sys.argv:
ind = sys.argv.index('-WD')
dir_path=sys.argv[ind+1]
if '-ID' in sys.argv:
ind = sys.argv.index('-ID')
input_dir_path = sys.argv[ind+1]
else:
input_dir_path = dir_path
output_dir_path = dir_path
if "-h" in args:
print main.__doc__
return False
if '-F' in args:
ind=args.index("-F")
meas_file = args[ind+1]
if '-fsa' in args:
ind = args.index("-fsa")
samp_file = args[ind+1]
if samp_file[0]!='/':
samp_file = os.path.join(input_dir_path, samp_file)
try:
open(samp_file,'rU')
ErSamps,file_type=pmag.magic_read(samp_file)
except:
print samp_file,' not found: '
print ' download csv file and import to MagIC with IODP_samples_magic.py'
if '-f' in args:
ind = args.index("-f")
mag_file= args[ind+1]
if "-loc" in args:
ind=args.index("-loc")
er_location_name=args[ind+1]
if "-A" in args:
noave=1
if not command_line:
dir_path = kwargs.get('dir_path', '.')
input_dir_path = kwargs.get('input_dir_path', dir_path)
output_dir_path = dir_path
meas_file = kwargs.get('meas_file', 'magic_measurements.txt')
mag_file = kwargs.get('mag_file', '')
samp_file = kwargs.get('samp_file', 'er_samples.txt')
specnum = kwargs.get('specnum', 1)
samp_con = kwargs.get('samp_con', '1')
if len(str(samp_con)) > 1:
samp_con, Z = samp_con.split('-')
else:
Z = ''
er_location_name = kwargs.get('er_location_name', '')
noave = kwargs.get('noave', 0) # default (0) means DO average
meth_code = kwargs.get('meth_code', "LP-NO")
# format variables
meth_code=meth_code+":FS-C-DRILL-IODP:SP-SS-C:SO-V"
meth_code=meth_code.strip(":")
if mag_file:
mag_file = os.path.join(input_dir_path, mag_file)
samp_file = os.path.join(input_dir_path, samp_file)
meas_file = os.path.join(output_dir_path, meas_file)
# validate variables
if not mag_file:
print "You must provide an IODP_jr6 format file"
return False, "You must provide an IODP_jr6 format file"
if not os.path.exists(mag_file):
print 'The input file you provided: {} does not exist.\nMake sure you have specified the correct filename AND correct input directory name.'.format(os.path.join(input_dir_path, mag_file))
return False, 'The input file you provided: {} does not exist.\nMake sure you have specified the correct filename AND correct input directory name.'.format(magfile)
if not os.path.exists(samp_file):
print 'samp_file', samp_file
print "Your input directory:\n{}\nmust contain an er_samples.txt file, or you must explicitly provide one".format(input_dir_path)
return False, "Your input directory:\n{}\nmust contain an er_samples.txt file, or you must explicitly provide one".format(input_dir_path)
# parse data
temp = os.path.join(output_dir_path, 'temp.txt')
fix_separation(mag_file, temp)
#os.rename('temp.txt', mag_file)
#data = open(mag_file, 'rU').readlines()
data=pd.read_csv(temp, delim_whitespace=True,header=None)
os.remove(temp)
samples,filetype = pmag.magic_read(samp_file)
data.columns=['specname','step','negz','y','x','expon','sample_azimuth','sample_dip','sample_bed_dip_direction','sample_bed_dip','bed_dip_dir2','bed_dip2','param1','param2','param3','param4','measurement_csd']
cart=np.array([data['x'],data['y'],-data['negz']]).transpose()
dir= pmag.cart2dir(cart).transpose()
data['measurement_dec']=dir[0]
data['measurement_inc']=dir[1]
data['measurement_magn_volume']=dir[2]*(10.0**data['expon']) # A/m - data in A/m
data['measurement_flag']='g'
data['measurement_standard']='u'
data['measurement_number']='1'
data['measurement_temp']='273'
data['er_location_name']=er_location_name
for rowNum, row in data.iterrows():
MagRec={}
spec_text_id=row['specname'].split('_')[1]
SampRecs=pmag.get_dictitem(samples,'er_sample_alternatives',spec_text_id,'has') # retrieve sample record for this specimen
if len(SampRecs)>0: # found one
MagRec['er_specimen_name']=SampRecs[0]['er_sample_name']
MagRec['er_sample_name']=MagRec['er_specimen_name']
MagRec['er_site_name']=MagRec['er_specimen_name']
MagRec["er_citation_names"]="This study"
MagRec['er_location_name']=er_location_name
MagRec['magic_software_packages']=version_num
MagRec["treatment_temp"]='%8.3e' % (273) # room temp in kelvin
MagRec["measurement_temp"]='%8.3e' % (273) # room temp in kelvin
MagRec["measurement_flag"]='g'
MagRec["measurement_standard"]='u'
MagRec["measurement_number"]='1'
MagRec["treatment_ac_field"]='0'
volume=float(SampRecs[0]['sample_volume'])
moment=row['measurement_magn_volume'] * volume
MagRec["measurement_magn_moment"]=str(moment)
MagRec["measurement_magn_volume"]=str(row['measurement_magn_volume'])
MagRec["measurement_dec"]='%7.1f'%(row['measurement_dec'])
MagRec["measurement_inc"]='%7.1f'%(row['measurement_inc'])
if row['step'] == 'NRM':
meas_type="LT-NO"
elif row['step'][0:2] == 'AD':
meas_type="LT-AF-Z"
treat=float(row['step'][2:])
MagRec["treatment_ac_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
elif row['step'][0] == 'TD':
meas_type="LT-T-Z"
treat=float(row['step'][2:])
MagRec["treatment_temp"]='%8.3e' % (treat+273.) # temp in kelvin
elif row['step'][0:3]=='ARM': #
meas_type="LT-AF-I"
treat=float(row['step'][3:])
MagRec["treatment_ac_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
MagRec["treatment_dc_field"]='%8.3e' %(50e-6) # assume 50uT DC field
MagRec["measurement_description"]='Assumed DC field - actual unknown'
elif row['step'][0:3]=='IRM': #
meas_type="LT-IRM"
treat=float(row['step'][3:])
MagRec["treatment_dc_field"]='%8.3e' %(treat*1e-3) # convert from mT to tesla
else:
print 'unknown treatment type for ',row
return False, 'unknown treatment type for ',row
MagRec['magic_method_codes']=meas_type
MagRecs.append(MagRec.copy())
else:
print 'sample name not found: ',row['specname']
MagOuts=pmag.measurements_methods(MagRecs,noave)
file_created, error_message = pmag.magic_write(meas_file,MagOuts,'magic_measurements')
if file_created:
return True, meas_file
else:
return False, 'Results not written to file'
if __name__ == '__main__':
main()