View on GitHub

IRAF Community Distribution

IRAF maintained by the community

Home | Installation | Packages | X11IRAF | PyRAF | Forum

Variable Declarations

Joe Childers wrote on Jun 04, 2007

I am new to writing IRAF scripts and have three questions about variable declarations:

The first is fairly general: In procedure scripts, when should you put variable declarations before the begin and when in the body of the code immediately before they are used? I see examples of both in the Introductory Users Guide, and I'm not sure how they behave differently.

The second question involves debugging. Having run a task once and made changes to its script file, how do you delete existing declarations so you can run your task again without getting illegal variable declarations?

The third question concerns the modes of the variables. There are query, auto, and hidden, and I think one more, but what exactly do they do? For instance, when I try to run a script below to change the IMAGETYP header field based on the first four letters of the filename I unexpectedly get queried for the value to assign to the variable extension. Why isn't the scan taking care of that?

Any other suggestions you can give me on my code will be appreciated as well, of course.

Thanks,

Joe Childers
Ball State

procedure fiximagetype

string extension           # the extension on the image files
string *imagelist          # the list of image files in the directory
struct imagefile           # the filename of each file as the list is traversed

begin

# --------------------------------------------------------------------

# Delete all existing lists because of the appends coming later

delete ("*.lst")

# --------------------------------------------------------------------

# figure out what extension the raw image files are using

dir ("*.fits") | scan(extension)         
if (extension == "no")
     extension = "fit"
else
     extension = "fits"

# --------------------------------------------------------------------

# Create a list of all images in the directory

ls ("*." // extension // " > images.lst")

# --------------------------------------------------------------------

# Set DARKTIME = (exposure) for all images, adding as necessary

unlearn hedit
hedit.add = !(defpar("hedit.DARKTIME"))
hedit ("@image.lst","DARKTIME","(exposure)",ver-)

# --------------------------------------------------------------------

# Update IMAGETYP on all image files based on the first four letters
#    of the file name
# Add each image file to the appropriate processing list

imagelist = "images.lst"

hedit.add = no

while (fscan (imagelist,imagefile) != EOF) {

     if (substr(imagefile,1,4) == "bias") {
          hedit (imagefile,"IMAGETYP","bias")
          print (imagefile,>>"bias.lst") }

     else {

          if (substr(imagefile,1,4) == "dark") {
               hedit (imagefile,"IMAGETYP","dark")
               print (imagefile,>>"dark.lst") }

          else {

               if (substr(imagefile,1,4) == "flat") {
                    hedit (imagefile,"IMAGETYP","flat")
                    print (imagefile,>>"flat.lst") }

               else {
                    hedit (imagefile,"IMAGETYP","object")
                    print (imagefile,>>"object.lst") }

               }

           }

}

end

Mike Fitzpatrick wrote on Jun 04, 2007


The first is fairly general: In procedure scripts, when should you put variable declarations before the begin and when in the body of the code immediately before they are used? I see examples of both in the Introductory Users Guide, and I'm not sure how they behave differently.


Any variables declared between the 'procedure' and the 'begin' are considered to be parameters of that task. Moreover, variables declared as arguments in the 'procedure' statement are 'query parameters', the rest are 'hidden' (i.e. can be epar/lpar but will use a default value if supplied). Variables declared after the 'begin' are just variables.


The second question involves debugging. Having run a task once and made changes to its script file, how do you delete existing declarations so you can run your task again without getting illegal variable declarations?


You need to "unlearn" the task to remove any stored parameters from your uparm directory, e.g. "cl> unlearn mytask" to pick up new parameters.


The third question concerns the modes of the variables. There are query, auto, and hidden, and I think one more, but what exactly do they do? For instance, when I try to run a script below to change the IMAGETYP header field based on the first four letters of the filename I unexpectedly get queried for the value to assign to the variable extension. Why isn't the scan taking care of that?


Since you don't supply a default value you are prompted for it each time the param is queried (even if it is hidden). Normal practice for query params is to store them to local script variables once at the top of the script to avoid repeated prompts. If you want to supply a default extension but not actually make it a query param, declare it as e.g.


string extension  = "fits"      { prompt="Filename extension" }


Cheers,
-Mike

Joe Childers wrote on Jun 04, 2007

Thank you for those tips.

I'm running into another problem, I'm getting an illegal variable declaration on line 15. I'm including just the first part of the script up to where I think the error is, as I've changed it to reflect the above.

First of all, there is no variable declaration at line 15 of the .cl file, counting from the top. With line 1 being the line that starts with procedure then it is the struct extensiontest = "junk" that is at line 15. Do the error reports ignore leading blank or commented lines for line numbering?

Second, I don't understand why I'm getting an error, if this is indeed where it's happening. Typing the same exact thing into the CL as a terminal script performs correctly. Can someone please advise what I am doing wrong?

Thanks, Joe

######################################################################
#                                                                    #
# fiximagetype.cl                                                    #
#                                                                    #
# Joe Childers June 2007                                             #
# Ball State University                                              #
#                                                                    #
# This script will use the first four letters of an image's filename #
#    to set its IMAGETYP                                             #
# This script will add DARKTIME = (exposure)                         #
#                                                                    #
# Run this script in the directory of the files you want to change   #
#                                                                    #
######################################################################

procedure fiximagetype

begin

# --------------------------------------------------------------------

# Delete all existing lists because of the appends coming later

delete ("*.lst")

# --------------------------------------------------------------------

# Create a list of all images in the directory

struct extensiontest = "junk"
dir ("*.fit") | scan (extensiontest)
if (extensiontest == "no files found")
     ls ("*.fits",>"images.lst")
else
     ls ("*.fit",>"images.lst")         

# --------------------------------------------------------------------

# etc. the rest of the script follows essentially as in my previous post

Mike Fitzpatrick wrote on Jun 04, 2007

Joe,

I think the error is actually in the ordering of the code, i.e. having the variable declaration following the delete() command. This command execution puts the parse into a different state where it isn't expecting variable declarations and so you get the error, you don't see it from the command line because the parser treats the interactive interpreter a little differently. Try putting all the variable declarations immediately following the 'begin' statement and before you start executing tasks.

Cheers,
-Mike

Last post on Jun 04, 2007