def compute_energyRa(wallFile, transportFile, phaseFile, outputFile):
    '''
    @description: compute the energy distribution
    between transport, phase transition, and
    internal energy
    '''

    # extract the heat flux provided by the wall from
    # the netcdf file, wallFile
    energyWa = extract_energyWa(wallFile)


    # extract the data for the energy flowing
    # across the domain borders
    energyTr = np.loadtxt(transportFile)


    # extract the data for the energy
    # used by the phase transition process
    energyPh = np.loadtxt(phaseFile)


    # find the file index matching the 
    # energyTr and energyPh data
    tmin = int(energyTr[0,0])
    while tmin<int(energyPh[0,0]):
        tmin+=1
    if(tmin>int(energyTr[-1,0])):
        print_mg_error('compute_energyRa')
        print_mg_error('cannot find the file index')
        print_mg_error('to match energyTr and energyPh')
        sys.exit(2)
    tmax = int(energyPh[-1,0])


    # compute the energy ratios
    if(os.path.isfile(outputFile)):
        os.remove(outputFile)

    out = open(outputFile,'w')

    for t in range(tmin,tmax+1):

        if(int(energyTr[t,1])!=int(energyPh[t-tmin,1])):
            print_mg_error('compute_energyRa')
            print_mg_error('mismatch between energyTr and energyPh data')
            sys.exit(2)

        energyTr_r = energyTr[t,2]/energyWa*100
        energyPh_r = energyPh[t-tmin,2]/energyWa*100
        energyIn_r = 100-(energyTr_r+energyPh_r)

        out.write("%i %f %f %f %f\n" % (t,energyTr[t,1],energyTr_r,energyPh_r,energyIn_r))

    out.close()
def parse_argv(argv):
    '''
    @description:
    parse the program options
    '''

    #default values
    mainDir = 'None'


    # store the options and the arguments
    # in opts, args
    try:
        opts, args = getopt.getopt(argv,
                                   "hi:",
                                   ["help",
                                    "input="])
    except getopt.GetoptError:
        display_help()
        sys.exit(2)

    if(len(opts)==0):
        display_help()
        sys.exit(2)


    mainDir = 'None'
    outputFile = 'None'

    # options
    for opt, arg in opts:

        if opt in ("-h", "--help"):
            display_help()
            sys.exit(2)

        elif opt in ("-i", "--input"):
            mainDir = arg


    # check for directory with the netcdf files
    if(mainDir=='None'):
        print_mg_error('directory for netcdf file not provided')
        display_help()
        sys.exit(2)

    else:
        if( not os.path.isdir(mainDir)):
            print_mg_error(mainDir+' does not exist')
            display_help()
            sys.exit(2)


    return mainDir
def extract_energyWa(wallFile):
    '''
    @description: extract the energy rate supplied by
    the wall from the header of the netcdf file
    '''

    cmd='ncdump -h '+wallFile
    args = shlex.split(cmd)
    output = subprocess.Popen(args,stdout=subprocess.PIPE).communicate()[0]

    output = output.split('wall_maximum_heat_source')[1].split(';')[0].replace('=','')

    try:
        energyWa = float(output)

    except ValueError:
        print_mg_error('extract_energyWa')
        print_mg_error('error when converting wall energy to float')
        print_mg_error('output from ncdump: ',output)
        sys.exit(2)

    return energyWa
def parse_argv(argv):
    '''
    @description:
    parse the program options
    '''

    #default values
    mainDir = 'None'


    # store the options and the arguments
    # in opts, args
    try:
        opts, args = getopt.getopt(argv,
                                   "hi:w:t:p:o:",
                                   ["help",
                                    "wall=",
                                    "transport=",
                                    "phase=",
                                    "output="])
    except getopt.GetoptError:
        display_help()
        sys.exit(2)

    if(len(opts)==0):
        display_help()
        sys.exit(2)


    wallFile = 'None'
    transportFile = 'None'
    phaseFile = 'None'
    outputFile = 'None'


    # options
    for opt, arg in opts:

        if opt in ("-h", "--help"):
            display_help()
            sys.exit(2)

        elif opt in ("-w", "--wall"):
            wallFile = arg

        elif opt in ("-t", "--transport"):
            transportFile = arg

        elif opt in ("-p", "--phase"):
            phaseFile = arg

        elif opt in ("-o", "--output"):
            outputFile = arg


    # check the wall file
    check_inputFile(wallFile,errorMg='wall file not provided')


    # check the transport file
    check_inputFile(transportFile,errorMg='transport file not provided')


    # check the phase file
    check_inputFile(phaseFile,errorMg='phase file not provided')


    # check for directory where the output file should be saved
    if(outputFile=='None'):
        print_mg_error('output path not provided')
        display_help()
        sys.exit(2)

    else:
        if( not os.path.isdir(os.path.dirname(outputFile)) and (not os.path.dirname(outputFile)=='')):
            print_mg_error('output dir does not exist')
            display_help()
            sys.exit(2)
    
    return wallFile, transportFile, phaseFile, outputFile