コード例 #1
0
    def plot(self,
             ifield=0,
             cut=0.5,
             levels=12,
             cmap='RdYlBu_r',
             png=False,
             step=1,
             normed=False,
             dpi=80,
             bgcolor=None,
             deminc=True,
             ic=False):
        """
        Plotting function (it can also write the png files)

        :param ifield: in case of a multiple-field movie file, you can change
                       the default field displayed using the parameter ifield
        :type ifield: int
        :param levels: number of contour levels
        :type levels: int
        :param cmap: name of the colormap
        :type cmap: str
        :param cut: adjust the contour extrema to max(abs(data))*cut
        :type cut: float
        :param png: save the movie as a series of png files when
                    set to True
        :type png: bool
        :param dpi: dot per inch when saving PNGs
        :type dpi: int
        :param bgcolor: background color of the figure
        :type bgcolor: str
        :param normed: the colormap is rescaled every timestep when set to True,
                       otherwise it is calculated from the global extrema
        :type normed: bool
        :param step: the stepping between two timesteps
        :type step: int
        :param deminc: a logical to indicate if one wants do get rid of the
                       possible azimuthal symmetry
        :type deminc: bool
        """

        if png:
            plt.ioff()
            if not os.path.exists('movie'):
                os.mkdir('movie')
        else:
            plt.ion()

        if not normed:
            vmin = -max(abs(self.data[ifield, ...].max()),
                        abs(self.data[ifield, ...].min()))
            vmin = cut * vmin
            vmax = -vmin
            # vmin, vmax = self.data.min(), self.data.max()
            cs = np.linspace(vmin, vmax, levels)

        if self.surftype == 'phi_constant':
            if self.n_theta_plot == self.n_theta_max:
                th = np.linspace(np.pi / 2., -np.pi / 2., self.n_theta_max)
                fig = plt.figure(figsize=(4, 8))
                th0 = th
            else:
                th0 = np.linspace(np.pi / 2., -np.pi / 2., self.n_theta_max)
                th1 = np.linspace(np.pi / 2., 3. * np.pi / 2.,
                                  self.n_theta_max)
                th = np.concatenate((th0, th1))
                fig = plt.figure(figsize=(6.5, 6))
                # Plotting trick using th0
                th0 = np.linspace(np.pi / 2, np.pi / 2 + 2. * np.pi,
                                  2 * self.n_theta_max)
            rr, tth = np.meshgrid(self.radius, th)
            xx = rr * np.cos(tth)
            yy = rr * np.sin(tth)
            xxout = rr.max() * np.cos(th0)
            yyout = rr.max() * np.sin(th0)
            xxin = rr.min() * np.cos(th0)
            yyin = rr.min() * np.sin(th0)

            if ic:
                rr, tth = np.meshgrid(self.radius_ic, th)
                xx_ic = rr * np.cos(tth)
                yy_ic = rr * np.sin(tth)
        elif self.surftype == 'r_constant':
            th = np.linspace(np.pi / 2., -np.pi / 2., self.n_theta_max)
            if deminc:
                phi = np.linspace(-np.pi, np.pi,
                                  self.n_phi_tot * self.minc + 1)
                xxout, yyout = hammer2cart(th, -np.pi)
                xxin, yyin = hammer2cart(th, np.pi)
            else:
                phi = np.linspace(-np.pi / self.minc, np.pi / self.minc,
                                  self.n_phi_tot)
                xxout, yyout = hammer2cart(th, -np.pi / self.minc)
                xxin, yyin = hammer2cart(th, np.pi / self.minc)
            ttheta, pphi = np.meshgrid(th, phi)
            xx, yy = hammer2cart(ttheta, pphi)
            fig = plt.figure(figsize=(8, 4))
        elif self.surftype == 'theta_constant':
            if deminc:
                phi = np.linspace(0., 2. * np.pi,
                                  self.n_phi_tot * self.minc + 1)
            else:
                phi = np.linspace(0., 2. * np.pi / self.minc, self.n_phi_tot)
            rr, pphi = np.meshgrid(self.radius, phi)
            xx = rr * np.cos(pphi)
            yy = rr * np.sin(pphi)
            xxout = rr.max() * np.cos(pphi)
            yyout = rr.max() * np.sin(pphi)
            xxin = rr.min() * np.cos(pphi)
            yyin = rr.min() * np.sin(pphi)
            if ic:
                rr, pphi = np.meshgrid(self.radius_ic, phi)
                xx_ic = rr * np.cos(pphi)
                yy_ic = rr * np.sin(pphi)
            fig = plt.figure(figsize=(6, 6))
        elif self.surftype == '3d volume':
            self.data = self.data[ifield, ..., 0]
            th = np.linspace(np.pi / 2., -np.pi / 2., self.n_theta_max)
            phi = np.linspace(-np.pi, np.pi, self.n_phi_tot)
            ttheta, pphi = np.meshgrid(th, phi)
            xx, yy = hammer2cart(ttheta, pphi)
            xxout, yyout = hammer2cart(th, -np.pi)
            xxin, yyin = hammer2cart(th, np.pi)
            fig = plt.figure(figsize=(8, 4))

        fig.subplots_adjust(top=0.99, right=0.99, bottom=0.01, left=0.01)
        ax = fig.add_subplot(111, frameon=False)

        for k in range(self.nvar):
            if k == 0:
                if normed:
                    vmin = -max(abs(self.data[ifield, k, ...].max()),
                                abs(self.data[ifield, k, ...].min()))
                    vmin = cut * vmin
                    vmax = -vmin
                    cs = np.linspace(vmin, vmax, levels)
                if self.surftype in ['r_constant', 'theta_constant']:
                    if deminc:
                        dat = symmetrize(self.data[ifield, k, ...], self.minc)
                        if ic:
                            datic = symmetrize(self.data_ic[ifield, k, ...],
                                               self.minc)
                    else:
                        dat = self.data[ifield, k, ...]
                        if ic:
                            datic = self.data_ic[ifield, k, ...]
                    im = ax.contourf(xx, yy, dat, cs, cmap=cmap, extend='both')
                    if ic:
                        ax.contourf(xx_ic,
                                    yy_ic,
                                    datic,
                                    cs,
                                    cmap=cmap,
                                    extend='both')
                else:
                    im = ax.contourf(xx,
                                     yy,
                                     self.data[ifield, k, ...],
                                     cs,
                                     cmap=cmap,
                                     extend='both')
                    if ic:
                        im_ic = ax.contourf(xx_ic,
                                            yy_ic,
                                            self.data_ic[ifield, k, ...],
                                            cs,
                                            cmap=cmap,
                                            extend='both')
                ax.plot(xxout, yyout, 'k-', lw=1.5)
                ax.plot(xxin, yyin, 'k-', lw=1.5)
                ax.axis('off')
                man = plt.get_current_fig_manager()
                man.canvas.draw()
            if k != 0 and k % step == 0:
                if not png:
                    print(k + self.var2 - self.nvar)
                plt.cla()
                if normed:
                    vmin = -max(abs(self.data[ifield, k, ...].max()),
                                abs(self.data[ifield, k, ...].min()))
                    vmin = cut * vmin
                    vmax = -vmin
                    cs = np.linspace(vmin, vmax, levels)
                if self.surftype in ['r_constant', 'theta_constant']:
                    if deminc:
                        dat = symmetrize(self.data[ifield, k, ...], self.minc)
                        if ic:
                            datic = symmetrize(self.data_ic[ifield, k, ...],
                                               self.minc)
                    else:
                        dat = self.data[ifield, k, ...]
                        if ic:
                            datic = self.data_ic[ifield, k, ...]
                    ax.contourf(xx, yy, dat, cs, cmap=cmap, extend='both')
                    if ic:
                        ax.contourf(xx_ic,
                                    yy_ic,
                                    datic,
                                    cs,
                                    cmap=cmap,
                                    extend='both')
                else:
                    ax.contourf(xx,
                                yy,
                                self.data[ifield, k, ...],
                                cs,
                                cmap=cmap,
                                extend='both')
                    if ic:
                        ax.contourf(xx_ic,
                                    yy_ic,
                                    self.data_ic[ifield, k, ...],
                                    cs,
                                    cmap=cmap,
                                    extend='both')
                ax.plot(xxout, yyout, 'k-', lw=1.5)
                ax.plot(xxin, yyin, 'k-', lw=1.5)
                ax.axis('off')
                man.canvas.draw()
            if png:
                filename = 'movie/img{:05d}.png'.format(k)
                print('write {}'.format(filename))
                # st = 'echo {}'.format(ivar) + ' > movie/imgmax'
                if bgcolor is not None:
                    fig.savefig(filename, facecolor=bgcolor, dpi=dpi)
                else:
                    fig.savefig(filename, dpi=dpi)
コード例 #2
0
    def avgStd(self,
               ifield=0,
               std=False,
               cut=0.5,
               levels=12,
               cmap='RdYlBu_r',
               ic=False):
        """
        Plot time-average or standard deviation

        :param ifield: in case of a multiple-field movie file, you can change
                       the default field displayed using the parameter ifield
        :type ifield: int
        :param std: the standard deviation is computed instead the average
                    when std is True
        :type std: bool
        :param levels: number of contour levels
        :type levels: int
        :param cmap: name of the colormap
        :type cmap: str
        :param cut: adjust the contour extrema to max(abs(data))*cut
        :type cut: float
        """
        if std:
            avg = self.data[ifield, ...].std(axis=0)
            if ic:
                avg_ic = self.data_ic[ifield, ...].std(axis=0)
        else:
            avg = self.data[ifield, ...].mean(axis=0)
            if ic:
                avg_ic = self.data_ic[ifield, ...].mean(axis=0)
        vmin = -max(abs(avg.max()), abs(avg.min()))
        vmin = cut * vmin
        vmax = -vmin
        cs = np.linspace(vmin, vmax, levels)

        if self.surftype == 'phi_constant':
            if self.n_theta_plot == self.n_theta_max:
                th = np.linspace(np.pi / 2., -np.pi / 2., self.n_theta_max)
                fig = plt.figure(figsize=(4, 8))
                th0 = th
            else:
                th0 = np.linspace(np.pi / 2., -np.pi / 2., self.n_theta_max)
                th1 = np.linspace(np.pi / 2., 3. * np.pi / 2.,
                                  self.n_theta_max)
                th = np.concatenate((th0, th1))
                fig = plt.figure(figsize=(6.5, 6))
                # Plotting trick using th0
                th0 = np.linspace(np.pi / 2, np.pi / 2 + 2. * np.pi,
                                  2 * self.n_theta_max)
            rr, tth = np.meshgrid(self.radius, th)
            xx = rr * np.cos(tth)
            yy = rr * np.sin(tth)
            xxout = rr.max() * np.cos(th0)
            yyout = rr.max() * np.sin(th0)
            xxin = rr.min() * np.cos(th0)
            yyin = rr.min() * np.sin(th0)
            if ic:
                rr, tth = np.meshgrid(self.radius_ic, th)
                xx_ic = rr * np.cos(tth)
                yy_ic = rr * np.sin(tth)
        elif self.surftype == 'r_constant':
            th = np.linspace(np.pi / 2., -np.pi / 2., self.n_theta_max)
            phi = np.linspace(-np.pi, np.pi, self.n_phi_tot)
            ttheta, pphi = np.meshgrid(th, phi)
            xx, yy = hammer2cart(ttheta, pphi)
            xxout, yyout = hammer2cart(th, -np.pi)
            xxin, yyin = hammer2cart(th, np.pi)
            fig = plt.figure(figsize=(8, 4))
        elif self.surftype == 'theta_constant':
            phi = np.linspace(0., 2. * np.pi, self.n_phi_tot)
            rr, pphi = np.meshgrid(self.radius, phi)
            xx = rr * np.cos(pphi)
            yy = rr * np.sin(pphi)
            xxout = rr.max() * np.cos(pphi)
            yyout = rr.max() * np.sin(pphi)
            xxin = rr.min() * np.cos(pphi)
            yyin = rr.min() * np.sin(pphi)
            fig = plt.figure(figsize=(6, 6))
        elif self.surftype == '3d volume':
            self.data = self.data[ifield, ..., 0]
            th = np.linspace(np.pi / 2., -np.pi / 2., self.n_theta_max)
            phi = np.linspace(-np.pi, np.pi, self.n_phi_tot)
            ttheta, pphi = np.meshgrid(th, phi)
            xx, yy = hammer2cart(ttheta, pphi)
            xxout, yyout = hammer2cart(th, -np.pi)
            xxin, yyin = hammer2cart(th, np.pi)
            fig = plt.figure(figsize=(8, 4))

        fig.subplots_adjust(top=0.99, right=0.99, bottom=0.01, left=0.01)
        ax = fig.add_subplot(111, frameon=False)
        ax.contourf(xx, yy, avg, cs, cmap=cmap, extend='both')
        if ic:
            ax.contourf(xx_ic, yy_ic, avg_ic, cs, cmap=cmap, extend='both')
        ax.plot(xxout, yyout, 'k-', lw=1.5)
        ax.plot(xxin, yyin, 'k-', lw=1.5)
        ax.axis('off')