Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding datetime procedure, and minor bug fix #5

Merged
merged 10 commits into from
Mar 20, 2016
6 changes: 3 additions & 3 deletions tests/test_cotede.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
12 changes: 12 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from wodpy import wod
import numpy

Expand Down Expand Up @@ -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
Expand Down
50 changes: 16 additions & 34 deletions wodpy/extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
20 changes: 19 additions & 1 deletion wodpy/wod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']
Expand Down