forked from PmagPy/PmagPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_samples.py
executable file
·92 lines (88 loc) · 3.29 KB
/
convert_samples.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
#!/usr/bin/env python
import string,sys,pmag,exceptions
#
#
def main():
"""
NAME
convert_samples.py
DESCRIPTION
takes an er_samples or magic_measurements format file and creates an orient.txt template
SYNTAX
convert_samples.py [command line options]
OPTIONS
-f FILE: specify input file, default is er_samples.txt
-F FILE: specify output file, default is: orient_LOCATION.txt
INPUT FORMAT
er_samples.txt or magic_measurements format file
OUTPUT
orient.txt format file
"""
#
# initialize variables
#
version_num=pmag.get_version()
orient_file,samp_file = "orient","er_samples.txt"
args=sys.argv
dir_path,out_path='.','.'
default_outfile = True
#
#
if '-WD' in args:
ind=args.index('-WD')
dir_path=args[ind+1]
if '-OD' in args:
ind=args.index('-OD')
out_path=args[ind+1]
if "-h" in args:
print main.__doc__
sys.exit()
if "-F" in args:
ind=args.index("-F")
orient_file=sys.argv[ind+1]
default_outfile = False
if "-f" in args:
ind=args.index("-f")
samp_file=sys.argv[ind+1]
orient_file=out_path+'/'+orient_file
samp_file=dir_path+'/'+samp_file
#
# read in file to convert
#
ErSamples=[]
Required=['sample_class','sample_type','sample_lithology','lat','long']
Samps,file_type=pmag.magic_read(samp_file)
Locs=[]
OrKeys=['sample_name','site_name','mag_azimuth','field_dip','sample_class','sample_type','sample_lithology','lat','long','stratigraphic_height','method_codes','site_description']
print "file_type", file_type # LJ
if file_type.lower()=='er_samples':
SampKeys=['er_sample_name','er_site_name','sample_azimuth','sample_dip','sample_class','sample_type','sample_lithology','sample_lat','sample_lon','sample_height','magic_method_codes','er_sample_description']
elif file_type.lower()=='magic_measurements':
SampKeys=['er_sample_name','er_site_name']
else:
print 'wrong file format; must be er_samples or magic_measurements only'
for samp in Samps:
if samp['er_location_name'] not in Locs:Locs.append(samp['er_location_name']) # get all the location names
for location_name in Locs:
loc_samps=pmag.get_dictitem(Samps,'er_location_name',location_name,'T')
OrOut=[]
for samp in loc_samps:
if samp['er_sample_name'] not in ErSamples:
ErSamples.append(samp['er_sample_name'])
OrRec={}
if 'sample_date' in samp.keys() and samp['sample_date'].strip()!="":
date=samp['sample_date'].split(':')
OrRec['date']=date[1]+'/'+date[2]+'/'+date[0][2:4]
for i in range(len(SampKeys)):
if SampKeys[i] in samp.keys():OrRec[OrKeys[i]]=samp[SampKeys[i]]
for key in Required:
if key not in OrRec.keys():OrRec[key]="" # fill in blank required keys
OrOut.append(OrRec)
loc=location_name.replace(" ","_")
if default_outfile:
outfile=orient_file+'_'+loc+'.txt'
else:
outfile=orient_file
pmag.magic_write(outfile,OrOut,location_name)
print "Data saved in: ", outfile
main()