Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

15
Python Crash Course Python Crash Course Pyraf Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1

Transcript of Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

Page 1: Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

Python Crash CoursePython Crash CoursePyrafPyraf

3rd year Bachelors

V1.0

dd 06-09-2013

Hour 1

Page 2: Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

PyRAF – scripting IRAF with PythonPyRAF – scripting IRAF with Python

• http://www.stsci.edu/resources/software_hardware/pyraf

• Command line to replace cl, allows most normal IRAF commands and Python at same prompt

• Can use IRAF tasks in Python scripts instead of having to create awkward cl scripts (or worse SPP)

Page 3: Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

PyRAF – command linePyRAF – command line

• Command and filename completion• Edit line and access history easily (like ecl or bash) • Use just as friendlier cl prompt or use Python whenever you want• Transfer data between IRAF tasks and Python• Use brackets for tasks when you want it to behave like Python

$ pyraf

Welcome to IRAF. To list the available commands, type ? or ??. To get

detailed information about a command, type `help <command>'. To run a

command or load a package, type its name. Type `bye' to exit a

package, or `logout' to get out of the CL. Type `news' to find out

what is new in the version of the system you are using.

Visit http://iraf.net if you have questions or to report problems.

The following commands or packages are currently defined:

clpackage/:

clpackage/ guiapps/ obsolete/ stlocal/ vo/

ctio/ images/ plot/ stsdas/ xdimsum/

dataio/ language/ proto/ system/

dbms/ lists/ rvsao/ tables/

esowfi/ mscred/ softools/ user/

fitsutil/ noao/ stecf/ utilities/

PyRAF 1.10 (r1352) Copyright (c) 2002 AURA

Python 2.7.3 Copyright (c) 2001-2012 Python Software Foundation.

Python/CL command line wrapper

.help describes executive commands

-->

Page 4: Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

PyRAF – command linePyRAF – command line

--> imheader dev$pix long+

dev$pix[512,512][short]: m51 B 600s

No bad pixels, min=-1., max=19936.

Line storage mode, physdim [512,512], length of user area 1621 s.u.

Created Mon 23:54:13 31-Mar-1997, Last modified Sun 16:37:53 12-Mar-2006

Pixel file "HDR$pix.pix" [ok]

'KPNO-IRAF' /

'31-03-97' /

IRAF-MAX= 1.993600E4 / DATA MAX

IRAF-MIN= -1.000000E0 / DATA MIN

IRAF-BPX= 16 / DATA BITS/PIXEL

IRAFTYPE= 'SHORT ' / PIXEL TYPE

CCDPICNO= 53 / ORIGINAL CCD PICTURE NUMBER

ITIME = 600 / REQUESTED INTEGRATION TIME (SECS)

TTIME = 600 / TOTAL ELAPSED TIME (SECS)

OTIME = 600 / ACTUAL INTEGRATION TIME (SECS)

DATA-TYP= 'OBJECT (0)' / OBJECT,DARK,BIAS,ETC.

DATE-OBS= '05/04/87' / DATE DD/MM/YY

RA = '13:29:24.00' / RIGHT ASCENSION

DEC = '47:15:34.00' / DECLINATION

--> imstat dev$pix

# IMAGE NPIX MEAN STDDEV MIN MAX

dev$pix 262144 108.3 131.3 -1. 19936.

--> imcopy dev$pix mycopy.fits

dev$pix -> mycopy.fits

--> stsdas

+------------------------------------------------------------+

| Space Telescope Science Data Analysis System |

| STSDAS Version 3.14 |

| |

| Space Telescope Science Institute, Baltimore, Maryland |

| Copyright (C) 2003 Association of Universities for |

| Research in Astronomy, Inc.(AURA) |

| See stsdas$copyright.stsdas for terms of use. |

| For help, send e-mail to [email protected] |

| |

+------------------------------------------------------------+

--> fitsio

--> catfits mycopy.fits

EXT# FITSNAME FILENAME EXTVE DIMENS BITPI OBJECT

0 mycopy.fits 512x512 16 m51 B 600s

Page 5: Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

PyRAF – command linePyRAF – command line

--> imstat 2MASS_NGC_0891_K.fits

# IMAGE NPIX MEAN STDDEV MIN MAX

2MASS_NGC_0891_K.fits 1200000 -8.661 99.44 -1001. 7207.

--> fname = "data/2MASS_NGC_0891_K.fits"

--> imstat fname

# IMAGE NPIX MEAN STDDEV MIN MAX

Error reading image fname ...

--> imstat(fname)

# IMAGE NPIX MEAN STDDEV MIN MAX

data/2MASS_NGC_0891_K.fits 1200000 -8.661 99.44 -1001. 7207.

Page 6: Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

PyRAF – parameter editingPyRAF – parameter editing

--> epar imarith

Page 7: Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

PyRAF – graphics adn image displayPyRAF – graphics adn image display

--> prow dev$pix 256

--> from pyraf import gwm

--> gmw.window(“My own graphic”)

--> gwm.delete(“My own graphic”)

$ ds9 &

-->tv.display dev$pix

Frame to be written into (1:16) (1):

Z1=35. z2=346.0218

Page 8: Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

PyRAF – running tasksPyRAF – running tasks

The mapping of IRAF CL syntax to Python syntax is generally quite straightforward. The most notable requirements are: •The task or package name must be prefixed with iraf. because they come from the iraf module. In scripts, use

•to load the iraf module. Note that the first time PyRAF is imported, the normal IRAF startup process is executed (which can take a while). NOAO is working on techniques to do fast, lightweight startups for stand-alone Python scripts that use PyRAF. At the moment the only such approach is to startup in a directory with a custom version of login.cl that defines a minimal IRAF environment.

•It is also possible to import tasks and packages directly using

from pyraf import iraf

from pyraf.iraf import stsdas,imcalc

Page 9: Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

PyRAF – running tasksPyRAF – running tasks

Setting and listing the task parameters

--> s = iraf.imhead("dev$pix", long=yes, Stdout=1)

--> print s[0]

dev$pix[512,512][short]: m51 B 600s

--> iraf.head(nl=3, Stdin=s)

dev$pix[512,512][short]: m51 B 600s

No bad pixels, min=-1., max=19936.

Line storage mode, physdim [512,512], length of user area 1621 s.u.

--> iraf.imcopy.input = "dev$pix"

--> iraf.imcopy.output = "mycopy.fits"

--> iraf.imcopy() # run the task with the new values

--> iraf.imcopy.lParam()

--> iraf.lpar(iraf.imcopy) # Note there are no quotation marks

--> iraf.lpar('imcopy')

Page 10: Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

PyRAF - command linePyRAF - command line

• Many IRAF tasks create output images or tables on disk, or just print to screen, so don’t need to pass information back (see later for this)

stsdas # note can’t unload packages

improject(sky_file_2D, sky_file_1D, projaxis=2, average='no')

imcalc(sky_file_1D, sky_file_1D, 'im1*%f'%apwidthratio)

# calculate effective gain and ron due to combined images

gain = 1.91; ron = 5.41

gain_eff = gain * ncombine

ron_eff = ron * sqrt(ncombine)

# sig = sci + sky

imcalc('%s,%s'%(sci_file, sky_file_1D), sig_file, 'im1+im2')

# sig = sqrt(sig * gain + ron**2) / gain

equation = 'sqrt(max(im1,0.0)/%(g)8.5f + %(r2)8.5f/%(g2)8.5f)'

equation = equation%{'g': gain_eff, 'r2': ron_eff**2,

'g2': gain_eff**2}

imcalc(sig_file, sig_file, equation)

Page 11: Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

PyRAF - scriptingPyRAF - scripting

• Use IRAF tasks in Python scripts• Just import iraf object from pyraf module

from pyraf import iraf

from glob import glob

images = glob(“*sci.fits”)

for img in images:

iraf.imstat(img)

newimg = img.replace(‘sci’, ‘sig’)

iraf.imcalc(img, newimg, ’sqrt(img)')

Page 12: Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

PyRAF - scriptingPyRAF - scripting

• Can specify ‘default’ task parameters in neat fashion, instead of having to include on every call of a task

iraf.imstat.nclip = 3

iraf.imstat.lsigma = 5

iraf.imstat.usigma = 5

# now every time I use imstat it uses sigma clipping

iraf.imstat(im1)

iraf.imstat(im2)

iraf.imstat(im3)

# can revert to task defaults by unlearning

iraf.unlearn(‘imstat’) # note task name is a string

Page 13: Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

PyRAF - scriptingPyRAF - scripting

• Useful to make shortcuts

# shortcut for a long task name

crrej = iraf.stsdas.hst_calib.wfpc.crrej

crrej.mask = ‘mymask.fits’

crrej.sigma = 5

crrej(in1, out1)

crrej(in2, out2)

crrej(in3, out3)

Page 14: Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

PyRAF – input and output from tasksPyRAF – input and output from tasks

• IRAF outputs lots of useful data to screen - how to get at it?

• Some tasks need user input - would be nice to automate these

• PyRAF defines extra parameters Stdout and Stdin for all tasks

– Stdout can be a filename, file object, or 1 (one) to direct to a list

– Stdin can be a filename, file object, or string

for img in images:

# get a list with a string for each line of output

output = iraf.imstat(img, format=‘no’,fields=‘midpt’,Stdout=1)

# output can then be parsed as necessary to get at info

med = float(output[0])

newimg = img.replace(‘sci’, ‘sub’)

iraf.imcalc(img, newimg, ’im1 - %f’%med)

Page 15: Python Crash Course Pyraf 3 rd year Bachelors V1.0 dd 06-09-2013 Hour 1.

Introduction to languageIntroduction to language

End