Exemple #1
0
def test_diffrot_submap_pad(aia171_test_submap):
    aia_srot = diffrot_map(aia171_test_submap, -0.5 * u.day, pad=True)
    assert aia_srot.dimensions >= aia171_test_submap.dimensions
    assert (aia171_test_submap.date -
            timedelta(days=0.5)) - aia_srot.date < timedelta(seconds=1)
    assert aia_srot.meta['naxis1'] == 35
    assert aia_srot.meta['naxis2'] == 18
def test_diffrot_submap_pad(aia171_test_submap):
    aia_srot = diffrot_map(aia171_test_submap, dt=-0.5 * u.day, pad=True)
    assert aia_srot.dimensions >= aia171_test_submap.dimensions
    assert (aia171_test_submap.date -
            TimeDelta(0.5 * u.day)) - aia_srot.date < TimeDelta(1 * u.second)
    assert aia_srot.meta['naxis1'] == 35
    assert aia_srot.meta['naxis2'] == 18
def test_diffrot_allen_submap_pad(aia171_test_submap):
    aia_srot = diffrot_map(aia171_test_submap, dt=-0.5 * u.day, pad=True, rot_type='allen')
    assert aia_srot.dimensions >= aia171_test_submap.dimensions
    assert (aia171_test_submap.date - timedelta(days=0.5)) - aia_srot.date < timedelta(seconds=1)
    assert aia_srot.meta['naxis1'] == 35
    assert aia_srot.meta['naxis2'] == 18
def test_diffrot_submap(aia171_test_submap):
    # Test a submap without padding
    aia_srot = diffrot_map(aia171_test_submap, '2011-02-14T12:00:00')
    assert aia_srot.dimensions == aia171_test_submap.dimensions
    assert (aia171_test_submap.date - timedelta(days=0.5)) - aia_srot.date < timedelta(seconds=1)
def test_diffrot_map(aia171_test_map):
    # Test a submap without padding
    aia_srot = diffrot_map(aia171_test_map, dt=-5 * u.day)
    assert aia_srot.dimensions == aia171_test_map.dimensions
    assert (aia171_test_map.date - timedelta(days=5)) - aia_srot.date < timedelta(seconds=1)
Exemple #6
0
def datacube(flist_chunk):
    # rebin region to desired fraction
    def rebin(a, *args):
        shape = a.shape
        lenShape = len(shape)
        factor = np.asarray(shape) / np.asarray(args)
        evList = ['a.reshape('] + \
                 ['args[%d],factor[%d],'%(i,i) for i in range(lenShape)] + \
                 [')'] + ['.mean(%d)'%(i+1) for i in range(lenShape)]
        return eval(''.join(evList))

    nf1 = len(flist_chunk)

    exposure = np.empty((nf1))
    timestamp = np.empty((nf1))
    visAvg = np.empty((mapShape[0], mapShape[1]))

    # image data is int16
    dCube = np.empty((mapShape[0], mapShape[1], nf1), dtype=np.int16)

    start_sub = timer()
    T1 = 0

    count = 0
    dimCount = 0

    # loop through datacube and extract timeseries, timestamps, exposures
    for filename in flist_chunk:
        smap = Map(filename).submap(c3, c4)
        exposure[count] = (smap.exposure_time).value
        timestamp[count] = Time(smap.date).jd
        dmap = diffrot_map(smap, time=dt0).data
        if dmap.shape != mapShape:
            dimenDiff = np.array(dmap.shape) - np.array(mapShape)
            dmap = dmap[:dmap.shape[0] - dimenDiff[0], :dmap.shape[1] -
                        dimenDiff[1]]
            dimCount += 1
        visAvg += (dmap / (smap.exposure_time).value)
        dCube[:, :, count] = dmap
        count += 1

        # estimate time remaining and print to screen
        T = timer()
        T2 = T - T1
        if count == 0:
            T_init = T - start_sub
            T_est = T_init * nf1
        else:
            T_est = T2 * (nf1 - count)
        T_min, T_sec = divmod(T_est, 60)
        T_hr, T_min = divmod(T_min, 60)

        print("Thread %i on row %i/%i, ETR: %i:%.2i:%.2i" %
              (rank, count, nf1, T_hr, T_min, T_sec),
              flush=True)
        T1 = T

    # ---- trim dataCube and visual image
    dCube = dCube[diffLatPix:-diffLatPix, xminI:-xminF]
    visAvg = visAvg[diffLatPix:-diffLatPix, xminI:-xminF]

    np.save('%s/chunk_%i_of_%i' % (processed_dir, rank + 1, size), dCube)

    print('Processor: %i, Dimension Errors: %i' % (rank + 1, dimCount),
          flush=True)

    #return dCube
    return exposure, timestamp, visAvg
def test_diffrot_noinputs(aia171_test_map):
    with pytest.raises(ValueError) as exc_info:
        diffrot_map(aia171_test_map)
    assert 'Either a time or an interval (`dt=`) needs to be provided' in str(exc_info.value)
def test_diffrot_submap(aia171_test_submap):
    # Test a submap without padding
    aia_srot = diffrot_map(aia171_test_submap, '2011-02-14T12:00:00')
    assert aia_srot.dimensions == aia171_test_submap.dimensions
    assert (aia171_test_submap.date - timedelta(days=0.5)) - aia_srot.date < timedelta(seconds=1)
aiamapcube = sunpy.map.Map(AIA_193_CUTOUT01_IMAGE,
                           AIA_193_CUTOUT02_IMAGE,
                           AIA_193_CUTOUT03_IMAGE,
                           cube=True)

############################################################################
# In case of running difference, we loop through all the maps in the
# aiamapcube and differentially rotate each map in the mapcube
# with respect to the previous map
# while in case of base difference we differentially
# rotate each map in the mapcube to the time of the base map.
# We then store all the difference maps in a list.
base_diffmap = []
running_diffmap = []
for i, map_i in enumerate(aiamapcube[1:]):
    aiamap_rot = diffrot.diffrot_map(map_i, time=aiamapcube[0].date)
    aiamapcube_rot = diffrot.diffrot_map(aiamapcube[i + 1],
                                         time=aiamapcube[i].date)
    diffdata = map_i.data - aiamap_rot.data
    smap_base = sunpy.map.Map(diffdata, map_i.meta)
    diffdata = aiamapcube_rot.data - map_i.data
    smap_run = sunpy.map.Map(diffdata, map_i.meta)
    smap_base.plot_settings['cmap'] = plt.get_cmap('Greys_r')
    smap_base.plot_settings['norm'] = colors.LogNorm(100, smap_base.max())
    smap_run.plot_settings['cmap'] = plt.get_cmap('Greys_r')
    smap_run.plot_settings['norm'] = colors.LogNorm(100, smap_run.max())
    base_diffmap.append(smap_base)
    running_diffmap.append(smap_run)

############################################################################
# This plots the original mapcube
def test_diffrot_submap(aia171_test_submap):
    # Test a submap without padding
    aia_srot = diffrot_map(aia171_test_submap, '2011-02-14T12:00:00')
    assert aia_srot.dimensions == aia171_test_submap.dimensions
    assert (aia171_test_submap.date - TimeDelta(0.5*u.day)) - aia_srot.date < TimeDelta(1*u.second)
def test_diffrot_map(aia171_test_map):
    # Test a submap without padding
    aia_srot = diffrot_map(aia171_test_map, dt=-5 * u.day)
    assert aia_srot.dimensions == aia171_test_map.dimensions
    assert (aia171_test_map.date - TimeDelta(5*u.day)) - aia_srot.date < TimeDelta(1*u.second)
# We create the MapCube using the AIA_193_CUTOUT sample data.
# To create a MapCube, we can call Map directly and add in a keyword to output a MapCube instead.
aiamapcube = sunpy.map.Map(AIA_193_CUTOUT01_IMAGE, AIA_193_CUTOUT02_IMAGE,
                           AIA_193_CUTOUT03_IMAGE, cube=True)

############################################################################
# In case of running difference, we loop through all the maps in the
# aiamapcube and differentially rotate each map in the mapcube
# with respect to the previous map
# while in case of base difference we differentially
# rotate each map in the mapcube to the time of the base map.
# We then store all the difference maps in a list.
base_diffmap = []
running_diffmap = []
for i, map_i in enumerate(aiamapcube[1:]):
    aiamap_rot = diffrot.diffrot_map(map_i, time=aiamapcube[0].date)
    aiamapcube_rot = diffrot.diffrot_map(aiamapcube[i+1], time=aiamapcube[i].date)
    diffdata = map_i.data - aiamap_rot.data
    smap_base = sunpy.map.Map(diffdata, map_i.meta)
    diffdata = aiamapcube_rot.data - map_i.data
    smap_run = sunpy.map.Map(diffdata, map_i.meta)
    smap_base.plot_settings['cmap'] = plt.get_cmap('Greys_r')
    smap_base.plot_settings['norm'] = colors.LogNorm(100, smap_base.max())
    smap_run.plot_settings['cmap'] = plt.get_cmap('Greys_r')
    smap_run.plot_settings['norm'] = colors.LogNorm(100, smap_run.max())
    base_diffmap.append(smap_base)
    running_diffmap.append(smap_run)

############################################################################
# This plots the original mapcube
aiamapcube.peek()
# We create the MapSequence using the AIA_193_CUTOUT sample data.
# To create a MapSequence, we can call Map directly and add in a keyword to output a MapSequence instead.
aiamapseq = sunpy.map.Map(AIA_193_CUTOUT01_IMAGE, AIA_193_CUTOUT02_IMAGE,
                          AIA_193_CUTOUT03_IMAGE, sequence=True)

############################################################################
# In case of running difference, we loop through all the maps in the
# aiamapseq and differentially rotate each map in the MapSequence
# with respect to the previous map
# while in case of base difference we differentially
# rotate each map in the MapSequence to the time of the base map.
# We then store all the difference maps in a list.
base_diffmap = []
running_diffmap = []
for i, map_i in enumerate(aiamapseq[1:]):
    aiamap_rot = diffrot.diffrot_map(map_i, time=aiamapseq[0].date)
    aiamapseq_rot = diffrot.diffrot_map(map_i, time=aiamapseq[i].date)
    diffdata = map_i.data - aiamap_rot.data
    smap_base = sunpy.map.Map(diffdata, map_i.meta)
    diffdata = aiamapseq_rot.data - map_i.data
    smap_run = sunpy.map.Map(diffdata, map_i.meta)
    smap_base.plot_settings['cmap'] = plt.get_cmap('Greys_r')
    smap_base.plot_settings['norm'] = colors.LogNorm(100, smap_base.max())
    smap_run.plot_settings['cmap'] = plt.get_cmap('Greys_r')
    smap_run.plot_settings['norm'] = colors.LogNorm(100, smap_run.max())
    base_diffmap.append(smap_base)
    running_diffmap.append(smap_run)

############################################################################
# This plots the original MapSequence
aiamapseq.peek()
aiamapseq = sunpy.map.Map(AIA_193_CUTOUT01_IMAGE,
                          AIA_193_CUTOUT02_IMAGE,
                          AIA_193_CUTOUT03_IMAGE,
                          sequence=True)

############################################################################
# In case of running difference, we loop through all the maps in the
# aiamapseq and differentially rotate each map in the MapSequence
# with respect to the previous map
# while in case of base difference we differentially
# rotate each map in the MapSequence to the time of the base map.
# We then store all the difference maps in a list.
base_diffmap = []
running_diffmap = []
for i, map_i in enumerate(aiamapseq[1:]):
    aiamap_rot = diffrot.diffrot_map(map_i, time=aiamapseq[0].date)
    aiamapseq_rot = diffrot.diffrot_map(map_i, time=aiamapseq[i].date)
    diffdata = map_i.data - aiamap_rot.data
    smap_base = sunpy.map.Map(diffdata, map_i.meta)
    diffdata = aiamapseq_rot.data - map_i.data
    smap_run = sunpy.map.Map(diffdata, map_i.meta)
    smap_base.plot_settings['cmap'] = plt.get_cmap('Greys_r')
    smap_base.plot_settings['norm'] = colors.LogNorm(100, smap_base.max())
    smap_run.plot_settings['cmap'] = plt.get_cmap('Greys_r')
    smap_run.plot_settings['norm'] = colors.LogNorm(100, smap_run.max())
    base_diffmap.append(smap_base)
    running_diffmap.append(smap_run)

############################################################################
# This plots the original MapSequence
aiamapseq.peek()
def test_diffrot_manyinputs(aia171_test_map):
    with pytest.raises(ValueError) as exc_info:
        diffrot_map(aia171_test_map, '2010-01-01', dt=3 * u.hour)
    assert 'Only a time or an interval is accepted' in str(exc_info.value)
def test_diffrot_noinputs(aia171_test_map):
    with pytest.raises(ValueError) as exc_info:
        diffrot_map(aia171_test_map)
    assert 'Either a time or an interval (`dt=`) needs to be provided' in str(exc_info.value)
def test_diffrot_allen_submap_pad(aia171_test_submap):
    aia_srot = diffrot_map(aia171_test_submap, dt=-0.5 * u.day, pad=True, rot_type='allen')
    assert aia_srot.dimensions >= aia171_test_submap.dimensions
    assert (aia171_test_submap.date - TimeDelta(0.5*u.day)) - aia_srot.date < TimeDelta(1*u.second)
    assert aia_srot.meta['naxis1'] == 35
    assert aia_srot.meta['naxis2'] == 18
def test_diffrot_map(aia171_test_map):
    # Test a submap without padding
    aia_srot = diffrot_map(aia171_test_map, dt=-5 * u.day)
    assert aia_srot.dimensions == aia171_test_map.dimensions
    assert (aia171_test_map.date - timedelta(days=5)) - aia_srot.date < timedelta(seconds=1)
def test_diffrot_map(aia171_test_map):
    # Test a submap without padding
    aia_srot = diffrot_map(aia171_test_map, dt=-5 * u.day)
    assert aia_srot.dimensions == aia171_test_map.dimensions
    assert (aia171_test_map.date -
            TimeDelta(5 * u.day)) - aia_srot.date < TimeDelta(1 * u.second)
def test_diffrot_manyinputs(aia171_test_map):
    with pytest.raises(ValueError) as exc_info:
        diffrot_map(aia171_test_map, '2010-01-01', dt=3 * u.hour)
    assert 'Only a time or an interval is accepted' in str(exc_info.value)
def test_diffrot_submap(aia171_test_submap):
    # Test a submap without padding
    aia_srot = diffrot_map(aia171_test_submap, '2011-02-14T12:00:00')
    assert aia_srot.dimensions == aia171_test_submap.dimensions
    assert (aia171_test_submap.date -
            TimeDelta(0.5 * u.day)) - aia_srot.date < TimeDelta(1 * u.second)
Exemple #22
0
def pltEovsaQlookImageSeries(timobjs,
                             spws,
                             vmaxs,
                             vmins,
                             aiawave,
                             bd,
                             fig=None,
                             axs=None,
                             imgoutdir=None,
                             overwrite=False,
                             verbose=False):
    from astropy.visualization.stretch import AsinhStretch
    from astropy.visualization import ImageNormalize
    plt.ioff()
    imgfiles = []
    dpi = 512. / 4
    aiaDataSource = {
        "0094": 8,
        "0193": 11,
        "0335": 14,
        # "4500": 17,
        "0171": 10,
        "0304": 13,
        "0131": 9,
        "1700": 16,
        "0211": 12,
        # "1600": 15,
        "_HMIcont": 18,
        "_HMImag": 19
    }

    tmjd = timobjs.mjd
    tmjd_base = np.floor(tmjd)
    tmjd_hr = (tmjd - tmjd_base) * 24
    for tidx, timobj in enumerate(tqdm(timobjs)):
        dateobj = timobj.to_datetime()
        timestr = dateobj.strftime("%Y-%m-%dT%H:%M:%SZ")
        tstrname = dateobj.strftime("%Y%m%dT%H%M%SZ")
        datestrdir = dateobj.strftime("%Y/%m/%d/")
        imgindir = imgfitsdir + datestrdir
        timobj_prevday = Time(tmjd[tidx] - 1, format='mjd')
        dateobj_prevday = timobj_prevday.to_datetime()
        datestrdir_prevday = dateobj_prevday.strftime("%Y/%m/%d/")
        imgindir_prevday = imgfitsdir + datestrdir_prevday

        # if not os.path.exists(imgindir): os.makedirs(imgindir)
        cmap = cm_smap.get_cmap('sdoaia304')

        if verbose:
            print('Processing EOVSA images for date {}'.format(
                dateobj.strftime('%Y-%m-%d')))
        key, sourceid = aiawave, aiaDataSource[aiawave]
        s, sp = bd, spws[bd]

        figoutname = os.path.join(
            imgoutdir,
            'eovsa_bd{:02d}_aia{}_{}.jpg'.format(s + 1, key, tstrname))

        if overwrite or (not os.path.exists(figoutname)):
            ax = axs[0]
            ax.cla()
            spwstr = '-'.join(
                ['{:02d}'.format(int(sp_)) for sp_ in sp.split('~')])
            t_hr = tmjd_hr[tidx]
            t_hr_st_blend = 2.0
            t_hr_ed_blend = 14.0
            if t_hr <= 8.0:
                eofile = imgindir_prevday + 'eovsa_{}.spw{}.tb.disk.fits'.format(
                    dateobj_prevday.strftime('%Y%m%d'), spwstr)
            else:
                eofile = imgindir + 'eovsa_{}.spw{}.tb.disk.fits'.format(
                    dateobj.strftime('%Y%m%d'), spwstr)
            if not os.path.exists(eofile):
                continue

            stretch = AsinhStretch(a=0.15)
            norm = ImageNormalize(vmin=vmins[s],
                                  vmax=vmaxs[s],
                                  stretch=stretch)
            eomap = er.readfits(eofile)
            eomap = eomap.resample(u.Quantity(eomap.dimensions) / 2)
            eomap.data[np.isnan(eomap.data)] = 0.0
            eomap_rot = diffrot_map(eomap, time=timobj)
            offlimbidx = np.where(eomap_rot.data == eomap_rot.data[0, 0])
            eomap_rot.data[offlimbidx] = eomap.data[offlimbidx]

            t_hr = tmjd_hr[tidx]

            if t_hr_st_blend <= t_hr <= t_hr_ed_blend:
                if t_hr <= 8.0:
                    eofile_blend = imgindir + 'eovsa_{}.spw{}.tb.disk.fits'.format(
                        dateobj.strftime('%Y%m%d'), spwstr)
                    alpha = 1.0 - (t_hr - t_hr_st_blend) / (t_hr_ed_blend -
                                                            t_hr_st_blend)

                else:
                    eofile_blend = imgindir_prevday + 'eovsa_{}.spw{}.tb.disk.fits'.format(
                        dateobj_prevday.strftime('%Y%m%d'), spwstr)
                    alpha = (t_hr - t_hr_st_blend) / (t_hr_ed_blend -
                                                      t_hr_st_blend)
                alpha_blend = 1.0 - alpha
                eomap_blend = er.readfits(eofile_blend)
                eomap_blend = eomap_blend.resample(
                    u.Quantity(eomap_blend.dimensions) / 2)
                eomap_blend.data[np.isnan(eomap_blend.data)] = 0.0
                eomap_rot_blend = diffrot_map(eomap_blend, time=timobj)
                offlimbidx = np.where(
                    eomap_rot_blend.data == eomap_rot_blend.data[0, 0])
                eomap_rot_blend.data[offlimbidx] = eomap_blend.data[offlimbidx]
                eomap_rot_plt = smap.Map((eomap_rot.data * alpha +
                                          eomap_rot_blend.data * alpha_blend),
                                         eomap_rot.meta)
            else:
                eomap_rot_plt = eomap_rot

            # eomap_plt.plot(axes=ax, cmap=cmap, norm=norm)
            eomap_rot_plt.plot(axes=ax, cmap=cmap, norm=norm)

            ax.set_xlabel('')
            ax.set_ylabel('')
            ax.set_xticklabels([])
            ax.set_yticklabels([])
            ax.text(0.02,
                    0.02,
                    'EOVSA {:.1f} GHz  {}'.format(
                        eomap.meta['CRVAL3'] / 1e9,
                        dateobj.strftime('%d-%b-%Y %H:%M UT')),
                    transform=ax.transAxes,
                    color='w',
                    ha='left',
                    va='bottom',
                    fontsize=9)
            ax.text(0.98,
                    0.02,
                    'Max Tb {:.0f} K'.format(np.nanmax(eomap.data)),
                    transform=ax.transAxes,
                    color='w',
                    ha='right',
                    va='bottom',
                    fontsize=9)
            ax.set_xlim(-1227, 1227)
            ax.set_ylim(-1227, 1227)

            ax = axs[1]
            # for key, sourceid in aiaDataSource.items():
            ax.cla()

            if not os.path.exists(imgoutdir): os.makedirs(imgoutdir)
            sdourl = 'https://api.helioviewer.org/v2/getJP2Image/?date={}&sourceId={}'.format(
                timestr, sourceid)
            # print(sdourl)
            sdofile = os.path.join(imgoutdir,
                                   'AIA' + key + '.{}.jp2'.format(tstrname))
            if not os.path.exists(sdofile):
                urllib.request.urlretrieve(sdourl, sdofile)

            if not os.path.exists(sdofile): continue
            sdomap = smap.Map(sdofile)
            norm = colors.Normalize()
            # sdomap_ = pmX.Sunmap(sdomap)
            if "HMI" in key:
                cmap = plt.get_cmap('gray')
            else:
                cmap = cm_smap.get_cmap('sdoaia' + key.lstrip('0'))
            sdomap.plot(axes=ax, cmap=cmap, norm=norm)
            # sdomap_.imshow(axes=ax, cmap=cmap, norm=norm)
            # sdomap_.draw_limb(axes=ax, lw=0.25, alpha=0.5)
            # sdomap_.draw_grid(axes=ax, grid_spacing=10. * u.deg, lw=0.25)
            ax.set_xlabel('')
            ax.set_ylabel('')
            ax.set_xticklabels([])
            ax.set_yticklabels([])
            ax.text(0.02,
                    0.02,
                    '{}/{} {}  {}'.format(
                        sdomap.observatory,
                        sdomap.instrument.split(' ')[0], sdomap.measurement,
                        sdomap.date.strftime('%d-%b-%Y %H:%M UT')),
                    transform=ax.transAxes,
                    color='w',
                    ha='left',
                    va='bottom',
                    fontsize=9)
            ax.set_xlim(-1227, 1227)
            ax.set_ylim(-1227, 1227)
            fig.savefig(figoutname, dpi=np.int(dpi), quality=85)
        imgfiles.append(figoutname)

    return imgfiles
Exemple #23
0
c3 = SkyCoord((x1 - diffLon) * u.arcsec,
              y1 * u.arcsec,
              frame=frames.Helioprojective)
c4 = SkyCoord((x2 + diffLon) * u.arcsec,
              y2 * u.arcsec,
              frame=frames.Helioprojective)

# get middle frame subregion & time to anchor derotation
midmap = Map(flist[mid_file]).submap(c3, c4)
dt0 = midmap.date
mapShape = midmap.data.shape

# calculate pixels to trim based off of what actual derotation trims
# *for some reason this result is different than method above
diffMapI = diffrot_map(mapI.submap(c3, c4), time=dt0)
diffMapF = diffrot_map(mapF.submap(c3, c4), time=dt0)

xminindI = np.argmin(np.fliplr(diffMapI.data), axis=1)[diffLatPix:-diffLatPix]
xminindF = np.argmin(diffMapF.data, axis=1)[diffLatPix:-diffLatPix]

xminI = mapShape[1] - np.min(xminindI)
xminF = mapShape[1] - np.min(xminindF)

del new_mapcube1, mc_shifts, diffMapI, diffMapF

# specify which chunks should be handled by each processor
subcube = np.array_split(flist, size)[rank]

start = timer()