diff --git a/tests/test_cotede.py b/tests/test_cotede.py index 90c99c5..4ca3fb2 100644 --- a/tests/test_cotede.py +++ b/tests/test_cotede.py @@ -57,13 +57,13 @@ def test_n_levels(self): def test_datetime(self): ''' - check datetime == 1934-08-07 10:22:11 + check datetime == 1934-08-07 10:22:12 ''' - truth = datetime(1934, 8, 7, 10, 22, 11) + truth = datetime(1934, 8, 7, 10, 22, 12) time = self.demoProfile.attributes['datetime'] assert time == truth, \ - 'time should have been 1934-08-07 10:22:11, instead read %s' \ + 'time should have been 1934-08-07 10:22:12, instead read %s' \ % time diff --git a/tests/tests.py b/tests/tests.py index ae37b74..f9de991 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -1,3 +1,4 @@ +from datetime import datetime from wodpy import wod import numpy @@ -142,6 +143,17 @@ def test_time(self): assert header_time == 10.37, 'header time should have been 10.37, instead read %f' % header_time + def test_datetime(self): + ''' + check datetime 1934-8-7 10:22:12 + ''' + + d = self.demoProfile.datetime() + assert d == datetime(1934, 8, 7, 10, 22, 12), \ + 'time should have been 1934-08-07 10:22:12, instead read %s' \ + % d + + def test_probe_type(self): ''' check probe type == 7 diff --git a/wodpy/extra.py b/wodpy/extra.py index 406a204..316167d 100644 --- a/wodpy/extra.py +++ b/wodpy/extra.py @@ -44,20 +44,24 @@ def __init__(self, profile): >>> profile = Wod4CoTeDe(fid) """ - if type(profile) == WodProfile: - self.p = profile - else: + try: self.p = WodProfile(profile) + except: + self.p = profile self.attributes = {} self.attributes['LATITUDE'] = self.p.latitude() self.attributes['LONGITUDE'] = self.p.longitude() self.attributes['uid'] = self.p.uid() - self.attributes['probe_code'] = int(self.p.probe_type()) - self.attributes['probe_type'] = probe_type_table[self.p.probe_type()] + try: + self.attributes['probe_code'] = int(self.p.probe_type()) + self.attributes['probe_type'] = \ + probe_type_table[self.p.probe_type()] + except: + self.attributes['probe_code'] = None + self.attributes['probe_type'] = None self.attributes['n_levels'] = self.p.n_levels() - - self.get_datetime() + self.attributes['datetime'] = self.p.datetime() self.data = {} # FIXME: pressure is 'almost' equal to depth. @@ -67,36 +71,14 @@ def __init__(self, profile): self.data['TEMP'] = self.p.t() self.data['TEMP_QC'] = self.p.t_qc_mask() self.data['PSAL'] = self.p.s() - self.data['oxygen'] = self.p.oxygen() - self.data['silicate'] = self.p.silicate() - self.data['phosphate'] = self.p.phosphate() - self.data['pH'] = self.p.pH() + for v in ['oxygen', 'silicate', 'phosphate', 'pH']: + try: + exec("self.data['%s'] = self.p.%s()" % (v, v)) + except: + pass def keys(self): return self.data.keys() def __getitem__(self, item): return self.data[item] - - def get_datetime(self): - """ Extract datetime from the WOD profile - - This was copied from AutoQC.DummyCNV() - """ - year = self.p.year() - month = self.p.month() - day = self.p.day() - if day == 0: day = 15 - time = self.p.time() - if time is None or time < 0 or time >= 24: - hours = 0 - minutes = 0 - seconds = 0 - else: - hours = int(time) - minutesf = (time - hours) * 60 - minutes = int(minutesf) - seconds = int((minutesf - minutes) * 60) - - self.attributes['datetime'] = datetime(year, month, - day, hours, minutes, seconds) diff --git a/wodpy/wod.py b/wodpy/wod.py index 9c95412..b03defe 100644 --- a/wodpy/wod.py +++ b/wodpy/wod.py @@ -2,6 +2,7 @@ import numpy as np import os import pandas as pd +from datetime import datetime, timedelta class WodProfile(object): """ Main class to parse a WOD ASCII file @@ -379,12 +380,29 @@ def month(self): def day(self): """ Returns the day. """ - return self.primary_header['Day'] + day = self.primary_header['Day'] + if day == 0: + return + else: + return day def time(self): """ Returns the time. """ return self.primary_header['Time'] + def datetime(self): + """ Returns the date and time as a datetime object. """ + time = self.time() + if time is None or time < 0 or time >= 24: + time = 0 + + try: + d = datetime(self.year(), self.month(), self.day()) + \ + timedelta(hours=time) + return d + except: + return + def cruise(self): """ return the cruise number """ return self.primary_header['Cruise number']