FAQ_LoaderUpDownload: sineIt.py

File sineIt.py, 1.3 KB (added by dkearney, 9 years ago)

sample science code

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
11from math import sin,pi
12
13if __name__ == '__main__':
14    inDeckName = sys.argv[1]
15
16    # try to open our file
17    try:
18        fh = open(inDeckName)
19    except IOError:
20        # we'll pass on dealing with the exception for now
21        # we'll just exit with a message
22        print 'Error opening file %s' % inDeckName
23        sys.exit(-1)
24
25    # read the special cryptic inputDeck
26    # inputDeck contains 3 numbers, each on a separate line
27    # the first number is the start value.
28    # the second number is the end value.
29    # the third number is the step.
30    # here we are splitting on whitespace (in this case, newlines '\n')
31    [start,end,step] = fh.read().split()
32    fh.close()
33
34    x = float(eval(start))
35    fstep = float(step)
36    outData = ''
37
38    while x < float(eval(end)):
39        # perform our sin calculation
40        y = sin(x)
41        # save the values to a string
42        outData += '%g %g\n' % (x,y)
43        # increment our loop control variable
44        x = x + fstep
45
46    # write our saved calculation values to disk
47    fh2 = open(inDeckName+'.out','w')
48    fh2.write(outData)
49    fh2.close()
50
51    # exit successfully
52    sys.exit(0)