Skip to content

Commit

Permalink
add image cadence output parameter, improve self test coverage and ro…
Browse files Browse the repository at this point in the history
…bustness
  • Loading branch information
scivision committed May 29, 2018
1 parent 43297ab commit ba17240
Show file tree
Hide file tree
Showing 7 changed files with 2,454 additions and 23 deletions.
14 changes: 13 additions & 1 deletion dascutils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def load(flist:Union[Path,list], azfn:Path=None, elfn:Path=None, treq:list=None,
reads FITS images and spatial az/el calibration for allsky camera
Bdecl is in degrees, from IGRF model
"""
forig = flist
warnings.filterwarnings('ignore', category=VerifyWarning)

if isinstance(flist,(str,Path)):
Expand Down Expand Up @@ -90,6 +91,9 @@ def load(flist:Union[Path,list], azfn:Path=None, elfn:Path=None, treq:list=None,
if len(flist)==0:
raise FileNotFoundError(f'no files found with wavelength(s) {wavelenreq}')
#%% iterate over image files
if len(flist)==0:
raise FileNotFoundError(f'no DASC FITS files found in {forig}')

if verbose:
print('Number of files',len(flist),'with wavelengths',np.unique(wavelen))

Expand Down Expand Up @@ -137,17 +141,25 @@ def load(flist:Union[Path,list], azfn:Path=None, elfn:Path=None, treq:list=None,
if wavelen is None:
data = xarray.Dataset({'unknown': (('time','y','x'), img)},
coords={'time':time})
if data.time.size > 1: # more than 1 image
data.attrs['cadence'] = time[1]-time[0] # NOTE: assumes uniform kinetic rate
else:
data = None
cadence = {}
for w in np.unique(wavelen):
d = xarray.Dataset({w:(('time','y','x'),img[wavelen==w,...])},
coords={'time':time[wavelen==w]})
if d.time.size > 1: # more than 1 image
cadence[w] = d.time[1] - d.time[0] # NOTE: assumes uniform kinetic rate
else:
cadence[w] = None

if data is None:
data = d
else:
data = xarray.merge((data,d), join='outer')


data.attrs['cadence'] = cadence

if lla is not None:
data.attrs['lat']=lla['lat']
Expand Down
1 change: 0 additions & 1 deletion tests/PKR_DASC_0428_20151007_082305.930.FITS

This file was deleted.

1 change: 1 addition & 0 deletions tests/PKR_DASC_0428_20151007_082355.961.FITS

Large diffs are not rendered by default.

1,105 changes: 1,105 additions & 0 deletions tests/PKR_DASC_0558_20151007_082351.743.FITS

Large diffs are not rendered by default.

1,094 changes: 1,094 additions & 0 deletions tests/PKR_DASC_0558_20151007_082404.243.FITS

Large diffs are not rendered by default.

208 changes: 208 additions & 0 deletions tests/PKR_DASC_0630_20151007_082359.586.FITS

Large diffs are not rendered by default.

54 changes: 33 additions & 21 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import xarray
import tempfile
import pytest
from datetime import datetime
from numpy.testing import assert_allclose
#
Expand All @@ -11,11 +12,9 @@

R = Path(__file__).parent

fn = R/'PKR_DASC_0428_20151007_082305.930.FITS'
azfn = R.parent/'cal/PKR_DASC_20110112_AZ_10deg.fits'
elfn = R.parent/'cal/PKR_DASC_20110112_EL_10deg.fits'

assert fn.is_file(),f'could not find test data file {fn}'
assert azfn.is_file(),f'could not find azimuth cal file {azfn}'
assert elfn.is_file(),f'could not find elevation cal file {azfn}'

Expand All @@ -26,37 +25,50 @@ def test_timestamp():
assert_allclose(du.totimestamp(['2012-01-03T08:32:02Z','2012-01-03T08:32:12Z']),
(1325579522.0, 1325579532.0))

def test_lost():
def test_basic_load():
# %% expected fail
with pytest.raises(FileNotFoundError) as e:
with tempfile.TemporaryDirectory() as d:
data = dio.load(d)
# %% most basic
data = dio.load(fn)
assert data[428].shape == (1,512,512)
assert 'az' not in data.data_vars
# %% a little more complex
data = dio.load(fn,azfn,elfn)
data = dio.load(R)
assert isinstance(data, xarray.Dataset)
assert 428 in data.data_vars
assert data[428].shape == (1,512,512)

d428 = data[428].dropna(dim='time')
assert d428.shape == (1,512,512)

d558 = data[558].dropna(dim='time')
assert d558.shape == (2,512,512)

d630 = data[630].dropna(dim='time')
assert d630.shape == (1,512,512)

assert 'az' not in data.data_vars
assert data.lat == 65.126
assert data.lon == -147.479
assert data.time.values == datetime(2015, 10, 7, 8, 23, 5, 930000)

assert d630.time.values == np.datetime64('2015-10-07T08:23:59.586000')
assert data.cadence[558].values == np.timedelta64(12500,'ms')

def test_full_load():
# %% single time request
data = dio.load(fn,azfn,elfn,'2012-01-03T08:32:02')
assert data[428].shape == (1,512,512)
data = dio.load(R, azfn,elfn,'2012-01-03T08:32:02')
assert data[558].shape == (1,512,512)
assert data.az.shape == (512,512)
assert data.el.shape == (512,512)
# %% multi-time request
data = dio.load(fn,azfn,elfn,('2012-01-03T08:32:02','2016-01-04'))
assert data[428].shape == (1,512,512)
assert data.time.item() == datetime(2015, 10, 7, 8, 23, 51, 743000)
# %% multi-time request and wavelength
data = dio.load(R, azfn,elfn,('2012-01-03T08:32:02','2016-01-04'),558)
assert data[558].shape == (2,512,512)
# %% wavelength request
data = dio.load(fn,azfn,elfn,None,428)
data = dio.load(R, azfn,elfn,None,428)
assert data[428].shape == (1,512,512)


def atest_download():
with tempfile.TemporaryDirectory() as d:
du.download(('2015-10-07T08:23:04','2015-10-07T08:23:06'),
'ftp://optics.gi.alaska.edu', 'PKR', d)
def test_download():

du.download(('2015-10-07T08:23:54','2015-10-07T08:23:56'),
'ftp://optics.gi.alaska.edu', 'PKR', R)



Expand Down

0 comments on commit ba17240

Please sign in to comment.