def btnComputeEvent(self):
        self.xlim = None
        self.ylim = None
        self.has_map = False

        ref_time = int(self.timeSelection.refIndex.text()) - 1
        test_time = int(self.timeSelection.testIndex.text()) - 1
        selected_variable = self.input.varBox.currentText().split('(')[0][:-1]

        try:
            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
                test_values = input_stream.read_var_in_frame(test_time, selected_variable)
        except (Serafin.SerafinRequestError, Serafin.SerafinValidationError) as e:
            QMessageBox.critical(None, 'Serafin Error', e.message, QMessageBox.Ok, QMessageBox.Ok)
            return

        values = test_values - ref_values
        self.ewsd = self.input.ref_mesh.element_wise_signed_deviation(values)

        self.updateStats(ref_time, test_time)
        self.updateHistogram()
    def btnComputeEvent(self):
        ref_time = int(self.timeSelection.refIndex.text()) - 1
        test_time = int(self.timeSelection.testIndex.text()) - 1
        init_time = int(self.initSelection.refIndex.text()) - 1
        selected_variable = self.input.varBox.currentText().split('(')[0][:-1]

        try:
            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
                test_values = input_stream.read_var_in_frame(test_time, selected_variable)
                init_values = input_stream.read_var_in_frame(init_time, selected_variable)
        except (Serafin.SerafinRequestError, Serafin.SerafinValidationError) as e:
            QMessageBox.critical(None, 'Serafin Error', e.message, QMessageBox.Ok, QMessageBox.Ok)
            return

        test_volume = self.input.ref_mesh.quadratic_volume(test_values - ref_values)
        ref_volume = self.input.ref_mesh.quadratic_volume(ref_values - init_values)
        if test_volume == 0 and ref_volume == 0:
            bss = 1
        else:
            with np.errstate(divide='ignore'):
                bss = 1 - test_volume / ref_volume
        self.resultTextBox.appendPlainText(self.template.format(ref_time+1, test_time+1, init_time+1, bss))
Example #3
0
    def btnComputeEvent(self):
        ref_time = int(self.timeSelection.refIndex.text()) - 1
        selected_variable = self.input.varBox.currentText().split('(')[0][:-1]

        mad = []
        try:
            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
                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))
        except (Serafin.SerafinRequestError,
                Serafin.SerafinValidationError) as e:
            QMessageBox.critical(None, 'Serafin Error', e.message,
                                 QMessageBox.Ok, QMessageBox.Ok)
            return
        self.plotViewer.plot(self.input.test_data.time, mad)
Example #4
0
    def btnComputeEvent(self):
        ref_time = int(self.timeSelection.refIndex.text()) - 1
        test_time = int(self.timeSelection.testIndex.text()) - 1
        selected_variable = self.input.varBox.currentText().split('(')[0][:-1]

        try:
            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
                test_values = input_stream.read_var_in_frame(
                    test_time, selected_variable)
        except (Serafin.SerafinRequestError,
                Serafin.SerafinValidationError) as e:
            QMessageBox.critical(None, 'Serafin Error', e.message,
                                 QMessageBox.Ok, QMessageBox.Ok)
            return

        values = test_values - ref_values
        msd = self.input.ref_mesh.mean_signed_deviation(values)
        mad = self.input.ref_mesh.mean_absolute_deviation(values)
        rmsd = self.input.ref_mesh.root_mean_square_deviation(values)
        self.resultTextBox.appendPlainText(
            self.template.format(ref_time + 1, test_time + 1, msd, mad, rmsd))
Example #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]

            try:
                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)
            except (Serafin.SerafinRequestError,
                    Serafin.SerafinValidationError) as e:
                QMessageBox.critical(None, 'Serafin Error', e.message,
                                     QMessageBox.Ok, QMessageBox.Ok)
                return

            self.plotViewer.plot(self.input.test_data.time, all_bss)
        self.plotViewer.show()
Example #6
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,
                               True) 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
Example #7
0
    def run_single(self, input_name, input_header, output_name, output_header,
                   pool):
        i = 0
        try:
            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()
        except (Serafin.SerafinRequestError,
                Serafin.SerafinValidationError) as e:
            QMessageBox.critical(self, 'Serafin Error', e.message,
                                 QMessageBox.Ok)
            return
Example #8
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.set_variables(scalars + vectors)
        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,
                               True) 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
    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
        try:
            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()
        except (Serafin.SerafinRequestError, Serafin.SerafinValidationError) as e:
            QMessageBox.critical(None, 'Serafin Error', e.message, QMessageBox.Ok, QMessageBox.Ok)
            return
Example #10
0
def update_culverts_file(args):
    with Serafin.Read(args.in_slf_ori, args.lang) as mesh_ori:
        mesh_ori.read_header()
    with Serafin.Read(args.in_slf_new, args.lang) as mesh_new:
        mesh_new.read_header()
    with open(args.in_txt, 'r') as in_txt:
        with open(args.out_txt, 'w', newline='') as out_txt:
            for i, line in enumerate(in_txt):
                if i < 3:
                    out_txt.write(line)
                else:
                    n1_ori, n2_ori, txt = line.split(maxsplit=2)
                    n1_new = mesh_new.header.nearest_node(
                        mesh_ori.header.x[int(n1_ori) - 1],
                        mesh_ori.header.y[int(n1_ori) - 1])
                    n2_new = mesh_new.header.nearest_node(
                        mesh_ori.header.x[int(n2_ori) - 1],
                        mesh_ori.header.y[int(n2_ori) - 1])
                    out_txt.write('%i %i %s' % (n1_new, n2_new, txt))
Example #11
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.fmt_float, self.mesh)
        progressBar = OutputProgressDialog()

        try:
            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])
        except (Serafin.SerafinRequestError, Serafin.SerafinValidationError) as e:
            QMessageBox.critical(None, 'Serafin Error', e.message, QMessageBox.Ok, QMessageBox.Ok)
            return

        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)
Example #12
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))]

        try:
            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.fmt_float)
                progressBar.setValue(5)
                QApplication.processEvents()

                with open(filename, 'w') as f2:
                    progressBar.connectToThread(calculator)
                    calculator.write_csv(f2)
        except (Serafin.SerafinRequestError,
                Serafin.SerafinValidationError) as e:
            QMessageBox.critical(None, 'Serafin Error', e.message,
                                 QMessageBox.Ok, QMessageBox.Ok)
            return

        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)
Example #13
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
        try:
            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()
        except (Serafin.SerafinRequestError, Serafin.SerafinValidationError) as e:
            QMessageBox.critical(None, 'Serafin Error', e.message, QMessageBox.Ok, QMessageBox.Ok)
            return

        progressBar.exec_()
        self.parent.outDialog()
Example #14
0
def slf_base(args):
    with Serafin.Read(args.in_slf, args.lang) as resin:
        resin.read_header()
        logger.info(resin.header.summary())
        resin.get_time()

        output_header = resin.header.copy()
        # Shift mesh coordinates if necessary
        if args.shift:
            output_header.transform_mesh([Transformation(0, 1, 1, args.shift[0], args.shift[1], 0)])
        # Set mesh origin coordinates
        if args.set_mesh_origin:
            output_header.set_mesh_origin(args.set_mesh_origin[0], args.set_mesh_origin[1])

        # Toggle output file endianness if necessary
        if args.toggle_endianness:
            output_header.toggle_endianness()

        # Convert to single precision
        if args.to_single_precision:
            if resin.header.is_double_precision():
                output_header.to_single_precision()
            else:
                logger.warn('Input file is already single precision! Argument `--to_single_precision` is ignored')

        # Remove variables if necessary
        if args.var2del:
            output_header.empty_variables()
            for var_ID, var_name, var_unit in zip(resin.header.var_IDs, resin.header.var_names, resin.header.var_units):
                if var_ID not in args.var2del:
                    output_header.add_variable(var_ID, var_name, var_unit)

        # Add new derived variables
        if args.var2add is not None:
            for var_ID in args.var2add:
                if var_ID in output_header.var_IDs:
                    logger.warn('Variable %s is already present (or asked)' % var_ID)
                else:
                    output_header.add_variable_from_ID(var_ID)

        us_equation = get_US_equation(args.friction_law)
        necessary_equations = get_necessary_equations(resin.header.var_IDs, output_header.var_IDs,
                                                      is_2d=resin.header.is_2d, us_equation=us_equation)

        with Serafin.Write(args.out_slf, args.lang, overwrite=args.force) as resout:
            resout.write_header(output_header)

            for time_index, time in tqdm(resin.subset_time(args.start, args.end, args.ech), unit='frame'):
                values = do_calculations_in_frame(necessary_equations, resin, time_index, output_header.var_IDs,
                                                  output_header.np_float_type, is_2d=output_header.is_2d,
                                                  us_equation=us_equation, ori_values={})
                resout.write_entire_frame(output_header, time, values)
Example #15
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()

        indices_nonempty = [i for i in range(len(self.input.lines)) if self.input.line_interpolators[i][0]]

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

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

                progressBar.setValue(1)
                QApplication.processEvents()

                with open(filename, 'w') as output_stream:
                    progressBar.connectToThread(process)

                    if self.intersect.isChecked():
                        process.write_csv(input_stream, selected_var_IDs, output_stream,
                                          self.input.line_interpolators, indices_nonempty)
                    else:
                        process.write_csv(input_stream, selected_var_IDs, output_stream,
                                          self.input.line_interpolators_internal, indices_nonempty)
        except (Serafin.SerafinRequestError, Serafin.SerafinValidationError) as e:
            QMessageBox.critical(None, 'Serafin Error', e.message, QMessageBox.Ok, QMessageBox.Ok)
            return

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

        if process.canceled:
            self.csvNameBox.clear()
            return
Example #16
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
        try:
            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()
        except (Serafin.SerafinRequestError, Serafin.SerafinValidationError) as e:
            QMessageBox.critical(None, 'Serafin Error', e.message, QMessageBox.Ok, QMessageBox.Ok)
            return

        progressBar.exec_()
        self.parent.outDialog()
Example #17
0
def slf_to_raster(args):
    with Serafin.Read(args.in_slf, args.lang) as resin:
        resin.read_header()
        header = resin.header
        logger.info(header.summary())
        resin.get_time()

        if args.vars is None:
            var_names = [
                var_name.decode('utf-8') for var_name in header.var_names
            ]
            var_IDs = header.var_IDs
        else:
            var_names = []
            var_IDs = []
            for var_ID, var_name in zip(header.var_IDs, header.var_names):
                if var_ID in args.vars:
                    var_names.append(var_name.decode('utf-8'))
                    var_IDs.append(var_ID)

        # Shift mesh coordinates if necessary
        if args.shift:
            header.transform_mesh(
                [Transformation(0, 1, 1, args.shift[0], args.shift[1], 0)])

        # Build output regular grid and matplotlib triangulation of the mesh
        m_xi, m_yi = np.meshgrid(
            np.arange(header.x.min(), header.x.max(), args.resolution),
            np.arange(header.y.min(), header.y.max(), args.resolution))
        triang = mtri.Triangulation(header.x,
                                    header.y,
                                    triangles=header.ikle_2d - 1)

        # Build list containing all interpolated variables on the regular grid
        array_list = []
        for i, (var_ID, var_name) in enumerate(zip(var_IDs, var_names)):
            values = resin.read_var_in_frame(args.frame_index, var_ID)
            interp = mtri.LinearTriInterpolator(triang, values)
            data = interp(
                m_xi,
                m_yi)[::-1]  # reverse array so the tif looks like the array
            array_list.append((var_name, data))
            logger.info(
                "Min and max values for interpolated %s variable: [%f, %f]" %
                (var_name, data.min(), data.max()))

        # Write data in the raster output file
        arrays2raster(args.out_tif, (header.x.min(), header.y.max()),
                      args.resolution, -args.resolution, array_list)
def bottom(inname, outname, i3s_names, overwrite, threshold):
    # global prev_line, zones, np_coord, Xt, Z, ref_rows, polyline
    with Serafin.Read(inname, 'fr') as resin:
        resin.read_header()

        if not resin.header.is_2d:
            sys.exit("The current script is working only with 2D meshes !")

        resin.get_time()

        # Define zones from polylines
        zones = []
        for i3s_name in i3s_names:
            zones += Zone.get_zones_from_i3s_file(i3s_name, threshold)

        with Serafin.Write(outname, 'fr', overwrite) as resout:
            output_header = resin.header
            resout.write_header(output_header)
            posB = output_header.var_IDs.index('B')

            for time_index, time in enumerate(resin.time):
                var = np.empty((output_header.nb_var, output_header.nb_nodes),
                               dtype=output_header.np_float_type)
                for i, var_ID in enumerate(output_header.var_IDs):
                    var[i, :] = resin.read_var_in_frame(time_index, var_ID)

                # Replace bottom locally
                nmodif = 0
                for i in range(
                        output_header.nb_nodes):  # iterate over all nodes
                    x, y = output_header.x[i], output_header.y[i]
                    pt = geo.Point(x, y)

                    for j, zone in enumerate(zones):
                        if zone.contains(pt):
                            # Current point is inside zone number j
                            #   and is between polylines a and b
                            print("node {} is in zone n°{}".format(i + 1, j))

                            # Replace value by a linear interpolation
                            z_int = zone.interpolate(pt)
                            print(z_int)
                            var[posB, i] = min(var[posB, i], z_int)

                            nmodif += 1
                            break

                resout.write_entire_frame(output_header, time, var)
                print("{} nodes were overwritten".format(nmodif))
Example #19
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.empty_variables()
        for var_ID in selected_vars:
            var_name, var_unit = input_data.selected_vars_names[var_ID]
            output_header.add_variable(var_ID, var_name, 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,
                               True) 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
Example #20
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,
                            True) 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
Example #21
0
def slf_last(args):
    with Serafin.Read(args.in_slf, args.lang) as resin:
        resin.read_header()
        logger.info(resin.header.summary())
        resin.get_time()

        output_header = resin.header.copy()
        # Shift mesh coordinates if necessary
        if args.shift:
            output_header.transform_mesh(
                [Transformation(0, 1, 1, args.shift[0], args.shift[1], 0)])

        # Toggle output file endianness if necessary
        if args.toggle_endianness:
            output_header.toggle_endianness()

        # Convert to single precision
        if args.to_single_precision:
            if resin.header.is_double_precision():
                output_header.to_single_precision()
            else:
                logger.warn(
                    'Input file is already single precision! Argument `--to_single_precision` is ignored'
                )

        values = np.empty((output_header.nb_var, output_header.nb_nodes),
                          dtype=output_header.np_float_type)
        with Serafin.Write(args.out_slf, args.lang,
                           overwrite=args.force) as resout:
            resout.write_header(output_header)

            time_index = len(resin.time) - 1
            time = resin.time[-1] if args.time is None else args.time

            for i, var_ID in enumerate(output_header.var_IDs):
                values[i, :] = resin.read_var_in_frame(time_index, var_ID)

            resout.write_entire_frame(output_header, time, values)
Example #22
0
def slf_sedi_chain(args):
    # Check that float parameters are positive (especially ws!)
    for arg in ('Cmud', 'ws', 'C', 'M'):
        if getattr(args, arg) < 0:
            logger.critical('The argument %s has to be positive' % args)
            sys.exit(1)

    with Serafin.Read(args.in_slf, args.lang) as resin:
        resin.read_header()
        logger.info(resin.header.summary())
        resin.get_time()

        us_equation = get_US_equation(args.friction_law)
        necessary_equations = get_necessary_equations(resin.header.var_IDs,
                                                      ['TAU'],
                                                      is_2d=True,
                                                      us_equation=us_equation)

        if resin.header.nb_frames < 1:
            logger.critical('The input file must have at least one frame!')
            sys.exit(1)

        output_header = resin.header.copy()
        # Shift mesh coordinates if necessary
        if args.shift:
            output_header.transform_mesh(
                [Transformation(0, 1, 1, args.shift[0], args.shift[1], 0)])

        # Toggle output file endianness if necessary
        if args.toggle_endianness:
            output_header.toggle_endianness()

        # Convert to single precision
        if args.to_single_precision:
            if resin.header.is_double_precision():
                output_header.to_single_precision()
            else:
                logger.warn(
                    'Input file is already single precision! Argument `--to_single_precision` is ignored'
                )

        output_header.empty_variables()
        output_header.add_variable_from_ID('B')
        output_header.add_variable_from_ID('EV')

        with Serafin.Write(args.out_slf, args.lang,
                           overwrite=args.force) as resout:
            resout.write_header(output_header)

            prev_time = None
            prev_tau = None
            initial_bottom = resin.read_var_in_frame(0, 'B')
            bottom = copy(initial_bottom)
            for time_index, time in enumerate(resin.time):
                tau = do_calculations_in_frame(necessary_equations,
                                               resin,
                                               time_index, ['TAU'],
                                               output_header.np_float_type,
                                               is_2d=True,
                                               us_equation=us_equation,
                                               ori_values={})[0]
                if prev_time is not None:
                    dt = time - prev_time
                    mean_tau = (prev_tau + tau) / 2
                    if args.Tcd > 0:
                        bottom += args.Cmud * args.ws * args.C * \
                                  (1 - np.clip(mean_tau/args.Tcd, a_min=None, a_max=1.)) * dt
                    if args.Tce > 0:
                        bottom -= args.Cmud * args.M * (np.clip(
                            mean_tau / args.Tce, a_min=1., a_max=None) -
                                                        1.) * dt

                evol_bottom = bottom - initial_bottom
                resout.write_entire_frame(output_header, time,
                                          np.vstack((bottom, evol_bottom)))

                prev_time = time
                prev_tau = tau
Example #23
0
def slf_3d_to_2d(args):
    with Serafin.Read(args.in_slf, args.lang) as resin:
        resin.read_header()
        logger.info(resin.header.summary())
        resin.get_time()

        if resin.header.is_2d:
            logger.critical('The input file is not 3D.')
            sys.exit(1)
        if 'Z' not in resin.header.var_IDs:
            logger.critical('The elevation variable Z is not found in the Serafin file.')
            sys.exit(1)
        if args.layer is not None:
            upper_plane = resin.header.nb_planes
            if args.layer < 1 or args.layer > upper_plane:
                logger.critical('Layer has to be in [1, %i]' % upper_plane)
                sys.exit(1)

        output_header = resin.header.copy_as_2d()
        # Shift mesh coordinates if necessary
        if args.shift:
            output_header.transform_mesh([Transformation(0, 1, 1, args.shift[0], args.shift[1], 0)])

        # Toggle output file endianness if necessary
        if args.toggle_endianness:
            output_header.toggle_endianness()

        # Convert to single precision
        if args.to_single_precision:
            if resin.header.is_double_precision():
                output_header.to_single_precision()
            else:
                logger.warn('Input file is already single precision! Argument `--to_single_precision` is ignored')

        if args.aggregation is not None:
            if args.aggregation == 'max':
                operation_type = operations.MAX
            elif args.aggregation == 'min':
                operation_type = operations.MIN
            else:  # args.aggregation == 'mean'
                 operation_type = operations.MEAN
            selected_vars = [var for var in output_header.iter_on_all_variables()]
            vertical_calculator = operations.VerticalMaxMinMeanCalculator(operation_type, resin, output_header,
                                                                          selected_vars, args.vars)
            output_header.set_variables(vertical_calculator.get_variables())  # sort variables

        # Add some elevation variables
        for var_ID in args.vars:
            output_header.add_variable_from_ID(var_ID)

        with Serafin.Write(args.out_slf, args.lang, overwrite=args.force) as resout:
            resout.write_header(output_header)

            vars_2d = np.empty((output_header.nb_var, output_header.nb_nodes_2d), dtype=output_header.np_float_type)
            for time_index, time in enumerate(tqdm(resin.time, unit='frame')):
                if args.aggregation is not None:
                    vars_2d = vertical_calculator.max_min_mean_in_frame(time_index)
                else:
                    for i, var in enumerate(output_header.var_IDs):
                        vars_2d[i, :] = resin.read_var_in_frame_as_3d(time_index, var)[args.layer - 1, :]
                resout.write_entire_frame(output_header, time, vars_2d)
Example #24
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
        try:
            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()
        except (Serafin.SerafinRequestError, Serafin.SerafinValidationError) as e:
            QMessageBox.critical(None, 'Serafin Error', e.message, QMessageBox.Ok, QMessageBox.Ok)
            return

        progressBar.exec_()
        self.parent.outDialog()
Example #25
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.empty_variables()
        for var in common_vars:
            name, unit = first_input.selected_vars_names[var]
            output_header.add_variable(var, name, 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,
                                   True) 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
Example #26
0
def slf_flux2d(args):
    if len(args.scalars) > 2:
        logger.critical('Only two scalars can be integrated!')
        sys.exit(2)

    # Read set of lines from input file
    polylines = []
    if args.in_sections.endswith('.i2s'):
        with BlueKenue.Read(args.in_sections) as f:
            f.read_header()
            for polyline in f.get_open_polylines():
                polylines.append(polyline)
    elif args.in_sections.endswith('.shp'):
        try:
            for polyline in Shapefile.get_open_polylines(args.in_sections):
                polylines.append(polyline)
        except ShapefileException as e:
            logger.critical(e)
            sys.exit(3)
    else:
        logger.critical('File "%s" is not a i2s or shp file.' %
                        args.in_sections)
        sys.exit(2)

    if not polylines:
        logger.critical('The file does not contain any open polyline.')
        sys.exit(1)
    logger.debug('The file contains {} open polyline{}.'.format(
        len(polylines), 's' if len(polylines) > 1 else ''))

    # Read Serafin file
    with Serafin.Read(args.in_slf, args.lang) as resin:
        resin.read_header()
        logger.info(resin.header.summary())
        resin.get_time()

        if not resin.header.is_2d:
            logger.critical('The file has to be a 2D Serafin!')
            sys.exit(3)

        # Determine flux computations properties
        var_IDs = args.vectors + args.scalars
        variables_missing = [
            var_ID for var_ID in var_IDs if var_ID not in resin.header.var_IDs
        ]
        if variables_missing:
            if len(variables_missing) > 1:
                logger.critical(
                    'Variables {} are not present in the Serafin file'.format(
                        variables_missing))
            else:
                logger.critical(
                    'Variable {} is not present in the Serafin file'.format(
                        variables_missing[0]))
            logger.critical(
                'Check also `--lang` argument for variable detection.')
            sys.exit(1)
        if var_IDs not in PossibleFluxComputation.common_fluxes():
            logger.warn(
                'Flux computations is not common. Check what you are doing (or the language).'
            )

        flux_type = PossibleFluxComputation.get_flux_type(var_IDs)

        section_names = ['Section %i' % (i + 1) for i in range(len(polylines))]
        calculator = FluxCalculator(flux_type, var_IDs, resin, section_names,
                                    polylines, args.ech)
        calculator.construct_triangles(tqdm)
        calculator.construct_intersections()
        result = []
        for time_index, time in enumerate(tqdm(resin.time, unit='frame')):
            i_result = [str(time)]
            values = []

            for var_ID in calculator.var_IDs:
                values.append(resin.read_var_in_frame(time_index, var_ID))

            for j in range(len(polylines)):
                intersections = calculator.intersections[j]
                flux = calculator.flux_in_frame(intersections, values)
                i_result.append(settings.FMT_FLOAT.format(flux))

            result.append(i_result)

        # Write CSV
        mode = 'w' if args.force else 'x'
        with open(args.out_csv, mode) as out_csv:
            calculator.write_csv(result, out_csv, args.sep)
Example #27
0
def bottom(args):
    if args.operations is None:
        args.operations = ['set'] * len(args.in_i3s_paths)
    if len(args.in_i3s_paths) != len(args.operations):
        raise RuntimeError

    # global prev_line, zones, np_coord, Xt, Z, ref_rows, polyline
    with Serafin.Read(args.in_slf, 'fr') as resin:
        resin.read_header()

        if not resin.header.is_2d:
            sys.exit("The current script is working only with 2D meshes !")

        resin.get_time()

        # Define zones from polylines
        zones = []
        for i3s_path, operator_str in zip(args.in_i3s_paths, args.operations):
            zones += Zone.get_zones_from_i3s_file(i3s_path, args.threshold, operator_str)

        with Serafin.Write(args.out_slf, 'fr', args.force) as resout:
            output_header = resin.header
            resout.write_header(output_header)
            pos_B = output_header.var_IDs.index('B')

            for time_index, time in enumerate(resin.time):
                var = np.empty((output_header.nb_var, output_header.nb_nodes), dtype=output_header.np_float_type)
                for i, var_ID in enumerate(output_header.var_IDs):
                    var[i, :] = resin.read_var_in_frame(time_index, var_ID)

                # Replace bottom locally
                nmodif = 0
                for i in range(output_header.nb_nodes):  # iterate over all nodes
                    x, y = output_header.x[i], output_header.y[i]
                    pt = geo.Point(x, y)
                    old_z = var[pos_B, i]

                    found = False
                    # Check if it is inside a zone
                    for j, zone in enumerate(zones):
                        if zone.contains(pt):
                            # Current point is inside zone number j and is between polylines a and b
                            z_int = zone.interpolate(pt)
                            new_z = zone.operator(z_int, old_z)
                            var[pos_B, i] = new_z

                            print("BOTTOM at node {} (zone n°{}) {} to {} (dz={})".format(
                                i + 1, j, operator_str, new_z, new_z - old_z
                            ))

                            nmodif += 1
                            found = True
                            break

                    if not found and args.rescue_distance > 0.0:
                        # Try to rescue some very close nodes
                        for j, zone in enumerate(zones):
                            if zone.polygon.distance(pt) < args.rescue_distance:
                                pt_projected = zone.get_closest_point(pt)

                                # Replace value by a linear interpolation
                                z_int = zone.interpolate(pt_projected)
                                new_z = zone.operator(z_int, old_z)
                                var[pos_B, i] = new_z

                                print("BOTTOM at node {} (zone n°{}, rescued) {} to {} (dz={})".format(
                                    i + 1, j, operator_str, new_z, new_z - old_z
                                ))

                                nmodif += 1
                                break

                resout.write_entire_frame(output_header, time, var)
                print("{} nodes were overwritten".format(nmodif))
Example #28
0
def ADCP_comp(args):
    x_mes = []
    y_mes = []
    cord_mes = open(args.inADCP_GPS).read().splitlines()
    for x_l in cord_mes:
        y, x = x_l.split(',')
        if x == NODATA or y == NODATA:
            print("Warning: one point is missing")
        else:
            x_mes.append(x)
            y_mes.append(y)
    x_mes = [float(a) for a in x_mes]
    y_mes = [float(a) for a in y_mes]
    inProj = Proj("+init=EPSG:%i" % args.inEPSG)
    outProj = Proj("+init=EPSG:%i" % args.outEPSG)
    x_mes, y_mes = transform(inProj, outProj, x_mes, y_mes)

    SCHEMA = {'geometry': 'LineString', 'properties': {'nom': 'str'}}
    with fiona.open(args.outADCP_GPS,
                    'w',
                    'ESRI Shapefile',
                    SCHEMA,
                    crs=from_epsg(args.outEPSG)) as out_shp:
        Ltest = LineString([(x_2, y_2) for x_2, y_2 in zip(x_mes, y_mes)])
        elem = {}
        elem['geometry'] = mapping(Ltest)
        elem['properties'] = {'nom': 'ADCP line'}
        out_shp.write(elem)

    p_raw = RawProfileObj(args.inADCP)
    processing_settings = {'proj_method': 2}
    startingpoint = dict(start=Vector(0, 0))
    p0 = ProcessedProfileObj(p_raw, processing_settings, startingpoint)
    profile_averaged = averaging.get_averaged_profile(p0, cfg={'order': 15})
    header = 'X;Y;Uadcp;Vadcp;MagnitudeXY;Hadcp\n'
    writeAscii2D(profile_averaged,
                 '{x};{y};{vx};{vy};{vmag};{depth}',
                 args.outADCP,
                 header=header)

    if args.inTELEMAC:
        with open(args.outT2DCSV, 'w', newline='') as csvfile:
            csvwriter = csv.writer(csvfile, delimiter=';')
            HEADER = [
                'folder', 'time_id', 'time', 'point_x', 'point_y', 'distance',
                'value'
            ]
            csvwriter.writerow(HEADER)

            for slf_path in args.inTELEMAC:
                folder = os.path.basename(os.path.split(slf_path)[0])
                with Serafin.Read(slf_path, 'fr') as resin:
                    resin.read_header()
                    logger.info(resin.header.summary())
                    resin.get_time()
                    output_header = resin.header.copy()
                    if args.shift:
                        output_header.transform_mesh([
                            Transformation(0, 1, 1, args.shift[0],
                                           args.shift[1], 0)
                        ])
                    mesh = MeshInterpolator(output_header, True)
                    lines = []
                    for poly in Shapefile.get_lines(args.outADCP_GPS,
                                                    shape_type=3):
                        lines.append(poly)
                    nb_nonempty, indices_nonempty, line_interpolators, line_interpolators_internal = \
                        mesh.get_line_interpolators(lines)
                    res = mesh.interpolate_along_lines(
                        resin, 'M', list(range(len(resin.time))),
                        indices_nonempty, line_interpolators, '{:.6e}')
                    csvwriter.writerows([[folder] + x[2] for x in res])
Example #29
0
def slf_int2d(args):
    # Read set of points file
    fields, indices = Shapefile.get_attribute_names(args.in_points)
    points = []
    attributes = []
    for point, attribute in Shapefile.get_points(args.in_points, indices):
        points.append(point)
        attributes.append(attribute)

    if not points:
        logger.critical('The Shapefile does not contain any point.')
        sys.exit(1)

    # Read Serafin file
    with Serafin.Read(args.in_slf, args.lang) as resin:
        resin.read_header()
        logger.info(resin.header.summary())

        if not resin.header.is_2d:
            logger.critical('The file has to be a 2D Serafin!')
            sys.exit(3)

        resin.get_time()

        output_header = resin.header.copy()

        mesh = MeshInterpolator(output_header, True)
        is_inside, point_interpolators = mesh.get_point_interpolators(points)
        nb_inside = sum(map(int, is_inside))

        if nb_inside == 0:
            logger.critical('No point inside the mesh.')
            sys.exit(3)
        logger.debug(
            'The file contains {} point{}. {} point{} inside the mesh'.format(
                len(points), 's' if len(points) > 1 else '', nb_inside,
                's are' if nb_inside > 1 else ' is'))

        var_IDs = output_header.var_IDs if args.vars is None else args.vars

        mode = 'w' if args.force else 'x'
        with open(args.out_csv, mode, newline='') as csvfile:
            csvwriter = csv.writer(csvfile, delimiter=args.sep)

            header = ['time_id', 'time']
            if args.long:
                header = header + [
                    'point_id', 'point_x', 'point_y', 'variable', 'value'
                ]
            else:
                for pt_id, (x, y) in enumerate(points):
                    for var in var_IDs:
                        header.append(
                            'Point %d %s (%s|%s)' %
                            (pt_id + 1, var, settings.FMT_COORD.format(x),
                             settings.FMT_COORD.format(y)))
            csvwriter.writerow(header)

            for time_index, time in enumerate(tqdm(resin.time, unit='frame')):
                values = [time_index, time]

                for var_ID in var_IDs:
                    var = resin.read_var_in_frame(time_index, var_ID)
                    for pt_id, (point, point_interpolator) in enumerate(
                            zip(points, point_interpolators)):
                        if args.long:
                            values_long = values + [str(pt_id + 1)] + [
                                settings.FMT_COORD.format(x) for x in point
                            ]

                        if point_interpolator is None:
                            if args.long:
                                csvwriter.writerow(values_long +
                                                   [var_ID, settings.NAN_STR])
                            else:
                                values.append(settings.NAN_STR)
                        else:
                            (i, j, k), interpolator = point_interpolator
                            int_value = settings.FMT_FLOAT.format(
                                interpolator.dot(var[[i, j, k]]))
                            if args.long:
                                csvwriter.writerow(values_long +
                                                   [var_ID, int_value])
                            else:
                                values.append(int_value)

                if not args.long: csvwriter.writerow(values)
Example #30
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.empty_variables()
        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.add_variable_str('', name, time_unit.upper())
        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,
                               True) 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