Exemplo n.º 1
0
    def _run_simple(self, input_data):
        """!
        @brief Write Serafin without any operator
        @param input_data <slf.datatypes.SerafinData>: input SerafinData stream
        """
        output_header = input_data.default_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):
                    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)
                    output_stream.write_entire_frame(
                        output_header, input_data.time[time_index], values)

                    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
Exemplo n.º 2
0
    def btnSubmitEvent(self):
        canceled, filename = save_dialog('Serafin', self.data.filename)
        if canceled:
            return

        # fetch the list of selected variables
        selected_vars = self.inputTab.getSelectedVariables()

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

        # fetch the list of selected frames
        if self.timeSelection.manualSelection.hasData:
            output_time_indices = self.timeSelection.getManualTime()
            output_message = 'Writing the output with variables %s for %d frame%s between frame %d and %d.' \
                             % (str(output_header.var_IDs), len(output_time_indices),
                             ['', 's'][len(output_time_indices) > 1], output_time_indices[0]+1, output_time_indices[-1]+1)
        else:
            start_index, end_index, sampling_frequency, output_time_indices = self.timeSelection.getTime(
            )
            output_message = 'Writing the output with variables %s between frame %d and %d with sampling frequency %d.' \
                             % (str(output_header.var_IDs), start_index, end_index, sampling_frequency)

        self.parent.inDialog()
        progressBar = OutputProgressDialog()

        # do some calculations
        with Serafin.Read(self.data.filename,
                          self.data.language) as input_stream:
            # instead of re-reading the header and the time, just do a copy
            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:
                logging.info(output_message)

                output_stream.write_header(output_header)

                # do some additional computations
                necessary_equations = get_necessary_equations(
                    self.data.header.var_IDs,
                    output_header.var_IDs,
                    is_2d=output_header.is_2d,
                    us_equation=self.data.us_equation)
                process = ExtractVariablesThread(necessary_equations,
                                                 self.data.us_equation,
                                                 input_stream, output_stream,
                                                 output_header,
                                                 output_time_indices)
                progressBar.connectToThread(process)
                process.run()

                if not process.canceled:
                    progressBar.outputFinished()
                progressBar.exec_()
                self.parent.outDialog()
Exemplo n.º 3
0
    def _run_max_min_mean(self, input_data):
        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
Exemplo n.º 4
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()
Exemplo n.º 5
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()
Exemplo n.º 6
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
Exemplo n.º 7
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)
Exemplo n.º 9
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
Exemplo n.º 10
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()
Exemplo n.º 11
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()
Exemplo n.º 12
0
                    nodes_included[j] = True

            nb_nodes_included = int(np.sum(nodes_included))
            print("Polyligne {} (avec {} points et une valeur à {}) contient {} noeuds".format(i, len(polyline.coords), value, nb_nodes_included))

            mask = Mask(value, polyline, nodes_included)
            masks.append(mask)

    if args.var is not None:
        var2D_list = args.var
    else:
        var2D_list = resin.varID

    var2D_pos = [resin.varID.index(x) for x in var2D_list]
    print(var2D_pos)

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

        for time in resin.time:
            var = resin.read_vars_in_frame(time, resout.varID)

            # Application aux zones
            for pos in var2D_pos:
                for mask in masks:
                    var[pos,:][mask.nodes_included] = mask.value

            resout.write_entire_frame(time, var)

Exemplo n.º 13
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
Exemplo n.º 14
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