Changeset 29

Show
Ignore:
Timestamp:
02/02/12 20:18:15
Author:
cbc
Message:

Modify in preparation of Nobletec and KML output.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • track/trunk/track.py

    r28 r29  
    66Usage: 
    77 
    8    python track.py glider_name start_date end_date 
    9  
    10    where start_date and end_date are of the form: YYYYMMDD. 
    11  
    12    start_date is inclusive. stop_date is exclusive. 
    13    An exception is raised if start_date < stop_date. 
     8   python track.py [positions [start_date [end_date]]] 
     9 
     10   where (optional) positions are the number of positions to report, and 
     11 
     12   where (optional) start_date and end_date are of the form: YYYYMMDD. 
     13 
     14       start_date is inclusive; stop_date is exclusive. 
     15 
     16   An exception is raised if start_date > stop_date. 
    1417""" 
    1518 
     
    2023from glob import glob 
    2124 
    22 glider_name = 'pelagia' 
    23 comm_names = ['freewave','network',] 
    24 start_date = '20120125' # inclusive 
    25 stop_date = '20120127'  # exclusive 
     25glider_dir = '/var/opt/gmc/gliders' 
     26gliders = ['pelagia', 'ramses'] 
     27comms = ['freewave', 'network', ] 
     28positions = 10  # report most recent number of positions 
     29tomorrow = datetime.now() + timedelta(1) 
     30start_date = '20120125'                       # inclusive 
     31stop_date = '%04d%02d%02d' % (tomorrow.year,  # exclusive 
     32                              tomorrow.month, 
     33                              tomorrow.day) 
    2634file_pattern_template = "%s_%s_%04d%02d%02dT*.log" 
    27 p_pattern = re.compile(r"^GPS Location:\s+(\S+)\s+\S\s+(\S+)\s+\S\s+\S+\s+(\S+)\s+") 
    28 c_pattern = re.compile(r"^Curr Time:\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+") 
     35p_pattern = re.compile(r"^GPS Location:\s+(\S+)\s+\S\s+(\S+)" 
     36                       r"\s+\S\s+\S+\s+(\S+)\s+") 
     37c_pattern = re.compile(r"^Curr Time:\s+(\S+)\s+(\S+)\s+(\S+)" 
     38                       r"\s+(\S+)\s+(\S+)\s+") 
    2939bad_coordinate = "69696969.000" 
    3040bad_freshness = "1e+308" 
    31 stale = 60.0 
    32 months = {'Jan':1, 
    33           'Feb':2, 
    34           'Mar':3, 
    35           'Apr':4, 
    36           'May':5, 
    37           'Jun':6, 
    38           'Jul':7, 
    39           'Aug':8, 
    40           'Sep':9, 
    41           'Oct':10, 
    42           'Nov':11, 
    43           'Dec':12, 
    44          } 
    45  
    46 def find(glider_name=glider_name, 
     41stale = 120.0  # position must not be older than this many seconds 
     42delta = timedelta(seconds=600)  # positions must be this many seconds apart 
     43months = {'Jan': 1, 
     44          'Feb': 2, 
     45          'Mar': 3, 
     46          'Apr': 4, 
     47          'May': 5, 
     48          'Jun': 6, 
     49          'Jul': 7, 
     50          'Aug': 8, 
     51          'Sep': 9, 
     52          'Oct': 10, 
     53          'Nov': 11, 
     54          'Dec': 12} 
     55shtnom = {} 
     56for month in months: 
     57    shtnom[months[month]] = month 
     58weekdays = {0: 'Mon', 
     59            1: 'Tue', 
     60            2: 'Wed', 
     61            3: 'Thu', 
     62            4: 'Fri', 
     63            5: 'Sat', 
     64            6: 'Sun'} 
     65 
     66 
     67def find(gliders=gliders, 
    4768         start_date=start_date, 
    4869         stop_date=stop_date): 
     
    5374                             int(stop_date[4:6]), 
    5475                             int(stop_date[6:])) 
    55     assert start_datetime < stop_datetime 
     76    try: 
     77        assert start_datetime < stop_datetime 
     78    except AssertionError: 
     79        print >> sys.stderr, "Start date is not less than stop date." 
     80        print __doc__ 
     81        sys.exit(2) 
    5682 
    5783    date = start_datetime 
     
    6187        date += timedelta(1) 
    6288 
    63     file_patterns = [file_pattern_template % (glider_name, 
    64                                               comm_name, 
    65                                               date.year, 
    66                                               date.month, 
    67                                               date.day,) 
    68                      for comm_name in comm_names 
    69                      for date in date_range] 
    70  
    71     log_dir = os.path.join('/var/opt/gmc/gliders',glider_name,'logs') 
    72     file_paths = [] 
    73     for file_pattern in file_patterns: 
    74         file_paths.extend(glob(os.path.join(log_dir, file_pattern))) 
     89    file_patterns = {} 
     90    for glider in gliders: 
     91        file_patterns[glider] = [file_pattern_template % (glider, 
     92                                                          comm, 
     93                                                          date.year, 
     94                                                          date.month, 
     95                                                          date.day,) 
     96                                 for comm in comms 
     97                                 for date in date_range] 
     98 
     99    log_dirs = {} 
     100    for glider in gliders: 
     101        log_dirs[glider] = os.path.join(glider_dir, glider, 'logs') 
     102 
     103    file_paths = {} 
     104    for glider in gliders: 
     105        file_paths[glider] = [] 
     106        for file_pattern in file_patterns[glider]: 
     107            file_paths[glider].extend(glob(os.path.join(log_dirs[glider], 
     108                                                        file_pattern))) 
    75109    return file_paths 
    76110 
    77 def parse(glider_name=glider_name, 
     111 
     112def parse(gliders=gliders, 
    78113          start_date=start_date, 
    79114          stop_date=stop_date): 
    80     file_paths = find(glider_name,start_date,stop_date) 
    81     points = [] 
    82     for file_path in file_paths: 
    83         handle = open(file_path) 
    84         contents = handle.readlines() 
    85         handle.close() 
    86         protents = contents[4:] 
    87         xref = zip(protents,contents) 
    88         for pline,cline in xref: 
    89             match = p_pattern.search(pline) 
    90             if match: 
    91                 lat,lon,fresh = match.groups() 
    92                 if lat == bad_coordinate: continue 
    93                 if lon == bad_coordinate: continue 
    94                 if fresh == bad_freshness: continue 
    95                 if float(fresh) >= stale: continue 
    96                 match = c_pattern.search(cline) 
     115    file_paths = find(gliders, start_date, stop_date) 
     116    points = {} 
     117    for glider in gliders: 
     118        points[glider] = [] 
     119        for file_path in file_paths[glider]: 
     120            handle = open(file_path) 
     121            contents = handle.readlines() 
     122            handle.close() 
     123            protents = contents[4:] 
     124            xref = zip(protents, contents) 
     125            for pline, cline in xref: 
     126                match = p_pattern.search(pline) 
    97127                if match: 
    98                     weekday, month, day, time, year = match.groups() 
    99                     points.append({'lat':lat, 
    100                                    'lon':lon, 
    101                                    'fresh':fresh, 
    102                                    'weekday':weekday, 
    103                                    'month':month, 
    104                                    'day':day, 
    105                                    'time':time, 
    106                                    'year':year,}) 
    107  
    108     points.sort(cmp=lambda x,y: cmp(datetime(int(x['year']), 
    109                                              months[x['month']], 
    110                                              int(x['day']), 
    111                                              int(x['time'][0:2]), 
    112                                              int(x['time'][3:5]), 
    113                                              int(x['time'][6:8])), 
    114                                     datetime(int(y['year']), 
    115                                              months[y['month']], 
    116                                              int(y['day']), 
    117                                              int(y['time'][0:2]), 
    118                                              int(y['time'][3:5]), 
    119                                              int(y['time'][6:8])))) 
     128                    lat, lon, fresh = match.groups() 
     129                    if lat == bad_coordinate: 
     130                        continue 
     131                    if lon == bad_coordinate: 
     132                        continue 
     133                    if fresh == bad_freshness: 
     134                        continue 
     135                    # if float(fresh) >= stale: 
     136                    #     continue 
     137                    match = c_pattern.search(cline) 
     138                    if match: 
     139                        weekday, month, day, time, year = match.groups() 
     140                        year = int(year) 
     141                        month = months[month] 
     142                        day = int(day) 
     143                        hour = int(time[0:2]) 
     144                        minute = int(time[3:5]) 
     145                        second = int(time[6:8]) 
     146                        stamp = datetime(year, month, day, 
     147                                         hour, minute, second) 
     148                        stamp = stamp - timedelta(seconds=float(fresh)) 
     149                        points[glider].append({'lat': lat, 
     150                                               'lon': lon, 
     151                                               'fresh': fresh, 
     152                                               'stamp': stamp, }), 
     153                        break 
     154 
     155        points[glider].sort(cmp=lambda x, y: cmp(x['stamp'], y['stamp'])) 
     156        # If two adjacent positions differ in time by less than delta, 
     157        # then remove the earlier position from the report. 
     158        # This effect is cumulative for a string of adjacent positions. 
     159        for pointa, pointb in zip(points[glider][:-1], points[glider][1:]): 
     160            if pointb['stamp'] - pointa['stamp'] < delta: 
     161                points[glider].remove(pointa) 
    120162 
    121163    return points 
    122164 
    123165if __name__ == '__main__': 
    124     num_args = len(sys.argv) 
    125     if not (num_args < 5): 
     166    if not (len(sys.argv) < 5): 
    126167        sys.exit(__doc__) 
    127168 
    128169    try: 
    129         glider_name = sys.argv[1] 
     170        positions = int(sys.argv[1]) 
    130171    except: 
    131172        pass 
    132173 
    133174    try: 
    134        start_date = sys.argv[2] 
    135        int(start_date) 
     175        start_date = sys.argv[2] 
     176        int(start_date) 
    136177    except: 
    137178        pass 
    138179 
    139180    try: 
    140        stop_date = sys.argv[3] 
    141        int(stop_date) 
     181        stop_date = sys.argv[3] 
     182        int(stop_date) 
    142183    except: 
    143184        pass 
    144185 
    145     out_dir = os.path.join('/home/localuser/deployment/tracks',glider_name) 
    146     try: 
    147         os.makedirs(out_dir) 
    148     except: 
    149         pass 
    150  
    151     contents = ["%s position report from %s to %s\n" %  
    152                 (glider_name, start_date, stop_date,),] 
    153     points = parse(glider_name,start_date,stop_date) 
    154     spec = "Lat: %s, Lon: %s on %s %s %02d, %04d at %s UTC\n" 
    155     contents.extend([spec % (point['lat'], 
    156                              point['lon'], 
    157                              point['weekday'], 
    158                              point['month'], 
    159                              int(point['day']), 
    160                              int(point['year']), 
    161                              point['time'],) 
    162                      for point in points]) 
    163  
    164     file_name = "%s_track_%s_%s.txt" % (glider_name, 
    165                                         start_date, 
    166                                         stop_date,) 
    167     handle = open(os.path.join(out_dir,file_name),"w") 
    168     handle.writelines(contents) 
    169     handle.close() 
     186    points = parse(gliders, start_date, stop_date) 
     187    spec = "Glider: %s, Lat: %s, Lon: %s on %s %s %02d, " \ 
     188           "%04d at %02d:%02d:%02d UTC\n" 
     189    for glider in gliders: 
     190        for point in points[glider][-positions:]: 
     191            print spec % (glider, 
     192                          point['lat'], 
     193                          point['lon'], 
     194                          weekdays[point['stamp'].weekday()], 
     195                          shtnom[point['stamp'].month], 
     196                          point['stamp'].day, 
     197                          point['stamp'].year, 
     198                          point['stamp'].hour, 
     199                          point['stamp'].minute, 
     200                          point['stamp'].second)