def __plot_to_images()

in analysis/webservice/algorithms/Tomogram.py [0:0]


    def __plot_to_images(self, td):
        ds, peaks = self.results()

        options = self.__computeOptions

        if 'latitude' in self.__slice:
            along = 'latitude'
            across = 'longitude'
            title_row = 'Latitude: {:3.4f}'
            xlabel = 'Longitude'
        else:
            along = 'longitude'
            across = 'latitude'
            title_row = 'Longitude: {:3.4f}'
            xlabel = 'Latitude'

        min_across = ds[across].min().item()
        max_across = ds[across].max().item()

        files = []

        for al_i in range(len(ds[along])):
            ds_sub = ds.isel({along: al_i})
            al = ds_sub[along].item()

            rows = ds_sub.tomo.to_numpy()

            peaks_sub = None

            if peaks is not None:
                peaks_sub = peaks[al_i]

            logger.info(f'Building plot for along slice {al_i} of {len(ds[along])}')

            plt.figure(figsize=(11, 7))
            if self.__style == 'db':
                opt_min = options.get_float_arg('cbarMin', None)
                opt_max = options.get_float_arg('cbarMax', None)

                v = dict(vmin=-30, vmax=-10)

                if opt_min is not None:
                    v['vmin'] = opt_min
                if opt_max is not None:
                    v['vmax'] = opt_max

                if v['vmin'] >= v['vmax']:
                    logger.warning('cbarMin >= cbarMax. Using defaults')
                    v = dict(vmin=-30, vmax=-10)
            else:
                v = dict(vmin=0, vmax=1)

            plt.pcolormesh(self.__coords['x'], self.__coords['y'], rows, cmap=self.__cmap, **v)
            plt.colorbar()
            plt.title(f'{self.meta()["dataset"]} tomogram slice\n{title_row.format(al)}')
            plt.xlabel(xlabel)
            plt.ylabel(f'Elevation w.r.t. {self.meta()["dataset"]} reference (m)')

            legend_handles = []

            if 'ch' in ds_sub.data_vars and 'ch_secondary' in ds_sub.data_vars:
                x_dim = ds_sub['ch'].dims[0]

                # Plot secondary first so primary covers it
                second_ch = plt.plot(
                    ds_sub[x_dim].to_numpy(),
                    ds_sub.ch_secondary.to_numpy(),
                    label=f'Secondary canopy height: {ds_sub.ch_secondary.attrs["_source"]}'
                )

                prim_ch = plt.plot(
                    ds_sub[x_dim].to_numpy(),
                    ds_sub.ch.to_numpy(),
                    label=f'Primary canopy height: {ds_sub.ch.attrs["_source"]}'
                )

                legend_handles.extend(prim_ch)
                legend_handles.extend(second_ch)

            if 'gh' in ds_sub.data_vars and 'gh_secondary' in ds_sub.data_vars:
                x_dim = ds_sub['gh'].dims[0]

                # Plot secondary first so primary covers it
                second_ch = plt.plot(
                    ds_sub[x_dim].to_numpy(),
                    ds_sub.gh_secondary.to_numpy(),
                    label=f'Secondary ground height: {ds_sub.gh_secondary.attrs["_source"]}'
                )

                prim_ch = plt.plot(
                    ds_sub[x_dim].to_numpy(),
                    ds_sub.gh.to_numpy(),
                    label=f'Primary ground height: {ds_sub.gh.attrs["_source"]}'
                )

                legend_handles.extend(prim_ch)
                legend_handles.extend(second_ch)

            if peaks_sub is not None:
                peak_plot = plt.plot(
                    peaks_sub[0], peaks_sub[1], color='red', alpha=0.75, linestyle='--'
                )

                legend_handles.extend(peak_plot)

            plt.legend(handles=legend_handles, loc='lower right')

            plt.ticklabel_format(useOffset=False)

            filename = f'tomogram_{self.meta()["dataset"]}_{along}_{al:3.4f}_{across}_{min_across:3.4f}_to_{max_across:3.4f}.png'

            plt.savefig(
                os.path.join(
                    td, filename
                ), format='png', facecolor='white')

            files.append((filename, os.path.join(td, filename)))
            plt.close()

        return files