1616from datetime import datetime
1717
1818DATETIME = 'dateTime8601'
19- MESSAGE_ID = 'Message ID'
19+ MESSAGE_ID = 'Message ID'
20+ R2R_EVENT = 'R2R_Event'
2021FILE_SUFFIX = '_elog.csv'
2122
2223INSTRUMENT_MAPPING = {
2324 "Attune" : "Attune Flow Cytometer" ,
25+ "Attune Flow Cytomoeter" : "Attune Flow Cytometer" ,
2426 "Bongo" : "Bongo Net" ,
2527 "Cytomeoter" : "Cytometer" ,
2628 "EK80" : "EK80 broadband" ,
2729 "IFCB_continuous" : "IFCB continuous" ,
2830 "IFCB Continuous" : "IFCB continuous" ,
2931 "IFCB 109" : "IFCB continuous" ,
32+ "IFCB" : "IFCB continuous" ,
3033 "Incubation" : "Incubation Grazing" ,
3134 "Incubation O2" : "Incubation Respiration O2" ,
35+ "Issacs Kidd Midwater Trawl" : "Isaacs-Kidd Midwater Trawl" ,
36+ "Midwater Trawl" : "RMT10 Midwater Trawl (Tucker-style)" ,
37+ "RMT8 Midwater Trawl" : "RMT10 Midwater Trawl (Tucker-style)" ,
3238 "RingNet" : "Ring Net" ,
3339 "RingNetIFCB Continuous" : "IFCB continuous" ,
3440 "SSW" : "Underway Science seawater diaphragm pump" ,
41+ "Stingray" : "Sting Ray" ,
3542 "SUNA V2" : "SUNAV2" ,
3643 "Thermosalinograph SBE45" : "Thermosalinographs on underway impeller " ,
3744 "Transmissometer 10" : "Transmissometer 10cm" ,
3845 "trans10" : "Transmissometer 10cm" ,
3946 "trans25" : "Transmissometer 25cm" ,
4047 "Underway diaphram pump" : "Underway Science seawater diaphragm pump" ,
4148 "Underway Impeller" : "Underway Science seawater impeller" ,
49+ "Underway Science seawater impeller pump" : "Underway Science seawater impeller" ,
4250 "Underway Science Seawater Diaphragm Pump" : "Underway Science seawater diaphragm pump" ,
51+ "Valeport Modus SVS" : "Valeport SVS"
4352}
4453
4554
@@ -72,23 +81,6 @@ def store_csv_file(self, cruise_name, csv_data):
7281 print (e , flush = True )
7382 raise
7483
75- def apply_corrections (self , path ):
76- corr = pd .read_excel (path )
77- corr [DATETIME ] = pd .to_datetime (corr [DATETIME ], utc = True )
78- corr .pop ('Instrument' )
79- corr .pop ('Action' )
80- return corr
81-
82- def apply_additions (self , addns_path ):
83- addns = pd .read_excel (addns_path )
84- addns [DATETIME ] = pd .to_datetime (addns [DATETIME ], utc = True , format = "ISO8601" )
85- # add placeholder columns
86- addns .insert (4 , MESSAGE_ID , np .nan )
87- addns .insert (4 , 'Longitude' , np .nan )
88- addns .insert (4 , 'Latitude' , np .nan )
89- addns .insert (4 , 'Cast' , np .nan )
90- return addns
91-
9284 def handle (self , * args , ** options ):
9385 cruise_name = options ['cruise_name' ]
9486
@@ -105,14 +97,13 @@ def handle(self, *args, **options):
10597 directory = f'/vast/corrected/{ cruise_name } /elog/'
10698 file_pattern = os .path .join (directory , '*_elog.csv' )
10799 matching_file = glob .glob (file_pattern )
108- if matching_file :
100+ if matching_file and cruise_name . lower () not in [ 'en608' , 'en617' , 'en627' ]: # serve the original elogs for these cruises
109101 file_path = matching_file [0 ]
110102 df = pd .read_csv (file_path , parse_dates = [DATETIME ], dtype = {'Station' : str , 'Cast' : str })
111103 try :
112104 df [DATETIME ] = pd .to_datetime (df [DATETIME ]).dt .tz_convert ('UTC' )
113105 except :
114106 df [DATETIME ] = pd .to_datetime (df [DATETIME ]).dt .tz_localize ('UTC' ) # en617
115- df [MESSAGE_ID ] = range (1 , len (df ) + 1 ) # assign message ids
116107 else :
117108 directory = f'/vast/raw/{ cruise_name } /elog/'
118109 file_pattern = os .path .join (directory , 'R2R_ELOG*FINAL*' ) # do not read corrections or additions files in elog dir
@@ -136,10 +127,13 @@ def handle(self, *args, **options):
136127 raw_instrument = row ['Instrument' ]
137128 instrument = INSTRUMENT_MAPPING .get (raw_instrument , raw_instrument )
138129
130+ message_id = int (row [MESSAGE_ID ]) if pd .notna (row [MESSAGE_ID ]) else None # Nan is a float
131+
139132 event , created = Event .objects .update_or_create (
140133 cruise = cruise ,
141- message_id = row [MESSAGE_ID ],
134+ r2r_event = row [R2R_EVENT ],
142135 defaults = {
136+ "message_id" : message_id ,
143137 "instrument" :instrument ,
144138 "action" :row ['Action' ],
145139 "station" :row ['Station' ],
@@ -151,6 +145,7 @@ def handle(self, *args, **options):
151145 )
152146
153147 csv_data .append ({
148+ R2R_EVENT : event .r2r_event ,
154149 MESSAGE_ID : event .message_id ,
155150 DATETIME : event .datetime ,
156151 "Instrument" : event .instrument ,
0 commit comments