Added common option to specify which csv fields you want
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Fri, 21 Jan 2011 14:52:28 +0000 (14:52 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Fri, 21 Jan 2011 14:52:28 +0000 (14:52 +0000)
bin/common.py

index eb67f8a85339a03c06649c7fc1931821cfbd0543..d7aba17f9ac732b99284066c20a31a4e048886af 100755 (executable)
@@ -1255,39 +1255,18 @@ class Nmea(Nmea1, Nmea5):
         else:
             return infosrc
 
-    csv_headers = [
-        'mmsi',
-        'flag',
-        'name',
-        'imo',
-        'callsign',
-        'type',
-        'length',
-        'width',
-        'datetime',
-        'status',
-        'sog',
-        'latitude',
-        'longitude',
-        'cog',
-        'heading',
-        'destination',
-        'eta',
-        'draught',
-        ]
-    
     '''
     Maps the csv header name to matching function to call to get the data.
     '''
     csv_name_to_function = {
-        'mmsi': lambda nmea: nmea.mmsi,
+        'mmsi': lambda nmea: nmea.strmmsi,
         'flag': get_flag,
         'name': Nmea5.get_name,
         'imo': lambda nmea: str(nmea.imo),
         'callsign': Nmea5.get_callsign,
-        'type': lambda nmea: str(nmea.type) + '-' + get_shiptype(nmea),
-        'length':lambda nmea: str(nmea.get_length(nmea)),
-        'width': lambda nmea: str(nmea.get_width(nmea)),
+        'type': lambda nmea: str(nmea.type) + '-' + nmea.get_shiptype(),
+        'length':lambda nmea: str(nmea.get_length()),
+        'width': lambda nmea: str(nmea.get_width()),
         'datetime': lambda nmea: datetime.utcfromtimestamp(nmea.get_last_timestamp()).strftime('%Y-%m-%dT%H:%M:%SZ'),
         'status': Nmea1.get_status,
         'sog': Nmea1.get_sog_str,
@@ -1300,57 +1279,64 @@ class Nmea(Nmea1, Nmea5):
         'draught': Nmea5.get_draught_str,
     }
 
-    def get_dump_row(self):
+    def get_dump_row(self, fieldnames):
         result = []
-        def _clean(txt):
-            if txt is None:
-                return ''
-            return txt.replace('\0','').replace('@', '').strip()
-        f = csv_name_to_function['mmsi']
-        result.append(self.strmmsi)
-        result.append(self.get_flag().encode('utf-8'))
-        result.append(self.get_name())
-        result.append(str(self.imo))
-        result.append(_clean(self.callsign))
-        result.append(str(self.type) + '-' + SHIP_TYPES.get(self.type, 'unknown'))
-        d = self.dim_bow + self.dim_stern
-        if d:
-            result.append(d)
-        else:
-            result.append(None)
-        d = self.dim_port + self.dim_starboard
-        if d:
-            result.append(d)
-        else:
-            result.append(None)
-        result.append(datetime.utcfromtimestamp(self.timestamp_1).strftime('%Y-%m-%dT%H:%M:%SZ'))
-        result.append(STATUS_CODES.get(self.status, 'unknown'))
-        if self.sog != AIS_SOG_NOT_AVAILABLE:
-            result.append(str(self.sog/AIS_SOG_SCALE))
-        else:
-            result.append(None)
-        if self.latitude != AIS_LAT_NOT_AVAILABLE:
-            result.append(str(self.latitude/AIS_LATLON_SCALE))
-        else:
-            result.append(None)
-        if self.longitude != AIS_LON_NOT_AVAILABLE:
-            result.append(str(self.longitude/AIS_LATLON_SCALE))
-        else:
-            result.append(None)
-        if self.cog != AIS_COG_NOT_AVAILABLE:
-            result.append(str(self.cog/10.))
-        else:
-            result.append(None)
-        if self.heading != AIS_NO_HEADING:
-            result.append(str(self.heading))
-        else:
-            result.append(None)
-        result.append(self.get_destination(''))
-        result.append(self.get_eta_str(''))
-        result.append(self.draught)
-        result.append(self.source_5)
+        for fieldname in fieldnames:
+            f = self.csv_name_to_function[fieldname]
+            result.append(f(self))
         return result
 
+    #def get_dump_row(self):
+    #    result = []
+    #    def _clean(txt):
+    #        if txt is None:
+    #            return ''
+    #        return txt.replace('\0','').replace('@', '').strip()
+
+    #    result.append(self.strmmsi)
+    #    result.append(self.get_flag().encode('utf-8'))
+    #    result.append(self.get_name())
+    #    result.append(str(self.imo))
+    #    result.append(_clean(self.callsign))
+    #    result.append(str(self.type) + '-' + SHIP_TYPES.get(self.type, 'unknown'))
+    #    d = self.dim_bow + self.dim_stern
+    #    if d:
+    #        result.append(d)
+    #    else:
+    #        result.append(None)
+    #    d = self.dim_port + self.dim_starboard
+    #    if d:
+    #        result.append(d)
+    #    else:
+    #        result.append(None)
+    #    result.append(datetime.utcfromtimestamp(self.timestamp_1).strftime('%Y-%m-%dT%H:%M:%SZ'))
+    #    result.append(STATUS_CODES.get(self.status, 'unknown'))
+    #    if self.sog != AIS_SOG_NOT_AVAILABLE:
+    #        result.append(str(self.sog/AIS_SOG_SCALE))
+    #    else:
+    #        result.append(None)
+    #    if self.latitude != AIS_LAT_NOT_AVAILABLE:
+    #        result.append(str(self.latitude/AIS_LATLON_SCALE))
+    #    else:
+    #        result.append(None)
+    #    if self.longitude != AIS_LON_NOT_AVAILABLE:
+    #        result.append(str(self.longitude/AIS_LATLON_SCALE))
+    #    else:
+    #        result.append(None)
+    #    if self.cog != AIS_COG_NOT_AVAILABLE:
+    #        result.append(str(self.cog/10.))
+    #    else:
+    #        result.append(None)
+    #    if self.heading != AIS_NO_HEADING:
+    #        result.append(str(self.heading))
+    #    else:
+    #        result.append(None)
+    #    result.append(self.get_destination(''))
+    #    result.append(self.get_eta_str(''))
+    #    result.append(self.draught)
+    #    result.append(self.source_5)
+    #    return result
+
 
 class BankNmea1(list):
     """
@@ -1923,6 +1909,11 @@ def main():
     parser.add_option('--no-headers',
         action='store_false', dest='csv_headers', default=True,
         help="skip CSV headers")
+
+    parser.add_option('--csv-fields',
+        action='store', type='str', dest='csv_fields',
+        default='mmsi,flag,name,imo,callsign,type,length,width,datetime,status,sog,latitude,longitude,cog,heading,destination,eta,draught',
+        help='Which fields should be extracted for csv output. Default=%default')
     #
 
     expert_group = OptionGroup(parser, "Expert Options",
@@ -2085,14 +2076,15 @@ def main():
     #
 
     if options.action == 'dump':
+        fields = options.csv_fields.split(',')
         output = csv.writer(sys.stdout)
         if options.csv_headers:
-            output.writerow(Nmea.csv_headers)
+            output.writerow(fields)
         for mmsi in target_mmsi_iterator:
             logging.debug('Considering %s', repr(mmsi))
             assert dt_end is not None
             for nmea in NmeaFeeder(mmsi, dt_end, dt_start, filters, granularity=options.granularity, max_count=options.max_count):
-                output.writerow(nmea.get_dump_row())
+                output.writerow(nmea.get_dump_row(fields))
 
     elif options.action == 'removemanual':
         if filters: