Skip to content

Commit

Permalink
xarray
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Nov 16, 2017
1 parent a44cd55 commit 3a38a95
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 58 deletions.
2 changes: 2 additions & 0 deletions .codeclimate.yml
@@ -0,0 +1,2 @@
languages:
python: true
34 changes: 11 additions & 23 deletions .travis.yml
@@ -1,38 +1,26 @@
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

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

4 changes: 2 additions & 2 deletions PlotDASC.py
Expand Up @@ -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)
plotdasc(img,p.odir,p.cadence,None,None)
10 changes: 8 additions & 2 deletions README.rst
Expand Up @@ -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
Expand All @@ -26,14 +31,15 @@ Install
=======
::

python setup.py develop
pip install -e .
Download raw DASC files by time
===========================
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.
Expand Down
39 changes: 26 additions & 13 deletions dascutils/readDASCfits.py
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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'],
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
@@ -0,0 +1,5 @@
[build-system]
requires = ["setuptools", "wheel",
'python-dateutil','pytz','numpy','astropy','sciencedates',
'scipy','matplotlib','seaborn',
"nose"]
28 changes: 11 additions & 17 deletions 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',
)


12 changes: 11 additions & 1 deletion 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()

0 comments on commit 3a38a95

Please sign in to comment.