Extracting scripts from these tutorials: Difference between revisions

From CASA Guides
Jump to navigationJump to search
(Undo revision 856 by Jgallimo (Talk))
(45 intermediate revisions by 12 users not shown)
Line 1: Line 1:
Hopefully the scripts contained in this documentation are (a) useful and (b) work. They were however developed with CASA still a work in progress, and so scripts may break as commands, arguments, and keywords change. We developed this script extractor to allow us to easily extract scripts from these pages and run them. Feel free to try it!
The series of commands shown in each tutorial can be written to a single casapy script using the CASA Guides script extractor. The script extractor is described here.


__TOC__
__TOC__


== How to Get the Script Extractor ==
== Introduction ==


Download the script extraction code.
The script extractor will produce a Python script that can be invoked inside casapy.  The extractor ignores all CASA Guide source code that is not Python.  Special UNIX commands (such as 'ls' and 'less') supported by CASA but not by Python are commented out, and interactive pauses are inserted when GUIs are invoked.  (The interactive pause is necessary to avoid table lock errors that are produced when multiple tasks are run simultaneously.)


<source lang="bash">
If you are using OSX, you must have Python 2.7.13 or greater. Due to OSX security updates, the script extractor will not work with earlier versions of Python.


# in bash
== How to Get the Script Extractor ==
ftp ftp.cv.nrao.edu
# log in anonymously with e-mail as password
cd NRAO-staff/jgallimo
get extractCASAscript.py
</source>
 
Wget may be even simpler if you have it installed.
 
<source lang="bash">
# in bash
wget ftp://ftp.cv.nrao.edu:/NRAO-staff/jgallimo/extractCASAscript.py
</source>
 
=== FTP didn't work? ===
 
Typical. Here's a copy of the source code, dated 30 Oct 2009.
 
<source lang="python">
#!/usr/bin/env python
#
# python script intended to extract CASA code from the CASA guide
# Wiki and collect it into a (CASA-executable) script.
# Jack Gallimore 8/10/09
# Updated to accommodate new web host 10/30/09.
 
 
import urllib
import urllib2
import sys
import codecs
import re
 
# globals
 
# define formatting junk that needs to be filtered
# If I were cleverer with regex the ensuing loop would probably not be necessary.
junkStr = ["<div dir=\"ltr\" style=\"text-align: left;\">"]
junkStr = junkStr + ["<div class=\"source-python\" style=\"font-family: monospace;\">"]
junkStr = junkStr + ["<pre>"]
junkStr = junkStr + ["</span>"]
junkStr = junkStr + ["</pre></div></div>"]
junkStr = junkStr + ["&#93;"]
junkStr = junkStr + ["&#91;"]
junkStr = junkStr + ["&#123;"]
junkStr = junkStr + ["&#125;"] # Daddy really needs to practice more regex
junkStr = junkStr + ["&nbsp;"]
paren1 = "&#40;"
paren2 = "&#41;"
quote1 = "&quot;"
substr1 = r"<span class=[^>]*>"
 
 
# define casa code blocks
beginBlock = "class=\"source-python\""
endBlock = "</pre></div></div>"
 
 
# function to clean up html strings (convert html markup to executable python)
def loseTheJunk(line):
    outline = line
    outline = re.sub(substr1, r'', outline)
    for junk in junkStr:
        outline = outline.replace(junk, "")
    outline = outline.replace(quote1, "\"")
    outline = outline.replace(paren1, "(")
    outline = outline.replace(paren2, ")")
  #some additional parsing -- scripting has slightly different
    #syntax than interactive session for tget, default, and go
    #(presumably among others).


    newline = outline
<!--
    newline = newline.replace(r'tget ', r'tget(')
[https://github.com/jaredcrossley/CASA-Guides-Script-Extractor/blob/master/extractCASAscript.py script extractor]
    newline = newline.replace(r'default ', r'default(')
-->
    newline = newline.replace(r'go', r'go(')
    if newline != outline: newline = newline + ')'
    outline = newline


    return outline
For most uses, the single-module [https://github.com/jaredcrossley/CASA-Guides-Script-Extractor/blob/master/extractCASAscript.py script extractor] is all you will need. This Python module is maintained in a github repository as part of a simple, automated benchmark testing system. If you like, you can browse the [https://github.com/jaredcrossley/CASA-Guides-Script-Extractor whole repository] also.
   
 
# start of main code
 
def main():
    try:
        baseURL = sys.argv[1]
    except:
        print 'No argument given.'
        print 'Syntax: extractCASAscript.py    \'http:blah.blah.edu/web_site/\''
        sys.exit(2)
 
    print "Rest assured. I'm trying to get " + baseURL + " for you now."
    outFile = baseURL.split('/')[-1] + '.py'
    outFile = outFile.replace("index.php?title=","")
    outFile = outFile.replace(":","")
    outFile = outFile.replace("_","")
   
    f = codecs.open(outFile, 'w','utf-8')
 
   
    req = urllib2.Request(baseURL)
    response = urllib2.urlopen(req)
    the_page = response.read().split("\n")
   
    iActive = 0
    print "Things are going well. Let me clean out some of that html markup."
    for line in the_page:
        if (iActive == 0):
            # see if this line begins a python code block
            temp = line.find(beginBlock)
            if temp > -1:
                iActive = 1
                outline = loseTheJunk(line)
                # make sure the endBlock isn't in the same line!
                temp = line.find(endBlock)
                if temp > -1:
                    iActive = 0
                line = "DontPrintMeBro" # avoid double printing if endBlock is on the same line
                #debug
                #print ""
                #print 'line = ' + line
                #print 'outline = ' + outline
                #print ""
                #debug
                print >>f, outline
        if (iActive == 1):
            if (line != "DontPrintMeBro"):
                outline = loseTheJunk(line)
                print >>f, outline
            temp = line.find(endBlock)
            if temp > -1:
                iActive = 0
    f.close()
    print "Great. I think I just wrote the file " + outFile + " in the current directory. No promises."
   
if __name__ == "__main__":
    main()
 
</source>


== How to Use the Script Extractor ==
== How to Use the Script Extractor ==


Make your newly acquired python script executable.
Make your newly acquired Python script executable.


<source lang="bash">
<source lang="bash">
# in bash
# in bash
mv ExtractCASAscript.py extractCASAscript.py
chmod u+x extractCASAscript.py
chmod u+x extractCASAscript.py
</source>
</source>


To run it, issue the python script name and give the URL as the argument. For example:
To run it, issue the Python script name and give the URL as the argument. For example:


<source lang="bash">
<source lang="bash">
# in bash
# in bash or csh
extractCASAscript.py http://casaguides.nrao.edu/index.php?title=Calibrating_a_VLA_5_GHz_continuum_survey
./extractCASAscript.py 'http://casaguides.nrao.edu/index.php?title=Calibrating_a_VLA_5_GHz_continuum_survey'
</source>
</source>


This command will automatically generate a script called <tt>CalibratingaVLA5GHzcontinuumsurvey.py</tt>.  This script can be run in CASA:


This command will automatically generate a script called "CalibratingaVLA5GHzcontinuumsurvey.py"
<source lang="python">
# in CASA
execfile('CalibratingaVLA5GHzcontinuumsurvey.py')
</source>


== Support ==


--[[User:Jgallimo|Jack Gallimore]] 21:13, 3 November 2009 (UTC)
Questions and bug reports should be submitted to either the [https://help.nrao.edu/ NRAO] or [https://help.almascience.org/ ALMA] helpdesks.

Revision as of 13:39, 10 May 2018

The series of commands shown in each tutorial can be written to a single casapy script using the CASA Guides script extractor. The script extractor is described here.

Introduction

The script extractor will produce a Python script that can be invoked inside casapy. The extractor ignores all CASA Guide source code that is not Python. Special UNIX commands (such as 'ls' and 'less') supported by CASA but not by Python are commented out, and interactive pauses are inserted when GUIs are invoked. (The interactive pause is necessary to avoid table lock errors that are produced when multiple tasks are run simultaneously.)

If you are using OSX, you must have Python 2.7.13 or greater. Due to OSX security updates, the script extractor will not work with earlier versions of Python.

How to Get the Script Extractor

For most uses, the single-module script extractor is all you will need. This Python module is maintained in a github repository as part of a simple, automated benchmark testing system. If you like, you can browse the whole repository also.

How to Use the Script Extractor

Make your newly acquired Python script executable.

# in bash
mv ExtractCASAscript.py extractCASAscript.py
chmod u+x extractCASAscript.py

To run it, issue the Python script name and give the URL as the argument. For example:

# in bash or csh
./extractCASAscript.py 'http://casaguides.nrao.edu/index.php?title=Calibrating_a_VLA_5_GHz_continuum_survey'

This command will automatically generate a script called CalibratingaVLA5GHzcontinuumsurvey.py. This script can be run in CASA:

# in CASA
execfile('CalibratingaVLA5GHzcontinuumsurvey.py')

Support

Questions and bug reports should be submitted to either the NRAO or ALMA helpdesks.