NEAMS.py 3.3 KB
Newer Older
T.J. Corona's avatar
T.J. Corona committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#=============================================================================
#
#  Copyright (c) Kitware, Inc.
#  All rights reserved.
#  See LICENSE.txt for details.
#
#  This software is distributed WITHOUT ANY WARRANTY; without even
#  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
#  PURPOSE.  See the above copyright notice for more information.
#
#=============================================================================
"""
Export script for PyARC workflows
"""

import sys

#from PyARC import PyARC

sys.dont_write_bytecode = True

import smtk
if 'pybind11' == smtk.wrappingProtocol():
    import smtk.attribute
    import smtk.model
    import smtk.simulation

from internal.writers import pyarc_writer


ExportScope = type('ExportScope', (object,), dict())
# ---------------------------------------------------------------------
def ExportCMB(spec):
    '''Entry function, called by CMB to write export files

    Returns boolean indicating success
    Parameters
    ----------
    spec: Top-level object passed in from CMB
    '''
    print 'Enter ExportCMB()'

    # Initialize scope instance to store spec values and other info
    scope = ExportScope()
    scope.logger = spec.getLogger()
    scope.sim_atts = spec.getSimulationAttributes()
    if scope.sim_atts is None:
        msg = 'ERROR - No simulation attributes'
        print msg
        raise Exception(msg)
    scope.model_manager = scope.sim_atts.refModelManager()
    mask = int(smtk.model.MODEL_ENTITY)
    model_ents = scope.model_manager.entitiesMatchingFlags(mask, True)
    if not model_ents:
        msg = 'No model - cannot export'
        print 'WARNING:', msg
        scope.logger.addWarning(msg)
        print 'Abort export - check logger'
        return False
    elif len(model_ents) > 1:
        msg = 'Multiple models - using first one'
        print 'WARNING:', msg
        scope.logger.addWarning(msg)
    scope.model_ent = model_ents.pop()

    # Get export spec attribute
    scope.export_atts = spec.getExportAttributes()
    if scope.export_atts is not None:
        att_list = scope.export_atts.findAttributes('ExportSpec')
    if len(att_list) > 1:
        msg = 'More than one ExportSpec instance -- ignoring all'
        print 'WARNING:', msg
        scope.logger.addWarning(msg)
        return False
    export_spec_att = att_list[0]

    # Initialize solver list
    solver_item = export_spec_att.findString('Analysis')
    if solver_item is None:
        msg = 'Missing \"Analysis\" item -- cannot export'
        scope.logger.addError(msg)
        raise Exception(msg)
    if not solver_item.isSet(0):
        msg = 'Analysis item is not set -- cannot export'
        scope.logger.addError(msg)
        raise Exception(msg)
    solver_string = solver_item.value(0)
    #print 'solver_string:', solver_string
    scope.solver_list = solver_string.split('.')

    # Get output filename (path)
    output_path = None
    file_item = export_spec_att.findFile('OutputFile')
    if file_item is not None:
        output_path = file_item.value(0)
    if not output_path:
        raise Exception('No output file specified')
    scope.output_path = output_path

    reload(pyarc_writer)
    writer = pyarc_writer.PyARCWriter()
    success = writer.write(scope)

    if success:
        print 'Successfully wrote', scope.output_path
    return success