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.3 2005/10/31 16:08:26 schwehr Exp $")
 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 for file in args:
123     print "\n------ "+file+" ------"
124     f = open (file,'r')
125     f.readline() # Skip header
126     prevwireout = None
127     prevlayback = None
128     distance = 0 # How far from the layback change is the speed value
129     foundLaybackChange = False
130     for line in f.readlines():
131         count += 1
132         if count % 10000==0: print file,count
133         if line[:3]==',,,':
134             continue  # Skip the end of csv files that have empty lines
135         fishdepth,shot,wireout,vert,layback,angle1,angle2 = 0,0,0,0,0,0,0
136         try:
137             fields  = line.split(',')
138             shot    = int(fields[shotIndex])
139             wireout = fields[wireoutIndex]
140             vert    = fields[fishdepthFBIndex]
141             layback = fields[laybackIndex]
142         except IndexError:
143             print count,'bad line',line
144             sys.exit('len of fields'+str(len(fields)))
145         if count%10000==0: print 'count,shot:',count,shot
146         speed = speeds[shot]
147         if None == speed:
148             #print 'None'
149             if layback != prevlayback or wireout != prevwireout:
150                 # We want to ditch values where the speed is not directly available
151                 #prevlayback = layback;    prevwireout = wireout
152                 print "found change in layback, but no speed at shot",shot
153                 foundLaybackChange = True
154                 distance += 1
155             skipped += 1
156             continue
157     
158         angle1 = rad2deg(acos (float(layback)/float(wireout)))
159         angle2 = rad2deg(asin (float(vert)/float(wireout)))
160         # angle1 should = angle2
161         outstr  = str(shot)
162         outstr += ' '+str(wireout)
163         outstr += ' '+str(vert)
164         outstr += ' '+str(layback)
165         outstr += ' '+('%.2f'%angle1);
166         outstr += ' '+str(speed)
167         outstr += ' '+str(distance)
168         outstr += '\n'
169 
170     
171         all.write(outstr)
172         if layback == prevlayback and wireout == prevwireout:
173             continue # only keep things where jenna calculated the layback
174         prevlayback = layback;    prevwireout = wireout
175 
176         distance = 0
177         foundLaybackChange=False
178     
179         res.write(outstr)
180         added +=1
181     
182     print 'Done making table', count,skipped, added
183     


syntax highlighted by Code2HTML, v. 0.9.1