From 3a38a956939acac22736d5424b56798b3687cee7 Mon Sep 17 00:00:00 2001 From: "Michael Hirsch, Ph.D" Date: Thu, 16 Nov 2017 14:58:52 -0500 Subject: [PATCH] xarray --- .codeclimate.yml | 2 ++ .travis.yml | 34 +++++++++++----------------------- PlotDASC.py | 4 ++-- README.rst | 10 ++++++++-- dascutils/readDASCfits.py | 39 ++++++++++++++++++++++++++------------- pyproject.toml | 5 +++++ setup.py | 28 +++++++++++----------------- tests/test.py | 12 +++++++++++- 8 files changed, 76 insertions(+), 58 deletions(-) create mode 100644 .codeclimate.yml create mode 100644 pyproject.toml diff --git a/.codeclimate.yml b/.codeclimate.yml new file mode 100644 index 0000000..d687b7e --- /dev/null +++ b/.codeclimate.yml @@ -0,0 +1,2 @@ +languages: + python: true diff --git a/.travis.yml b/.travis.yml index 486d70a..5917367 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,12 @@ -language: generic +language: python +fast_finish: true -os: - - linux - - osx +python: + - 3.6 + +os: linux -env: - - TRAVIS_PYTHON_VERSION=3.6 +group: edge notifications: email: false @@ -13,26 +14,13 @@ notifications: git: depth: 3 -before_install: - - if [[ $TRAVIS_OS_NAME == osx ]]; then - wget https://repo.continuum.io/miniconda/Miniconda3-latest-MacOSX-x86_64.sh -O miniconda.sh; - else - wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh; - fi - - - bash miniconda.sh -b -p $HOME/miniconda - - export PATH=$HOME/miniconda/bin:$PATH - - hash -r - - conda config --set always_yes yes --set changeps1 no - - conda update -q conda - - conda create -n test python=$TRAVIS_PYTHON_VERSION - - source activate test - - pip -q install coveralls +before_install: + - pip -q install coveralls -install: - - python setup.py develop +install: pip install -e . script: coverage run tests/test.py -v after_success: coveralls + diff --git a/PlotDASC.py b/PlotDASC.py index 6f51258..1c17d65 100755 --- a/PlotDASC.py +++ b/PlotDASC.py @@ -29,6 +29,6 @@ def plotdasc(img,wavelength,odir,cadence,rows,cols): - img,times,waz,wel,wlla,wwl = readallDasc(p.indir,p.azfn,p.elfn,p.wavelength,p.minmax,p.tlim) + img = readallDasc(p.indir,p.azfn,p.elfn,p.wavelength,p.minmax,p.tlim) - plotdasc(img,wwl,p.odir,p.cadence,None,None) \ No newline at end of file + plotdasc(img,p.odir,p.cadence,None,None) \ No newline at end of file diff --git a/README.rst b/README.rst index 86eb434..5177a1d 100644 --- a/README.rst +++ b/README.rst @@ -3,11 +3,16 @@ .. image:: http://img.shields.io/badge/powered%20by-AstroPy-orange.svg?style=flat :target: http://www.astropy.org/ + .. image:: https://travis-ci.org/scivision/dascutils.svg?branch=master :target: https://travis-ci.org/scivision/dascutils .. image:: https://coveralls.io/repos/github/scivision/dascutils/badge.svg?branch=master - :target: https://coveralls.io/github/scivision/dascutils?branch=master + :target: https://coveralls.io/github/scivision/dascutils?branch=master + +.. image:: https://api.codeclimate.com/v1/badges/36b08deedc7d2bf750c8/maintainability + :target: https://codeclimate.com/github/scivision/dascutils/maintainability + :alt: Maintainability ============ DASC utils @@ -26,7 +31,7 @@ Install ======= :: - python setup.py develop + pip install -e . Download raw DASC files by time =========================== @@ -34,6 +39,7 @@ Example download October 7, 2015 from 8:23 to 8:54 UTC:: ./DownloadDASC.py 2015-10-07T08:23Z 2015-10-07T08:54Z + -o download directory -c clobber existing files -s three-letter site acronym PKR for poker flat etc. diff --git a/dascutils/readDASCfits.py b/dascutils/readDASCfits.py index 882c61b..6e14d26 100644 --- a/dascutils/readDASCfits.py +++ b/dascutils/readDASCfits.py @@ -4,40 +4,50 @@ Run standalone from PlayDASC.py """ from pathlib import Path +from dateutil.parser import parse +import xarray +import logging from warnings import filterwarnings # 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 dateutil.parser import parse # from . import totimestamp # from sciencedates import forceutc -def readallDasc(indir,azfn,elfn,wl,minmax,tlim=None): +def readallDasc(indir:Path, azfn:Path, elfn:Path, wl, minmax, tlim=None) -> xarray.DataArray: """ returns Dasc images in list by wavelength, then by time """ - - img = []; times = []; wlused=[] + imgs=[] for w in wl: try: data,azel,sensorloc,time = readCalFITS(indir,azfn,elfn,w,minmax,tlim) - img.append(data['image']) - times.append(time) - wlused.append(w) + + img = xarray.DataArray(data['image'], + coords={'time':time[:,0], + 'x':range(data['image'][0].shape[-1]), + 'y':range(data['image'][0].shape[-2]), + }, + dims=['time','y','x']) + + imgs.append(img) except FileNotFoundError: pass -#%% histogram + +# %% histogram try: az = azel[0] el = azel[1] except Exception: #azel data wasn't loaded az=el=None +# %% prepare metadata + imgs[0].attrs={'sensorloc':sensorloc,'az':az,'el':el} + + return imgs - return img,times,az,el,sensorloc,wlused def readCalFITS(indir,azfn,elfn,wl,minmax,tlim=None): indir = Path(indir).expanduser() @@ -47,8 +57,10 @@ def readCalFITS(indir,azfn,elfn,wl,minmax,tlim=None): #flist = [] #for w in wl: flist = sorted(indir.glob("PKR_DASC_0{}_*.FITS".format(wl))) + return readDASC(flist,azfn,elfn,minmax,tlim) + def readDASC(flist,azfn=None,elfn=None,minmax=None,treq=None): """ reads FITS images and spatial az/el calibration for allsky camera @@ -59,7 +71,8 @@ def readDASC(flist,azfn=None,elfn=None,minmax=None,treq=None): if not flist: raise FileNotFoundError('no files of this wavelength') - flist = np.atleast_1d(flist) + if isinstance(flist,(str,Path)): + flist = [flist] treq = totimestamp(treq) #%% read one file mode @@ -99,7 +112,7 @@ def readDASC(flist,azfn=None,elfn=None,minmax=None,treq=None): #%% preallocate, assuming all images the same size for f in flist: #find the first "good" file try: - with fits.open(str(f),mode='readonly') as h: + with fits.open(f, mode='readonly') as h: img = h[0].data sensorloc={'lat':h[0].header['GLAT'], 'lon':h[0].header['GLON'], @@ -116,7 +129,7 @@ def readDASC(flist,azfn=None,elfn=None,minmax=None,treq=None): #%% iterate over image files for i,fn in enumerate(flist): try: - with fits.open(str(fn),mode='readonly') as h: + with fits.open(fn, mode='readonly') as h: expstart = forceutc(parse(h[0].header['OBSDATE'] + ' ' + h[0].header['OBSSTART'])).timestamp() times[i,:] = [expstart,expstart + h[0].header['EXPTIME']] #EXPTIME is in seconds diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..7d73ed6 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,5 @@ +[build-system] +requires = ["setuptools", "wheel", +'python-dateutil','pytz','numpy','astropy','sciencedates', +'scipy','matplotlib','seaborn', + "nose"] diff --git a/setup.py b/setup.py index 439d4f6..467bf7c 100644 --- a/setup.py +++ b/setup.py @@ -1,31 +1,25 @@ #!/usr/bin/env python -req = ['nose','python-dateutil','pytz','numpy','astropy','scipy','matplotlib'] -pipreq = ['sciencedates'] - -import pip -try: - import conda.cli - conda.cli.main('install',*req) -except Exception as e: - pip.main(['install'] + req) -pip.main(['install'] + pipreq) +req = ['nose','python-dateutil','pytz','numpy','astropy','sciencedates'] # %% -from setuptools import setup +from setuptools import setup, find_packages setup(name='dascutils', - packages=['dascutils'], + packages=find_packages(), author='Michael Hirsch, Ph.D.', url='https://github.com/scivision/dascutils', description='Utilities for UAF Digital All-Sky Camera: reading and plotting', - version = '0.5', - classifiers=[ 'Intended Audience :: Science/Research', + version = '0.5.0', + classifiers=[ + 'Intended Audience :: Science/Research', 'Development Status :: 4 - Beta', 'License :: OSI Approved :: MIT License', 'Topic :: Scientific/Engineering :: Atmospheric Science', - 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3', ], - extras_requires={'themisasi':['themisasi']}, - install_requires=req+pipreq, + extras_require={'io':['themisasi'], + 'plot':['scipy','matplotlib'],}, + install_requires=req, + python_requires='>=3.6', ) diff --git a/tests/test.py b/tests/test.py index ea5b57e..9f6b137 100755 --- a/tests/test.py +++ b/tests/test.py @@ -1,4 +1,14 @@ #!/usr/bin/env python - +from pathlib import Path +import numpy.testing as npt +# from dascutils.readDASCfits import readallDasc +# +fn = 'tests/PKR_DASC_0428_20151007_082305.930.FITS' +rdir = Path(__file__).parents[0] + +def test_readdasc(): + imgs = readallDasc(rdir,None,None,[428],None) +if __name__ == '__main__': + npt.run_module_suite() \ No newline at end of file