Package ais :: Module ais_uscg_splitstations
[hide private]
[frames] | no frames]

Source Code for Module ais.ais_uscg_splitstations

  1  #!/usr/bin/env python 
  2   
  3  __version__ = '$Revision: 4791 $'.split()[1] 
  4  __date__ = '$Date: 2007-01-02 $'.split()[1] 
  5  __author__ = 'xmlbinmsg' 
  6   
  7  __doc__=''' 
  8   
  9  Split USCG N-AIS messages into separate stations 
 10   
 11  @requires: U{epydoc<http://epydoc.sourceforge.net/>} > 3.0alpha3 
 12   
 13  @author: '''+__author__+''' 
 14  @version: ''' + __version__ +''' 
 15  @var __date__: Date of last svn commit 
 16  @undocumented: __version__ __author__ __doc__ parser 
 17  @status: under development 
 18  @license: GPL 
 19  ''' 
 20   
 21  import sys, os 
 22   
23 -def getStation(line, withR=False):
24 fields=line.split(',') 25 foundChecksum=False 26 for f in fields: 27 # must come after the checksum field 28 if len(f)<1: continue 29 if not foundChecksum: 30 if -1==f.find('*'): continue # FIX: Is this sufficient to make sure this is a checksum? 31 #if '0'==f[0]: continue 32 foundChecksum=True 33 continue 34 if len(f)<2: continue 35 if f[0] != 'r': continue 36 if withR: return f 37 return f[1:] 38 return None
39 40
41 -def splitstations(logfile, subdir=None, basename=None, withR=False, verbose=False, stationSubdirs=False):
42 ''' 43 @param logfile: file like object to read from 44 @param subdir: put the files in a subdirectory 45 @param withR: keep the r in front of the station name 46 @param verbose: be loud 47 ''' 48 if subdir!=None and not os.access(subdir,os.X_OK): #F_OK): 49 os.mkdir(subdir) 50 else: subdir='.' 51 52 stations={} 53 for line in logfile: 54 station = getStation(line) 55 if None==station: continue 56 57 # Handle opening the file if have not seen the station before 58 if station not in stations: 59 if verbose: print 'New station:',station 60 filename = subdir + '/' + station 61 if stationSubdirs: 62 if not os.access(filename,os.X_OK): os.mkdir(filename) 63 filename += '/log.ais' 64 stations[station] = file(filename,'a') 65 66 stations[station].write(line) 67 68 69 if verbose: 70 print 'Finished file. Station count =',len(stations)
71 72 73 if __name__=='__main__': 74 75 from optparse import OptionParser 76 parser = OptionParser(usage="%prog [options]", version="%prog "+__version__) 77 78 # parser.add_option('--doc-test',dest='doctest',default=False,action='store_true', 79 # help='run the documentation tests') 80 # parser.add_option('--unit-test',dest='unittest',default=False,action='store_true', 81 # help='run the unit tests') 82 parser.add_option('-b','--base-name',dest='basename',default=None, 83 help='prepend to each station name [default: %default]') 84 parser.add_option('-s','--subdir',dest='subdir',default=None, 85 help='Put all the stations in a subdirectory [default: %default]') 86 parser.add_option('-S','--with-station-subdirs',dest='withStationSubdirs',default=False,action='store_true', 87 help='Make the station name be a subdir with log.ais as the filename') 88 parser.add_option('-r','--with-r',dest='withR',default=False,action='store_true', 89 help='Keep the r in the station name') 90 parser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true', 91 help='Make the test output verbose') 92 93 (options,args) = parser.parse_args() 94 success=True 95 96 for filename in args: 97 if options.verbose: print 'Processing file:',filename 98 logfile = open(filename) 99 splitstations(logfile, options.subdir, options.basename, options.withR, options.verbose,options.withStationSubdirs) 100