Package: cadccutout
Cutout library written in Python that uses Astropy APIs.InstallationCan be installed usingpip install cadccutoutin Python 2.7 or 3.X.APICutouts are performed unidirectionally, meaning the library assumes an
input stream that can only be read once, rather than seeking. If more
than one HDU was requested, then each HDU is iterated over and compared
from top to bottom. Single HDU requests are shortcircuited using the
Astropyastropy.io.fits.getdata()functionPython 3.x has a potential issue when appending to an output stream
where Astropy incorrectly sets a mode to prevent appending (SeeAstropy
GitHub 7856).Example 1Perform a cutout of a file using thecfitsiocutout string format.importtempfilefromcadccutoutimportOpenCADCCutouttest_subject=OpenCADCCutout()output_file=tempfile.mkstemp(suffix='.fits')input_file='/path/to/file.fits'# Cutouts are in cfitsio format.cutout_region_string='[300:800,810:1000]'# HDU 0 along two axes.# Needs to have 'append' flag set.  The cutout() method will write out the data.withopen(output_file,'ab+')asoutput_writer,open(input_file,'rb')asinput_reader:test_subject.cutout(input_reader,output_writer,cutout_region_string,'FITS')Example 2 (CADC)Perform a cutout from an input stream from an HTTP request.importtempfilefromcadccutoutimportOpenCADCCutoutfromcadcdataimportCadcDataClienttest_subject=OpenCADCCutout()anonSubject=net.Subject()data_client=CadcDataClient(anonSubject)output_file=tempfile.mkstemp(suffix='.fits')archive='HST'file_name='n8i311hiq_raw.fits'input_stream=data_client.get_file(archive,file_name)# Cutouts are in cfitsio format.cutout_region_string='[SCI,10][80:220,100:150]'# SCI version 10, along two axes.# Needs to have 'append' flag set.  The cutout() method will write out the data.withopen(output_file,'ab+')asoutput_writer:test_subject.cutout(input_stream,output_writer,cutout_region_string,'FITS')Command Line AccessThe executablecadccutoutis installed by default, or the module can be run usingpython-mcadccutout.Runningcadccutout-d--infilepath/to/source.fits--outfilepath/to/output.fits [100:400]cadccutout-d--infilepath/to/source.fits--outfilepath/to/output.fits "CIRCLE=10 60 0.5"Running in DockerThe providedDockerfilecan be used to build an image based on the desired
Python version.Build an image for Python 2.7:docker build--build-argPYTHON_VERSION=2.7-topencadc/cadccutout:2.7-alpine.Then execute it (/usr/src/datais the location of the source files).  This will send the output to standard out:docker run--rm-v$(pwd):/usr/src/dataopencadc/cadccutout:2.7-alpinecadccutout--infile/usr/src/data/myfile.fits [100:400]ordocker run--rm--mounttype=bind,source=$(pwd),target=/usr/src/dataopencadc/cadccutout:2.7-alpinecadccutout--infile/usr/src/data/myfile.fits [100:400]Build an image for Python 3.6:docker build--build-argPYTHON_VERSION=3.6-topencadc/cadccutout:3.6-alpine.Then execute it (/usr/src/datais the location of the source files).  This will send the output to a FITS file:docker run--rm-v$(pwd):/usr/src/dataopencadc/cadccutout:3.6-alpinecadccutout--infile/usr/src/data/myfile.fits--outfile/usr/src/data/mycutout_0_100_400.fits [100:400]ordocker run--rm--mounttype=bind,source=$(pwd,target=/usr/src/dataopencadc/cadccutout:3.6-alpinecadccutout--infile/usr/src/data/myfile.fits--outfile/usr/src/data/mycutout_0_100_400.fits [100:400]TestingDockerThe easiest thing to do is to run it with docker. OpenCADC has anAstroQuery docker imageavailable for runtime available in Python
2.7, 3.5, 3.6, and 3.7.Run tests in DockerYou can mount the local dev directory to the image and run the python
test that way. From inside the dev (working) directory:Python 3.7:docker run--rm-v$(pwd):/usr/src/appopencadc/astroquery:3.7-alpinepython setup.py testordocker run--rm--mounttype=bind,source=$(pwd),target=/usr/src/appopencadc/astroquery:3.7-alpinepython setup.py testPython 2.7:docker run--rm-v$(pwd):/usr/src/appopencadc/astroquery:2.7-alpinepython setup.py testordocker run--rm--mounttype=bind,source=$(pwd),target=/usr/src/appopencadc/astroquery:2.7-alpinepython setup.py test