1 #!/usr/bin/env python
  2 """
  3 Parse Jenna's monster excel spreadsheets to get the layback info.
  4 Want to know what the layback-wireout-angle-speed relationship is.
  5 """
  6 __author__="Kurt Schwehr"
  7 
  8 import sys
  9 
 10 from optparse import OptionParser
 11 myparser = OptionParser(usage="%prog [options]",version="%prog $Id: cracks_cruise_layback.py,v 1.4 2005/10/31 16:37:38 schwehr Exp schwehr $")
 12 (options,args) = myparser.parse_args()
 13 
 14 if len(args)==0: sys.exit("ERROR: Need to specify the csv files on the command line")
 15 
 16 #
 17 # Show the field numbers
 18 #
 19 f = open('line41_43_lb.csv','r')
 20 fieldnames = f.readline().strip().split(',')
 21 for i in range(len(fieldnames)):
 22     print '#',i, fieldnames[i]
 23 
 24 # The excel CSV header line looks like this:
 25 # 0 Line
 26 # 1 Shot
 27 # 2 Lat
 28 # 3 Lon
 29 # 4 Year
 30 # 5 JD:Hr:Min:Sec
 31 # 6 Log Bk Line#
 32 # 7 Wire Out #
 33 # 8 Wire Out (m)
 34 # 9 Seafloor TWT
 35 # 10 FWC TWT
 36 # 11 Fish Depth (m)
 37 # 12 FD + Freeboard
 38 # 13 Layback (m)
 39 # 14 Lat Offset (m) 
 40 # 15 Lon Offset (m)
 41 # 16 Lat Offset (dd)
 42 # 17 Lon Offset (dd)
 43 # 18 Dir
 44 # 19 Lat (Corr)
 45 # 20 Lon (Corr)
 46 
 47 
 48 ########################################
 49 # Load the speed table from disk.  If there are 2 shotpoints with the
 50 # same number, ditch both
 51 
 52 speedfile = open('speeds.dat')
 53 speeds = [None]*int(2.3e6) # Make a HUGE lookup table
 54 count = 0
 55 added = 0
 56 skipped = 0
 57 print 'speed length',len(speeds)
 58 for line in speedfile.xreadlines():
 59     if count%100000==0: print count
 60     count += 1
 61     #if count%10!=0: continue # Debugging load the table faster
 62     shot,speed = line.split()
 63     shot = int(shot)
 64     speed = float(speed)
 65     try:
 66         if speeds[shot] == None:
 67             speeds[shot] = speed
 68             added += 1
 69             if speed>8 or speed<-8: # There are some really wrong speeds in the table
 70                 speeds[shot]=None
 71                 print 'speed',speed
 72                 
 73         else:
 74             speeds[shot] = None  # Nuke both shots with this shotpoint
 75             skipped += 1
 76     except IndexError:
 77         sys.exit('out of range: '+str(shot))
 78 
 79 print 'Loaded speed table... count, skipped, added: ', count, skipped, added
 80 
 81 
 82 ########################################
 83 # Dump the actual speed table to disk
 84 
 85 s = open('speeds.check','w')
 86 count = 0
 87 for i in range(len(speeds)):
 88     if i%4 != 0: continue
 89     speed = speeds[i]
 90     if speed == None: continue #speed = -1
 91     else: count += 1
 92     s.write(str(i)+' '+str(speed)+'\n')
 93 del(s)
 94 print 'wrote this many speed values:',count
 95 
 96 ########################################
 97 # Scan through files
 98 
 99 shotIndex = 1
100 wireoutIndex = 8 # meters
101 fishdepthFBIndex = 12 # Fishdepth + freeboard (dist from block to sea surface)
102 laybackIndex = 13
103 
104 from math import acos,asin
105 from units import rad2deg  # pmag-kds-py
106 
107 all = open('all-results.dat','w')
108 all.write('#   1     2     3     4       5      6        7\n')
109 all.write('# shot wireout vert layback angle1 speed  shot_offset_for_speed\n\n')
110 
111 res = open('results.dat','w')
112 res.write('#   1     2     3     4       5      6        7\n')
113 res.write('# shot wireout vert layback angle1 speed  shot_offset_for_speed\n\n')
114 
115 # FIX: make sure that if there is no speed that if there is a layback
116 # change, I don't pick it up when the speed starts up again.
117 
118 count = 0
119 skipped = 0
120 added = 0
121 
122 results = [] # Cache the results so can do a least squares fit
123 resultsSpeed = []
124 resultsWireout = []
125 #resultsLayback = []
126 resultsAngle1 = []
127 
128 for file in args:
129     print "\n------ "+file+" ------"
130     f = open (file,'r')
131     f.readline() # Skip header
132     prevwireout = None
133     prevlayback = None
134     distance = 0 # How far from the layback change is the speed value
135     foundLaybackChange = False
136     for line in f.readlines():
137         count += 1
138         if count % 10000==0: print file,count
139         if line[:3]==',,,':
140             continue  # Skip the end of csv files that have empty lines
141         fishdepth,shot,wireout,vert,layback,angle1,angle2 = 0,0,0,0,0,0,0
142         try:
143             fields  = line.split(',')
144             shot    = int(fields[shotIndex])
145             wireout = fields[wireoutIndex]
146             vert    = fields[fishdepthFBIndex]
147             layback = fields[laybackIndex]
148         except IndexError:
149             print count,'bad line',line
150             sys.exit('len of fields'+str(len(fields)))
151         if count%10000==0: print 'count,shot:',count,shot
152         speed = speeds[shot]
153         if None == speed:
154             #print 'None'
155             if layback != prevlayback or wireout != prevwireout:
156                 # We want to ditch values where the speed is not directly available
157                 #prevlayback = layback;    prevwireout = wireout
158                 print "found change in layback, but no speed at shot",shot
159                 foundLaybackChange = True
160                 distance += 1
161             skipped += 1
162             continue
163     
164         angle1 = rad2deg(acos (float(layback)/float(wireout)))
165         angle2 = rad2deg(asin (float(vert)/float(wireout)))
166         # angle1 should = angle2
167         outstr  = str(shot)
168         outstr += ' '+str(wireout)
169         outstr += ' '+str(vert)
170         outstr += ' '+str(layback)
171         outstr += ' '+('%.2f'%angle1);
172         outstr += ' '+str(speed)
173         outstr += ' '+str(distance)
174         outstr += '\n'
175 
176     
177         all.write(outstr)
178         if layback == prevlayback and wireout == prevwireout:
179             continue # only keep things where jenna calculated the layback
180         prevlayback = layback;    prevwireout = wireout
181 
182         results.append((shot,wireout,vert,layback,angle1,speed,distance))
183         resultsSpeed.append(speed)
184         resultsWireout.append(wireout)
185         resultsAngle1.append(angle1)
186 
187         distance = 0
188         foundLaybackChange=False
189     
190         res.write(outstr)
191         added +=1
192     
193     print 'Done making table', count,skipped, added


syntax highlighted by Code2HTML, v. 0.9.1