FAQ_LoaderUpDownload: wrapper.py

File wrapper.py, 3.1 KB (added by dkearney, 6 years ago)
Line 
1#! /bin/sh
2#
3""":"
4exec python $0 ${1+"$@"}
5"""
6
7# This script represents a bit of science code that takes in 3 numeric
8# values and outputs a file with values of a sine function.
9
10import sys
11import os
12import re
13import Rappture
14
15if __name__ == '__main__':
16
17    inDeckName = sys.argv[1]
18
19    lib = Rappture.library(inDeckName)
20
21    if lib == None:
22        # we'll pass on dealing with the exception for now
23        # we'll just exit with a message
24        print 'Error opening file %s' % inDeckName
25        sys.exit(-1)
26
27    # check to see if the user uploaded their own data
28    # or if they inputted the values
29    inputType = lib.get('input.loader.current')
30    if inputType == 'Uploaded data':
31        # they uploaded their datafile
32        # we need to write this data to a file so our science code can read it
33        # find the location of where rappture stored the data from their file
34        # and store the data int the writeData variable
35        writeDataLocation = lib.get('input.loader.upload.to')
36        writeData = lib.get(writeDataLocation+'.current')
37    else:
38        # they manually entered their data into the GUI
39        # grab each value and create a string we can write to a file.
40        start = lib.get('input.string(start).current')
41        end = lib.get('input.string(end).current')
42        step = lib.get('input.number(step).current')
43        writeData = '%s\n%s\n%s' % (start,end,step)
44
45    # prepare our semi-unique file name
46    outFileName = 'inputDeck.' + Rappture.tools.getDriverNumber(inDeckName)
47    fh = open(outFileName, 'w')
48    if fh != None:
49        # write our input deck to the file
50        fh.write(writeData)
51        fh.close()
52
53    # run the science code
54    dirpath = os.path.dirname(os.path.abspath(sys.argv[0]))
55    Rappture.tools.getCommandOutput('%s/sineIt.py %s' % (dirpath,outFileName))
56
57    # our science code outputted the answer to a file
58    # grab that file and add the answer to the Rappture library object
59    dataFileName = outFileName + '.out'
60    data = ''
61
62    # try to open our data file
63    try:
64        dFH = open(dataFileName)
65
66        # read the data from the data file
67        data = dFH.read()
68        dFH.close()
69    except IOError:
70        # we'll pass on dealing with the exception for now
71        # send a message to the user letting them know there was an error
72        errMsg = 'Error opening file %s' % dataFileName
73        lib.put('output.string(errorLog).current',errMsg,1)
74
75
76    # store the data inside of the rappture library
77    lib.put('output.curve(sin_data).about.label','sin curve')
78    lib.put('output.curve(sin_data).about.label','sample output of the sin function')
79    lib.put('output.curve(sin_data).component.xy',data)
80
81    # remove any temporary files
82    try:
83        os.remove(outFileName)
84        os.remove(dataFileName)
85    except OSError:
86        # we were unable to remove the files,
87        # send a note to the user
88        errMsg = 'Error while removing tmp files'
89        lib.put('output.string(errorLog).current',errMsg,1)
90
91    # signal to the Rappture GUI that processing has completed
92    Rappture.result(lib)
93
94    # exit gracefully
95    sys.exit(0)