Ejemplo n.º 1
0
def DropDn_stackplt_handler(attr, old, new):
    import Tkinter
    import tkFileDialog
    if DropDn_stackplt.value == "Open":
        tkRoot = Tkinter.Tk()
        tkRoot.withdraw()  # Close the root window
        fin = tkFileDialog.askopenfilename(initialdir=database_dir,
                                           initialfile='stackplt-' + PlotID +
                                           '.npy')
        if fin:
            MkPlot_args_dict['stackpltfile'] = fin
            outfile = database_dir + 'MkPlot_args.json'
            DButil.updatejsonfile(outfile, MkPlot_args_dict)
            Div_info.text = """<p><b>Refresh</b> the <b>web page</b> to load new stackplot</p>"""
    elif DropDn_stackplt.value == "Save As":
        tkRoot = Tkinter.Tk()
        tkRoot.withdraw()  # Close the root window
        fout = tkFileDialog.asksaveasfilename(initialdir=database_dir,
                                              initialfile='stackplt-' +
                                              PlotID + '.npy')
        if fout:
            imgdict
            np.save(fout, imgdict)
            Div_info.text = """<p>Stack-plot saved to <b>{}</b></p>.""".format(
                fout)
    else:
        pass
Ejemplo n.º 2
0
def FitSlit(xx, yy, cutwidth, cutang, cutlength, s=None, method='Polyfit', ascending=True):
    if len(xx) <= 3 or method == 'Polyfit':
        '''polynomial fit'''
        out = DButil.polyfit(xx, yy, cutlength, len(xx) - 1 if len(xx) <= 3 else 2)
        xs, ys, posangs = out['xs'], out['ys'], out['posangs']
    else:
        if method == 'Param_Spline':
            '''parametic spline fit'''
            out = DButil.paramspline(xx, yy, cutlength, s=s)
            xs, ys, posangs = out['xs'], out['ys'], out['posangs']
        else:
            '''spline fit'''
            out = DButil.spline(xx, yy, cutlength, s=s)
            xs, ys, posangs = out['xs'], out['ys'], out['posangs']
    if not ascending and (method != 'Param_Spline' or len(xx) <= 3):
        xs, ys = xs[::-1], ys[::-1]
        posangs = posangs[::-1]
    dist = DButil.findDist(xs, ys)
    dists = np.cumsum(dist)
    posangs2 = posangs + np.pi / 2
    cutwidths = dists * np.tan(cutang) + cutwidth
    xs0 = xs - cutwidths / 2. * np.cos(posangs2)
    ys0 = ys - cutwidths / 2. * np.sin(posangs2)
    xs1 = xs + cutwidths / 2. * np.cos(posangs2)
    ys1 = ys + cutwidths / 2. * np.sin(posangs2)
    return {'xcen': xs, 'ycen': ys, 'xs0': xs0, 'ys0': ys0, 'xs1': xs1, 'ys1': ys1, 'cutwidth': cutwidths,
            'posangs': posangs, 'posangs2': posangs2, 'dist': dists}
Ejemplo n.º 3
0
def tab2_panel_XCorr_update():
    global clickmode
    if tab2_BUT_XCorr.label == 'XCorr':
        clickmode = 'doubleclick'
        But_ClickMode.label = 'ClickMode: Double'
        tab2_BUT_XCorr.label = 'GoXCorr'
        tab2_panel_Div_exit.text = '<p><b>click two points in dynamic spectrum to select time and frequency range.</b></p>'
    elif 'GoXCorr':
        clickmode = 'singleclick'
        But_ClickMode.label = 'ClickMode: Single'
        tab2_BUT_XCorr.label = 'XCorr'
        global dspecDF_select
        time0, time1 = Time(
            (dspecDF_select['time'].min() + timestart) / 3600. / 24.,
            format='jd'), Time(
                (dspecDF_select['time'].max() + timestart) / 3600. / 24.,
                format='jd')
        freq0, freq1 = dspecDF_select['freq'].min(
        ), dspecDF_select['freq'].max()
        timeidx0 = next(i for i in xrange(tab2_ntim)
                        if tab2_tim[i] >= time0.mjd * 24. * 3600.)
        timeidx1 = next(i for i in xrange(tab2_ntim - 1, -1, -1)
                        if tab2_tim[i] <= time1.mjd * 24. * 3600.) + 1
        freqidx0 = next(i for i in xrange(tab2_nfreq) if tab2_freq[i] >= freq0)
        freqidx1 = next(i for i in xrange(tab2_nfreq - 1, -1, -1)
                        if tab2_freq[i] <= freq1) + 1
        dspecSel = tab2_r_dspec.data_source.data['image'][0][freqidx0:(
            freqidx1 + 1), timeidx0:(timeidx1 + 1)]
        freqSel = tab2_freq[freqidx0:(freqidx1 + 1)]
        timSel = tab2_tim[timeidx0:(timeidx1 + 1)]
        CC_dict = DButil.XCorrMap(dspecSel, timSel, freqSel)
        CC_save = struct_dir + 'CC_save.npz'
        np.savez(CC_save,
                 spec=dspecSel,
                 specfit=CC_dict['zfit'],
                 ccmax=CC_dict['ccmax'],
                 ccpeak=CC_dict['ccpeak'],
                 tim=CC_dict['x'],
                 ntim=CC_dict['nx'],
                 timfit=CC_dict['xfit'],
                 ntimfit=CC_dict['nxfit'],
                 freq=CC_dict['y'],
                 nfreq=CC_dict['ny'],
                 freqv=CC_dict['yv'],
                 freqa=CC_dict['ya'],
                 fidxv=CC_dict['yidxv'],
                 fidxa=CC_dict['yidxa'])
        try:
            tab2_panel_Div_exit.text = '<p><b>{}</b> saved.</p>'.format(
                CC_save)
        except:
            pass
        port = DButil.getfreeport()
        print 'bokeh serve {}DataBrowser/XCorr --show --port {} &'.format(
            suncasa_dir, port)
        os.system('bokeh serve {}DataBrowser/XCorr --show --port {} &'.format(
            suncasa_dir, port))
        ports.append(port)
        tab2_r_dspec_patch.data_source.data = {'xx': [], 'yy': []}
Ejemplo n.º 4
0
    def make_mapcube(self, trange, outfile=None, fov=None, wavelength='171', binpix=1, dt_data=1, derotate=False, tosave=True, superpixel=False):
        if isinstance(trange, list):
            if isinstance(trange[0], Time):
                trange = Time([trange[0], trange[-1]])
                fitsfile = DButil.readsdofile(datadir=self.fitsdir, wavelength=wavelength, jdtime=trange.jd)
            else:
                fitsfile = trange
        elif isinstance(trange, Time):
            fitsfile = DButil.readsdofile(datadir=self.fitsdir, wavelength=wavelength, jdtime=trange.jd)
        else:
            print('Input trange format not recognized. trange can either be a file list or a timerange of astropy Time object')

        maplist = []
        print 'Loading fits files....'
        for ll in tqdm(fitsfile[::dt_data]):
            maptmp = sunpy.map.Map(ll)
            if fov:
                x0, x1, y0, y1 = fov
                try:
                    submaptmp = maptmp.submap(u.Quantity([x0 * u.arcsec, x1 * u.arcsec]), u.Quantity([y0 * u.arcsec, y1 * u.arcsec]))
                except:
                    from astropy.coordinates import SkyCoord
                    bl = SkyCoord(x0 * u.arcsec, y0 * u.arcsec, frame=maptmp.coordinate_frame)
                    tr = SkyCoord(x1 * u.arcsec, y1 * u.arcsec, frame=maptmp.coordinate_frame)
                    submaptmp = maptmp.submap(bl, tr)
            else:
                submaptmp = maptmp
            if superpixel:
                submaptmp = submaptmp.superpixel(u.Quantity([binpix * u.pix] * 2))/(np.float(binpix)**2)
            else:
                submaptmp = submaptmp.resample(u.Quantity(submaptmp.dimensions) / binpix)
            if submaptmp.detector == 'HMI':
                pass
            else:
                try:
                    submaptmp = DButil.normalize_aiamap(submaptmp)
                except:
                    pass
            maplist.append(submaptmp)
        if derotate:
            mapcube = mapcube_solar_derotate(sunpy.map.Map(maplist, cube=True))
        else:
            mapcube = sunpy.map.Map(maplist, cube=True)
        trange = Time([mapcube[0].date, mapcube[-1].date])
        self.fitsfile = fitsfile
        self.dt_data = dt_data
        self.mapcube = mapcube
        self.mapcube_info()

        if tosave:
            if not outfile:
                outfile = 'mapcube_{0}_bin{3}_dtdata{4}_{1}_{2}'.format(mapcube[0].meta['wavelnth'], trange[0].isot[:-4].replace(':', ''),
                                                                        trange[1].isot[:-4].replace(':', ''), binpix, dt_data)
            for ll in xrange(42):
                if os.path.exists(outfile):
                    if not os.path.exists(outfile + '_{}'.format(ll)):
                        outfile = outfile + '_{}'.format(ll)
            self.mapcube_tofile(outfile)
        gc.collect()
Ejemplo n.º 5
0
def dumpCurrFS(StrID, IDdict):
    out_json = event_dir + 'CurrFS.json'
    struct_id = StrID['str_id'][0] + '/'
    FS_config = {'datadir': {'event_id': event_id, 'struct_id': struct_id, 'clean_id': IDdict['cleanid'] + '/',
                             'imfit_id': IDdict['imfitid'] + '/',
                             'FS_specfile': event_dir + struct_id + StrID['str_id'][0] + '_' + StrID['date'][
                                 0] + 'T' + str(StrID['timeran'][0]).translate(None, ':') + '.spec.npz'}}
    DButil.updatejsonfile(out_json, FS_config)
    return FS_config
Ejemplo n.º 6
0
def maxfit_iter(imgfiles, box, imidx):
    from taskinit import ia, rg
    try:
        from astropy.io import fits as pyfits
    except:
        try:
            import pyfits
        except ImportError:
            raise ImportError(
                'Neither astropy nor pyfits exists in this CASA installation')
    img = imgfiles[imidx]

    try:
        if (not ia.open(img)):
            raise Exception, "Cannot create image analysis tool using " + img
        print('Processing image: ' + img)
        hdr = pyfits.getheader(img)
        pols = DButil.polsfromfitsheader(hdr)
        freqs = DButil.freqsfromfitsheader(hdr)
        ndx, ndy, nchans, npols = ia.shape()
        blc, trc = [0, 0], [ndx, ndy]
        if 'box' in locals():
            if box != '':
                blc[0], blc[1], trc[0], trc[1] = [
                    int(ll) for ll in box.split(',')
                ]
        results = {}
        for itpp in pols:
            results[itpp] = {'results': {}, 'converged': []}
        for ll in range(nchans):
            for pp, itpp in enumerate(pols):
                comp = 'component{}'.format(ll)
                r = rg.box(blc=[blc[0], blc[1], ll, pp],
                           trc=[trc[0], trc[1], ll, pp])
                iachan = ia.subimage(region=r, dropdeg=True)
                try:
                    result_dict = iachan.maxfit(point=True, negfind=False)
                    result_dict['component0']['converged'] = True
                    result_dict['component0']['flux']['polarisation'] = itpp
                    result_dict['component0']['spectrum']['frequency']['m0'][
                        'value'] = float(freqs[ll])
                    results[itpp]['results'][comp] = result_dict['component0']
                    results[itpp]['converged'].append(True)
                except:
                    results[itpp]['converged'].append(False)
        results[itpp]['results']['nelements'] = results[itpp]['results'].keys()
        # update timestamp
        timstr = hdr['date-obs']
        return [True, timstr, img, results]
    except Exception, instance:
        casalog.post(str('*** Error in imfit ***') + str(instance))
        # raise instance
        return [False, timstr, img, {}]
Ejemplo n.º 7
0
def tab1_update_CleanStrID():
    global dftmp
    global ports
    if tab1_selected_StrID_entry:
        StrIDList = pd.read_json(event_dir + 'StrID_list_tmp.json')
        StrIDList = StrIDList.sort_values(by='timeran', ascending=1)
        StrID = StrIDList.iloc[tab1_selected_StrID_entry[0]]
        out_json = event_dir + StrID['str_id'][0] + '.json'
        StrID.to_json(out_json)
        struct_id = StrID['str_id'][0] + '/'
        FS_config = dumpCurrFS(StrID, {'cleanid': Text_CleanID.value, 'imfitid': ''})
        FS_specfile = FS_config['datadir']['FS_specfile']
        CleanIDdir = event_dir + struct_id + Text_CleanID.value + '/'
        print CleanIDdir
        if not os.path.exists(CleanIDdir):
            os.makedirs(CleanIDdir)
        FS_dspecDF = CleanIDdir + 'dspecDF-base'
        if not os.path.exists(FS_specfile):
            time0, time1 = StrID['time'][0], StrID['time'][1]
            freq0, freq1 = StrID['freq'][0], StrID['freq'][-1]
            bl = tab1_specdata['bl']
            spec = tab1_specdata['spec']
            npol = tab1_specdata['npol']
            nbl = tab1_specdata['nbl']
            ntim = tab1_specdata['ntim']
            nfreq = tab1_specdata['nfreq']
            tim = tab1_specdata['tim'][:]
            freq = tab1_specdata['freq'] / 1e9
            timeidx0 = next(i for i in xrange(ntim) if tim[i] >= time0)
            timeidx1 = next(i for i in xrange(ntim - 1, -1, -1) if tim[i] <= time1) + 1
            freqidx0 = next(i for i in xrange(nfreq) if freq[i] >= freq0)
            freqidx1 = next(i for i in xrange(nfreq - 1, -1, -1) if freq[i] <= freq1) + 1
            spec = spec[:, :, freqidx0:(freqidx1 + 1), timeidx0:(timeidx1 + 1)]
            tim = tim[timeidx0:(timeidx1 + 1)]
            freq = freq[freqidx0:(freqidx1 + 1)] * 1.0e9
            ntim = len(tim)
            nfreq = len(freq)
            np.savez(FS_specfile, spec=spec, tim=tim, freq=freq, bl=bl, npol=npol, nbl=nbl, nfreq=nfreq, ntim=ntim)

        if os.path.exists(FS_dspecDF):
            Text_CleanID.value = DButil.getcurtimstr()
            tab1_Div_Tb.text = """<p>CleanID existed. Click <b>FSview</b> to see the results or use the <b>new CleanID</b> to continue.</p>"""
        else:
            port = DButil.getfreeport()
            print 'bokeh serve {}DataBrowser/ToClean --show --port {} &'.format(suncasa_dir, port)
            os.system('bokeh serve {}DataBrowser/ToClean --show --port {} &'.format(suncasa_dir, port))
            ports.append(port)
            tab1_Div_Tb.text = """<p>Check the <b>ToClean </b> in the <b>new tab</b></p>"""
    else:
        tab1_Div_Tb.text = """<p><b>Warning: No StrID selected. Select one StrID first!!!</b></p>"""
Ejemplo n.º 8
0
def Buttonaskdir_handler():
    import Tkinter
    import tkFileDialog
    global SDOdir
    tkRoot = Tkinter.Tk()
    tkRoot.withdraw()  # Close the root window
    in_path = tkFileDialog.askdirectory(initialdir=SDOdir, parent=tkRoot) + '/'
    tkRoot.destroy()
    if in_path:
        Text_sdodir.value = in_path
        SDOdir = in_path
        config_main['datadir']['SDOdir'] = SDOdir
        fout = suncasa_dir + 'DataBrowser/config.json'
        DButil.updatejsonfile(fout, config_main)
        print in_path
Ejemplo n.º 9
0
def tab1_update_VDSpecStrID():
    global dftmp
    global ports
    if tab1_selected_StrID_entry:
        StrIDList = pd.read_json(event_dir + 'StrID_list_tmp.json')
        StrIDList = StrIDList.sort_values(by='timeran', ascending=1)
        StrID = StrIDList.iloc[tab1_selected_StrID_entry[0]]
        struct_id = StrID['str_id'][0] + '/'
        CleanID = tab1_Select_CleanID.value
        ImfitID = tab1_Select_ImfitID.value
        dumpCurrFS(StrID, {'cleanid': CleanID, 'imfitid': ImfitID})
        ImfitIDdir = event_dir + struct_id + CleanID + '/' + ImfitID + '/'
        FS_dspecDF = ImfitIDdir + 'dspecDF-save'
        if CleanIDdir != '' and os.path.exists(FS_dspecDF):
            tab1_Div_FSview.text = """<p>Check the <b>VDSpec</b> in the <b>new tab</b></p>"""
            port = DButil.getfreeport()
            print 'bokeh serve {}DataBrowser/FSview/VDSpec --show --port {} &'.format(
                suncasa_dir, port)
            os.system(
                'bokeh serve {}DataBrowser/FSview/VDSpec --show --port {} &'.
                format(suncasa_dir, port))
            ports.append(port)
        else:
            tab1_Div_FSview.text = """<p>Click <b>FSview </b> to extract images information first!!</p>"""
    else:
        tab1_Div_FSview.text = """<p><b>Warning: No StrID selected. Select one StrID first!!!</b></p>"""
Ejemplo n.º 10
0
def MkPlot():
    [tst, ted] = gettime()
    if ted.mjd <= tst.mjd:
        Div_info.text = '''Error: start time must occur earlier than end time. please re-enter start time and end time!!!'''
    else:
        labelsactive = [
            Wavelngth_checkbox.labels[ll] for ll in Wavelngth_checkbox.active
        ]
        Slabelsactive = set(labelsactive)
        Swavelength = set(
            ["1700", "1600", "304", "171", "193", "211", "335", "94", "131"])
        if len(Swavelength.intersection(Slabelsactive)) == 1:
            MkPlot_args_dict['wavelength'] = list(
                Swavelength.intersection(Slabelsactive))[0]
            MkPlot_args_dict['tst'] = tst.iso
            MkPlot_args_dict['ted'] = ted.iso
            MkPlot_args_dict['PlotID'] = Text_PlotID.value
            fout = database_dir + 'MkPlot_args.json'
            with open(fout, 'w') as fp:
                json.dump(MkPlot_args_dict, fp)
            Div_info.text = '''<p><b>{} saved.</b></p>'''.format(fout)
            port = DButil.getfreeport()
            print 'bokeh serve {}aiaBrowser/MkPlot --show --port {} &'.format(
                suncasa_dir, port)
            os.system(
                'bokeh serve {}aiaBrowser/MkPlot --show --port {} &'.format(
                    suncasa_dir, port))
            ports.append(port)
            Div_info.text = Div_info.text + """<p>Check the <b>MkPlot</b> in the <b>new tab</b></p>"""
        else:
            Div_info.text = Div_info.text + """<p>Choose one <b>AIA</b> wavelength to <b>MkPlot!!</b></p>"""
Ejemplo n.º 11
0
    def __init__(self, sunmap, aia=False):
        if aia:
            try:
                sunmap = DButil.normalize_aiamap(sunmap)
            except:
                pass
            data = sunmap.data
            data[data < 1.0] = 1.0
            self.sunmap = smap.Map(data, sunmap.meta)
        else:
            self.sunmap = sunmap

        try:
            top_right_coord = self.sunmap.top_right_coord
            bottom_left_coord = self.sunmap.bottom_left_coord
            self.xrange = np.array([
                bottom_left_coord.Tx.to(u.arcsec).value,
                top_right_coord.Tx.to(u.arcsec).value
            ]) * u.arcsec
            self.yrange = np.array([
                bottom_left_coord.Ty.to(u.arcsec).value,
                top_right_coord.Ty.to(u.arcsec).value
            ]) * u.arcsec
        except:
            self.xrange = self.sunmap.xrange
            self.yrange = self.sunmap.yrange
Ejemplo n.º 12
0
def tab2_BUT_tCLN_param_reload():
    global tab2_tCLN_Param_dict
    tkRoot = Tkinter.Tk()
    tkRoot.withdraw()  # Close the root window
    fin = tkFileDialog.askopenfilename(initialdir=CleanID_dir,
                                       initialfile='CASA_CLN_args.json')
    if fin.endswith('.json'):
        tab2_tCLN_Param_dict = DButil.loadjsonfile(fin)
    else:
        tab2_tCLN_Param_dict = DButil.loadjsonfile(CleanID_dir +
                                                   'CASA_CLN_args.json')
    tab2_Div_tCLN_text = '<p><b>#  ptclean :: Parallelized clean in consecutive time steps</b></p>' + ' '.join(
        "<p><b>{}</b> = {}</p>".format(key, val)
        for (key, val) in tab2_tCLN_Param_dict.items())
    tab2_Div_tCLN.text = tab2_Div_tCLN_text
    tab2_Div_tCLN2.text = '<p>CASA arguments reload from config file in <b>{}</b>.</p>'.format(
        CleanID_dir)
Ejemplo n.º 13
0
def tab1_selection_StrID_entry(attrname, old, new):
    global tab1_selected_StrID_entry
    global CleanIDdir, CleanIDdirdict
    tab1_selected_StrID_entry = tab1_SRC_StrIDPatch.selected['1d']['indices']
    StrID = StrIDList.iloc[tab1_selected_StrID_entry[0]]
    struct_id = StrID['str_id'][0] + '/'
    in_path = event_dir + struct_id
    CleanIDdirdict = DButil.getlatestfile(directory=in_path)
    if CleanIDdirdict:
        tab1_Select_CleanID.options = [os.path.basename(ll) for ll in CleanIDdirdict['items']]
        tab1_Select_CleanID.value = os.path.basename(CleanIDdirdict['latest'])
        CleanIDdir = CleanIDdirdict['latest']
    else:
        tab1_Select_CleanID.options = []
        tab1_Select_CleanID.value = ''
        CleanIDdir = ''
        tab1_Div_FSview.text = """<p>Click <b>ToClean </b> to make synthesis images first!!</p>"""
    Text_CleanID.value = DButil.getcurtimstr()
Ejemplo n.º 14
0
 def __init__(self, sunmap, aia=False):
     if aia:
         try:
             sunmap = DButil.normalize_aiamap(sunmap)
         except:
             pass
         data = sunmap.data
         data[data < 1.0] = 1.0
         self.sunmap = smap.Map(data, sunmap.meta)
     else:
         self.sunmap = sunmap
Ejemplo n.º 15
0
def tab1_Select_CleanID_handler(attrname, old, new):
    global ImfitIDdir, CleanIDdir
    tab1_selected_StrID_entry = tab1_SRC_StrIDPatch.selected['1d']['indices']
    StrID = StrIDList.iloc[tab1_selected_StrID_entry[0]]
    struct_id = StrID['str_id'][0] + '/'
    CleanIDdir = event_dir + struct_id + tab1_Select_CleanID.value
    ImfitIDdirdict = DButil.getlatestfile(directory=CleanIDdir, prefix='ImfitID_')
    if ImfitIDdirdict:
        tab1_Select_ImfitID.options = [os.path.basename(ll) for ll in ImfitIDdirdict['items']]
        tab1_Select_ImfitID.value = os.path.basename(ImfitIDdirdict['latest'])
        ImfitIDdir = ImfitIDdirdict['latest']
    else:
        tab1_Select_ImfitID.options = []
        tab1_Select_ImfitID.value = ''
        ImfitIDdir = ''
Ejemplo n.º 16
0
def getimprofile(data, cutslit, xrange=None, yrange=None):
    num = len(cutslit['xcen'])
    if num > 1:
        intens = np.zeros(num)
        ndy, ndx = data.shape
        if xrange is not None and yrange is not None:
            xs0 = (cutslit['xs0'] - xrange[0]) / (xrange[1] - xrange[0]) * ndx
            xs1 = (cutslit['xs1'] - xrange[0]) / (xrange[1] - xrange[0]) * ndx
            ys0 = (cutslit['ys0'] - yrange[0]) / (yrange[1] - yrange[0]) * ndy
            ys1 = (cutslit['ys1'] - yrange[0]) / (yrange[1] - yrange[0]) * ndy
        else:
            xs0 = cutslit['xs0']
            xs1 = cutslit['xs1']
            ys0 = cutslit['ys0']
            ys1 = cutslit['ys1']
        for ll in range(num):
            inten = DButil.improfile(data, [xs0[ll], xs1[ll]], [ys0[ll], ys1[ll]], interp='nearest')
            intens[ll] = np.mean(inten)
        intensdist = {'x': cutslit['dist'], 'y': intens}
        return intensdist
Ejemplo n.º 17
0
    def plot_mapcube(self, mapcube=None, hdr=False, vmax=None, vmin=None, cmap=None, diff=False, sav_img=False, out_dir=None, dpi=100, anim=False):
        '''

        :param mapcube:
        :param hdr:
        :param vmax:
        :param vmin:
        :param diff:
        :param sav_img:
        :param out_dir:
        :param dpi:
        :param anim:
        :return:
        '''
        if mapcube:
            mapcube_plot = deepcopy(mapcube)
        else:
            if diff:
                mapcube_plot = deepcopy(self.mapcube_diff)
            else:
                mapcube_plot = deepcopy(self.mapcube)
        if mapcube_plot is None:
            print('No mapcube found. Load a mapcube first!')
            return
        if not isinstance(mapcube_plot, sunpy.map.mapcube.MapCube):
            print('mapcube must be a instance of sunpy.map.mapcube.MapCube')
            return
        if hdr:
            maplist = []
            for idx, smap in enumerate(tqdm(mapcube_plot)):
                smap = DButil.sdo_aia_scale_hdr(smap)
                maplist.append(sunpy.map.Map(smap.data, mapcube_plot[idx].meta))
            mapcube_plot = sunpy.map.Map(maplist, cube=True)
        if not diff:
            maplist = []
            for idx, smap in enumerate(tqdm(mapcube_plot)):
                mapdata = mapcube_plot[idx].data
                mapdata[np.where(smap.data < 1)] = 1
                maplist.append(sunpy.map.Map(mapdata, mapcube_plot[idx].meta))
            mapcube_plot = sunpy.map.Map(maplist, cube=True)
        self.mapcube_plot = mapcube_plot
        # sp = stackplot(parent_obj = self, mapcube = mapcube_plot)
        fig_mapcube = plt.figure()
        try:
            if self.mapcube_plot[0].observatory == 'SDO':
                clrange = DButil.sdo_aia_scale_dict(mapcube_plot[0].meta['wavelnth'])
            else:
                clrange = {'high': None, 'log': False, 'low': None}
        except:
            clrange = {'high': None, 'log': False, 'low': None}
        if not vmax:
            vmax = clrange['high']
        if not vmin:
            vmin = clrange['low']
        if sav_img:
            if out_dir is None:
                out_dir = './'

            ax, im1 = self.plot_map(mapcube_plot[0], vmax=vmax, vmin=vmin, cmap=cmap, diff=diff, returnImAx=True)
            if anim:
                import matplotlib
                matplotlib.use("Agg")
                import matplotlib.animation as animation
                nframe = len(mapcube_plot)

                def update_frame(num):
                    smap = mapcube_plot[int(num)]
                    # smap.data[smap.data<1]=1
                    im1.set_data(smap.data)
                    # im1.set_extent(list(smap.xrange.value) + list(smap.yrange.value))
                    ax.set_title('{} {} {} {}'.format(smap.observatory, smap.detector, smap.wavelength, smap.meta['t_obs']))
                    fig_mapcube.canvas.draw()
                    return

                ani = animation.FuncAnimation(fig_mapcube, update_frame, nframe, interval=50, blit=False)

            if not silent:
                prompt = ''
                while not (prompt.lower() in ['y', 'n']):
                    prompt = raw_input('Satisfied with current FOV? [y/n]')
                if prompt.lower() == 'n':
                    return
            if anim:
                print 'Saving movie to {}'.format(out_dir)
                Writer = animation.writers['ffmpeg']
                writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
                ani.save('{0}/{2}{1}.mp4'.format(out_dir, mapcube_plot[0].meta['wavelnth'], mapcube_plot[0].detector), writer=writer)
            else:
                plt.ioff()
                print 'Saving images to {}'.format(out_dir)
                for smap in tqdm(mapcube_plot):
                    im1.set_data(smap.data)
                    # im1.set_extent(list(smap.xrange.value) + list(smap.yrange.value))
                    # ax.set_xlim(list(smap.xrange.value))
                    # ax.set_ylim(list(smap.yrange.value))
                    if smap.meta.has_key('t_obs'):
                        tstr = smap.meta['t_obs']
                    else:
                        tstr = smap.meta['date-obs']
                    ax.set_title('{} {} {} {}'.format(smap.observatory, smap.detector, smap.wavelength, tstr))
                    t_map = Time(tstr)
                    fig_mapcube.canvas.draw()
                    fig_mapcube.savefig('{0}/{3}{1}-{2}.png'.format(out_dir, smap.meta['wavelnth'],
                                                                    t_map.iso.replace(' ', 'T').replace(':', '').replace('-', '')[:-4],
                                                                    smap.detector), format='png', dpi=dpi)
                plt.ion()
        else:
            ax, im1 = self.plot_map(mapcube_plot[0], vmax=vmax, vmin=vmin, cmap=cmap, diff=diff, returnImAx=True)
            plt.subplots_adjust(bottom=0.10)
            dims = mapcube_plot[0].dimensions
            diagpix = int(np.sqrt(dims[0] ** 2 + dims[1] ** 2).value)
            axcolor = 'lightgoldenrodyellow'
            # axStackplt = plt.axes([0.8, 0.02, 0.10, 0.05], facecolor=axcolor)
            # bStackplt = Button(axStackplt, 'StackPlt')
            pixscale = ((self.fov[1] - self.fov[0]) / dims[0].value + (self.fov[3] - self.fov[2]) / dims[1].value) / 2.0
            axFrame = plt.axes([0.10, 0.03, 0.40, 0.02], facecolor=axcolor)
            # axFrame = self.divider_im.append_axes('bottom', size='1.5%', pad=0.2)
            sFrame = Slider(axFrame, 'frame', 0, len(mapcube_plot) - 1, valinit=0, valfmt='%0.0f')
            axCutwdth = plt.axes([0.65, 0.02, 0.20, 0.01], facecolor=axcolor)
            # axCutwdth = self.divider_im.append_axes('bottom', size='1.5%', pad=0.2)
            self.sCutwdth = Slider(axCutwdth, 'Width[pix]', 1, int(diagpix / 4.0), valinit=5, valfmt='%0.0f')
            axCutang = plt.axes([0.65, 0.04, 0.20, 0.01], facecolor=axcolor)
            self.sCutang = Slider(axCutang, 'Angle[deg]', -45.0, 45.0, valinit=0.0, valfmt='%.1f')
            axCutlngth = plt.axes([0.65, 0.06, 0.20, 0.01], facecolor=axcolor)
            self.sCutlngth = Slider(axCutlngth, 'Length[pix]', 20, int(diagpix * 4), valinit=150, valfmt='%0.0f')
            self.cutslitbd = CutslitBuilder(ax, cutwidth=self.sCutwdth.val, cutang=self.sCutang.val / 180. * np.pi, cutlength=self.sCutlngth.val, scale=pixscale)

            # def bStackplt_update(event):
            #     # print bStackplt.val
            #     print('button clicked')
            #
            # bStackplt.on_clicked(bStackplt_update)

            def sFrame_update(val):
                frm = sFrame.val
                smap = mapcube_plot[int(frm)]
                # smap.data[smap.data<1]=1
                im1.set_data(smap.data)
                if smap.meta.has_key('t_obs'):
                    tstr = smap.meta['t_obs']
                else:
                    tstr = smap.meta['date-obs']
                ax.set_title('{} {} {} {}'.format(smap.observatory, smap.detector, smap.wavelength, tstr))
                fig_mapcube.canvas.draw()

            sFrame.on_changed(sFrame_update)

            def sCutwdth_update(val):
                wdth = self.sCutwdth.val
                self.cutslitbd.cutwidth = wdth
                self.cutslitbd.update()

            self.sCutwdth.on_changed(sCutwdth_update)

            def sCutang_update(val):
                ang = self.sCutang.val / 180. * np.pi
                self.cutslitbd.cutang = ang
                self.cutslitbd.update()

            self.sCutang.on_changed(sCutang_update)

            def sCutlngth_update(val):
                lngth = self.sCutlngth.val
                self.cutslitbd.cutlength = lngth
                self.cutslitbd.update()

            self.sCutlngth.on_changed(sCutlngth_update)
        return
Ejemplo n.º 18
0
def plot_map(smap, dspec=None, diff=False, SymLogNorm=False, linthresh=0.5, returnImAx=False, *args, **kwargs):
    import sunpy.cm.cm as cm  ## to bootstrap sdoaia color map
    import matplotlib.cm as cm
    import matplotlib.colors as colors
    from suncasa.utils import DButil
    clrange = DButil.sdo_aia_scale_dict(wavelength=smap.meta['wavelnth'])
    plt.clf()
    if dspec:
        ax = plt.subplot(121)
    else:
        ax = plt.subplot()
    if 'vmin' in kwargs.keys():
        vmin = kwargs['vmin']
    else:
        vmin = clrange['low']
    if 'vmax' in kwargs.keys():
        vmax = kwargs['vmax']
    else:
        vmax = clrange['high']
    if diff:
        if SymLogNorm:
            norm = colors.SymLogNorm(linthresh=linthresh, vmin=vmin, vmax=vmax)
        else:
            norm = colors.Normalize(vmin=vmin, vmax=vmax)
    else:
        norm = colors.LogNorm(vmin=vmin, vmax=vmax)
    try:
        imshow_args = {'cmap': cm.get_cmap('sdoaia{}'.format(smap.meta['wavelnth'])), 'norm': norm,
                       'interpolation': 'nearest', 'origin': 'lower'}
    except:
        imshow_args = {'cmap': 'gray', 'norm': norm,
                       'interpolation': 'nearest', 'origin': 'lower'}
    if smap.coordinate_system.x == 'HG':
        xlabel = 'Longitude [{lon}]'.format(lon=smap.spatial_units.x)
    else:
        xlabel = 'X-position [{xpos}]'.format(xpos=smap.spatial_units.x)
    if smap.coordinate_system.y == 'HG':
        ylabel = 'Latitude [{lat}]'.format(lat=smap.spatial_units.y)
    else:
        ylabel = 'Y-position [{ypos}]'.format(ypos=smap.spatial_units.y)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    imshow_args.update({'extent': list(smap.xrange.value) + list(smap.yrange.value)})
    if smap.detector == 'HMI':
        im1 = ax.imshow(np.rot90(smap.data, 2), **imshow_args)
    else:
        im1 = ax.imshow(smap.data, **imshow_args)
    plt.title('{} {} {} {}'.format(smap.observatory, smap.detector, smap.wavelength, smap.meta['t_obs']))
    plt.colorbar(im1, ax=ax, label='DN counts per second')
    ax.set_autoscale_on(False)
    if dspec:
        fig = plt.gcf()
        # if figsize:
        #     fig.set_size_inches(figsize)
        # else:
        #     fig.set_size_inches(13, 5)
        ax2 = plt.subplot(122)
        im2 = plt.pcolormesh(dspec['x'], dspec['y'], dspec['dspec'], **dspec['args'])
        date_format = mdates.DateFormatter('%H:%M:%S')
        ax2.xaxis_date()
        ax2.xaxis.set_major_formatter(date_format)
        fig.autofmt_xdate(rotation=30)
        ax2.yaxis.set_label_text(dspec['ytitle'])
        plt.colorbar(im2, ax=ax2, label=dspec['ctitle'])
        ax2.set_autoscale_on(False)
        if 'axvspan' in dspec.keys():
            vspan = ax2.axvspan(dspec['axvspan'][0], dspec['axvspan'][1], alpha=0.5, color='white')
        if 'xs' in dspec.keys() and 'ys' in dspec.keys():
            ax2.plot(dspec['xs'], dspec['ys'], '--', lw=2.0, alpha=0.7, c='black')
        if 'xlim' in dspec.keys():
            ax2.set_xlim(dspec['xlim'])
        if 'ylim' in dspec.keys():
            ax2.set_ylim(dspec['ylim'])
        if returnImAx:
            return ax, im1, ax2, im2, vspan
        else:
            return ax, ax2
    else:
        if returnImAx:
            return ax, im1
        else:
            return ax
Ejemplo n.º 19
0
    ys = p(xs)
    rms = np.sqrt(np.sum((np.polyval(z, x) - y) ** 2) / len(x))
    return {'xs': xs, 'ys': ys, 'rms': rms}


datadir = '/Users/fisher/Desktop/work/2017/LLL/aia/'
mkmc = 1
if mkmc:
    from sunpy.physics.transforms.solar_rotation import mapcube_solar_derotate

    t1, t2 = Time('2014-11-09T10:00:00'), Time('2014-11-09T13:00:00')
    binwdth = 2
    dt_data = 1
    trange = [t1.jd, t2.jd]
    SDOdir = '/Volumes/NAOC-001/work/database/aiaBrowserData/Download/'
    sdofile = DButil.readsdofile(datadir=SDOdir, wavelength='171', jdtime=trange)
    # SDOdir = '/Volumes/NAOC-001/work/2015/20141111/data/prep/0171/'
    # sdofile = glob.glob(SDOdir + 'AIA20141109_*_0171.fits')
    # sdofile = sorted(sdofile)
    # x0, x1, y0, y1 = -660., -575., -200., -115.
    x0, x1, y0, y1 = -850, -500., 730., 1200.  # large
    maplist = []
    print 'Loading fits files....'
    for ll in tqdm(sdofile[::dt_data]):
        maptmp = sunpy.map.Map(ll)
        submaptmp = maptmp.submap(u.Quantity([x0 * u.arcsec, x1 * u.arcsec]),
                                  u.Quantity([y0 * u.arcsec, y1 * u.arcsec]))
        submaptmp = submaptmp.resample(u.Quantity(submaptmp.dimensions) / binwdth)
        if submaptmp.detector == 'HMI':
            pass
        else:
Ejemplo n.º 20
0
def main(year, month, day=None, ndays=10, bd=3, show_warning=False):
    '''
    By default, the subroutine create EOVSA monthly movie
    '''
    if not show_warning:
        import warnings
        warnings.filterwarnings("ignore")
    # tst = datetime.strptime("2017-04-01", "%Y-%m-%d")
    # ted = datetime.strptime("2019-12-31", "%Y-%m-%d")
    if day is None:
        dst, ded = calendar.monthrange(year, month)
        tst = datetime(year, month, 1)
        ted = datetime(year, month, ded)
    else:
        if year:
            ted = datetime(year, month, day)
        else:
            ted = datetime.now() - timedelta(days=2)
        tst = Time(np.fix(Time(ted).mjd) - ndays, format='mjd').datetime
    tsep = datetime.strptime('2019-02-22', "%Y-%m-%d")

    vmaxs = [70.0e4, 35e4, 22e4, 16e4, 10e4, 8e4, 8e4]
    vmins = [-18.0e3, -8e3, -4.8e3, -3.4e3, -2.1e3, -1.6e3, -1.6e3]
    plt.ioff()
    fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(8, 4))
    fig.subplots_adjust(bottom=0.0,
                        top=1.0,
                        left=0.0,
                        right=1.0,
                        hspace=0.0,
                        wspace=0.0)

    dateobs = tst
    imgfileslist = []
    while dateobs < ted:
        monthstrdir = dateobs.strftime("%Y/%m/")
        imgoutdir = imgfitstmpdir + monthstrdir
        movieoutdir = pltfigdir + monthstrdir
        for odir in [imgoutdir, movieoutdir]:
            if not os.path.exists(odir):
                os.makedirs(odir)

        # dateobs = datetime.strptime("2019-12-21", "%Y-%m-%d")

        if dateobs > tsep:
            spws = ['0~1', '2~5', '6~10', '11~20', '21~30', '31~43', '44~49']
        else:
            spws = ['1~3', '4~9', '10~16', '17~24', '25~30']
        # spw = spws[bd:bd + 1]
        # vmax = vmaxs[bd:bd + 1]
        # vmin = vmins[bd:bd + 1]
        aiawave = '0304'

        tdateobs = Time(Time(dateobs).mjd + np.arange(0, 24, 1) / 24,
                        format='mjd')
        imgfiles = pltEovsaQlookImageSeries(tdateobs,
                                            spws,
                                            vmaxs,
                                            vmins,
                                            aiawave,
                                            bd,
                                            fig=fig,
                                            axs=axs,
                                            overwrite=False,
                                            imgoutdir=imgoutdir)
        imgfileslist = imgfileslist + imgfiles
        dateobs = dateobs + timedelta(days=1)
    moviename = 'eovsa_bd{:02d}_aia304_{}'.format(bd + 1,
                                                  dateobs.strftime("%Y%m"))
    DButil.img2movie(imgprefix=imgfileslist, outname=moviename, img_ext='jpg')
    os.system('mv {} {}'.format(
        os.path.join(imgoutdir + '../', moviename + '.mp4'),
        os.path.join(movieoutdir, moviename + '.mp4')))
    plt.close('all')
    if os.path.exists(fsfile):
        hdu.close()
        plt_ax = False

    if tidx == 0:
        ax = axs[0]
        timetext = ax.text(0.95,
                           0.02,
                           eomap.date.strftime('%H:%M:%S'),
                           ha='right',
                           va='bottom',
                           transform=ax.transAxes,
                           color='w',
                           fontweight='bold')
    else:
        timetext.set_text(eomap.date.strftime('%H:%M:%S'))

    tim_axvspan_xy = tim_axvspan.get_xy()
    tim_axvspan_xy[np.array([0, 1, 4]),
                   0] = Time(imres['BeginTime'][tidx]).plot_date
    tim_axvspan_xy[np.array([2, 3]),
                   0] = Time(imres['EndTime'][tidx]).plot_date
    tim_axvspan.set_xy(tim_axvspan_xy)
    # tim_axvspan.set_xy(tim_axvspan_xy)
    figname = fsfile[:-9] + '.png'
    fig.savefig(figname, dpi=100)

plt.ion()

DButil.img2html_movie('{}/EO'.format(outdir))
Ejemplo n.º 22
0
def plot_compsite_map(vlafile,
                      aiafile,
                      outfile='',
                      label='',
                      pol=0,
                      chans=[],
                      chan_mask=None,
                      x_range=[],
                      y_range=[],
                      levels=[0.9],
                      plotstyle='centroid',
                      figsize=(10, 8),
                      figdpi=100,
                      width=[5, 5],
                      zorder=1,
                      maponly=False,
                      vlaonly=False,
                      dspecdata={},
                      thrshd=None,
                      cmap='jet',
                      plt_clbar=True,
                      aiaplt_args={
                          'axes': None,
                          'cmap': None,
                          'vmax': None,
                          'vmin': None
                      },
                      **kwargs):
    cmap = cm.get_cmap(cmap)
    if aiaplt_args['cmap'] is None:
        aiaplt_args.pop('cmap', None)
    if outfile:
        plt.ioff()
    if not vlaonly:
        if type(aiafile) != sunpy.map.sources.sdo.AIAMap and type(
                aiafile) != sunpy.map.compositemap.CompositeMap:
            aiamap = sunpy.map.Map(aiafile)
        else:
            aiamap = aiafile
        if x_range and y_range:
            try:
                aiamap = aiamap.submap(u.Quantity(x_range * u.arcsec),
                                       u.Quantity(y_range * u.arcsec))
            except:
                from astropy.coordinates import SkyCoord
                bl = SkyCoord(x_range[0] * u.arcsec,
                              y_range[0] * u.arcsec,
                              frame=aiamap.coordinate_frame)
                tr = SkyCoord(x_range[1] * u.arcsec,
                              y_range[1] * u.arcsec,
                              frame=aiamap.coordinate_frame)
                aiamap = aiamap.submap(bl, tr)

    if type(vlafile) == dict:
        if dspecdata:
            if dspecdata['stack'] == 'Vstack':
                fig = plt.figure(figsize=(9, 12))
                plt.subplot(2, 1, 1)
            elif dspecdata['stack'] == 'Hstack':
                fig = plt.figure(figsize=(20, 8))
                plt.subplot(1, 2, 1)
        else:
            if aiaplt_args['axes'] is None:
                fig = plt.figure(figsize=figsize)
                plt.subplot()
            else:
                fig = plt.gcf()
        if not vlaonly:
            aiamap.plot(**aiaplt_args)
            ax1 = plt.gca()
            ax1.set_autoscale_on(False)
        else:
            ax1 = aiaplt_args['axes']
            ax1.set_autoscale_on(False)

        if label:
            ax1.text(0.98,
                     0.98,
                     label,
                     horizontalalignment='right',
                     verticalalignment='top',
                     color='white',
                     transform=ax1.transAxes,
                     fontsize=14)
        clrange = vlafile['ColorMapper']['crange']
        if not maponly:
            cargsort = np.argsort(np.array(vlafile['ColorMapper']['c']))
            cargsort = cargsort[::zorder]
            vlafile['x'] = np.array(vlafile['x'])[cargsort]
            vlafile['y'] = np.array(vlafile['y'])[cargsort]
            vlafile['ColorMapper']['c'] = np.array(
                vlafile['ColorMapper']['c'])[cargsort]
            vlafile['s'] = np.array(vlafile['s'])[cargsort]
            im1 = ax1.scatter(vlafile['x'],
                              vlafile['y'],
                              c=vlafile['ColorMapper']['c'],
                              s=vlafile['s'],
                              vmin=clrange[0],
                              vmax=clrange[-1],
                              cmap=cmap,
                              **kwargs)
        else:
            im1 = ax1.scatter([], [],
                              c=[],
                              s=[],
                              vmin=clrange[0],
                              vmax=clrange[-1],
                              cmap=cmap,
                              **kwargs)
        if not dspecdata:
            if plt_clbar:
                cb1 = plt.colorbar(im1, orientation='vertical', ax=ax1)
                if type(vlafile) == dict:
                    if 'title' in vlafile['ColorMapper'].keys():
                        cb1.set_label(vlafile['ColorMapper']['title'])
                    else:
                        cb1.set_label('Frequency [GHz]')
                else:
                    cb1.set_label('Frequency [GHz]')
        else:
            tim = dspecdata['time']
            dt = np.mean(np.diff(tim))
            cmapspec = cmap
            cmapspec.set_bad('white', 1.0)
            normspec = colors.Normalize(vmin=dspecdata['drange'][0],
                                        vmax=dspecdata['drange'][1])
            if dspecdata['stack'] == 'Vstack':
                plt.subplot(2, 1, 2)
            elif dspecdata['stack'] == 'Hstack':
                plt.subplot(1, 2, 2)
            ax2 = plt.gca()
            im2 = plt.pcolormesh(tim,
                                 dspecdata['freq'],
                                 dspecdata['peak'],
                                 cmap=cmapspec,
                                 norm=normspec)
            ax2.add_patch(
                patches.Rectangle((vlafile['t'], dspecdata['freq'][0]),
                                  dt,
                                  dspecdata['freq'][-1] - dspecdata['freq'][0],
                                  facecolor='black',
                                  edgecolor='white',
                                  alpha=0.3))
            ax2.set_xlim(tim[0], tim[-1])
            ax2.set_ylim(dspecdata['freq'][0], dspecdata['freq'][-1])
            ax2.set_title('Vector Dynamic spectrum')
            labels = ax2.get_xticks().tolist()
            newlabels = [
                Time(lb / 24. / 3600., format='jd').iso.split(' ')[1]
                for lb in labels
            ]
            ax2.set_xticklabels(newlabels, rotation=45)
            if plt_clbar:
                cb1 = plt.colorbar(im1, orientation='vertical', ax=ax1)
                if type(vlafile) == dict:
                    if 'title' in vlafile['ColorMapper'].keys():
                        cb1.set_label(vlafile['ColorMapper']['title'])
                    else:
                        cb1.set_label('Frequency [GHz]')
                else:
                    cb1.set_label('Frequency [GHz]')
                cb2 = plt.colorbar(im2, orientation='vertical', ax=ax2)
                cb2.set_label('Max intensity [Jy/beam]')
    else:
        if aiaplt_args['axes'] is None:
            fig = plt.figure(figsize=figsize)
            plt.subplot()
        else:
            fig = plt.gcf()
        if not vlaonly:
            aiamap.plot(**aiaplt_args)
            ax1 = plt.gca()
            ax1.set_autoscale_on(False)
        else:
            ax1 = aiaplt_args['axes']
            ax1.set_autoscale_on(False)
        # ax1.xaxis.set_ticks_position('top')
        # ax1.yaxis.set_ticks_position('right')
        if label:
            ax1.text(0.98,
                     0.98,
                     label,
                     horizontalalignment='right',
                     verticalalignment='top',
                     color='white',
                     transform=ax1.transAxes,
                     fontsize=14)
        # # Prevent the image from being re-scaled while overplotting.
        hdulist = fits.open(vlafile)
        hdu = hdulist[0]
        nfreq = hdu.data.shape[1]
        if not maponly:
            vladata = hdu.data[pol, 0, :, :]
            vlamap = sunpy.map.Map((vladata, hdu.header))
            XX, YY = np.meshgrid(np.arange(vlamap.data.shape[1]),
                                 np.arange(vlamap.data.shape[0]))
            mapxy = vlamap.pixel_to_data(XX * u.pix, YY * u.pix)
            mapx, mapy = mapxy.Tx.value, mapxy.Ty.value
            clrange = (hdu.header['CRVAL3'] +
                       np.arange(nfreq) * hdu.header['CDELT3']) / 1e9
            if len(chans) == 0:
                chans = range(0, nfreq)
            if plotstyle == 'centroid':
                pltdata = {'x': [], 'y': [], 's': [], 'c': []}
                for idx, chan in enumerate(chans):
                    vladata = hdu.data[pol, chan, :, :]
                    if np.nanmax(vladata):
                        vlamap.data = vladata
                        maxxy = maxfit(vlamap, mapxy=[mapx, mapy], width=width)
                        if maxxy:
                            x, y = maxxy
                            pltdata['x'].append(x)
                            pltdata['y'].append(y)
                            pltdata['c'].append(
                                (hdu.header['CRVAL3'] +
                                 chan * hdu.header['CDELT3']) / 1e9)
                            pltdata['s'].append(np.nanmax(vladata))
                cargsort = np.argsort(np.array(pltdata['c']))
                cargsort = cargsort[::zorder]
                pltdata['x'] = np.array(pltdata['x'])[cargsort]
                pltdata['y'] = np.array(pltdata['y'])[cargsort]
                pltdata['c'] = np.array(pltdata['c'])[cargsort]
                pltdata['s'] = np.array(pltdata['s'])[cargsort]
                im1 = ax1.scatter(pltdata['x'],
                                  pltdata['y'],
                                  c=pltdata['c'],
                                  s=pltdata['s'],
                                  vmin=clrange[0],
                                  vmax=clrange[-1],
                                  cmap=cmap,
                                  **kwargs)
                if plt_clbar:
                    cb1 = plt.colorbar(im1, orientation='vertical', ax=ax1)
                    cb1.set_label('Frequency [GHz]')
            elif plotstyle == 'contour':
                nchan = len(chans)
                for idx, chan in enumerate(chans):
                    if chan_mask is not None:
                        freq = (hdu.header['CRVAL3'] +
                                chan * hdu.header['CDELT3']) / 1e9
                        if freq not in chan_mask:
                            continue
                    vladata = hdu.data[pol, chan, :, :]
                    vlamap = sunpy.map.Map((vladata, hdu.header))
                    SRC_vlamap_contour = DButil.get_contour_data(mapx,
                                                                 mapy,
                                                                 vlamap.data,
                                                                 levels=levels)
                    if SRC_vlamap_contour.data['xs']:
                        for ii, xs in enumerate(SRC_vlamap_contour.data['xs']):
                            x, y = xs, SRC_vlamap_contour.data['ys'][ii]
                            if not thrshd or np.nanmax(vladata) >= thrshd:
                                plt.plot(x,
                                         y,
                                         color=cmap(
                                             int(float(chan) / nfreq * 255)),
                                         zorder=nchan + zorder * idx,
                                         **kwargs)
                if plt_clbar:
                    fig.subplots_adjust(right=0.8)
                    cax1 = fig.add_axes([0.85, 0.1, 0.01, 0.8])
                    norm = colors.Normalize(vmin=clrange[0], vmax=clrange[-1])
                    cb1 = colorbar.ColorbarBase(cax1,
                                                cmap=cmap,
                                                norm=norm,
                                                orientation='vertical')
                    cb1.set_label('Frequency [GHz]')
    try:
        cb1.ax.set_aspect(40)
        cb1.ax.tick_params(direction='in')
    except:
        pass
    try:
        cb2.ax.set_aspect(40)
        cb2.ax.tick_params(direction='in')
    except:
        pass

    if aiaplt_args['axes'] is None:
        fig.tight_layout(pad=3)
    if outfile:
        if outfile.endswith('.eps'):
            fig.savefig(outfile, format='eps', dpi=int(figdpi))
        elif outfile.endswith('.pdf'):
            fig.savefig(outfile, format='pdf', dpi=int(figdpi))
        elif outfile.endswith('.png'):
            fig.savefig(outfile, format='png', dpi=int(figdpi))
        elif outfile.endswith('.jpeg'):
            fig.savefig(outfile, format='jpeg', dpi=int(figdpi))
        elif outfile.endswith('.jpg'):
            fig.savefig(outfile, format='jpg', dpi=int(figdpi))
        else:
            raise ValueError(
                'Can not save {}. Provide a filename with extension of eps, pdf, png or jpeg.'
                .format(outfile))
Ejemplo n.º 23
0
def plotmap(vlafile,
            aiafile,
            outfile='',
            label='',
            pol=0,
            chans=[],
            x_range=[],
            y_range=[],
            levels=[0.9],
            plotstyle='centroid',
            figsize=(10, 8),
            figdpi=100,
            zorder=-1,
            maponly=False,
            dspecdata={},
            **kwargs):
    import matplotlib.gridspec as gridspec
    if outfile:
        plt.ioff()
    if type(aiafile) != sunpy.map.sources.sdo.AIAMap:
        aiamap = sunpy.map.Map(aiafile)
    else:
        aiamap = aiafile
    if x_range and y_range:
        aiamap = aiamap.submap(u.Quantity(x_range * u.arcsec),
                               u.Quantity(y_range * u.arcsec))

    if type(vlafile) == dict:
        if dspecdata:
            if dspecdata['stack'] == 'Vstack':
                gs = gridspec.GridSpec(2,
                                       2,
                                       width_ratios=[1, 0.02],
                                       height_ratios=[1, 0.5])
                fig = plt.figure(figsize=(9, 12))
            elif dspecdata['stack'] == 'Hstack':
                gs = gridspec.GridSpec(1,
                                       4,
                                       width_ratios=[1, 0.02, 1.5, 0.02],
                                       height_ratios=[1])
                fig = plt.figure(figsize=(18, 8))
            ax1 = plt.subplot(gs[0], projection=aiamap)
            ax2 = plt.subplot(gs[1])
            ax3 = plt.subplot(gs[2])
            ax4 = plt.subplot(gs[3])
        else:
            gs = gridspec.GridSpec(1,
                                   2,
                                   width_ratios=[1, 0.05],
                                   height_ratios=[1])
            fig = plt.figure(figsize=figsize)
            ax1 = plt.subplot(gs[0], projection=aiamap)
            ax2 = plt.subplot(gs[1])
        aiamap.plot(axes=ax1)
        ax1.set_autoscale_on(False)

        if label:
            ax1.text(0.98,
                     0.98,
                     label,
                     horizontalalignment='right',
                     verticalalignment='top',
                     color='white',
                     transform=ax1.transAxes,
                     fontsize=14)
        freqs = vlafile['ColorMapper']['crange']
        if not maponly:
            nchan = len(vlafile['ColorMapper']['c'])
            for idx, chan in enumerate(vlafile['ColorMapper']['c']):
                x, y = [vlafile['x'][idx], vlafile['y'][idx]] * u.arcsec
                s = vlafile['s'][idx]
                ax1.scatter(x.to(u.deg),
                            y.to(u.deg),
                            transform=ax1.get_transform('world'),
                            facecolor=cm.jet(
                                int((chan - freqs[0]) /
                                    (freqs[-1] - freqs[0]) * 255)),
                            edgecolor='black',
                            s=s,
                            zorder=nchan + zorder * idx,
                            **kwargs)
        if not dspecdata:
            cmap = cm.jet
            norm = colors.Normalize(vmin=freqs[0], vmax=freqs[-1])
            cb1 = colorbar.ColorbarBase(ax2,
                                        cmap=cmap,
                                        norm=norm,
                                        orientation='vertical')
            if type(vlafile) == dict:
                cb1.set_label(vlafile['ColorMapper']['title'])
            else:
                cb1.set_label('Frequency [GHz]')
        else:
            tim = dspecdata['time']
            dt = np.mean(np.diff(tim))
            cmapspec = cm.jet
            cmapspec.set_bad('white', 1.0)
            normspec = colors.Normalize(vmin=dspecdata['drange'][0],
                                        vmax=dspecdata['drange'][1])
            ax3.pcolormesh(tim,
                           dspecdata['freq'],
                           dspecdata['peak'],
                           cmap=cmapspec,
                           norm=normspec)
            ax3.add_patch(
                patches.Rectangle((vlafile['t'], dspecdata['freq'][0]),
                                  dt,
                                  dspecdata['freq'][-1] - dspecdata['freq'][0],
                                  facecolor='black',
                                  edgecolor='white',
                                  alpha=0.3))
            ax3.set_xlim(tim[0], tim[-1] + dt)
            ax3.set_ylim(dspecdata['freq'][0], dspecdata['freq'][-1])
            ax3.set_title('Vector Dynamic spectrum')
            labels = ax3.get_xticks().tolist()
            newlabels = [
                Time(lb / 24. / 3600., format='jd').iso.split(' ')[1]
                for lb in labels
            ]
            ax3.set_xticklabels(newlabels, rotation=45)
            if dspecdata['stack'] == 'Vstack':
                fig.tight_layout(h_pad=3, pad=3)
                ax3.set_ylabel('Frequency [GHz]')
                pos1 = ax1.get_position()
                pos2 = ax2.get_position()
                pos4 = ax4.get_position()
                pos2new = [
                    pos2.x0 - (1.0 - pos1.x1) / 2, pos2.y0, pos2.width,
                    pos2.height
                ]
                ax2.set_position(pos2new)
                pos4new = [
                    pos4.x0 - (1.0 - pos1.x1) / 2, pos4.y0, pos4.width,
                    pos4.height
                ]
                ax4.set_position(pos4new)
            elif dspecdata['stack'] == 'Hstack':
                fig.tight_layout(w_pad=0, pad=5)
                ax3.set_yticklabels([])
                pos2 = ax2.get_position()
                pos4 = ax4.get_position()
                pos2new = [pos2.x0 - 0.03, pos2.y0, pos2.width, pos2.height]
                ax2.set_position(pos2new)
                pos4new = [pos4.x0 - 0.01, pos4.y0, pos4.width, pos4.height]
                ax4.set_position(pos4new)
            cmap = cm.jet
            norm = colors.Normalize(vmin=freqs[0], vmax=freqs[-1])
            cb1 = colorbar.ColorbarBase(ax2,
                                        cmap=cmap,
                                        norm=norm,
                                        orientation='vertical')
            cb2 = colorbar.ColorbarBase(ax4,
                                        cmap=cmapspec,
                                        norm=normspec,
                                        orientation='vertical')
            if type(vlafile) == dict:
                cb1.set_label(vlafile['ColorMapper']['title'])
            else:
                cb1.set_label('Frequency [GHz]')
            cb2.set_label('Max intensity [Jy/beam]')
    else:
        fig = plt.figure(figsize=figsize)
        ax1 = plt.subplot(projection=aiamap)
        aiamap.plot()
        if label:
            ax1.text(0.98,
                     0.98,
                     label,
                     horizontalalignment='right',
                     verticalalignment='top',
                     color='white',
                     transform=ax1.transAxes,
                     fontsize=14)
        # # Prevent the image from being re-scaled while overplotting.
        ax1.set_autoscale_on(False)
        hdulist = fits.open(vlafile)
        hdu = hdulist[0]
        if not maponly:
            vladata = hdu.data[pol, 0, :, :]
            vlamap = sunpy.map.Map((vladata, hdu.header))
            XX, YY = np.meshgrid(np.arange(vlamap.data.shape[1]),
                                 np.arange(vlamap.data.shape[0]))
            mapx, mapy = vlamap.pixel_to_data(XX * u.pix, YY * u.pix)
            mapx, mapy = mapx.value, mapy.value
            nfreq = hdu.data[pol, :, :, :].shape[1]
            if len(chans) == 0:
                chans = range(0, nfreq)
            nchan = len(chans)
            if plotstyle == 'centroid':
                for idx, chan in enumerate(chans):
                    vladata = hdu.data[pol, chan, :, :]
                    if np.nanmax(vladata):
                        vlamap.data = vladata
                        maxxy = maxfit(vlamap, mapxy=[mapx, mapy])
                        if maxxy:
                            x, y = maxxy * u.arcsec
                            plt.scatter(x.to(u.deg),
                                        y.to(u.deg),
                                        transform=ax1.get_transform('world'),
                                        facecolor=cm.jet(
                                            int(float(chan) / nfreq * 255)),
                                        edgecolor='black',
                                        s=np.nanmax(vladata),
                                        zorder=nchan + zorder * idx,
                                        **kwargs)
            elif plotstyle == 'contour':
                for idx, chan in enumerate(chans):
                    vladata = hdu.data[pol, chan, :, :]
                    vlamap = sunpy.map.Map((vladata, hdu.header))
                    SRC_vlamap_contour = DButil.get_contour_data(mapx,
                                                                 mapy,
                                                                 vlamap.data,
                                                                 levels=levels)
                    if SRC_vlamap_contour.data['xs']:
                        xs = SRC_vlamap_contour.data['xs'][0] * u.arcsec
                        ys = SRC_vlamap_contour.data['ys'][0] * u.arcsec
                        plt.plot(xs.to(u.deg),
                                 ys.to(u.deg),
                                 transform=ax1.get_transform('world'),
                                 color=cm.jet(int(float(chan) / nfreq * 255)),
                                 zorder=nchan + zorder * idx,
                                 **kwargs)

        freqs = (hdu.header['CRVAL3'] +
                 np.arange(nfreq) * hdu.header['CDELT3']) / 1e9
        fig.subplots_adjust(right=0.8)
        cax1 = fig.add_axes([0.85, 0.1, 0.05, 0.8])
        cmap = cm.jet
        norm = colors.Normalize(vmin=freqs[0], vmax=freqs[-1])
        cb1 = colorbar.ColorbarBase(cax1,
                                    cmap=cmap,
                                    norm=norm,
                                    orientation='vertical')
        cb1.set_label('Frequency [GHz]')

    if outfile:
        if outfile.endswith('.eps'):
            fig.savefig(outfile, format='eps', dpi=int(figdpi))
        elif outfile.endswith('.pdf'):
            fig.savefig(outfile, format='pdf', dpi=int(figdpi))
        elif outfile.endswith('.png'):
            fig.savefig(outfile, format='png', dpi=int(figdpi))
        elif outfile.endswith('.jpeg'):
            fig.savefig(outfile, format='jpeg', dpi=int(figdpi))
        elif outfile.endswith('.jpg'):
            fig.savefig(outfile, format='jpg', dpi=int(figdpi))
        else:
            raise ValueError(
                'Can not save {}. Provide a filename with extension of eps, pdf, png or jpeg.'
                .format(outfile))
Ejemplo n.º 24
0
def DownloadData():
    [tst, ted] = gettime()
    if ted.mjd <= tst.mjd:
        Div_JSOC_info.text = '''Error: start time must occur earlier than end time. please re-enter start time and end time!!!'''
    elif len(Wavelngth_checkbox.active) == 0:
        Div_JSOC_info.text = '''Error: at least choose one wavelength!!!'''
    else:
        Div_JSOC_info.text = ''''''
        c = drms.Client(verbose=True)
        export_protocol = 'fits'
        if not Text_email.value:
            Div_JSOC_info.text = '''Error: provide your JSOC registered email address!!!'''
            raise RuntimeError('Email address is required.')
        else:
            if not c.check_email(Text_email.value):
                Div_JSOC_info.text = '''Error: <b>Email address</b> is not valid or not registered.!!!'''
                raise RuntimeError(
                    'Email address is not valid or not registered.')
            else:
                config_main['core']['JSOC_reg_email'] = Text_email.value
                DButil.updatejsonfile(suncasa_dir + 'DataBrowser/config.json',
                                      config_main)

        labelsactive = [
            Wavelngth_checkbox.labels[ll] for ll in Wavelngth_checkbox.active
        ]
        if 'goes' in labelsactive:
            global goes
            tr = TimeRange(tst.iso, ted.iso)
            goes = GOESLightCurve.create(tr)
            fout = database_dir + 'goes-' + Text_PlotID.value
            with open(fout, 'wb') as fp:
                pickle.dump(goes, fp)
            Div_JSOC_info.text = """<p>{} saved.</p>""".format(fout)
            MkPlot_args_dict['goesfile'] = os.path.basename(fout)
            labelsactive.pop(labelsactive.index('goes'))
        for series in ['hmi.M_45s', 'aia.lev1_uv_24s', 'aia.lev1_euv_12s']:
            waves = []
            for ll in labelsactive:
                if serieslist[ll] == series:
                    waves.append(ll)
            if len(waves) > 0:
                # try:
                tsel = tst.iso.replace(' ', 'T') + '_TAI-' + ted.iso.replace(
                    ' ', 'T') + '_TAI'
                wave = ','.join(waves)
                cadence = Text_Cadence.value
                if cadence[-1] == 's' and get_num(cadence) < 12:
                    cadence = '12s'
                    Text_Cadence.value = cadence
                if series == 'aia.lev1_uv_24s':
                    if cadence[-1] == 's' and get_num(cadence) < 24:
                        cadence = '24s'
                if series == 'hmi.M_45s':
                    wave = ''
                    if cadence[-1] == 's' and get_num(cadence) < 45:
                        cadence = '45s'
                segments = 'image'
                qstr = '%s[%s@%s][%s]{%s}' % (series, tsel, cadence, wave,
                                              segments)
                print qstr
                r = c.export(qstr,
                             method='url',
                             protocol=export_protocol,
                             email=Text_email.value)
                Div_JSOC_info.text = Div_JSOC_info.text + """<p>Submitting export request <b>{}</b>...</p>""".format(
                    qstr)
                Div_JSOC_info.text = Div_JSOC_info.text + """<p>Request URL: {}</p>""".format(
                    r.request_url)
                Div_JSOC_info.text = Div_JSOC_info.text + """<p>{:d} file(s) available for download.</p>""".format(
                    len(r.urls))
                idx2download = DButil.FileNotInList(
                    r.data['filename'],
                    DButil.readsdofile(datadir=SDOdir,
                                       wavelength=wave,
                                       jdtime=[tst.jd, ted.jd],
                                       isexists=True))
                if len(idx2download) > 0:
                    Div_JSOC_info.text = Div_JSOC_info.text + """<p><b>Downloading</b>....</p>"""
                    r.download(SDOdir, index=idx2download)
                else:
                    Div_JSOC_info.text = Div_JSOC_info.text + """<p>Target file(s) existed.</p>"""

                filename = glob.glob(SDOdir + '*.fits')
                if len(filename) > 0:
                    dirs = DButil.getsdodir(filename)
                    for ll, dd in enumerate(dirs['dir']):
                        if not os.path.exists(SDOdir + dd):
                            os.makedirs(SDOdir + dd)
                        os.system('mv {}/*{}*.fits {}{}'.format(
                            SDOdir, dirs['timstr'][ll], SDOdir, dd))

                Div_JSOC_info.text = Div_JSOC_info.text + """<p>Download <b>finished</b>.</p>"""
                Div_JSOC_info.text = Div_JSOC_info.text + """<p>file(s) downloaded to <b>{}</b></p>""".format(
                    os.path.abspath(SDOdir))
Ejemplo n.º 25
0
    # for ll in xrange(5010, 5010 + 10):
    #     os.system('fuser -n tcp -k {}'.format(ll))
elif platform == "darwin":
    print 'Runing EvtBrowser in OS X platform'
    # for ll in xrange(5010, 5010 + 10):
    #     os.system(
    #         'port=($(lsof -i tcp:{}|grep python2.7 |cut -f2 -d" ")); [[ -n "$port" ]] && kill -9 $port'.format(ll))
    #     os.system(
    #         'port=($(lsof -i tcp:{}|grep Google |cut -f2 -d" ")); [[ -n "$port" ]] && kill -9 $port'.format(ll))
elif platform == "win32":
    print 'Runing EvtBrowser in Windows platform'

ports = []
'''load config file'''
suncasa_dir = os.path.expandvars("${SUNCASA}") + '/'
DButil.initconfig(suncasa_dir)
'''load config file'''
config_main = DButil.loadjsonfile(suncasa_dir + 'DataBrowser/config.json')

database_dir = os.path.expandvars(
    config_main['datadir']['database']) + '/aiaBrowserData/'
if not os.path.exists(database_dir):
    os.makedirs(database_dir)

SDOdir = DButil.getSDOdir(config_main, database_dir, suncasa_dir)
print SDOdir
if not os.path.exists(SDOdir):
    os.makedirs(SDOdir)

YY_select_st = Select(
    title="Start Time: Year",
Ejemplo n.º 26
0
def lightcurves(timerange, outdir='./', specfile=None, goes=True, hessifile=None, fermifile=None, ylog=False, hessi_smoth=0, dspec_cmap='cubehelix',
                vmax=None, vmin=None):
    from sunpy.lightcurve import GOESLightCurve
    from sunpy.time import TimeRange, parse_time
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    from astropy.time import Time
    import numpy as np
    import numpy.ma as ma
    from scipy.signal import medfilt
    import matplotlib.colors as colors
    from scipy.io import readsav
    from suncasa.utils import DButil
    import os

    timerange = Time(timerange)
    if hessifile:
        if not os.path.exists(hessifile):
            hessi_script = 'HESSI_lc.pro'
            print('Run the script {} in SSWIDL to download RHESSI summary file first!'.format(hessi_script))
            fi = open(hessi_script, 'wb')
            fi.write("time_range = ['{}','{}'] \n".format(timerange[0].datetime.strftime('%Y-%b-%d %H:%M:%S'),
                                                          timerange[1].datetime.strftime('%Y-%b-%d %H:%M:%S')))
            fi.write('obs_obj = hsi_obs_summary(obs_time_interval=time_range) \n')
            fi.write('data = obs_obj->getdata() \n')
            fi.write('info = obs_obj->get( / info) \n')
            fi.write('obs_data = data.countrate \n')
            fi.write('obs_times = obs_obj->getdata(/time) \n')
            fi.write('obs_times_str  = anytim(obs_times,/CCSDS) \n')
            fi.write('obs_energies  = fltarr(2,n_elements(info.energy_edges)-1) \n')
            fi.write('for ll=0,n_elements(info.energy_edges)-2 do begin \n')
            fi.write('    obs_energies[0,ll]=info.energy_edges[ll] \n')
            fi.write('    obs_energies[1,ll]=info.energy_edges[ll+1] \n')
            fi.write('endfor \n')
            fi.write('save,filename="{}",OBS_DATA,OBS_ENERGIES,obs_times_str \n'.format(hessifile))
            fi.write('end \n')
            fi.close()
            return -1
        hessi = readsav(hessifile)
        hessi_tim = Time(list(hessi['obs_times_str']))
        hessi_tim_plt = hessi_tim.plot_date

    specdata = np.load(specfile)
    spec = specdata['spec']
    if len(spec.shape) == 4:
        (npol, nbl, nfreq, ntim) = spec.shape
    else:
        (nfreq, ntim) = spec.shape
    spec = np.mean(np.mean(spec, axis=0), axis=0)
    freq = specdata['freq']
    freqghz = freq / 1e9
    spec_tim = Time(specdata['tim'] / 3600. / 24., format='mjd')

    fidx_plt = np.linspace(0, nfreq - nfreq / 8.0 / 2.0, 8).astype(np.int) + nfreq / 8.0 / 2.0

    try:
        plt.style.use('seaborn-bright')
        params = {'font.size': 8, 'axes.grid': False, 'axes.facecolor': 'w', 'xtick.color': '#555555', 'ytick.color': '#555555',
                  'xtick.major.size': 2.0, 'ytick.major.size': 2.0, 'xtick.minor.size': 1.0, 'ytick.minor.size': 1.0, 'axes.axisbelow': False,
                  'axes.xmargin': 0.0, 'axes.ymargin': 0.0, 'axes.linewidth': 0.5, 'xtick.major.pad': 1.5, 'ytick.major.pad': 1.5,
                  'lines.linewidth': 1.0}
        mpl.rcParams.update(params)
    except:
        pass

    if goes:
        tr = TimeRange(timerange.iso)
        goes = GOESLightCurve.create(tr)
        dates = mpl.dates.date2num(parse_time(goes.data.index))

    tr_plt = Time(timerange)

    fig, axs = plt.subplots(nrows=3, ncols=1, sharex=True, figsize=(8, 6))
    ax = axs[0]
    tidx_hessi, = np.where((hessi_tim >= tr_plt[0]) & (hessi_tim <= tr_plt[1]))
    for idx, eg in enumerate(hessi['obs_energies']):
        flux_hessi = ma.masked_array(hessi['obs_data'][tidx_hessi, idx])
        if hessi_smoth > 0:
            flux_hessi = DButil.smooth(flux_hessi, 20)
            flux_hessi = flux_hessi / np.nanmax(flux_hessi)
            ax.step(hessi_tim_plt[tidx_hessi], DButil.smooth(flux_hessi, hessi_smoth), label='{:.0f}-{:.0f} keV'.format(eg[0], eg[1]))
        else:
            ax.step(hessi_tim_plt[tidx_hessi], flux_hessi, label='{:.0f}-{:.0f} keV'.format(eg[0], eg[1]))

    if ylog:
        ax.set_yscale("log", nonposy='clip')
    else:
        ax.set_yscale("linear", nonposy='clip')
    ax.legend()
    ax.set_ylabel('Count Rate [s$^{-1}$ detector$^{-1}$ ]')

    ax = axs[1]
    tidx_goes, = np.where((dates >= tr_plt[0].plot_date) & (dates <= tr_plt[1].plot_date))
    if goes:
        ax.plot(dates[tidx_goes], goes.data['xrsb'][tidx_goes] / np.nanmax(goes.data['xrsb'][tidx_goes]), label='GOES 1.0--8.0 $\AA$')
        ax.plot(dates[tidx_goes], goes.data['xrsa'][tidx_goes] / np.nanmax(goes.data['xrsa'][tidx_goes]), label='GOES 0.5--4.0 $\AA$')
    tidx_spec, = np.where((spec_tim >= tr_plt[0]) & (spec_tim <= tr_plt[1]))
    if len(tidx_spec) > 1:
        spec_tim_plt = spec_tim[tidx_spec[0]:tidx_spec[-1]].plot_date
        flux_colors = []
        for idx, fidx in enumerate(fidx_plt):
            flux_plt = medfilt(spec[fidx, tidx_spec[0]:tidx_spec[-1]], 7)
            p = ax.plot(spec_tim_plt, flux_plt / np.nanmax(flux_plt), label='{:.2f} GHz'.format(freqghz[fidx]))
            flux_colors.append(p[0].get_color())
        ax.set_ylabel('Flux (Normalized)')
        ax.set_ylim(0, 1.1)
        ax.legend()

        ax = axs[2]
        spec_plt = spec[:, tidx_spec[0]:tidx_spec[-1]]
        ax.pcolormesh(spec_tim_plt, freqghz, spec_plt, cmap=dspec_cmap, norm=colors.LogNorm(vmax=vmax, vmin=vmin))
        ax.set_ylabel('Frequency [GHz]')
        formatter = mpl.dates.DateFormatter('%H:%M:%S')
        ax.xaxis.set_major_formatter(formatter)
        ax.fmt_xdata = formatter
        ax.set_xlim(tr_plt.plot_date)
        ax.set_xlabel('Start time ({})'.format(tr_plt[0].datetime.strftime('%d-%b-%y %H:%M:%S')))
        for idx, fidx in enumerate(fidx_plt):
            ax.axhline(freqghz[fidx], color=flux_colors[idx], ls=':')
    else:
        print('Warning: No radio data in the timerange. Proceed without dynamic spectrum.')

    fig.tight_layout()
    fig.subplots_adjust(hspace=0.06)
    imgdir = outdir + '/fig01_{}-{}.png'.format(tr_plt[0].datetime.strftime('%H%M%S'), tr_plt[1].datetime.strftime('%H%M%S'))
    fig.savefig(imgdir, dpi=200)
    print('Save image to ' + imgdir)
Ejemplo n.º 27
0
def plt_qlook_image(imres, figdir=None, specdata=None, verbose=True, stokes='I,V', fov=None):
    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 = './'

    observatory = 'EOVSA'
    polmap = {'RR': 0, 'LL': 1, 'I': 0, 'V': 1}
    pols = stokes.split(',')
    npols = len(pols)
    # SRL = set(['RR', 'LL'])
    # SXY = set(['XX', 'YY', 'XY', 'YX'])
    Spw = sorted(list(set(imres['Spw'])))
    nspw = len(Spw)
    # Freq = set(imres['Freq']) ## list is an unhashable type
    Freq = sorted(uniq(imres['Freq']))

    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()
    import matplotlib.gridspec as gridspec
    spec = specdata['spec']
    (npol, nbl, nfreq, ntim) = spec.shape
    tidx = range(ntim)
    fidx = range(nfreq)
    tim = specdata['tim']
    freq = specdata['freq']
    freqghz = freq / 1e9
    pol = ''.join(pols)
    spec_tim = Time(specdata['tim'] / 3600. / 24., format='mjd')
    timstrr = spec_tim.plot_date
    if npols == 1:
        if pol == 'RR':
            spec_plt = spec[0, 0, :, :]
        elif pol == 'LL':
            spec_plt = spec[1, 0, :, :]
        elif pol == 'I':
            spec_plt = (spec[0, 0, :, :] + spec[1, 0, :, :]) / 2.
        elif pol == 'V':
            spec_plt = (spec[0, 0, :, :] - spec[1, 0, :, :]) / 2.
        spec_plt = [spec_plt]
        print 'plot the dynamic spectrum in pol ' + pol  # ax1 = fig.add_subplot(211)

        hnspw = nspw / 2
        ncols = hnspw
        nrows = 2 + 2  # 1 image: 1x1, 1 dspec:2x4
        fig = plt.figure(figsize=(8, 8))
        gs = gridspec.GridSpec(nrows, ncols)
        axs = [plt.subplot(gs[0, 0])]
        for ll in range(1, nspw):
            axs.append(plt.subplot(gs[ll / hnspw, ll % hnspw], sharex=axs[0], sharey=axs[0]))
        for ll in range(nspw):
            axs.append(plt.subplot(gs[ll / hnspw + 2, ll % hnspw], sharex=axs[0], sharey=axs[0]))
        axs_dspec = [plt.subplot(gs[2:, :])]
        cmaps = ['jet']
    elif npols == 2:
        R_plot = np.absolute(spec[0, 0, :, :])
        L_plot = np.absolute(spec[1, 0, :, :])
        if pol == 'RRLL':
            spec_plt = [R_plot, L_plot]
            polstr = ['RR', 'LL']
            cmaps = ['jet'] * 2
        if pol == 'IV':
            I_plot = (R_plot + L_plot) / 2.
            V_plot = (R_plot - L_plot) / 2.
            spec_plt = [I_plot, V_plot]
            polstr = ['I', 'V']
            cmaps = ['jet', 'RdBu']
        print 'plot the dynamic spectrum in pol ' + pol

        hnspw = nspw / 2
        ncols = hnspw + 2  # 1 image: 1x1, 1 dspec:2x2
        nrows = 2 + 2
        fig = plt.figure(figsize=(12, 8))
        gs = gridspec.GridSpec(nrows, ncols)
        axs = [plt.subplot(gs[0, 0])]
        for ll in range(1, nspw):
            axs.append(plt.subplot(gs[ll / hnspw, ll % hnspw], sharex=axs[0], sharey=axs[0]))
        for ll in range(nspw):
            axs.append(plt.subplot(gs[ll / hnspw + 2, ll % hnspw], sharex=axs[0], sharey=axs[0]))
        axs_dspec = [plt.subplot(gs[:2, hnspw:])]
        axs_dspec.append(plt.subplot(gs[2:, hnspw:], sharex=axs_dspec[0], sharey=axs_dspec[0]))

    fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
    timetext = fig.text(0.01, 0.98, '', color='w', fontweight='bold', fontsize=12, ha='left', va='top')
    for i in range(ntime):
        plt.ioff()
        # plt.clf()
        for ax in axs:
            ax.cla()
        plttime = btimes_sort[i, 0]
        # tofd = plttime.mjd - np.fix(plttime.mjd)
        suci = suc_sort[i]
        # 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])
        timetext.set_text(plttime.iso[:19])
        if verbose:
            print 'Plotting image at: ', plttime.iso

        if i == 0:
            dspecvspans = []
            for pol in range(npols):
                ax = axs_dspec[pol]
                ax.pcolormesh(timstrr, freqghz, spec_plt[pol], cmap=cmaps[pol])
                ax.xaxis_date()
                ax.xaxis.set_major_formatter(DateFormatter("%H:%M:%S"))
                # plt.xticks(rotation=45)
                ax.set_xlim(timstrr[tidx[0]], timstrr[tidx[-1]])
                ax.set_ylim(freqghz[fidx[0]], freqghz[fidx[-1]])
                ax.set_xlabel('Time [UT]')
                ax.set_ylabel('Frequency [GHz]')
                for idx, freq in enumerate(Freq):
                    ax.axhspan(freq[0], freq[1], linestyle='dotted', edgecolor='w', alpha=0.7, facecolor='none')
                    xtext, ytext = ax.transAxes.inverted().transform(ax.transData.transform([timstrr[tidx[0]], np.mean(freq)]))
                    ax.text(xtext + 0.01, ytext, 'spw ' + Spw[idx], color='w', transform=ax.transAxes, fontweight='bold', ha='left', va='center',
                            fontsize=8, alpha=0.5)
                ax.text(0.01, 0.98, 'Stokes ' + pols[pol], color='w', transform=ax.transAxes, fontweight='bold', ha='left', va='top')
                dspecvspans.append(ax.axvspan(btimes[i].plot_date, etimes[i].plot_date, color='w', alpha=0.4))
                ax_pos = ax.get_position().extents
                x0, y0, x1, y1 = ax_pos
                h, v = x1 - x0, y1 - y0
                x0_new = x0 + 0.15 * h
                y0_new = y0 + 0.15 * v
                x1_new = x1 - 0.05 * h
                y1_new = y1 - 0.05 * v
                ax.set_position(mpl.transforms.Bbox([[x0_new, y0_new], [x1_new, y1_new]]))
        else:
            for pol in range(npols):
                xy = dspecvspans[pol].get_xy()
                xy[:, 0][np.array([0, 1, 4])] = btimes[i].plot_date
                xy[:, 0][np.array([2, 3])] = etimes[i].plot_date
                dspecvspans[pol].set_xy(xy)

        for n in range(nspw):
            image = images_sort[i, n]
            # fig.add_subplot(nspw/3, 3, n+1)
            # fig.add_subplot(2, nspw / 2, n + 1)
            for pol in range(npols):
                if suci[n]:
                    try:
                        eomap = smap.Map(image)
                    except:
                        continue
                    sz = eomap.data.shape
                    if len(sz) == 4:
                        eomap.data = eomap.data[min(polmap[pols[pol]], eomap.meta['naxis4'] - 1), 0, :, :].reshape((sz[2], sz[3]))
                    # resample the image for plotting
                    if fov is not None:
                        fov = [np.array(ll) for ll in fov]
                        pad = max(np.diff(fov[0])[0], np.diff(fov[1])[0])
                        eomap = eomap.submap((fov[0] + np.array([-1.0, 1.0]) * pad) * u.arcsec, (fov[1] + np.array([-1.0, 1.0]) * pad) * u.arcsec)
                    else:
                        dim = u.Quantity([256, 256], u.pixel)
                        eomap = eomap.resample(dim)
                    eomap.plot_settings['cmap'] = plt.get_cmap(cmaps[pol])
                    # import pdb
                    # pdb.set_trace()
                    eomap.plot(axes=axs[n + nspw * pol])
                    eomap.draw_limb()
                    eomap.draw_grid()
                    ax = plt.gca()
                    ax.set_autoscale_on(False)
                    if fov:
                        # pass
                        ax.set_xlim(fov[0])
                        ax.set_ylim(fov[1])
                    else:
                        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('~')]
                    # if len(freqran) == 1:
                    #     ax.text(0.98, 0.01, '{0:.1f} GHz'.format(freqran[0]), color='w',
                    #             transform=ax.transAxes, fontweight='bold', ha='right')
                    # else:
                    #     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.text(0.98, 0.01, 'Stokes {1} @ {0:.3f} GHz'.format(eomap.meta['crval3'] / 1e9, pols[pol]), 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.xaxis.set_visible(False)
                    ax.yaxis.set_visible(False)
                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)
                    # resample the image for plotting
                    if fov:
                        fov = [np.array(ll) for ll in fov]
                        pad = max(np.diff(fov[0])[0], np.diff(fov[1])[0])
                        try:
                            eomap = eomap.submap((fov[0] + np.array([-1.0, 1.0]) * pad) * u.arcsec, (fov[1] + np.array([-1.0, 1.0]) * pad) * u.arcsec)
                        except:
                            x0, x1 = fov[0] + np.array([-1.0, 1.0]) * pad
                            y0, y1 = fov[1] + np.array([-1.0, 1.0]) * pad
                            bl = SkyCoord(x0 * u.arcsec, y0 * u.arcsec, frame=eomap.coordinate_frame)
                            tr = SkyCoord(x1 * u.arcsec, y1 * u.arcsec, frame=eomap.coordinate_frame)
                            eomap = eomap.submap(bl, tr)
                    else:
                        dim = u.Quantity([256, 256], u.pixel)
                        eomap = eomap.resample(dim)
                    eomap.plot_settings['cmap'] = plt.get_cmap(cmaps[pol])
                    eomap.plot(axes=axs[n + nspw * pol])
                    eomap.draw_limb()
                    eomap.draw_grid()
                    ax = plt.gca()
                    ax.set_autoscale_on(False)
                    if fov:
                        # pass
                        ax.set_xlim(fov[0])
                        ax.set_ylim(fov[1])
                    else:
                        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.text(0.98, 0.01, 'Stokes {1} @ {0:.3f} GHz'.format(0., pols[pol]), 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.xaxis.set_visible(False)
                    ax.yaxis.set_visible(False)
        figname = observatory + '_qlimg_' + plttime.isot.replace(':', '').replace('-', '')[:19] + '.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: ' + os.path.join(figdir_, figname)
        plt.savefig(os.path.join(figdir_, figname))
    plt.close(fig)
    DButil.img2html_movie(figdir_)
Ejemplo n.º 28
0
def imfit_iter(imgfiles, doreg, tims, msinfofile, ephem, box, region, chans,
               stokes, mask, includepix, excludepix, residual, model,
               estimates, logfile, append, newestimates, complist, overwrite,
               dooff, offset, fixoffset, stretch, rms, noisefwhm, summary,
               imidx):
    from taskinit import iatool, rg
    import pdb
    try:
        from astropy.io import fits as pyfits
    except:
        try:
            import pyfits
        except ImportError:
            raise ImportError(
                'Neither astropy nor pyfits exists in this CASA installation')
    img = imgfiles[imidx]

    if doreg:
        # check if ephemfile and msinfofile exist
        if not ephem:
            print("ephemeris info does not exist!")
            return
        if not tims:
            print("timestamp of the image does not exist!")
            return
        try:
            tim = tims[imidx]
            helio = vla_prep.ephem_to_helio(msinfo=msinfofile,
                                            ephem=ephem,
                                            reftime=tim)
            fitsfile = [img.replace('.image', '.fits')]
            vla_prep.imreg(imagefile=[img],
                           fitsfile=fitsfile,
                           helio=helio,
                           toTb=False,
                           scl100=True)
            img = img.replace('.image', '.fits')
        except:
            print 'Failure in vla_prep. Skipping this image file: ' + img

    myia = iatool()
    try:
        if (not myia.open(img)):
            raise Exception, "Cannot create image analysis tool using " + img
        print('Processing image: ' + img)
        hdr = pyfits.getheader(img)
        pols = DButil.polsfromfitsheader(hdr)
        ndx, ndy, nchans, npols = myia.shape()
        results = {}
        for itpp in pols:
            results[itpp] = {}
        for pp, itpp in enumerate(pols):
            r = rg.box(blc=[0, 0, 0, pp], trc=[ndx, ndy, nchans, pp])
            myiapol = myia.subimage(region=r, dropdeg=True)
            results[itpp] = myiapol.fitcomponents(box=box,
                                                  region=region,
                                                  chans=chans,
                                                  stokes=stokes,
                                                  mask=mask,
                                                  includepix=includepix,
                                                  excludepix=excludepix,
                                                  residual=residual,
                                                  model=model,
                                                  estimates=estimates,
                                                  logfile=logfile,
                                                  append=append,
                                                  newestimates=newestimates,
                                                  complist=complist,
                                                  overwrite=overwrite,
                                                  dooff=dooff,
                                                  offset=offset,
                                                  fixoffset=fixoffset,
                                                  stretch=stretch,
                                                  rms=rms,
                                                  noisefwhm=noisefwhm,
                                                  summary=summary)
        # update timestamp

        timstr = hdr['date-obs']
        return [True, timstr, img, results]
    except Exception, instance:
        casalog.post(str('*** Error in imfit ***') + str(instance))
        # raise instance
        return [False, timstr, img, {}]
Ejemplo n.º 29
0
    def plot_stackplot(self, mapcube=None, hdr=False, vmax=None, vmin=None, cmap=None, layout_vert=False, diff=False, uni_cm=False, sav_img=False,
                       out_dir=None, dpi=100, anim=False, frm_range=None, cutslitplt=None, silent=False, refresh=True):
        if mapcube:
            mapcube_plot = deepcopy(mapcube)
        else:
            mapcube_plot = self.mapcube_plot
        if mapcube_plot is None:
            print('No mapcube found. Load a mapcube first!')
            return
        if not isinstance(mapcube_plot, sunpy.map.mapcube.MapCube):
            print('mapcube must be a instance of sunpy.map.mapcube.MapCube')
            return
            maplist = []
            for idx, smap in enumerate(tqdm(mapcube_plot)):
                smap = DButil.sdo_aia_scale_hdr(smap)
                maplist.append(sunpy.map.Map(smap.data, mapcube_plot[idx].meta))
            mapcube_plot = sunpy.map.Map(maplist, cube=True)
        if refresh:
            self.make_stackplot(mapcube_plot, frm_range=frm_range)
        if layout_vert:
            fig_mapcube = plt.figure(figsize=(7, 7))
        else:
            fig_mapcube = plt.figure(figsize=(14, 7))
        try:
            clrange = DButil.sdo_aia_scale_dict(mapcube_plot[0].meta['wavelnth'])
        except:
            clrange = {'high': None, 'log': False, 'low': None}
        if not vmax:
            vmax = clrange['high']
        if not vmin:
            vmin = clrange['low']
        norm = colors.Normalize(vmin=np.min(self.stackplt), vmax=np.max(self.stackplt))
        cutslitplt = self.cutslitbd.cutslitplt
        if not cmap:
            try:
                cmap = cm.get_cmap('sdoaia{}'.format(mapcube_plot[0].meta['wavelnth']))
            except:
                cmap = 'gray_r'

        dspec = {'dspec': self.stackplt, 'x': self.tplt.plot_date, 'y': cutslitplt['dist'], 'ytitle': 'Distance [arcsec]',
                 'ctitle': 'DN counts per second', 'args': {'norm': norm, 'cmap': cmap}}

        dtplot = np.mean(np.diff(self.tplt.plot_date))
        dspec['axvspan'] = [self.tplt[0].plot_date, self.tplt[0].plot_date + dtplot]
        if sav_img:
            if out_dir is None:
                out_dir = './'

            ax, im1, ax2, im2, vspan = self.plot_map(mapcube_plot[0], dspec, vmax=vmax, vmin=vmin, diff=diff, returnImAx=True, uni_cm=uni_cm,
                                                layout_vert=layout_vert)
            plt.subplots_adjust(bottom=0.10)
            ax.plot(cutslitplt['xcen'], cutslitplt['ycen'], color='white', ls='solid')
            ax.plot(cutslitplt['xs0'], cutslitplt['ys0'], color='white', ls='dotted')
            ax.plot(cutslitplt['xs1'], cutslitplt['ys1'], color='white', ls='dotted')
            if anim:
                import matplotlib
                matplotlib.use("Agg")
                import matplotlib.animation as animation
                nframe = len(mapcube_plot)

                def update_frame2(num):
                    frm = int(num)
                    smap = mapcube_plot[frm]
                    # smap.data[smap.data<1]=1
                    im1.set_data(smap.data)
                    # im1.set_extent(list(smap.xrange.value) + list(smap.yrange.value))
                    vspan_xy = vspan.get_xy()
                    vspan_xy[np.array([0, 1, 4]), 0] = self.tplt[frm].plot_date
                    if frm < len(self.tplt) - 1:
                        vspan_xy[np.array([2, 3]), 0] = self.tplt[frm + 1].plot_date
                    else:
                        vspan_xy[np.array([2, 3]), 0] = self.tplt[frm].plot_date
                    vspan.set_xy(vspan_xy)
                    ax.set_title('{} {} {} {}'.format(smap.observatory, smap.detector, smap.wavelength, smap.meta['t_obs']))
                    fig_mapcube.canvas.draw()
                    return

                ani = animation.FuncAnimation(fig_mapcube, update_frame2, nframe, interval=50, blit=False)

            if not silent:
                prompt = ''
                while not (prompt.lower() in ['y', 'n']):
                    prompt = raw_input('Satisfied with current FOV? [y/n]')
                if prompt.lower() == 'n':
                    return
            if anim:
                print 'Saving movie to {}'.format(out_dir)
                Writer = animation.writers['ffmpeg']
                writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
                ani.save('{0}/Stackplot-{2}{1}.mp4'.format(out_dir, mapcube_plot[0].meta['wavelnth'], mapcube_plot[0].detector), writer=writer)
            else:
                plt.ioff()
                print 'Saving images to {}'.format(out_dir)
                for frm, smap in enumerate(tqdm(mapcube_plot)):
                    im1.set_data(smap.data)
                    # im1.set_extent(list(smap.xrange.value) + list(smap.yrange.value))
                    # ax.set_xlim(list(smap.xrange.value))
                    # ax.set_ylim(list(smap.yrange.value))
                    if smap.meta.has_key('t_obs'):
                        tstr = smap.meta['t_obs']
                    else:
                        tstr = smap.meta['date-obs']
                    ax.set_title('{} {} {} {}'.format(smap.observatory, smap.detector, smap.wavelength, tstr))
                    vspan_xy = vspan.get_xy()
                    vspan_xy[np.array([0, 1, 4]), 0] = self.tplt[frm].plot_date
                    if frm < len(self.tplt) - 1:
                        vspan_xy[np.array([2, 3]), 0] = self.tplt[frm + 1].plot_date
                    else:
                        vspan_xy[np.array([2, 3]), 0] = self.tplt[frm].plot_date
                    vspan.set_xy(vspan_xy)
                    t_map = Time(tstr)
                    fig_mapcube.canvas.draw()
                    fig_mapcube.savefig('{0}/Stackplot-{3}{1}-{2}.png'.format(out_dir, smap.meta['wavelnth'],
                                                                              t_map.iso.replace(' ', 'T').replace(':', '').replace('-', '')[:-4],
                                                                              smap.detector), format='png', dpi=dpi)
                plt.ion()
        else:
            ax, im1, ax2, im2, vspan = self.plot_map(mapcube_plot[0], dspec, vmax=vmax, vmin=vmin, diff=diff, returnImAx=True, uni_cm=uni_cm,
                                                layout_vert=layout_vert)
            plt.subplots_adjust(bottom=0.10)
            ax.plot(cutslitplt['xcen'], cutslitplt['ycen'], color='white', ls='solid')
            ax.plot(cutslitplt['xs0'], cutslitplt['ys0'], color='white', ls='dotted')
            ax.plot(cutslitplt['xs1'], cutslitplt['ys1'], color='white', ls='dotted')
            axcolor = 'lightgoldenrodyellow'
            axframe2 = plt.axes([0.1, 0.03, 0.40, 0.02], facecolor=axcolor)
            sframe2 = Slider(axframe2, 'frame', 0, len(mapcube_plot) - 1, valinit=0, valfmt='%0.0f')

            def update2(val):
                frm = int(sframe2.val)
                smap = mapcube_plot[frm]
                im1.set_data(smap.data)
                if smap.meta.has_key('t_obs'):
                    tstr = smap.meta['t_obs']
                else:
                    tstr = smap.meta['date-obs']
                ax.set_title('{} {} {} {}'.format(smap.observatory, smap.detector, smap.wavelength, tstr))
                vspan_xy = vspan.get_xy()
                vspan_xy[np.array([0, 1, 4]), 0] = self.tplt[frm].plot_date
                if frm < len(self.tplt) - 1:
                    vspan_xy[np.array([2, 3]), 0] = self.tplt[frm + 1].plot_date
                else:
                    vspan_xy[np.array([2, 3]), 0] = self.tplt[frm].plot_date
                vspan.set_xy(vspan_xy)
                fig_mapcube.canvas.draw()

            sframe2.on_changed(update2)
        return
Ejemplo n.º 30
0
    def plot_map(self, smap, dspec=None, diff=False, cmap=None, SymLogNorm=False, linthresh=0.5, returnImAx=False, layout_vert=False, uni_cm=False, *args,
                 **kwargs):
        import sunpy.cm.cm as cm  ## to bootstrap sdoaia color map
        import matplotlib.cm as cm
        import matplotlib.colors as colors
        try:
            clrange = DButil.sdo_aia_scale_dict(wavelength=smap.meta['wavelnth'])
        except:
            clrange = {'high': None, 'log': False, 'low': None}
        plt.clf()
        if dspec:
            if layout_vert:
                ax = plt.subplot(211)
            else:
                ax = plt.subplot(121)
        else:
            ax = plt.subplot()
        if 'vmin' in kwargs.keys():
            vmin = kwargs['vmin']
        else:
            vmin = clrange['low']
        if 'vmax' in kwargs.keys():
            vmax = kwargs['vmax']
        else:
            vmax = clrange['high']
        if diff:
            if SymLogNorm:
                norm = colors.SymLogNorm(linthresh=linthresh, vmin=vmin, vmax=vmax)
            else:
                norm = colors.Normalize(vmin=vmin, vmax=vmax)
        else:
            if clrange['log']:
                norm = colors.LogNorm(vmin=vmin, vmax=vmax)
            else:
                norm = colors.Normalize(vmin=vmin, vmax=vmax)

        if not cmap:
            try:
                cmap = cm.get_cmap('sdoaia{}'.format(smap.meta['wavelnth']))
            except:
                cmap = 'gray_r'
        imshow_args = {'cmap': cmap, 'norm': norm, 'interpolation': 'nearest', 'origin': 'lower'}
        try:
            if smap.coordinate_system.x == 'HG':
                xlabel = 'Longitude [{lon}]'.format(lon=smap.spatial_units.x)
            else:
                xlabel = 'X-position [{xpos}]'.format(xpos=smap.spatial_units.x)
            if smap.coordinate_system.y == 'HG':
                ylabel = 'Latitude [{lat}]'.format(lat=smap.spatial_units.y)
            else:
                ylabel = 'Y-position [{ypos}]'.format(ypos=smap.spatial_units.y)
        except:
            if smap.coordinate_system.axis1 == 'HG':
                xlabel = 'Longitude [{lon}]'.format(lon=smap.spatial_units.axis1)
            else:
                xlabel = 'X-position [{xpos}]'.format(xpos=smap.spatial_units.axis1)
            if smap.coordinate_system.axis2 == 'HG':
                ylabel = 'Latitude [{lat}]'.format(lat=smap.spatial_units.axis2)
            else:
                ylabel = 'Y-position [{ypos}]'.format(ypos=smap.spatial_units.axis2)

        ax.set_xlabel(xlabel)
        ax.set_ylabel(ylabel)
        imshow_args.update({'extent': list(smap.xrange.to(u.arcsec).value) + list(smap.yrange.to(u.arcsec).value)})
        if smap.detector == 'HMI':
            im1 = ax.imshow(np.rot90(smap.data, 2), **imshow_args)
        else:
            im1 = ax.imshow(smap.data, **imshow_args)
        plt.title('{} {} {} {}'.format(smap.observatory, smap.detector, smap.wavelength, smap.meta['date-obs']))
        self.divider_im = make_axes_locatable(ax)
        cax = self.divider_im.append_axes('right', size='1.5%', pad=0.05)
        cax.tick_params(direction='in')
        plt.colorbar(im1, ax=ax, cax=cax, label='DN counts per second')
        ax.set_autoscale_on(False)
        if dspec:
            fig = plt.gcf()
            # if figsize:
            #     fig.set_size_inches(figsize)
            # else:
            #     fig.set_size_inches(13, 5)
            if uni_cm:
                dspec['args']['norm'] = norm
            if layout_vert:
                ax2 = plt.subplot(212)
            else:
                ax2 = plt.subplot(122)
            im2 = plt.pcolormesh(dspec['x'], dspec['y'], dspec['dspec'], **dspec['args'])
            date_format = mdates.DateFormatter('%H:%M:%S')
            ax2.xaxis_date()
            ax2.xaxis.set_major_formatter(date_format)
            for xlabel in ax2.get_xmajorticklabels():
                xlabel.set_rotation(30)
                xlabel.set_horizontalalignment("right")
            ax2.yaxis.set_label_text(dspec['ytitle'])
            self.divider_dspec = make_axes_locatable(ax2)
            cax = self.divider_dspec.append_axes('right', size='1.5%', pad=0.05)
            cax.tick_params(direction='in')
            plt.colorbar(im2, ax=ax2, cax=cax, label=dspec['ctitle'])
            ax2.set_autoscale_on(False)
            if 'axvspan' in dspec.keys():
                vspan = ax2.axvspan(dspec['axvspan'][0], dspec['axvspan'][1], alpha=0.5, color='white')
            if 'xs' in dspec.keys() and 'ys' in dspec.keys():
                ax2.plot(dspec['xs'], dspec['ys'], '--', lw=2.0, alpha=0.7, c='black')
            if 'xlim' in dspec.keys():
                ax2.set_xlim(dspec['xlim'])
            if 'ylim' in dspec.keys():
                ax2.set_ylim(dspec['ylim'])
            if returnImAx:
                return ax, im1, ax2, im2, vspan
            else:
                return ax, ax2
        else:
            if returnImAx:
                return ax, im1
            else:
                return ax  # ax.autoscale(True, 'both', True)  # ax.autoscale_view(True, True, True)  # ax.relim(visible_only=True)