예제 #1
0
    def btnSubmitEvent(self):
        selected_var_IDs = self.getSelectedVariables()

        if not selected_var_IDs:
            QMessageBox.critical(
                self, 'Error',
                'Choose at least one output variable before submit!',
                QMessageBox.Ok)
            return

        canceled, filename = save_dialog('CSV')
        if canceled:
            return

        self.csvNameBox.setText(filename)
        logging.info('Writing the output to %s' % filename)
        self.parent.inDialog()

        sampling_frequency = int(self.timeSampling.text())
        selected_time = self.data.time[::sampling_frequency]
        indices_inside = [
            i for i in range(len(self.points))
            if self.point_interpolators[i] is not None
        ]

        # initialize the progress bar
        process = WriteCSVProcess(self.parent.csv_separator,
                                  self.parent.digits, self.mesh)
        progressBar = OutputProgressDialog()

        with Serafin.Read(self.data.filename,
                          self.data.language) as input_stream:
            input_stream.header = self.data.header
            input_stream.time = self.data.time

            progressBar.setValue(1)
            QApplication.processEvents()

            with open(filename, 'w') as output_stream:
                progressBar.connectToThread(process)
                process.write_csv(
                    input_stream, selected_time, selected_var_IDs,
                    output_stream, indices_inside,
                    [self.points[i] for i in indices_inside],
                    [self.point_interpolators[i] for i in indices_inside])
        if not process.canceled:
            progressBar.outputFinished()
        progressBar.exec_()
        self.parent.outDialog()

        if process.canceled:
            self.csvNameBox.clear()
            return

        self.parent.imageTab.getData(selected_var_IDs, indices_inside)
        self.parent.tab.setTabEnabled(1, True)
예제 #2
0
def slf_to_vtk_2d(slf_name, slf_header, vtk_name, scalars, vectors,
                  variable_names, time_index):
    with Serafin.Read(slf_name, slf_header.language) as input_stream:
        input_stream.header = slf_header

        with open(vtk_name, 'w') as output_stream:
            # write header
            header = '# vtk DataFile Version 2.0\nMesh export\nASCII\nDATASET UNSTRUCTURED_GRID\n\n'
            output_stream.write(header)

            # write vertices
            output_stream.write('POINTS %d float\n' % slf_header.nb_nodes)
            for ix, iy in zip(slf_header.x, slf_header.y):
                output_stream.write('%.6f %.6f 0.\n' % (ix, iy))
            output_stream.write('\n')

            # write cells
            output_stream.write(
                'CELLS %d %d\n' %
                (slf_header.nb_elements, slf_header.nb_elements * 4))

            ikle = slf_header.ikle_2d - 1
            for k1, k2, k3 in ikle:
                output_stream.write('3 %d %d %d\n' % (k1, k2, k3))
            output_stream.write('\n')

            output_stream.write('CELL_TYPES %d\n' % slf_header.nb_elements)
            for _ in range(slf_header.nb_elements):
                output_stream.write('5\n')
            output_stream.write('\n')

            # write scalar and vector data
            output_stream.write('POINT_DATA %d\n' % slf_header.nb_nodes)

            for scalar in scalars:
                values = input_stream.read_var_in_frame(time_index, scalar)
                name = variable_names[scalar]

                output_stream.write(
                    'SCALARS %s float\nLOOKUP_TABLE default\n' % name)
                for v in values:
                    output_stream.write('%.6f\n' % v)
                output_stream.write('\n')

            for triple in vectors:
                u_values = input_stream.read_var_in_frame(
                    time_index, triple[0])
                v_values = input_stream.read_var_in_frame(
                    time_index, triple[1])
                name = variable_names[triple]

                output_stream.write('VECTORS %s float\n' % name)
                for u, v in zip(u_values, v_values):
                    output_stream.write('%.6f %.6f 0.\n' % (u, v))
                output_stream.write('\n')
예제 #3
0
    def btnSubmitEvent(self):
        if not self.conditions:
            QMessageBox.critical(self, 'Error', 'Add at least one condition.',
                                 QMessageBox.Ok)
            return

        start_index = int(self.timeSelection.startIndex.text()) - 1
        end_index = int(self.timeSelection.endIndex.text())
        time_indices = list(range(start_index, end_index))

        if len(time_indices) == 1:
            QMessageBox.critical(self, 'Error',
                                 'Start and end frame cannot be the same.',
                                 QMessageBox.Ok)
            return

        canceled, filename = save_dialog('Serafin', self.data.filename)
        if canceled:
            return

        # deduce header from selected variable IDs and write header
        output_header = self.getOutputHeader()
        output_message = 'Computing Arrival / Duration between frame %d and %d.' \
                          % (start_index+1, end_index)
        self.parent.inDialog()
        logging.info(output_message)
        progressBar = OutputProgressDialog()

        # do some calculations
        with Serafin.Read(self.data.filename,
                          self.data.language) as input_stream:
            input_stream.header = self.data.header
            input_stream.time = self.data.time

            progressBar.setValue(5)
            QApplication.processEvents()

            with Serafin.Write(filename, self.data.language) as output_stream:
                process = ArrivalDurationThread(input_stream, self.conditions,
                                                time_indices)
                progressBar.connectToThread(process)
                values = process.run()

                if not process.canceled:
                    values = self._convertTimeUnit(time_indices, values)
                    output_stream.write_header(output_header)
                    output_stream.write_entire_frame(output_header,
                                                     self.data.time[0], values)
                    progressBar.outputFinished()

        progressBar.exec_()
        self.parent.outDialog()
예제 #4
0
    def btnComputeEvent(self):
        ref_time = int(self.timeSelection.refIndex.text()) - 1
        selected_variable = self.input.varBox.currentText().split('(')[0][:-1]

        with Serafin.Read(self.input.ref_data.filename,
                          self.input.ref_data.language) as input_stream:
            input_stream.header = self.input.ref_data.header
            input_stream.time = self.input.ref_data.time
            ref_values = input_stream.read_var_in_frame(
                ref_time, selected_variable)

        mad = []
        with Serafin.Read(self.input.test_data.filename,
                          self.input.test_data.language) as input_stream:
            input_stream.header = self.input.test_data.header
            input_stream.time = self.input.test_data.time
            for i in range(len(self.input.test_data.time)):
                values = input_stream.read_var_in_frame(
                    i, selected_variable) - ref_values
                mad.append(self.input.ref_mesh.mean_absolute_deviation(values))

        self.plotViewer.plot(self.input.test_data.time, mad)
예제 #5
0
    def btnEvolutionEvent(self):
        if not self.has_figure:
            all_bss = []
            ref_time = int(self.timeSelection.refIndex.text()) - 1

            init_time = int(self.initSelection.refIndex.text()) - 1
            selected_variable = self.input.varBox.currentText().split(
                '(')[0][:-1]

            with Serafin.Read(self.input.ref_data.filename,
                              self.input.ref_data.language) as input_stream:
                input_stream.header = self.input.ref_data.header
                input_stream.time = self.input.ref_data.time
                ref_values = input_stream.read_var_in_frame(
                    ref_time, selected_variable)

            with Serafin.Read(self.input.test_data.filename,
                              self.input.test_data.language) as input_stream:
                input_stream.header = self.input.test_data.header
                input_stream.time = self.input.test_data.time
                init_values = input_stream.read_var_in_frame(
                    init_time, selected_variable)
                ref_volume = self.input.ref_mesh.quadratic_volume(ref_values -
                                                                  init_values)

                for index in range(len(self.input.test_data.time)):
                    test_values = input_stream.read_var_in_frame(
                        index, selected_variable)

                    test_volume = self.input.ref_mesh.quadratic_volume(
                        test_values - ref_values)
                    if test_volume == 0 and ref_volume == 0:
                        bss = 1
                    else:
                        with np.errstate(divide='ignore'):
                            bss = 1 - test_volume / ref_volume
                    all_bss.append(bss)
            self.plotViewer.plot(self.input.test_data.time, all_bss)
        self.plotViewer.show()
예제 #6
0
    def btnSubmitEvent(self):
        # fetch the list of selected variables
        selected_vars = self._getSelectedVariables()
        if not selected_vars:
            QMessageBox.critical(self, 'Error',
                                 'Select at least one variable.',
                                 QMessageBox.Ok)
            return

        canceled, filename = save_dialog('Serafin', self.data.filename)
        if canceled:
            return

        # deduce header from selected variable IDs and write header
        output_header = self.getOutputHeader(selected_vars)

        start_index = int(self.timeSelection.startIndex.text()) - 1
        end_index = int(self.timeSelection.endIndex.text())
        time_indices = list(range(start_index, end_index))
        var = self.varBox.currentText().split(' (')[0]

        output_message = 'Computing SynchMax of variables %s between frame %d and %d.' \
                          % (str(list(map(lambda x: x[0], selected_vars[1:]))), start_index+1, end_index)
        self.parent.inDialog()
        logging.info(output_message)
        progressBar = OutputProgressDialog()

        # do some calculations
        with Serafin.Read(self.data.filename,
                          self.data.language) as input_stream:
            input_stream.header = self.data.header
            input_stream.time = self.data.time

            progressBar.setValue(5)
            QApplication.processEvents()

            with Serafin.Write(filename, self.data.language) as output_stream:
                process = SynchMaxThread(input_stream, selected_vars[1:],
                                         time_indices, var)
                progressBar.connectToThread(process)
                values = process.run()

                if not process.canceled:
                    output_stream.write_header(output_header)
                    output_stream.write_entire_frame(output_header,
                                                     self.data.time[0], values)
                    progressBar.outputFinished()

        progressBar.exec_()
        self.parent.outDialog()
예제 #7
0
    def _run_synch_max(self, input_data):
        """!
        @brief Write Serafin with `SynchMax` operator
        @param input_data <slf.datatypes.SerafinData>: input SerafinData stream
        """
        selected_vars = [
            var for var in input_data.selected_vars
            if var in input_data.header.var_IDs
        ]
        output_header = input_data.header.copy()
        output_header.nb_var = len(selected_vars)
        output_header.var_IDs, output_header.var_names, output_header.var_units = [], [], []
        for var_ID in selected_vars:
            var_name, var_unit = input_data.selected_vars_names[var_ID]
            output_header.var_IDs.append(var_ID)
            output_header.var_names.append(var_name)
            output_header.var_units.append(var_unit)
        if input_data.to_single:
            output_header.to_single_precision()

        nb_frames = len(input_data.selected_time_indicies)
        with Serafin.Read(input_data.filename,
                          input_data.language) as input_stream:
            input_stream.header = input_data.header
            input_stream.time = input_data.time

            calculator = operations.SynchMaxCalculator(
                input_stream, selected_vars, input_data.selected_time_indicies,
                input_data.metadata['var'])

            for i, time_index in enumerate(
                    input_data.selected_time_indicies[1:]):
                calculator.synch_max_in_frame(time_index)

                self.progress_bar.setValue(100 * (i + 1) / nb_frames)
                QApplication.processEvents()

            values = calculator.finishing_up()
            with Serafin.Write(self.filename,
                               input_data.language) as output_stream:
                output_stream.write_header(output_header)
                output_stream.write_entire_frame(output_header,
                                                 input_data.time[0], values)
        self.success('Output saved to {}.'.format(self.filename))
        return True
예제 #8
0
def slf_to_xml(slf_name, slf_header, xml_name, scalar, time_index):
    """!
    @brief Write LandXML file from a scalar variable of a Serafin file
    @param slf_name <str>: path to the input Serafin file
    @param slf_header <slf.Serafin.SerafinHeader>: input Serafin header
    @param xml_name <str>: output LandXML filename
    @param scalar <str>: variable to write
    @param time_index <int>: the index of the frame (0-based)
    """
    # fetch scalar variable values
    with Serafin.Read(slf_name, slf_header.language) as input_stream:
        input_stream.header = slf_header
        scalar_values = input_stream.read_var_in_frame(time_index, scalar)

    # write LandXML
    with open(xml_name, 'w') as xml:
        xml.write('<?xml version="1.0" ?>\n')
        xml.write('<!-- Title: %s -->\n' %
                  slf_header.title.decode('utf-8').strip())
        xml.write(
            '<!-- This file contains x and y reversed in purpose (because arcpy is buggy) -->\n'
        )
        xml.write(
            '<LandXML version="1.2" xmlns="http://www.landxml.org/schema/LandXML-1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.landxml.org/schema/LandXML-1.2 http://www.landxml.org/schema/LandXML-1.2/LandXML-1.2.xsd">\n'
        )
        xml.write('  <Surfaces>\n')
        xml.write('    <Surface name="%s at frame %i/%i">\n' %
                  (scalar, time_index + 1, slf_header.nb_frames))
        xml.write('      <Definition surfType="TIN">\n')
        xml.write('        <Pnts>\n')
        for i, (x, y,
                z) in enumerate(zip(slf_header.x, slf_header.y,
                                    scalar_values)):
            xml.write('          <P id="%d">%.4f %.4f %.4f</P>\n' %
                      (i + 1, y, x, z))
        xml.write('        </Pnts>\n')
        xml.write('        <Faces>\n')
        for i, (a, b, c) in enumerate(slf_header.ikle_2d):
            xml.write('          <F id="%d">%d %d %d</F>\n' % (i + 1, a, b, c))
        xml.write('        </Faces>\n')
        xml.write('      </Definition>\n')
        xml.write('    </Surface>\n')
        xml.write('  </Surfaces>\n')
        xml.write('</LandXML>\n')
예제 #9
0
    def btnSubmitEvent(self):
        canceled, filename = save_dialog('CSV')
        if canceled:
            return
        self.csvNameBox.setText(filename)

        sampling_frequency = int(self.timeSampling.text())
        flux_type, self.var_IDs, flux_title = self._getFluxSection()
        self.parent.tab.setTabEnabled(1, False)

        logging.info('Writing the output to %s' % filename)
        self.parent.inDialog()

        # initialize the progress bar
        progressBar = OutputProgressDialog()

        # do the calculations
        names = ['Section %d' % (i+1) for i in range(len(self.polylines))]

        with Serafin.Read(self.data.filename, self.data.language) as input_stream:
            input_stream.header = self.data.header
            input_stream.time = self.data.time
            calculator = FluxCalculatorThread(flux_type, self.var_IDs,
                                              input_stream, names, self.polylines, sampling_frequency,
                                              self.mesh, self.parent.csv_separator, self.parent.digits)
            progressBar.setValue(5)
            QApplication.processEvents()

            with open(filename, 'w') as f2:
                progressBar.connectToThread(calculator)
                calculator.write_csv(f2)

        if not calculator.canceled:
            progressBar.outputFinished()
        progressBar.exec_()
        self.parent.outDialog()

        if calculator.canceled:
            self.csvNameBox.clear()
            return

        # unlock the image viewer
        self.parent.imageTab.getData(flux_title)
        self.parent.tab.setTabEnabled(1, True)
예제 #10
0
    def run_single(self, input_name, input_header, output_name, output_header, pool):
        i = 0
        with Serafin.Read(input_name, input_header.language) as input_stream:
            input_stream.header = input_header
            input_stream.get_time()
            inv_nb_frames = len(input_stream.time)

            with Serafin.Write(output_name, input_header.language) as output_stream:
                output_stream.write_header(output_header)

                for time_value, value_array in pool.evaluate_expressions(self.augmented_path,
                                                                         input_stream, self.selected_expressions):
                    if self.canceled:
                        return
                    i += 1
                    output_stream.write_entire_frame(output_header, time_value, value_array)

                    self.tick.emit(100 * i * self.inv_nb_files * inv_nb_frames)
                    QApplication.processEvents()
def getSelaFrame(inname, outname, overwrite, posList, timeList):
    """
    Export frames from a Serafin file with a 0-based indexing list.
    A negative position accesses elements from the end of the list counting backwards (-1 returns the last
    frame)
    """
    # Check unicity of method
    if posList and timeList:
        sys.exit("Precise either posList or timeList")
    if not posList and not timeList:
        sys.exit("Precise a posList or a timeList")
        # posList = range(len(resin.time))

    with Serafin.Read(inname) as resin:
        resin.readHeader()
        resin.get_time()

        if timeList:
            posList = []
            for time in timeList:
                posList.append(
                    min(range(len(resin.time)),
                        key=lambda i: abs(resin.time[i] - time)))
            print("Position(s) found = {}".format(posList))

        with Serafin.Write(outname, overwrite) as resout:
            resout.copy_header(resin)
            resout.write_header()

            for pos in posList:
                try:
                    time = resin.time[pos]
                except IndexError:
                    sys.exit(
                        "ERROR: position n°{} is not in range [{},{}] (or in opposite interval)"
                        .format(pos, 1, len(resin.time)))
                common_data.log("Write frame number {} (time = {})".format(
                    pos, time))
                var = resin.read_entire_frame(time)
                resout.write_entire_frame(time, var)
예제 #12
0
 def _run_layer_selection(self, input_data):
     """!
     @brief Write Serafin with `Select Single Layer` operator
     @param input_data <slf.datatypes.SerafinData>: input SerafinData stream
     """
     output_header = input_data.build_2d_output_header()
     with Serafin.Read(input_data.filename,
                       input_data.language) as input_stream:
         input_stream.header = input_data.header
         input_stream.time = input_data.time
         with Serafin.Write(self.filename,
                            input_data.language) as output_stream:
             output_stream.write_header(output_header)
             for i, time_index in enumerate(
                     input_data.selected_time_indices):
                 # FIXME Optimization: Do calculations only on target layer and avoid reshaping afterwards
                 values = do_calculations_in_frame(
                     input_data.equations,
                     input_stream,
                     time_index,
                     input_data.selected_vars,
                     output_header.np_float_type,
                     is_2d=output_header.is_2d,
                     us_equation=input_data.us_equation)
                 new_shape = (values.shape[0],
                              input_stream.header.nb_planes,
                              values.shape[1] //
                              input_stream.header.nb_planes)
                 values_at_layer = values.reshape(
                     new_shape)[:, input_data.metadata['layer_selection'] -
                                1, :]
                 output_stream.write_entire_frame(
                     output_header, input_data.time[time_index],
                     values_at_layer)
                 self.progress_bar.setValue(
                     100 * (i + 1) / len(input_data.selected_time_indices))
                 QApplication.processEvents()
     self.success('Output saved to {}.'.format(self.filename))
     return True
예제 #13
0
def slf_to_shp(slf_name, slf_header, shp_name, variables, time_index):
    # separate vectors from scalars
    coupled, non_coupled, mothers, angles = detect_vector_couples(
        variables, slf_header.var_IDs)

    # fetch all variables values
    values = {}
    with Serafin.Read(slf_name, slf_header.language) as input_stream:
        input_stream.header = slf_header
        for var in variables:
            values[var] = input_stream.read_var_in_frame(time_index, var)

    # compute mothers not in the file
    for mother, brother, sister in mothers:
        values[mother] = np.sqrt(
            np.square(values[brother]) + np.square(values[sister]))

    # compute angles
    for brother, sister in angles:
        values['Angle(%s,%s)' % (brother, sister)] = np.degrees(
            np.arctan2(values[sister], values[brother]))

    # write shp
    key_order = coupled + non_coupled + [mother for mother, _, _ in mothers] \
                        + ['Angle(%s,%s)' % (brother, sister) for brother, sister in angles]

    w = shapefile.Writer(shapefile.POINT)
    for name in key_order:
        w.field(name, 'N', decimal=4)

    for i, (x, y) in enumerate(zip(slf_header.x, slf_header.y)):
        w.point(x, y, shapeType=shapefile.POINT)

        val = []
        for var in key_order:
            val.append(values[var][i])
        w.record(*val)
    w.save(shp_name)
예제 #14
0
from slf import Serafin, common_data

parser = myargparse(description=__doc__, add_args=['force', 'verbose'])
parser.add_argument("inname1", help="reference Serafin input filename")
parser.add_argument("inname2", help="Serafin input filename to subtract")
parser.add_argument("outname", help="Serafin output filename")
parser.add_argument(
    "--velMag",
    help=
    "compute velocity 2D (or 3D) difference instead of a simple difference for U, V (and W)",
    action="store_true")
args = parser.parse_args()

common_data.verbose = args.verbose

with Serafin.Read(args.inname1) as res1, Serafin.Read(args.inname2) as res2:
    res1.readHeader()
    res2.readHeader()

    res1.get_time()
    res2.get_time()

    # Check coherence between input files
    if not res1.sameMesh(res2):
        sys.exit("mesh {} and {} are different".format(inname1, inname2))

    with Serafin.Write(args.outname, args.force) as resout:
        resout.copy_header(res1)

        # Find common variables
        varID_list = res1.commonVarID(res2)
예제 #15
0
    def btnSubmitEvent(self):
        # fetch the list of selected variables
        selected_vars = self._getSelectedVariables()
        if not selected_vars:
            QMessageBox.critical(self, 'Error',
                                 'Select at least one variable.',
                                 QMessageBox.Ok)
            return

        canceled, filename = save_dialog('Serafin', self.data.filename)
        if canceled:
            return

        # separate scalars and vectors
        scalars, vectors, additional_equations = operations.scalars_vectors(
            self.data.header.var_IDs, selected_vars)

        # get the operation type
        if self.maxButton.isChecked():
            max_min_type = operations.MAX
        elif self.minButton.isChecked():
            max_min_type = operations.MIN
        else:
            max_min_type = operations.MEAN

        # deduce header from selected variable IDs and write header
        output_header = self.getOutputHeader(scalars, vectors)

        start_index = int(self.timeSelection.startIndex.text()) - 1
        end_index = int(self.timeSelection.endIndex.text())
        time_indices = list(range(start_index, end_index))

        output_message = 'Computing %s of variables %s between frame %d and %d.' \
                          % ('Max' if self.maxButton.isChecked() else ('Min' if self.minButton.isChecked() else 'Mean'),
                             str(output_header.var_IDs), start_index+1, end_index)
        self.parent.inDialog()
        logging.info(output_message)
        progressBar = OutputProgressDialog()

        # do some calculations
        with Serafin.Read(self.data.filename,
                          self.data.language) as input_stream:
            input_stream.header = self.data.header
            input_stream.time = self.data.time

            progressBar.setValue(5)
            QApplication.processEvents()

            with Serafin.Write(filename, self.data.language) as output_stream:
                process = MaxMinMeanThread(max_min_type, input_stream, scalars,
                                           vectors, time_indices,
                                           additional_equations)
                progressBar.connectToThread(process)
                values = process.run()

                if not process.canceled:
                    output_stream.write_header(output_header)
                    output_stream.write_entire_frame(output_header,
                                                     self.data.time[0], values)
                    progressBar.outputFinished()

        progressBar.exec_()
        self.parent.outDialog()
def int2d(resname,
          i2s_inname,
          outname=None,
          outname_short=None,
          overwrite=False,
          sep=',',
          digits=4):
    # global poly, final, res, df_all_points, df_points, values, df_poly, ipoly, row_values, df_append, S, Hmax, df_append_wet, Hmoy, values_short, Zmoy

    # df_poly: ['dx', 'dy'] (index = id_poly)
    # df_points: ['id_poly', 'id_pt', 'x', 'y', 'dx', 'dist']
    # df_all_points: list de df_points
    varID_list = ['N', 'P']

    with Serafin.Read(resname) as res:
        res.readHeader()
        res.get_time()

        # ~> Compute mesh triangulation and sample segments
        res.compute_element_barycenters()
        res.compute_triangulation()

        # Build final and points
        final = []
        df_poly = pd.DataFrame()
        df_all_points = []

        # Read input polylines file
        with BlueKenueRead_i2s(i2s_inname) as in_i2s:
            in_i2s.read_header()

            with BlueKenueWrite_i2s('check.i2s',
                                    True) as out_i2s:  #FIXME: AVOID check.i2s
                # Prepare output i2s file
                out_i2s.copy_header(in_i2s)
                out_i2s.auto_keywords()
                out_i2s.write_header()

                # Loop on input polylines
                for ipoly, (value,
                            poly) in enumerate(in_i2s.iter_on_polylines()):
                    print("Lecture polyligne {} de valeur {}".format(
                        ipoly, value))
                    [(xa, ya), (xb, yb)] = list(poly.coords)
                    long = math.sqrt((xb - xa)**2 + (yb - ya)**2)
                    df_poly = df_poly.append(pd.DataFrame({
                        'dx': [(xb - xa) / long],
                        'dy': [(yb - ya) / long],
                        'value': [value]
                    }),
                                             ignore_index=True)
                    final.append([])
                    polyline = []
                    df_points = pd.DataFrame(
                        columns=['id_poly', 'id_pt', 'x', 'y'])
                    for i, ((x, y), ponderation) in enumerate(
                            res.iter_intersect_segment(xa, ya, xb, yb)):
                        final[ipoly].append(((x, y), ponderation))
                        df_points.loc[i] = [ipoly, i, x, y]
                        polyline.append((x, y))

                    # Compute cumulative distance (of the current polyline)
                    df_points['dx'] = np.sqrt(
                        np.power(
                            np.roll(df_points['x'], 1) - df_points['x'], 2) +
                        np.power(
                            np.roll(df_points['y'], 1) - df_points['y'], 2))
                    df_points['dx'].iloc[0] = 0.0
                    df_points['dist'] = df_points['dx'].cumsum(
                    )  # cumulative sum
                    df_all_points.append(df_points)

                    # Write output poyline (with added points)
                    out_i2s.write_polyline(LineString(polyline), value)

        mode = 'w' if overwrite else 'x'
        with open(outname_short, mode, newline='') as csvfile:
            fieldnames = ['time']
            for x in range(ipoly + 1):
                fieldnames.append(str(x))
            csvwriter = csv.writer(csvfile, delimiter=',')
            csvwriter.writerow(fieldnames)

            # ~> Read and interpolate results
            varID2write = copy.copy(varID_list)
            varID2write.append('Un')

            first = True
            for time in res.time:
                if True:  #DEBUG time < 5*24*3600:
                    print("Temps {}".format(time))
                    var = res.read_vars_in_frame(time, varID_list)

                    # Initialiaze values
                    values = pd.DataFrame()

                    # Initialiaze values_short
                    values_short = df_poly.copy()

                    values_Q = [str(time)]

                    for ipoly, ponderations in enumerate(final):
                        # ~> Interpolate
                        df_append = df_all_points[ipoly].copy()
                        # Add columns
                        #df_append['time'] = time
                        for varID in varID2write:
                            df_append[varID] = np.nan
                        for i, ((x, y),
                                ponderation) in enumerate(ponderations):
                            row_values = np.zeros(len(varID_list))
                            for node, coeff in ponderation.items():
                                row_values = row_values + coeff * var[:,
                                                                      node - 1]
                            df_append.loc[i, varID_list] = row_values

                        # Deduce Un (dot product)
                        df_append['Un'] = (
                            df_append['N'] *
                            df_poly.loc[df_append['id_poly'],
                                        'dx'].reset_index(drop=True) -
                            df_append['P'] *
                            df_poly.loc[df_append['id_poly'],
                                        'dy'].reset_index(drop=True))
                        df_append['Un'] = -1 * df_append[
                            'Un']  # FIXME: bidouille debit positif
                        values = values.append(df_append)

                        # Compute discharge
                        df_append['integ'] = (3 * df_append['Un'] +
                                              3 * np.roll(df_append['Un'], 1))
                        Q = (df_append['integ'] * df_append['dx'] / 6).sum()

                        values_Q.append(round(Q, digits))

                    # Export in CSV file
                    if first:
                        mode = 'w'
                        header = True
                        first = False
                    else:
                        mode = 'a'
                        header = False

                    del values_short['dx']
                    del values_short['dy']

                    csvwriter.writerow(values_Q)
    args.preffixname = "r2d_extraction"
    args.var = ['H', 'U', 'V', 'M']  # ['H']
    args.time = [0]
    args.i2s_name = None  #"zone-titi.i2s"
    args.shift = [976000, 6550000]
    args.verbose = True

RHO_EAU = 1000.  # kg/m3
add_angle = True

common_data.verbose = args.verbose
# cur_path = os.getcwd()
# (export_folder, export_preffix) = os.path.split(args.preffixname)
export_preffix = args.preffixname

with Serafin.Read(args.slf_name) as resin:
    resin.readHeader()

    if resin.type != '2D':
        sys.exit("The current script is working only with 2D meshes !")

    if args.i2s_name:
        print("Lecture du fichier {}".format(args.i2s_name))
        with BlueKenueRead_i2s(args.i2s_name) as in_i2s:
            in_i2s.read_header()
            for i, (value, polyline) in enumerate(in_i2s.iter_on_polylines()):
                # Gestion des erreurs
                if i != 0:
                    raise NotImplementedError(
                        "Une seule polyligne est attendue")
                if not polyline.is_valid:
예제 #18
0
class Mask:
    """
    Mask: definir une zone pour laquelle, les noeuds inclus dedans sont traités différement

    Attributs:
    * value = valeur
    TODO
    """

    def __init__(self, value, geom, nodes_included):
        self.value = value
        self.geom = geom
        self.nodes_included = nodes_included


with Serafin.Read(args.inname) as resin:
    resin.readHeader()

    if resin.type != '2D':
        sys.exit("The current script is working only with 2D meshes !")

    resin.get_time()

    # Define zones from polylines
    print("~> Lecture des polylignes et recherche des noeuds inclus dedans")
    masks = []
    with BlueKenueRead_i2s(args.i2s_name) as in_i2s:
        in_i2s.read_header()
        for i, (value, polyline) in enumerate(in_i2s.iter_on_polylines()):
            if not polyline.is_valid:
                sys.exit("ERROR: polyline {} is not valid (probably because it intersects itself) !".format(i))
예제 #19
0
    def _run_max_min_mean(self, input_data):
        """!
        @brief Write Serafin with `Temporal Min/Max/Mean` operator
        @param input_data <slf.datatypes.SerafinData>: input SerafinData stream
        """
        selected = [(var, input_data.selected_vars_names[var][0],
                     input_data.selected_vars_names[var][1])
                    for var in input_data.selected_vars]
        scalars, vectors, additional_equations = operations.scalars_vectors(
            input_data.header.var_IDs, selected, input_data.us_equation)
        output_header = input_data.header.copy()
        output_header.nb_var = len(scalars) + len(vectors)
        output_header.var_IDs, output_header.var_names, output_header.var_units = [], [], []
        for var_ID, var_name, var_unit in scalars + vectors:
            output_header.var_IDs.append(var_ID)
            output_header.var_names.append(var_name)
            output_header.var_units.append(var_unit)
        if input_data.to_single:
            output_header.to_single_precision()

        with Serafin.Read(input_data.filename,
                          input_data.language) as input_stream:
            input_stream.header = input_data.header
            input_stream.time = input_data.time
            has_scalar, has_vector = False, False
            scalar_calculator, vector_calculator = None, None
            if scalars:
                has_scalar = True
                scalar_calculator = operations.ScalarMaxMinMeanCalculator(
                    input_data.operator, input_stream, scalars,
                    input_data.selected_time_indices, additional_equations)
            if vectors:
                has_vector = True
                vector_calculator = operations.VectorMaxMinMeanCalculator(
                    input_data.operator, input_stream, vectors,
                    input_data.selected_time_indices, additional_equations)
            for i, time_index in enumerate(input_data.selected_time_indices):

                if has_scalar:
                    scalar_calculator.max_min_mean_in_frame(time_index)
                if has_vector:
                    vector_calculator.max_min_mean_in_frame(time_index)

                self.progress_bar.setValue(
                    100 * (i + 1) / len(input_data.selected_time_indices))
                QApplication.processEvents()

            if has_scalar and not has_vector:
                values = scalar_calculator.finishing_up()
            elif not has_scalar and has_vector:
                values = vector_calculator.finishing_up()
            else:
                values = np.vstack((scalar_calculator.finishing_up(),
                                    vector_calculator.finishing_up()))

            with Serafin.Write(self.filename,
                               input_data.language) as output_stream:
                output_stream.write_header(output_header)
                output_stream.write_entire_frame(output_header,
                                                 input_data.time[0], values)
        self.success('Output saved to {}.'.format(self.filename))
        return True
예제 #20
0
    def _run_arrival_duration(self, input_data):
        """!
        @brief Write Serafin with `Compute Arrival Duration` operator
        @param input_data <slf.datatypes.SerafinData>: input SerafinData stream
        """
        conditions, table, time_unit = input_data.metadata['conditions'], \
                                       input_data.metadata['table'], input_data.metadata['time unit']

        output_header = input_data.header.copy()
        output_header.nb_var = 2 * len(conditions)
        output_header.var_IDs, output_header.var_names, output_header.var_units = [], [], []
        for row in range(len(table)):
            a_name = table[row][1]
            d_name = table[row][2]
            for name in [a_name, d_name]:
                output_header.var_IDs.append('')
                output_header.var_names.append(bytes(name, 'utf-8').ljust(16))
                output_header.var_units.append(
                    bytes(time_unit.upper(), 'utf-8').ljust(16))
        if input_data.to_single:
            output_header.to_single_precision()

        with Serafin.Read(input_data.filename,
                          input_data.language) as input_stream:
            input_stream.header = input_data.header
            input_stream.time = input_data.time
            calculators = []

            for i, condition in enumerate(conditions):
                calculators.append(
                    operations.ArrivalDurationCalculator(
                        input_stream, input_data.selected_time_indices,
                        condition))
            for i, index in enumerate(input_data.selected_time_indices[1:]):
                for calculator in calculators:
                    calculator.arrival_duration_in_frame(index)

                self.progress_bar.setValue(
                    100 * (i + 1) / len(input_data.selected_time_indices))
                QApplication.processEvents()

            values = np.empty(
                (2 * len(conditions), input_data.header.nb_nodes))
            for i, calculator in enumerate(calculators):
                values[2 * i, :] = calculator.arrival
                values[2 * i + 1, :] = calculator.duration

            if time_unit == 'minute':
                values /= 60
            elif time_unit == 'hour':
                values /= 3600
            elif time_unit == 'day':
                values /= 86400
            elif time_unit == 'percentage':
                values *= 100 / (
                    input_data.time[input_data.selected_time_indices[-1]] -
                    input_data.time[input_data.selected_time_indices[0]])

            with Serafin.Write(self.filename,
                               input_data.language) as output_stream:
                output_stream.write_header(output_header)
                output_stream.write_entire_frame(output_header,
                                                 input_data.time[0], values)
        self.success('Output saved to {}.'.format(self.filename))
        return True
예제 #21
0
def int2d(resname,
          xyzname,
          varID_list,
          outCSV,
          overwrite=False,
          sep=',',
          digits=4):
    with Serafin.Read(resname) as res:
        res.readHeader()
        res.get_time()

        # Read points coord and id from CSV file
        points = pd.read_csv(xyzname, sep=sep, header=0, index_col=0)
        common_data.log("{} points found in {}".format(len(points.index),
                                                       xyzname))

        # Compute ponderation for each target points
        res.compute_element_barycenters()
        ponderation = {}
        for ptID, coord in points.iterrows():
            common_data.log("Search position of point {}".format(ptID))
            ponderation[ptID] = res.ponderation_in_element(
                coord['x'], coord['y'])
        common_data.log("Ponderation coefficients: {}".format(ponderation))

        # Read results
        first = True
        for time in res.time:
            var = res.read_vars_in_frame(time, varID_list)
            df_var = pd.DataFrame(index=None, columns=None, dtype='float64')

            for ptID in points.index:
                result = np.zeros((res.nplan, len(varID_list)))
                for node, coeff in ponderation[ptID].items():
                    nodes = [(node - 1) + x * res.nnode2d
                             for x in range(res.nplan)]  # 3D nodes -1
                    result = result + coeff * (var[:, nodes].transpose())
                df_append = pd.DataFrame(result,
                                         columns=varID_list,
                                         dtype='float64')

                # Add columns
                df_append['time'] = str(time)
                df_append['iplan'] = list(
                    [str(x + 1) for x in df_append.index]
                )  # Bricolage pour retrouver le numero du plan et passer en str pour eviter le float
                df_append['id'] = ptID
                df_append['x'] = list(
                    str(x) for x in points['x'][
                        df_append['id']])  # list pour effacer index
                df_append['y'] = list(
                    str(x) for x in points['y'][
                        df_append['id']])  # list pour effacer index
                df_var = df_var.append(df_append)

            if first:
                mode = 'w' if overwrite else 'x'
                df_var.to_csv(outCSV,
                              mode=mode,
                              sep=sep,
                              index=False,
                              float_format='%1.' + str(digits - 1) + 'e')
                first = False
            else:
                df_var.to_csv(outCSV,
                              mode='a',
                              sep=sep,
                              index=False,
                              float_format='%1.' + str(digits - 1) + 'e',
                              header=None)
예제 #22
0
    def _run_project_mesh(self, first_input):
        """!
        @brief Write Serafin with `Projet Mesh` operator
        @param input_data <slf.datatypes.SerafinData>: input SerafinData stream
        """
        operation_type = first_input.operator
        second_input = first_input.metadata['operand']

        if second_input.filename == self.filename:
            self.fail('cannot overwrite to the input file.')
            return

        # common vars
        first_vars = [
            var for var in first_input.header.var_IDs
            if var in first_input.selected_vars
        ]
        second_vars = [
            var for var in second_input.header.var_IDs
            if var in second_input.selected_vars
        ]
        common_vars = []
        for var in first_vars:
            if var in second_vars:
                common_vars.append(var)
        if not common_vars:
            self.fail('the two input files do not share common variables.')
            return False

        # common frames
        first_frames = [
            first_input.start_time + first_input.time_second[i]
            for i in first_input.selected_time_indices
        ]
        second_frames = [
            second_input.start_time + second_input.time_second[i]
            for i in second_input.selected_time_indices
        ]
        common_frames = []
        for first_index, first_frame in zip(first_input.selected_time_indices,
                                            first_frames):
            for second_index, second_frame in zip(
                    second_input.selected_time_indices, second_frames):
                if first_frame == second_frame:
                    common_frames.append((first_index, second_index))
        if not common_frames:
            self.fail('the two input files do not share common time frames.')
            return False

        # construct output header
        output_header = first_input.header.copy()
        output_header.nb_var = len(common_vars)
        output_header.var_IDs, output_header.var_names, output_header.var_units = [], [], []
        for var in common_vars:
            name, unit = first_input.selected_vars_names[var]
            output_header.var_IDs.append(var)
            output_header.var_names.append(name)
            output_header.var_units.append(unit)
        if first_input.to_single:
            output_header.to_single_precision()

        # map points of A onto mesh B
        mesh = MeshInterpolator(second_input.header, False)

        if second_input.triangles:
            mesh.index = second_input.index
            mesh.triangles = second_input.triangles
        else:
            self.construct_mesh(mesh)
            second_input.index = mesh.index
            second_input.triangles = mesh.triangles

        is_inside, point_interpolators = mesh.get_point_interpolators(
            list(zip(first_input.header.x, first_input.header.y)))

        # run the calculator
        with Serafin.Read(first_input.filename,
                          first_input.language) as first_in:
            first_in.header = first_input.header
            first_in.time = first_input.time

            with Serafin.Read(second_input.filename,
                              second_input.language) as second_in:
                second_in.header = second_input.header
                second_in.time = second_input.time

                calculator = operations.ProjectMeshCalculator(
                    first_in, second_in, common_vars, is_inside,
                    point_interpolators, common_frames, operation_type)

                with Serafin.Write(self.filename,
                                   first_input.language) as out_stream:
                    out_stream.write_header(output_header)

                    for i, (first_time_index, second_time_index) in enumerate(
                            calculator.time_indices):
                        values = calculator.operation_in_frame(
                            first_time_index, second_time_index)
                        out_stream.write_entire_frame(
                            output_header,
                            calculator.first_in.time[first_time_index], values)

                        self.progress_bar.setValue(100 * (i + 1) /
                                                   len(common_frames))
                        QApplication.processEvents()

        self.success(
            'Output saved to {}.\nThe two files has {} common variables and {} common frames.\n'
            'The mesh A has {} / {} nodes inside the mesh B.'.format(
                self.filename, len(common_vars), len(common_frames),
                sum(is_inside), first_input.header.nb_nodes))
        return True
예제 #23
0
    def btnSubmitEvent(self):
        if self.secondTable.rowCount() == 0:
            QMessageBox.critical(
                self, 'Error',
                'Choose at least one output variable before submit!',
                QMessageBox.Ok)
            return

        canceled, filename = save_dialog('Serafin',
                                         input_names=[
                                             self.input.first_data.filename,
                                             self.input.second_data.filename
                                         ])
        if canceled:
            return

        # deduce header from selected variable IDs
        output_header = self._getOutputHeader()
        time_indices = self.input.common_frames
        operation_type = {
            0: operations.PROJECT,
            1: operations.DIFF,
            2: operations.REV_DIFF,
            3: operations.MAX_BETWEEN,
            4: operations.MIN_BETWEEN
        }[self.operationBox.currentIndex()]
        self.parent.inDialog()
        progressBar = OutputProgressDialog()

        # do some calculations
        with Serafin.Read(self.input.first_data.filename,
                          self.input.first_data.language) as first_in:
            first_in.header = self.input.first_data.header
            first_in.time = self.input.first_data.time

            with Serafin.Read(self.input.second_data.filename,
                              self.input.second_data.language) as second_in:
                second_in.header = self.input.second_data.header
                second_in.time = self.input.second_data.time

                progressBar.setValue(5)
                QApplication.processEvents()

                with Serafin.Write(
                        filename,
                        self.input.first_data.language) as out_stream:

                    out_stream.write_header(output_header)
                    process = ProjectMeshThread(first_in, second_in,
                                                out_stream, output_header,
                                                self.input.is_inside,
                                                self.input.point_interpolators,
                                                time_indices, operation_type)
                    progressBar.connectToThread(process)
                    process.run()

                    if not process.canceled:
                        progressBar.outputFinished()

        progressBar.exec_()
        self.parent.outDialog()
#!/usr/bin/python3
"""
@brief:
Vérifier que les extrêmes des variables d'unu résultat 2D
"""

import numpy as np
import os

from common.arg_command_line import myargparse
from slf import common_data, Serafin

parser = myargparse(description=__doc__, add_args=['verbose'])
parser.add_argument("resname", help="Serafin input filename")
args = parser.parse_args()

common_data.verbose = args.verbose

with open('arf.csv', 'w') as fileout:
    with Serafin.Read(args.resname) as res:
        res.readHeader()
        res.get_time()

        fileout.write(';'.join(['time', 'H_min', 'Vmax']) + '\n')
        for time in res.time:
            H = res.read_var_in_frame(time, 'H')
            M = res.read_var_in_frame(time, 'M')
            fileout.write(';'.join(
                str(x) for x in [time, H.min(), M.max()]) + '\n')
            fileout.flush()
예제 #25
0
args.time = [0]
args.verbose = True
args.sep = ";"


common_data.verbose = args.verbose

nodes = [1, 10, 100]  # liste de noeuds (numerotation à partir de 1 comme BlueKenue)

with open(args.outcsv, 'w', newline='') as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=args.sep)
    csvwriter.writerow(['file', 'time'] + nodes)

    for slf_file in args.slf_list:
        print(">>>>> FICHIER RESULTAT T2D : {} <<<<<".format(slf_file))
        with Serafin.Read(slf_file) as resin:
            resin.readHeader()

            if resin.type != '2D':
                sys.exit("The current script is working only with 2D meshes !")

            resin.get_time()

            for time in args.time:
                try:
                    print(resin.time.index(time))
                except ValueError:
                    print("/!\ Le temps {} n'est pas dans le fichier.".format(time))
                    sys.exit("Temps possibles : {}".format(resin.time))

                data = resin.read_var_in_frame(time, args.var)[[n+1 for n in nodes]]