Commit 5e3b1843 authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Implement np_cluster legacy result parser

parent cde330cd
Loading
Loading
Loading
Loading
+123 −61
Original line number Diff line number Diff line
@@ -62,7 +62,7 @@ def main():
                if (config['application'] == 'CLU'):
                    try:
                        errors += parse_legacy_oclu(config)
                    except Error as ex:
                    except Exception as ex:
                        print(ex)
                        errors += 1
    return errors
@@ -104,6 +104,8 @@ def parse_arguments():
            skip_arg = True
        elif (arg.startswith("--app=")):
            config['application'] = split_arg[1]
        elif (arg.startswith("--selection=")):
            config['selection'] = split_arg[1]
        elif (arg.startswith("--help")):
            config['help_mode'] = True
        else:
@@ -119,26 +121,61 @@ def parse_legacy_oclu(config):
    errors = 0
    oclu_name = config['result_file']
    root_name = config['output_name']
    out22 = None # Cluster average cross-sections
    out20 = None # Cluster average cross-sections
    out31 = None # Cluster differential cross-sections in state -1
    out32 = None # Cluster differential cross-sections in state +1
    out91 = None # Cluster radiation pressure forces in state +1
    out92 = None # Cluster radiation pressure forces in state -1
    out40 = None # Cluster integrated asymmetry parameter and radiation pressure
    out51 = None # Cluster differential asymmetry parameter and radiation pressure forces in state -1
    out52 = None # Cluster differential asymmetry parameter and radiation pressure forces in state +1
    try:
        oclu_file = open(oclu_name, "r") # open the OCLU file for reading
        file_line = "first line" # a string to parse the OCLU file lines
        if ('ALL' in config['selection'] or 'ICS' in config['selection']):
            out22 = open(root_name + "_ics.csv", "w") # open a file for integrated cross sections
            out20 = open(root_name + "_ics.csv", "w") # open a file for integrated cross sections
            out20.write("Wavelength,ScaSec,AbsSec,ExtSec\n")
        if ('ALL' in config['selection'] or 'DCS' in config['selection']):
            out31 = open(root_name + "_dcs1.csv", "w") # open a file for differential cross-sections in state -1
            out31.write("Wavelength,THi,THs,PHi,PHs,ScaSec,AbsSec,ExtSec\n")
            out32 = open(root_name + "_dcs2.csv", "w") # open a file for differential cross-sections in state +1
        if ('ALL' in config['selection'] or 'RAP' in config['selection']):
            out91 = open(root_name + "_dcs2.csv", "w") # open a file for differential cross-sections in state +1
            out92 = open(root_name + "_dcs1.csv", "w") # open a file for differential cross-sections in state -1
            out32.write("Wavelength,THi,THs,PHi,PHs,ScaSec,AbsSec,ExtSec\n")
        if ('ALL' in config['selection'] or 'IRP' in config['selection']):
            out40 = open(root_name + "_irp.csv", "w") # open a file for integrated radiation pressure forces
            out40.write("Wavelength,CosAv,RaPr\n")
        if ('ALL' in config['selection'] or 'DRP' in config['selection']):
            out51 = open(root_name + "_drp1.csv", "w") # open a file for differential radiation pressure forces in state -1
            out51.write("Wavelength,THi,THs,PHi,PHs,CosAv,RaPr\n")
            out52 = open(root_name + "_drp1.csv", "w") # open a file for differential radiation pressure forces in state +1
            out52.write("Wavelength,THi,THs,PHi,PHs,CosAv,RaPr\n")

        # Define the quantities that you need to extract
        alam = 0.0

        # Read the output file preamble
        for i in range(2):
            file_line = oclu_file.readline()
        nsph = int(file_line[0:6])
        for i in range(nsph + 3):
            file_line = oclu_file.readline()
        thifirst = float(file_line[0:11].replace("D", "E"))
        thistep = float(file_line[12:21].replace("D", "E"))
        thilast = float(file_line[22:31].replace("D", "E"))
        thsfirst = float(file_line[32:41].replace("D", "E"))
        thsstep = float(file_line[42:51].replace("D", "E"))
        thslast = float(file_line[52:61].replace("D", "E"))
        nthi = 1 if thistep == 0.0 else 1 + int((thilast - thifirst) / thistep)
        nths = 1 if thsstep == 0.0 else 1 + int((thslast - thsfirst) / thsstep)
        for i in range(2):
            file_line = oclu_file.readline()
        phifirst = float(file_line[0:11].replace("D", "E"))
        phistep = float(file_line[12:21].replace("D", "E"))
        philast = float(file_line[22:31].replace("D", "E"))
        phsfirst = float(file_line[32:41].replace("D", "E"))
        phsstep = float(file_line[42:51].replace("D", "E"))
        phslast = float(file_line[52:61].replace("D", "E"))
        nphi = 1 if phistep == 0.0 else 1 + int((philast - phifirst) / phistep)
        nphs = 1 if phsstep == 0.0 else 1 + int((phslast - phsfirst) / phsstep)
        ndirs = nthi * nths * nphi * nphs
        
        # Parsing loop until the end of the OCLU file
        while (file_line != ""):
            file_line = oclu_file.readline() # read the next OCLU file line
@@ -165,13 +202,30 @@ def parse_legacy_oclu(config):
                        extsm = float(file_line[32:45].replace("D", "E"))
                        # we can write the average values, similarly to fort.22
                        # note that \n puts a new line at the end of the string
                        output_line = "{0:15.4E}{1:15.4E}{2:15.4E}{3:15.4E}\n".format(alam, scasm, abssm, extsm)
                        if (out22 is not None): out22.write(output_line)
                        output_line = "{0:.7E},{1:.7E},{2:.7E},{3:.7E}\n".format(alam, scasm, abssm, extsm)
                        if (out20 is not None): out20.write(output_line)
                        # we know that the asymmetry parameter and the radiation
                        # pressure forces will be after 8 more lines
                        for i in range(8):
                            file_line = oclu_file.readline()
                        cosav = float(file_line[8:23].replace("D", "E"))
                        rapr = float(file_line[31:46].replace("D", "E"))
                        output_line = "{0:.7E},{1:.7E},{2:.7E}\n".format(alam, cosav, rapr)
                        if (out40 is not None): out40.write(output_line)
                        found_averages = True # terminate the inner loop
                # the averages were written. We look for CLUSTER section
                # using another inner loop
                found_differentials = False
                while (not found_differentials):
                    for di in range(ndirs):
                        while ("JTH =" not in file_line):
                            file_line = oclu_file.readline()
                        file_line = oclu_file.readline()
                        tidg = float(file_line[7:17].replace("D", "E"))
                        pidg = float(file_line[24:34].replace("D", "E"))
                        tsdg = float(file_line[41:51].replace("D", "E"))
                        psdg = float(file_line[58:68].replace("D", "E"))
                        while ("     CLUSTER" not in file_line):
                            file_line = oclu_file.readline()
                        if ("     CLUSTER" in file_line):
                            # we found CLUSTER. We know cross-sections for
@@ -185,7 +239,7 @@ def parse_legacy_oclu(config):
                            abc31 = float(file_line[17:30].replace("D", "E"))
                            exc31 = float(file_line[32:45].replace("D", "E"))
                            # we can write the differential values, similarly to fort.31
                        output_line = "{0:15.4E}{1:15.4E}{2:15.4E}{3:15.4E}\n".format(alam, scc31, abc31, exc31)
                            output_line = "{0:.7E},{1:.3E},{2:.3E},{3:.3E},{4:.3E},{5:.7E},{6:.7E},{7:.7E}\n".format(alam, tidg, tsdg, pidg, psdg, scc31, abc31, exc31)
                            if (out31 is not None): out31.write(output_line)
                            # we know that RAPRS values for polarization state -1
                            # are after 9 more lines
@@ -194,10 +248,11 @@ def parse_legacy_oclu(config):
                                # the following check is needed to parse C++ output
                                if ("INSERTION" in file_line):
                                    file_line = oclu_file.readline()
                        raprs92 = float(file_line[33:46].replace("D", "E"))
                            cosav = float(file_line[8:23].replace("D", "E"))
                            rapr = float(file_line[31:46].replace("D", "E"))
                            # we can write the RAPRS values
                        output_line = "  ALAM={0:15.7E}, RAPRS={1:15.7E}\n".format(alam, raprs92)
                        if (out92 is not None): out92.write(output_line)
                            output_line = "{0:.7E},{1:.3E},{2:.3E},{3:.3E},{4:.3E},{5:.7E},{6:.7E}\n".format(alam, tidg, tsdg, pidg, psdg, cosav, rapr)
                            if (out51 is not None): out51.write(output_line)
                            # we know the differential values for polarization
                            # state 1 are after 9 more lines
                            for i in range(9):
@@ -209,7 +264,7 @@ def parse_legacy_oclu(config):
                            abc32 = float(file_line[17:30].replace("D", "E"))
                            exc32 = float(file_line[32:45].replace("D", "E"))
                            # we can write the differential values, similarly to fort.31
                        output_line = "{0:15.4E}{1:15.4E}{2:15.4E}{3:15.4E}\n".format(alam, scc32, abc32, exc32)
                            output_line = "{0:.7E},{1:.3E},{2:.3E},{3:.3E},{4:.3E},{5:.7E},{6:.7E},{7:.7E}\n".format(alam, tidg, tsdg, pidg, psdg, scc32, abc32, exc32)
                            if (out32 is not None): out32.write(output_line)
                            # we know that RAPRS values for polarization state 1
                            # are after 9 more lines
@@ -218,20 +273,22 @@ def parse_legacy_oclu(config):
                                # the following check is needed to parse C++ output
                                if ("INSERTION" in file_line):
                                    file_line = oclu_file.readline()
                        raprs91 = float(file_line[33:46].replace("D", "E"))
                            cosav = float(file_line[8:23].replace("D", "E"))
                            rapr = float(file_line[31:46].replace("D", "E"))
                            # we can write the RAPRS values
                        output_line = "  ALAM={0:15.7E}, RAPRS={1:15.7E}\n".format(alam, raprs91)
                        if (out91 is not None): out91.write(output_line)
                            output_line = "{0:.7E},{1:.3E},{2:.3E},{3:.3E},{4:.3E},{5:.7E},{6:.7E}\n".format(alam, tidg, tsdg, pidg, psdg, cosav, rapr)
                            if (out52 is not None): out52.write(output_line)
                    found_differentials = True # terminate the inner loop
            # The parsing loop ends here

        if (out22 is not None): out22.close()
        if (out20 is not None): out20.close()
        if (out31 is not None): out31.close()
        if (out32 is not None): out32.close()
        if (out91 is not None): out91.close()
        if (out92 is not None): out92.close()
        if (out40 is not None): out40.close()
        if (out51 is not None): out51.close()
        if (out52 is not None): out52.close()
        oclu_file.close() # close the OCLU file
    except Error as ex:
    except Exception as ex:
        print(ex)
        errors += 1
    return errors
@@ -249,7 +306,12 @@ def print_help():
    print("--in INPUT               File containing the results of the model calculation (mandatory).")
    print("--out OUTPUT             Root name for the output CSV data files (mandatory).             ")
    print("--app=[SPH|CLU|INCLU]    Application whose output needs to be parsed (mandatory).         ")
    print("--format=[HDF5|LEGACY]   Format of the result file to be parsed (autodetected by default).")
    print("--format=HDF5|LEGACY     Format of the result file to be parsed (autodetected by default).")
    print("--selection=[ALL|...]    Select the data to tabulate. Default is ALL. Optional filters can")
    print("                         be provided as a bracketed lists of the following values: ICS (i.")
    print("                         e. Integrated Cross Sections), DCS (i. e. Differential Cross Sec-")
    print("                         tions), IRP (i. e. Integrated Radiation Pressures), or DRP (i. e.")
    print("                         Differential Radiation Pressures)                                ")
    print("--help                   Print this help and exit.                                        ")
    print("                                                                                          ")