Changes between Version 2 and Version 3 of FAQ_LoaderUpDownload


Ignore:
Timestamp:
Jul 6, 2006 12:30:35 PM (18 years ago)
Author:
dkearney
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • FAQ_LoaderUpDownload

    v2 v3  
    140140        sys.exit(-1)
    141141
     142    # check to see if the user uploaded their own data
     143    # or if they inputted the values
    142144    inputType = lib.get('input.loader.current')
    143145    if inputType == 'Uploaded data':
     146        # they uploaded their datafile
     147        # we need to write this data to a file so our science code can read it
     148        # find the location of where rappture stored the data from their file
     149        # and store the data int the writeData variable
    144150        writeDataLocation = lib.get('input.loader.upload.to')
    145151        writeData = lib.get(writeDataLocation+'.current')
    146152    else:
     153        # they manually entered their data into the GUI
     154        # grab each value and create a string we can write to a file.
    147155        start = lib.get('input.string(start).current')
    148156        end = lib.get('input.string(end).current')
     
    150158        writeData = '%s\n%s\n%s' % (start,end,step)
    151159
     160    # prepare our semi-unique file name
    152161    outFileName = 'inputDeck.' + getDriverId(inDeckName)
    153162    fh = open(outFileName, 'w')
    154163    if fh != None:
     164        # write our input deck to the file
    155165        fh.write(writeData)
    156166        fh.close()
    157167
    158168    # run the science code
    159     getCommandOutput('./sineIt.py %s' % outFileName)
    160 
     169    dirpath = os.path.dirname(sys.argv[0])
     170    getCommandOutput('%s/sineIt.py %s' % (dirpath,outFileName))
     171
     172    # our science code outputted the answer to a file
     173    # grab that file and add the answer to the Rappture library object
    161174    dataFileName = outFileName + '.out'
     175    data = ''
    162176
    163177    # try to open our data file
    164178    try:
    165179        dFH = open(dataFileName)
     180
     181        # read the data from the data file
     182        data = dFH.read()
     183        dFH.close()
    166184    except IOError:
    167185        # we'll pass on dealing with the exception for now
    168         # we'll just exit with a message
    169         print 'Error opening file %s' % dataFileName
    170         sys.exit(-1)
    171 
    172     data = dFH.read()
    173     dFH.close()
    174 
     186        # send a message to the user letting them know there was an error
     187        errMsg = 'Error opening file %s' % dataFileName
     188        lib.put('output.string(errorLog).current',errMsg,1)
     189
     190
     191    # store the data inside of the rappture library
    175192    lib.put('output.curve(sin_data).about.label','sin curve')
    176193    lib.put('output.curve(sin_data).about.label','sample output of the sin function')
    177194    lib.put('output.curve(sin_data).component.xy',data)
    178195
     196    # remove any temporary files
     197    try:
     198        os.remove(outFileName)
     199        os.remove(dataFileName)
     200    except OSError:
     201        # we were unable to remove the files,
     202        # send a note to the user
     203        errMsg = 'Error while removing tmp files'
     204        lib.put('output.string(errorLog).current',errMsg,1)
     205
     206    # signal to the Rappture GUI that processing has completed
    179207    Rappture.result(lib)
     208
     209    # exit gracefully
    180210    sys.exit(0)
    181211
     
    232262}}}
    233263
    234 
    235 
    236 
     264== How it works ==
     265
     266As always, first the user starts simulation and the graphical user interface comes up.
     267
     268
     269By clicking on the loader (drop down menu) and choosing the "Upload" option the user
     270can pick a file from their local computer and have it uploaded to the rappture graphical
     271user interface.
     272
     273After the user uploads their data, it is stored in the xml file at the location described in the loader:
     274{{{
     275<input>
     276  <loader>
     277    <upload>
     278      <to>input.string(uploadedFile)</to>
     279      <prompt>Upload Input Deck.</prompt>
     280    </upload>
     281  </loader>
     282...
     283}}}
     284In this case the contents of the uploaded file was saved to ''input.string(uploadedFile).current'' as described by the path ''input.loader.upload.to''.
     285
     286Next the user clicks the simulate button. Rappture writes the inputs to the driver.xml file and executes the tool command (wrapper.py).
     287wrapper.py reads the xml file, checks to see if the user uploaded data to the graphical user interface or manually filled in the values.
     288You can check to see if the user uploaded data by comparing the value of ''input.loader.current'' to the string 'Uploaded data'. If they match, then you know the user uploaded data into the graphical user interface. As the wrapper.py script shows, to get the data that was uploaded by the user, simply issue a get command with the "upload to" location specified.
     289
     290In the wrapper.py python script above we issue these commands:
     291{{{
     292        writeDataLocation = lib.get('input.loader.upload.to')
     293        writeData = lib.get(writeDataLocation+'.current')
     294}}}
     295
     296now writeData holds the information that was uploaded and we can write it to a file, which is what the science code expects.
     297
     298{{{
     299    # prepare our semi-unique file name
     300    outFileName = 'inputDeck.' + getDriverId(inDeckName)
     301    fh = open(outFileName, 'w')
     302    if fh != None:
     303        # write our input deck to the file
     304        fh.write(writeData)
     305        fh.close()
     306}}}
     307
     308After writing the data to a file, we call the science code with the name of the input deck. The science code reads the input deck, calculates its science and writes the answer back to a file. The wrapper.py script then reads and parses the result file from the science code and places the results back into the rappture library.
    237309
    238310