-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
passing self-test for load and download--major API rework to use xarr…
…ay.Dataset
- Loading branch information
Showing
12 changed files
with
213 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
image: | ||
- Visual Studio 2017 | ||
|
||
stack: python 3 | ||
|
||
environment: | ||
PY_DIR: C:\Python36-x64 | ||
|
||
clone_depth: 3 | ||
|
||
before_build: | ||
- cmd: set PATH=%PY_DIR%;%PY_DIR%\Scripts;%PATH% | ||
|
||
build_script: | ||
- pip install -e .[tests] | ||
|
||
after_build: | ||
- pytest -v | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[run] | ||
cover_pylib = false | ||
omit = | ||
/home/travis/virtualenv/* | ||
|
||
[report] | ||
exclude_lines = | ||
pragma: no cover | ||
def __repr__ | ||
RuntimeError | ||
NotImplementedError | ||
ImportError | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#!/usr/bin/env python | ||
""" | ||
Reads DASC allsky cameras images in FITS formats into GeoData. | ||
Run standalone from PlayDASC.py | ||
""" | ||
from pathlib import Path | ||
import warnings # corrupt FITS files let off a flood of AstroPy warnings | ||
from astropy.io.fits.verify import VerifyWarning | ||
import logging | ||
from astropy.io import fits | ||
import numpy as np | ||
from datetime import timedelta | ||
from dateutil.parser import parse | ||
import xarray | ||
|
||
|
||
def load(flist:list, azfn:Path=None, elfn:Path=None, minmax:tuple=None, treq:list=None) -> xarray.Dataset: | ||
""" | ||
reads FITS images and spatial az/el calibration for allsky camera | ||
Bdecl is in degrees, from IGRF model | ||
""" | ||
warnings.filterwarnings('ignore', category=VerifyWarning) | ||
|
||
if not flist: | ||
raise FileNotFoundError('no files of this wavelength') | ||
|
||
if isinstance(flist,(str,Path)): | ||
flist = [flist] | ||
|
||
#%% read one file mode | ||
if treq is not None: | ||
expstart = [] | ||
|
||
for i,fn in enumerate(flist): | ||
try: | ||
with fits.open(fn, mode='readonly') as h: | ||
expstart.append(parse(h[0].header['OBSDATE'] + ' ' + h[0].header['OBSSTART'])) | ||
except IOError: #many corrupted files, accounted for by preallocated vectors | ||
pass | ||
|
||
expstart = np.array(expstart) | ||
|
||
if isinstance(treq,float) or len(treq) == 1: # single frame | ||
fi = np.nanargmin(abs(expstart-treq)) #index number in flist desired | ||
elif len(treq)==2: #frames within bounds | ||
if treq[0] is not None: | ||
fi = (treq[0] <= expstart) | ||
if treq[1] is not None: | ||
fi &= (expstart < treq[1]) | ||
elif treq[1] is not None: | ||
fi = (expstart < treq[1]) | ||
else: | ||
fi = slice(None) | ||
else: | ||
fi = slice(None) | ||
|
||
flist = flist[fi] | ||
if len(flist)==0: | ||
raise FileNotFoundError('no files found within time limits') | ||
|
||
if isinstance(flist,Path): # so that we can iterate | ||
flist = [flist] | ||
#%% iterate over image files | ||
time = []; img= []; wavelen = [] | ||
|
||
for i,fn in enumerate(flist): | ||
try: | ||
with fits.open(fn, mode='readonly') as h: | ||
assert h[0].header['BITPIX']==16,'this function assumes unsigned 16-bit data' | ||
expstart = parse(h[0].header['OBSDATE'] + 'T' + h[0].header['OBSSTART']) | ||
|
||
time.append((expstart, expstart + timedelta(seconds=h[0].header['EXPTIME']))) #EXPTIME is in seconds | ||
|
||
wavelen.append(int(h[0].header['FILTWAV'])) | ||
|
||
sensorloc={'lat':h[0].header['GLAT'], | ||
'lon':h[0].header['GLON'], | ||
'alt_m':200.} # TODO use real altitude | ||
|
||
""" | ||
DASC iKon cameras are/were 14-bit at least through 2015. So what they did was | ||
just write unsigned 14-bit data into signed 16-bit integers, which doesn't overflow | ||
since 14-bit \in {0,16384}. | ||
These files do not have a BZERO value. Someday when they're written correctly this | ||
code may need updating. | ||
Further, there was a RAID failure that filled the data files with random values. | ||
Don Hampton says about 90% of data OK, but 10% NOK. | ||
""" | ||
|
||
I = np.rot90(h[0].data,-1) #NOTE: rotation to match online AVIs from UAF website. It's not transpose, and the cal file seems off. | ||
if not 'BZERO' in h[0].header.keys(): | ||
I[I>16384] = 0 #extreme, corrupted data | ||
I = I.clip(0,16384).astype(np.uint16) #discard bad values for 14-bit cameras. | ||
|
||
img.append(I) | ||
|
||
except (IOError,TypeError) as e: | ||
logging.warning(f'{fn} has error {e}') | ||
|
||
# %% collect output | ||
img = np.array(img) | ||
time = np.array(time) | ||
wavelen = np.array(wavelen) | ||
wavelengths = np.unique(wavelen) | ||
# %% deal with corrupted data | ||
if minmax is not None: | ||
img[(img<minmax[0]) | (img>minmax[1])] = 1 #instead of 0 for lognorm | ||
|
||
ds = {} | ||
for w in wavelengths: | ||
ds[w] = (('time','y','x'),img[wavelen==w,...]) | ||
|
||
data = xarray.Dataset(ds, | ||
coords={'time':time[:,0]}, | ||
attrs={'timeend':time[:,1], | ||
'sensorloc':sensorloc}) | ||
|
||
if azfn is not None and elfn is not None: | ||
with fits.open(Path(azfn).expanduser(),mode='readonly') as h: | ||
data['az'] = (('y','x'),h[0].data) | ||
with fits.open(Path(elfn).expanduser(),mode='readonly') as h: | ||
data['el'] = (('y','x'),h[0].data) | ||
|
||
|
||
return data |
Oops, something went wrong.