Skip to content

Commit 0c12bb5

Browse files
committed
updated stations.py to use validation.py
1 parent 4b49222 commit 0c12bb5

2 files changed

Lines changed: 20 additions & 86 deletions

File tree

cloudside/station.py

Lines changed: 20 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# metar stuff
2424
import metar
2525

26+
from . import validate
2627

2728
try:
2829
from tqdm import tqdm
@@ -68,6 +69,8 @@ def __init__(self, sta_id, city=None, state=None, country=None,
6869
self.country = country
6970
self.position = metar.datatypes.position(lat, lon)
7071
self._max_attempts = max_attempts
72+
self.lat = lat
73+
self.lon = lon
7174

7275
if show_progress:
7376
self.tracker = tqdm
@@ -123,8 +126,8 @@ def _find_dir(self, src, step):
123126
*src* : 'asos' or 'wunderground'
124127
*step* : 'raw' or 'flat' or 'compile'
125128
'''
126-
_check_src(src)
127-
_check_step(step)
129+
validate.source(src)
130+
validate.step(step)
128131
return os.path.join(self.datadir, self.sta_id, src.lower(), step.lower())
129132

130133
def _find_file(self, timestamp, src, step):
@@ -137,8 +140,8 @@ def _find_file(self, timestamp, src, step):
137140
*step* : 'raw' or 'flat'
138141
'''
139142
date = timestamp.to_datetime()
140-
_check_src(src)
141-
_check_step(step)
143+
validate.source(src)
144+
validate.step(step)
142145

143146
if step == 'raw':
144147
ext = 'dat'
@@ -196,7 +199,7 @@ def _url_by_date(self, timestamp, src='wunderground'):
196199
'''
197200
date = timestamp.to_datetime()
198201
"http://www.wunderground.com/history/airport/KDCA/1950/12/18/DailyHistory.html?format=1"
199-
_check_src(src)
202+
validate.source(src)
200203
if src.lower() == 'wunderground':
201204
baseurl = 'http://www.wunderground.com/history/airport/%s' % self.sta_id
202205
endurl = 'DailyHistory.html?&&theprefset=SHOWMETAR&theprefvalue=1&format=1'
@@ -227,11 +230,11 @@ def _make_data_file(self, timestamp, src, step):
227230
*src* : 'asos' or 'wunderground'
228231
*step* : 'raw' or 'flat'
229232
'''
230-
_check_src(src)
231-
_check_step(step)
233+
validate.source(src)
234+
validate.step(step)
232235
destination = self._find_dir(src, step)
233236
datafile = self._find_file(timestamp, src, step)
234-
_check_dirs(destination.split(os.path.sep))
237+
validate.data_directory(destination.split(os.path.sep))
235238
return os.path.join(destination, datafile)
236239

237240
def _fetch_data(self, timestamp, attempt, src='asos', force_download=False):
@@ -278,9 +281,9 @@ def _fetch_data(self, timestamp, attempt, src='asos', force_download=False):
278281
errorfile.write('error on: %s\n' % (url,))
279282

280283
outfile.close()
281-
status = _check_file(outname)
284+
status = validate.file_status(outname)
282285
else:
283-
status = _check_file(outname)
286+
status = validate.file_status(outname)
284287

285288
errorfile.close()
286289
return status
@@ -312,13 +315,13 @@ def _process_file(self, timestamp, src):
312315
*timestamp* : a pandas timestamp object
313316
'''
314317
#pdb.set_trace()
315-
_check_src(src)
318+
validate.source(src)
316319
rawfilename = self._make_data_file(timestamp, src, 'raw')
317320
flatfilename = self._make_data_file(timestamp, src, 'flat')
318321
if not os.path.exists(rawfilename):
319322
rawstatus, attempt = self._attempt_download(timestamp, src, attempt=0)
320323
else:
321-
rawstatus = _check_file(rawfilename)
324+
rawstatus = validate.file_status(rawfilename)
322325

323326
if not os.path.exists(flatfilename) and rawstatus == 'ok':
324327
datain = open(rawfilename, 'r')
@@ -389,7 +392,7 @@ def _process_file(self, timestamp, src):
389392

390393
datain.close()
391394
dataout.close()
392-
flatstatus = _check_file(flatfilename)
395+
flatstatus = validate.file_status(flatfilename)
393396
return flatfilename, flatstatus
394397

395398
def _read_csv(self, timestamp, src):
@@ -415,7 +418,7 @@ def _read_csv(self, timestamp, src):
415418
if not os.path.exists(flatfilename):
416419
flatfilename, flatstatus = self._process_file(timestamp, src)
417420

418-
flatstatus = _check_file(flatfilename)
421+
flatstatus = validate.file_status(flatfilename)
419422
if flatstatus == 'ok':
420423
data = pandas.read_csv(flatfilename, index_col=False, parse_dates=[icol], header=headerrows[src])
421424
data.set_index(data.columns[icol], inplace=True)
@@ -440,7 +443,7 @@ def _get_data(self, startdate, enddate, source, filename):
440443
Returns:
441444
*data* : a pandas data frame of the data for this station
442445
'''
443-
_check_src(source)
446+
validate.source(source)
444447

445448
freq = {
446449
'asos': 'MS',
@@ -473,7 +476,7 @@ def _get_data(self, startdate, enddate, source, filename):
473476

474477
if filename is not None:
475478
compdir = self._find_dir(source, 'compile')
476-
_check_dirs(compdir.split(os.path.sep))
479+
validate.data_directory(compdir.split(os.path.sep))
477480
final_data.to_csv(os.path.join(compdir, filename))
478481

479482
return final_data
@@ -543,7 +546,7 @@ def getWunderground_NonAirportData(self, startdate, enddate, filename=None):
543546

544547
def _get_compiled_files(self, source):
545548
compdir = self._find_dir(source, 'compile')
546-
_check_dirs(compdir.split(os.path.sep))
549+
validate.data_directory(compdir.split(os.path.sep))
547550
compfiles = os.listdir(compdir)
548551
return compdir, compfiles
549552

@@ -594,56 +597,6 @@ def _parse_date(datestring):
594597
return dateval
595598

596599

597-
def _check_src(src):
598-
'''
599-
checks that a *src* value is valid
600-
'''
601-
if src.lower() not in ('wunderground', 'asos', 'wunder_nonairport'):
602-
raise ValueError('src must be one of "wunderground" or "asos"')
603-
604-
605-
def _check_step(step):
606-
'''
607-
checks that a *step* value is valid
608-
'''
609-
if step.lower() not in ('raw', 'flat', 'compile'):
610-
raise ValueError('step must be one of "raw" or "flat"')
611-
612-
613-
def _check_file(filename):
614-
'''
615-
confirms that a raw file isn't empty
616-
'''
617-
try:
618-
testfile = open(filename, 'r')
619-
lines = testfile.readlines()
620-
testfile.close()
621-
if len(lines) > 1:
622-
status = 'ok'
623-
else:
624-
status = 'bad'
625-
626-
except IOError:
627-
status = 'not there'
628-
629-
return status
630-
631-
632-
def _check_dirs(subdirs):
633-
'''
634-
checks to see that a directory exists. if not, it makes it.
635-
'''
636-
if not os.path.exists(subdirs[0]):
637-
#print('making '+subdirs[0])
638-
os.mkdir(subdirs[0])
639-
640-
if len(subdirs) > 1:
641-
topdir = [os.path.join(subdirs[0], subdirs[1])]
642-
for sd in subdirs[2:]:
643-
topdir.append(sd)
644-
_check_dirs(topdir)
645-
646-
647600
def _date_ASOS(metarstring):
648601
'''get date/time of asos reading'''
649602
yr = int(metarstring[13:17]) # year

cloudside/tests/stations_tests.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -209,25 +209,6 @@ def test_parse_dates(self):
209209
ntools.assert_equal(dd.month, kd.month)
210210
ntools.assert_equal(dd.day, kd.day)
211211

212-
def test_check_src(self):
213-
station._check_src('asos')
214-
station._check_src('wunderground')
215-
ntools.assert_raises(ValueError, station._check_src, 'fart')
216-
217-
def test_check_step(self):
218-
station._check_step('flat')
219-
station._check_step('raw')
220-
ntools.assert_raises(ValueError, station._check_step, 'fart')
221-
222-
def test_check_file(self):
223-
known_results = ['bad', 'ok', 'not there']
224-
for n, known_result in enumerate(known_results, 1):
225-
fn = getTestFile('testfile{:d}'.format(n))
226-
ntools.assert_equal(station._check_file(fn), known_result)
227-
228-
def test_check_dirs(self):
229-
pass
230-
231212
def test_date_asos(self):
232213
teststring = '24229KPDX PDX20010101000010001/01/01 00:00:31 5-MIN KPDX'
233214
knowndate = datetime(2001, 1, 1, 0, 0)

0 commit comments

Comments
 (0)