Skip to content

Commit ec4696b

Browse files
authored
Merge pull request #9 from WHOIGit/r2r_event
add r2r_event to event log
2 parents 28ab1b5 + 72d6e82 commit ec4696b

File tree

5 files changed

+68
-25
lines changed

5 files changed

+68
-25
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Generated by Django 5.2.3 on 2025-06-17 17:43
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('core', '0009_hplc_alter_cruise_start_time'),
10+
]
11+
12+
operations = [
13+
migrations.RemoveConstraint(
14+
model_name='event',
15+
name='unique_cruise_message_id',
16+
),
17+
migrations.AddField(
18+
model_name='event',
19+
name='r2r_event',
20+
field=models.CharField(blank=True, max_length=30, null=True),
21+
),
22+
migrations.AddField(
23+
model_name='historicalevent',
24+
name='r2r_event',
25+
field=models.CharField(blank=True, max_length=30, null=True),
26+
),
27+
migrations.AlterField(
28+
model_name='event',
29+
name='message_id',
30+
field=models.IntegerField(null=True),
31+
),
32+
migrations.AlterField(
33+
model_name='historicalevent',
34+
name='message_id',
35+
field=models.IntegerField(null=True),
36+
),
37+
migrations.AddConstraint(
38+
model_name='event',
39+
constraint=models.UniqueConstraint(fields=('cruise', 'r2r_event'), name='unique_cruise_r2r_event'),
40+
),
41+
]

api/core/models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ def __str__(self):
204204

205205
class Event(models.Model):
206206
cruise = models.ForeignKey(Cruise, on_delete=models.CASCADE, related_name='events')
207-
message_id = models.IntegerField()
207+
r2r_event = models.CharField(max_length=30, null=True, blank=True)
208+
message_id = models.IntegerField(null=True)
208209
instrument = models.CharField(max_length=100)
209210
action = models.CharField(max_length=32)
210211
station = models.CharField(max_length=100)
@@ -216,7 +217,7 @@ class Event(models.Model):
216217

217218
class Meta:
218219
constraints = [
219-
UniqueConstraint(fields=['cruise', 'message_id'], name='unique_cruise_message_id')
220+
UniqueConstraint(fields=['cruise', 'r2r_event'], name='unique_cruise_r2r_event')
220221
]
221222

222223
def __str__(self):

api/ctd/management/commands/importniskin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ def handle(self, *args, **options):
165165
cruise = Cruise.objects.get(name__iexact=cruise_name)
166166

167167
directory = f'/vast/raw/{cruise_name}/ctd/'
168-
btl_files = sorted(glob.glob(os.path.join(directory, '*.btl')))
168+
btl_files = sorted(
169+
f for f in glob.glob(os.path.join(directory, '*.btl'))
170+
if not f.endswith('_original.btl')
171+
)
169172
if cruise_name.lower() == "en627":
170173
added_dir = os.path.join(directory, "cast_1_files_used_for_corrected_cast_2")
171174
btl_files += sorted(glob.glob(os.path.join(added_dir, '*.btl')))

api/events/management/commands/importevent.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,39 @@
1616
from datetime import datetime
1717

1818
DATETIME = 'dateTime8601'
19-
MESSAGE_ID = 'Message ID'
19+
MESSAGE_ID = 'Message ID'
20+
R2R_EVENT = 'R2R_Event'
2021
FILE_SUFFIX = '_elog.csv'
2122

2223
INSTRUMENT_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,

api/nut/management/commands/importnut.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ def read_nut_data(self, cruise, merged):
154154

155155
# set date, lat, lon, depth to NaN when there is no bottle file for the cast
156156
btl_dir = f'/vast/raw/{cruise}/ctd/'
157-
for file in sorted(glob.glob(os.path.join(btl_dir, '*.asc'))):
157+
for file in sorted(
158+
f for f in glob.glob(os.path.join(btl_dir, '*.asc'))
159+
if not f.endswith('_original.asc')
160+
):
158161
if cruise == 'en627':
159162
file = file.replace("_u", "")
160163
btl_file = file[:-3] + 'btl'

0 commit comments

Comments
 (0)