예제 #1
0
def plt_qlook_image(imres, figdir=None, verbose=True, synoptic=False):
    from matplotlib import pyplot as plt
    from sunpy import map as smap
    from sunpy import sun
    from matplotlib import colors
    import astropy.units as u
    if not figdir:
        figdir = './'
    nspw = len(set(imres['Spw']))
    plttimes = list(set(imres['BeginTime']))
    ntime = len(plttimes)
    # sort the imres according to time
    images = np.array(imres['ImageName'])
    btimes = Time(imres['BeginTime'])
    etimes = Time(imres['EndTime'])
    spws = np.array(imres['Spw'])
    suc = np.array(imres['Succeeded'])
    inds = btimes.argsort()
    images_sort = images[inds].reshape(ntime, nspw)
    btimes_sort = btimes[inds].reshape(ntime, nspw)
    suc_sort = suc[inds].reshape(ntime, nspw)
    spws_sort = spws[inds].reshape(ntime, nspw)
    if verbose:
        print '{0:d} figures to plot'.format(ntime)
    plt.ioff()
    fig = plt.figure(figsize=(8, 8))
    plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
    for i in range(ntime):
        plt.ioff()
        plt.clf()
        plttime = btimes_sort[i, 0]
        tofd = plttime.mjd - np.fix(plttime.mjd)
        suci = suc_sort[i]
        if not synoptic:
            if tofd < 16. / 24. or sum(
                    suci
            ) < nspw - 2:  # if time of the day is before 16 UT (and 24 UT), skip plotting (because the old antennas are not tracking)
                continue
        #fig=plt.figure(figsize=(9,6))
        #fig.suptitle('EOVSA @ '+plttime.iso[:19])
        if synoptic:
            fig.text(0.01,
                     0.98,
                     plttime.iso[:10],
                     color='w',
                     fontweight='bold',
                     fontsize=12,
                     ha='left')
        else:
            fig.text(0.01,
                     0.98,
                     plttime.iso[:19],
                     color='w',
                     fontweight='bold',
                     fontsize=12,
                     ha='left')
        if verbose:
            print 'Plotting image at: ', plttime.iso
        for n in range(nspw):
            plt.ioff()
            image = images_sort[i, n]
            #fig.add_subplot(nspw/3, 3, n+1)
            fig.add_subplot(nspw / 2, 2, n + 1)
            if suci[n]:
                try:
                    eomap = smap.Map(image)
                except:
                    continue
                sz = eomap.data.shape
                if len(sz) == 4:
                    eomap.data = eomap.data.reshape((sz[2], sz[3]))
                eomap.data[np.isnan(eomap.data)] = 0.0
                #resample the image for plotting
                dim = u.Quantity([256, 256], u.pixel)
                eomap = eomap.resample(dim)
                eomap.plot_settings['cmap'] = plt.get_cmap('jet')
                eomap.plot_settings['norm'] = colors.Normalize(vmin=-1e5,
                                                               vmax=1e6)
                eomap.plot()
                if not synoptic:
                    eomap.draw_limb()
                eomap.draw_grid()
                ax = plt.gca()
                ax.set_xlim([-1080, 1080])
                ax.set_ylim([-1080, 1080])
                spwran = spws_sort[i, n]
                freqran = [int(s) * 0.5 + 2.9 for s in spwran.split('~')]
                ax.text(0.98,
                        0.01,
                        '{0:.1f} - {1:.1f} GHz'.format(freqran[0], freqran[1]),
                        color='w',
                        transform=ax.transAxes,
                        fontweight='bold',
                        ha='right')
                ax.set_title(' ')
                #ax.set_title('spw '+spws_sort[i,n])
                #ax.text(0.01,0.02, plttime.isot,transform=ax.transAxes,color='white')
                ax.set_xlabel('')
                ax.set_ylabel('')
                ax.set_xticklabels([''])
                ax.set_yticklabels([''])
            else:
                #make an empty map
                data = np.zeros((512, 512))
                header = {
                    "DATE-OBS": plttime.isot,
                    "EXPTIME": 0.,
                    "CDELT1": 5.,
                    "NAXIS1": 512,
                    "CRVAL1": 0.,
                    "CRPIX1": 257,
                    "CUNIT1": "arcsec",
                    "CTYPE1": "HPLN-TAN",
                    "CDELT2": 5.,
                    "NAXIS2": 512,
                    "CRVAL2": 0.,
                    "CRPIX2": 257,
                    "CUNIT2": "arcsec",
                    "CTYPE2": "HPLT-TAN",
                    "HGLT_OBS":
                    sun.heliographic_solar_center(plttime)[1].value,
                    "HGLN_OBS": 0.,
                    "RSUN_OBS":
                    sun.solar_semidiameter_angular_size(plttime).value,
                    "RSUN_REF": sun.constants.radius.value,
                    "DSUN_OBS":
                    sun.sunearth_distance(plttime).to(u.meter).value,
                }
                eomap = smap.Map(data, header)
                eomap.plot_settings['cmap'] = plt.get_cmap('jet')
                eomap.plot_settings['norm'] = colors.Normalize(vmin=-1e5,
                                                               vmax=1e6)
                eomap.plot()
                if not synoptic:
                    eomap.draw_limb()
                eomap.draw_grid()
                ax = plt.gca()
                ax.set_xlim([-1080, 1080])
                ax.set_ylim([-1080, 1080])
                #ax.set_title('spw '+spwran+'( )'))
                spwran = spws_sort[i, n]
                freqran = [int(s) * 0.5 + 2.9 for s in spwran.split('~')]
                spwran = spws_sort[i, n]
                #ax.set_title('{0:.1f} - {1:.1f} GHz'.format(freqran[0],freqran[1]))
                ax.text(0.98,
                        0.01,
                        '{0:.1f} - {1:.1f} GHz'.format(freqran[0], freqran[1]),
                        color='w',
                        transform=ax.transAxes,
                        fontweight='bold',
                        ha='right')
                ax.set_title(' ')

                #ax.text(0.01,0.02, plttime.isot,transform=ax.transAxes,color='white')
                ax.set_xlabel('')
                ax.set_ylabel('')
                ax.set_xticklabels([''])
                ax.set_yticklabels([''])
        fig_tdt = plttime.to_datetime()
        if synoptic:
            fig_subdir = fig_tdt.strftime("%Y/")
            figname = 'eovsa_qlimg_' + plttime.iso[:10].replace('-',
                                                                '') + '.png'
        else:
            fig_subdir = fig_tdt.strftime("%Y/%m/%d/")
            figname = 'eovsa_qlimg_' + plttime.isot.replace(':', '').replace(
                '-', '')[:15] + '.png'
        figdir_ = figdir + fig_subdir
        if not os.path.exists(figdir_):
            os.makedirs(figdir_)
        if verbose:
            print 'Saving plot to :' + figdir_ + figname
        plt.savefig(figdir_ + figname)
    plt.close(fig)
예제 #2
0
def plt_qlook_image(imres, figdir=None, verbose=True, synoptic=False):
    from matplotlib import pyplot as plt
    from sunpy import map as smap
    from sunpy import sun
    from matplotlib import colors
    import astropy.units as u
    from suncasa.utils import plot_mapX as pmX
    # from matplotlib import gridspec as gridspec

    if not figdir:
        figdir = './'

    nspw = len(set(imres['Spw']))
    plttimes = list(set(imres['BeginTime']))
    ntime = len(plttimes)
    # sort the imres according to time
    images = np.array(imres['ImageName'])
    btimes = Time(imres['BeginTime'])
    etimes = Time(imres['EndTime'])
    spws = np.array(imres['Spw'])
    suc = np.array(imres['Succeeded'])
    inds = btimes.argsort()
    images_sort = images[inds].reshape(ntime, nspw)
    btimes_sort = btimes[inds].reshape(ntime, nspw)
    suc_sort = suc[inds].reshape(ntime, nspw)
    if verbose:
        print('{0:d} figures to plot'.format(ntime))
    plt.ioff()
    fig = plt.figure(figsize=(8, 8))

    plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
    axs = []
    ims = []
    pltst = 0
    for i in range(ntime):
        plt.ioff()
        plttime = btimes_sort[i, 0]
        tofd = plttime.mjd - np.fix(plttime.mjd)
        suci = suc_sort[i]
        if not synoptic:
            if tofd < 16. / 24. or sum(
                    suci
            ) < nspw - 2:  # if time of the day is before 16 UT (and 24 UT), skip plotting (because the old antennas are not tracking)
                continue
            else:
                if pltst == 0:
                    i0 = i
                    pltst = 1
        else:
            if pltst == 0:
                i0 = i
                pltst = 1
        if i == i0:
            if synoptic:
                timetext = fig.text(0.01,
                                    0.98,
                                    plttime.iso[:10],
                                    color='w',
                                    fontweight='bold',
                                    fontsize=12,
                                    ha='left')
            else:
                timetext = fig.text(0.01,
                                    0.98,
                                    plttime.iso[:19],
                                    color='w',
                                    fontweight='bold',
                                    fontsize=12,
                                    ha='left')
        else:
            if synoptic:
                timetext.set_text(plttime.iso[:10])
            else:
                timetext.set_text(plttime.iso[:19])
        if verbose:
            print('Plotting image at: ', plttime.iso)
        for n in range(nspw):
            plt.ioff()
            if i == i0:
                if nspw == 1:
                    ax = fig.add_subplot(111)
                else:
                    ax = fig.add_subplot(nspw / 2, 2, n + 1)
                axs.append(ax)
            else:
                ax = axs[n]
            image = images_sort[i, n]
            if suci[n] or os.path.exists(image):
                try:
                    eomap = smap.Map(image)
                except:
                    continue
                data = eomap.data
                sz = data.shape
                if len(sz) == 4:
                    data = data.reshape((sz[2], sz[3]))
                data[np.isnan(data)] = 0.0
                # add a basin flux to the image to avoid negative values
                data = data + 0.8e5
                data[data < 0] = 0.0
                data = np.sqrt(data)
                eomap = smap.Map(data, eomap.meta)
                # resample the image for plotting
                dim = u.Quantity([256, 256], u.pixel)
                eomap = eomap.resample(dim)
            else:
                # make an empty map
                data = np.zeros((256, 256))
                header = {
                    "DATE-OBS": plttime.isot,
                    "EXPTIME": 0.,
                    "CDELT1": 10.,
                    "NAXIS1": 256,
                    "CRVAL1": 0.,
                    "CRPIX1": 128.5,
                    "CUNIT1": "arcsec",
                    "CTYPE1": "HPLN-TAN",
                    "CDELT2": 10.,
                    "NAXIS2": 256,
                    "CRVAL2": 0.,
                    "CRPIX2": 128.5,
                    "CUNIT2": "arcsec",
                    "CTYPE2": "HPLT-TAN",
                    "HGLT_OBS":
                    sun.heliographic_solar_center(plttime)[1].value,
                    "HGLN_OBS": 0.,
                    "RSUN_OBS":
                    sun.solar_semidiameter_angular_size(plttime).value,
                    "RSUN_REF": sun.constants.radius.value,
                    "DSUN_OBS":
                    sun.sunearth_distance(plttime).to(u.meter).value,
                }
                eomap = smap.Map(data, header)
            if i == i0:
                eomap_ = pmX.Sunmap(eomap)
                # im = eomap_.imshow(axes=ax, cmap='jet', norm=colors.LogNorm(vmin=0.1, vmax=1e8))
                im = eomap_.imshow(axes=ax,
                                   cmap='jet',
                                   norm=colors.Normalize(vmin=150, vmax=700))
                ims.append(im)
                if not synoptic:
                    eomap_.draw_limb(axes=ax)
                eomap_.draw_grid(axes=ax)
                ax.set_xlim([-1080, 1080])
                ax.set_ylim([-1080, 1080])
                try:
                    cfreq = eomap.meta['crval3'] / 1.0e9
                    bdwid = eomap.meta['cdelt3'] / 1.0e9
                    ax.text(0.98,
                            0.01,
                            '{0:.1f} - {1:.1f} GHz'.format(
                                cfreq - bdwid / 2.0, cfreq + bdwid / 2.0),
                            color='w',
                            transform=ax.transAxes,
                            fontweight='bold',
                            ha='right')
                except:
                    pass
                ax.set_title(' ')
                ax.set_xlabel('')
                ax.set_ylabel('')
                ax.set_xticklabels([''])
                ax.set_yticklabels([''])
            else:
                ims[n].set_data(eomap.data)

        fig_tdt = plttime.to_datetime()
        if synoptic:
            fig_subdir = fig_tdt.strftime("%Y/")
            figname = 'eovsa_qlimg_' + plttime.iso[:10].replace('-',
                                                                '') + '.png'
        else:
            fig_subdir = fig_tdt.strftime("%Y/%m/%d/")
            figname = 'eovsa_qlimg_' + plttime.isot.replace(':', '').replace(
                '-', '')[:15] + '.png'
        figdir_ = figdir + fig_subdir
        if not os.path.exists(figdir_):
            os.makedirs(figdir_)
        if verbose:
            print('Saving plot to :' + figdir_ + figname)

        plt.savefig(figdir_ + figname)
    plt.close(fig)
예제 #3
0
def plt_qlook_image(imres, figdir=None, verbose=True):
    from matplotlib import pyplot as plt
    from sunpy import map as smap
    from sunpy import sun
    import astropy.units as u
    if not figdir:
        figdir = './'
    nspw = len(set(imres['Spw']))
    plttimes = list(set(imres['BeginTime']))
    ntime = len(plttimes)
    # sort the imres according to time
    images = np.array(imres['ImageName'])
    btimes = Time(imres['BeginTime'])
    etimes = Time(imres['EndTime'])
    spws = np.array(imres['Spw'])
    suc = np.array(imres['Succeeded'])
    inds = btimes.argsort()
    images_sort = images[inds].reshape(ntime, nspw)
    btimes_sort = btimes[inds].reshape(ntime, nspw)
    suc_sort = suc[inds].reshape(ntime, nspw)
    spws_sort = spws[inds].reshape(ntime, nspw)
    if verbose:
        print '{0:d} figures to plot'.format(ntime)
    for i in range(ntime):
        #for i in range(1):
        plt.ioff()
        fig = plt.figure(figsize=(11, 6))
        plttime = btimes_sort[i, 0]
        fig.suptitle('EOVSA @ ' + plttime.iso[:19])
        if verbose:
            print 'Plotting image at: ', plttime.iso
            suci = suc_sort[i]
        for n in range(nspw):
            plt.ioff()
            image = images_sort[i, n]
            fig.add_subplot(nspw / 3, 3, n + 1)
            if suci[n]:
                try:
                    eomap = smap.Map(image)
                except:
                    continue
                sz = eomap.data.shape
                if len(sz) == 4:
                    eomap.data = eomap.data.reshape((sz[2], sz[3]))
                eomap.plot_settings['cmap'] = plt.get_cmap('jet')
                eomap.plot()
                eomap.draw_limb()
                eomap.draw_grid()
                ax = plt.gca()
                ax.set_xlim([-1050, 1050])
                ax.set_ylim([-1050, 1050])
                ax.set_title('spw ' + spws_sort[i, n])
                #ax.text(0.01,0.02, plttime.isot,transform=ax.transAxes,color='white')
                if n != nspw - 3:
                    ax.set_xlabel('')
                    ax.set_ylabel('')
                    ax.set_xticklabels([''])
                    ax.set_yticklabels([''])
            else:
                #make an empty map
                data = np.zeros((512, 512))
                header = {
                    "DATE-OBS": plttime.isot,
                    "EXPTIME": 0.,
                    "CDELT1": 5.,
                    "NAXIS1": 512,
                    "CRVAL1": 0.,
                    "CRPIX1": 257,
                    "CUNIT1": "arcsec",
                    "CTYPE1": "HPLN-TAN",
                    "CDELT2": 5.,
                    "NAXIS2": 512,
                    "CRVAL2": 0.,
                    "CRPIX2": 257,
                    "CUNIT2": "arcsec",
                    "CTYPE2": "HPLT-TAN",
                    "HGLT_OBS":
                    sun.heliographic_solar_center(plttime)[1].value,
                    "HGLN_OBS": 0.,
                    "RSUN_OBS":
                    sun.solar_semidiameter_angular_size(plttime).value,
                    "RSUN_REF": sun.constants.radius.value,
                    "DSUN_OBS":
                    sun.sunearth_distance(plttime).to(u.meter).value,
                }
                eomap = smap.Map(data, header)
                eomap.plot_settings['cmap'] = plt.get_cmap('jet')
                eomap.plot()
                eomap.draw_limb()
                eomap.draw_grid()
                ax = plt.gca()
                ax.set_xlim([-1050, 1050])
                ax.set_ylim([-1050, 1050])
                ax.set_title('spw ' + spws_sort[i, n])
                #ax.text(0.01,0.02, plttime.isot,transform=ax.transAxes,color='white')
                if n != 6:
                    ax.set_xlabel('')
                    ax.set_ylabel('')
                    ax.set_xticklabels([''])
                    ax.set_yticklabels([''])
        figname = 'eovsa_qlimg_' + plttime.isot.replace(':', '').replace(
            '-', '')[:15] + '.png'
        fig_tdt = plttime.to_datetime()
        fig_subdir = fig_tdt.strftime("%Y/%m/%d/")
        figdir_ = figdir + fig_subdir
        if not os.path.exists(figdir_):
            os.makedirs(figdir_)
        if verbose:
            print 'Saving plot to :' + figdir_ + figname
        plt.savefig(figdir_ + figname)
        plt.close(fig)