def prep_files(wave): aia_files_131 = glob.glob( '/Users/laurahayes/QPP/interesting_event_2014-611/aia_full/*{:s}*.fits' .format(wave)) aia_files_131.sort() maps = [] for f in aia_files_131: try: m = sunpy.map.Map(f) maps.append(m) except: print(f) map_list = [] for m in maps: m_updated_pointing = update_pointing(m) m_registered = register(m_updated_pointing) #m_normalized = sunpy.map.Map(m_registered.data/m_registered.exposure_time.to(u.s).value, #m_registered.meta) map_list.append(m_registered) map_list = sunpy.map.Map(map_list, sequence=True) for m in map_list: m.save(savedir + 'aia_{:s}_'.format(wave) + str(m.date) + '.fits')
def aia_prep(aia_map): """ Prep an AIA map. This runs: - `aiapy.calibrate.update_pointing` - `aiapy.calibrate.register` - `calibrate.correct_degradation(aia_map)` And normalises the data by exposure time to get the data in units of DN/s. Parameters ---------- aia_map : sunpy.map.sources.AIAMap A full disc AIA map. Returns ------- aia_map : sunpy.map.sources.AIAMap Prepped map. """ from aiapy import calibrate aia_map = calibrate.update_pointing(aia_map) aia_map = calibrate.register(aia_map) aia_map = calibrate.correct_degradation(aia_map) # Convert from DN to DN/s aia_map = sunpy.map.Map(aia_map.data / aia_map.exposure_time.to_value(u.s), aia_map.meta) return aia_map
def aiaprep(fitsFile): map = Map(fitsFile) from aiapy.calibrate import register, update_pointing, normalize_exposure m_updated_pointing = update_pointing(map) m_registered = register(m_updated_pointing) m_normalized = normalize_exposure(m_registered) return m_normalized
def run_prep(m): m_updated_pointing = update_pointing(m) m_registered = register(m_updated_pointing) data_normalized = m_registered.data/m_registered.exposure_time.to(u.s).value meta_normalized = m_registered.meta meta_normalized['EXPTIME'] = 1.0 m_normalized = sunpy.map.Map(data_normalized, meta_normalized) return m_normalized
def prep(aia_sunpy_map, isHMI=False): from aiapy.calibrate import register, update_pointing, normalize_exposure if not isHMI: aia_sunpy_map = update_pointing(aia_sunpy_map) del aia_sunpy_map m_registered = register(m_updated_pointing) del m_updated_pointing return normalize_exposure(m_registered) else: return register(aia_sunpy_map)
def gen_lvl_1p5(lvl1_fname_list,lvl1_path): for af in lvl1_fname_list: m = sunpy.map.Map(lvl1_path+'/'+af) m_up = update_pointing(m) m_reg = register(m_up) m_norm = sunpy.map.Map(m_reg.data/m_reg.exposure_time.to(u.s).value, m_reg.meta) new_name = af.replace('lev1', 'lev1.5') m_norm.save(lvl1_path+'/'+new_name) print('to lvl 1.5: {} at {}'.format(str(m_norm.wavelength),str(m_norm.date))) return
def do_prep(mapy): # calculate psf if local file doesn't exist if not os.path.exists( "/Users/laurahayes/QPP/interesting_event_2014-611/aia_ana/psf_{:s}.npy" .format(wave)): print("calculating psf as file not found") if isinstance(mm, list): psf = aiapy.psf.psf(mm[0].wavelength) else: psf = aiapy.psf.psf(mm.wavelength) np.save("psf_{:s}".format(wave), psf) # load in psf array and deconvolve psf = np.load("psf_{:s}.npy".format(wave)) t1 = time.time() m_deconvolved = aiapy.psf.deconvolve(mapy, psf=psf) t2 = time.time() - t1 print("deconvolution done {:f}".format(t2)) # aia_prep to level 1.5 t1 = time.time() m_updated_pointing = update_pointing(m_deconvolved) m_registered = register(m_updated_pointing) m_normalized = normalize_exposure(m_registered) t3 = time.time() - t1 def reduce_mem(m): return m._new_instance(m.data.astype(np.float32), m.meta) final_maps = reduce_mem(m_normalized) basepath = "/Users/laurahayes/QPP/interesting_event_2014-611/aia_prepped_deconvolve/" filename = "prepped_aia_{:.0f}_{:s}.fits".format( final_maps.wavelength.value, final_maps.date.strftime("%Y%m%d_%H%M%S")) t1 = time.time() final_maps.save(basepath + filename) t4 = time.time() - t1 print("it took {:f} seconds to deconvolve".format(t2)) print("it took {:f} seconds to prep".format(t3)) print("it took {:f} seconds to write".format(t4))
# commands I used in iPython (prob should be a Juypter notebook?) #caveats: openCV implementation has a wrong pixel center or padding (I think) # cupy implementation is not ideal, I just did the simplest # possible array copying and function replacement import update_register as ur from aiapy.calibrate import register, update_pointing import sunpy.map path = './AIA_data/171A/aia_lev1_171a_2017_09_10t01_17_09_35z_image_lev1.fits' m = sunpy.map.Map(path) m_up = update_pointing(m) %timeit m_r1 = register(m_up) %timeit m_r2 = ur.cfd_register(m_up) %timeit m_r3 = ur.CV_register(m_up) %timeit m_r4 = ur.cupy_register(m_up) """ local machine, WSL: OG: ~12 sec JI, new cfd: ~3sec openCV: 1.5 sec (no cupy) NVIDIA raplab: OG: ~8 sec JI, new cfd: ~2.1 sec (no openCV) cupy: either ~2.1 sec (first call) or 1.4 sec (subsequent)
import aiapy.psf as psf_ from aiapy.calibrate import register, update_pointing import os ############################################################################## # Use the SunPy tool `Fido <https://docs.sunpy.org/en/v3.0.1/guide/acquiring_data/fido.html>`_ to find and download level 1 AIA data. q = Fido.search(a.Time('2011-06-07T06:52:00', '2011-06-07T06:52:10'), a.Instrument('AIA'), a.Wavelength(wavemin=171*u.angstrom, wavemax=171*u.angstrom)) aia_map = sunpy.map.Map(Fido.fetch(q)) ############################################################################## # Convert to level 1.5 AIA data. See the `registering and aligning level 1 data <https://aiapy.readthedocs.io/en/latest/generated/gallery/prepping_level_1_data.html>`_ # example in aiapy documentation for more details. m_updated_pointing = update_pointing(aia_map) m_registered = register(m_updated_pointing) m_normalized = sunpy.map.Map( m_registered.data/m_registered.exposure_time.to(u.s).value, m_registered.meta) ############################################################################## # Compute the point-spread function (PSF) and use it to deconvolve the image. # Warning: the PSF computation can take over 16 minutes on a CPU. If you have # an NVIDIA GPU and CuPy installed, then PSF will automatically use it. # See the `PSF documentation <https://aiapy.readthedocs.io/en/latest/api/aiapy.psf.psf.html>`_ for details. psf = psf_.psf(m_normalized.wavelength) map_deconvolved = psf_.deconvolve(m_normalized, psf=psf) ############################################################################## # Save the deconvolved image as a FITS file without compression using SunPy.
def astro_plot( file_list, rel_indexes, all_images, wvln, ROI_LATLON, n_row_col, save_path, threshold=(False, 1), regions_highlight=(), showPreview=False, ): """ This function is analogous to sunpy plotting, but uses astropy maps instead. Optimised for parallel processing :param file_list: List of files from glob :param rel_indexes: Which section this process takes care of :param all_images: List containing all files :param wvln: Wavelength (unitless) :param main_roi: Region of interest to be separated: [x0, y0, length, height] :param n_row_col: Amount of rows and columns to separate in :param vmin_vmax: min and max exposure for the wavelength :param save_path: Path to save figures and lightcurves to :param threshold: Whether to set a threshold or not. If true, then input? :param regions_highlight: Which regions to highlight """ import datetime # Need to open the first fits file! # Create a n x n x n_obs array os.makedirs(save_path, exist_ok=True) # Need to define ROI and exposure limits; Create reference submap to then with respect to it large_roi = Mtool.ROI_defCoords(ROI_LATLON) large_roi_ref = large_roi.copy() smap_ref = sunpy.map.Map(all_images[0]) # Fetch the first map all_hpc = sunpy.map.all_coordinates_from_map(smap_ref) all_hgs = all_hpc.transform_to("heliographic_stonyhurst") # Set out empty time and lcurve ndarrays sub_time_array = np.array(np.arange(len(all_images)), dtype="str") sub_lcurve = np.zeros([n_row_col, n_row_col, len(all_images)]) sub_pixel_array = np.zeros([n_row_col, n_row_col, len(all_images)]) # For each of the images, we need to advance the regions for relevantIndex, file in zip(rel_indexes, file_list): m = sunpy.map.Map(file) m_updated_pointing = update_pointing(m) m_registered = register(m_updated_pointing) aia_curr = normalize_exposure(m_registered) aia_curr_base_copy = deepcopy(aia_curr) header = aia_curr.fits_header sub_time_array[relevantIndex] = datetime.datetime.strptime( header["DATE-OBS"], "%Y-%m-%dT%H:%M:%S.%f") # If not the first Map, we must track the Region of Interest if relevantIndex > 0: large_roi = Mtool.calculateLons(smap_ref, aia_curr, Coords=large_roi_ref) pass small_rois = Mtool.splitCoords(large_roi, n_row_col) # largeSegment = Mtool.getSegment(large_roi, aia_curr, all_hgs_coords=all_hgs) # These are exclusively the images. The order will hold after vector works! for regionIndex, segmentCoord in small_rois: if regionIndex in regions_highlight: smallCurrSegment = Mtool.getSegment(segmentCoord, aia_curr, all_hgs_coords=all_hgs) # We translate the number into new form row, col = translate_number(regionIndex, n_row_col) # Rotate vector such that it matches the image when 'lower' is used smallRegData = aia_curr.data[smallCurrSegment] # subreg_init = smallRegData.copy() # Calculate some threshold and use it to only display much higher than given if threshold[0]: minus_plus_mean = np.array([ smallRegData.mean() - threshold[1] * smallRegData.std(), smallRegData.mean() + threshold[1] * smallRegData.std(), ]) above_two_sd = smallRegData > minus_plus_mean[1] below_two_sd = smallRegData < minus_plus_mean[1] smallRegData[below_two_sd] = 0 ref_mean = smallRegData[above_two_sd].mean() smallRegData[below_two_sd] = np.nan n_pixels = np.count_nonzero(~np.isnan(smallRegData)) sub_pixel_array[row, col, relevantIndex] = n_pixels elif not threshold[0]: n_pixels = np.count_nonzero(~np.isnan(smallRegData)) sub_pixel_array[row, col, relevantIndex] = n_pixels ref_mean = smallRegData.mean() sub_lcurve[row, col, relevantIndex] = ref_mean aia_curr.data[smallCurrSegment] = 0 # TODO : Make a figure using a copy of the base AIA plot, with the area that is selected blacked out # Save as row_col # curr_subRegion_savePath = f"{save_path}/{col:02d}_{row:02d}/" # Showing a preview to check whether working # aia_curr.data[segment] = 0 bottom_left = SkyCoord(250 * u.arcsec, 350 * u.arcsec, frame=aia_curr.coordinate_frame) submap = aia_curr.submap(bottom_left=bottom_left, width=500 * u.arcsec, height=500 * u.arcsec) plt.figure(figsize=(10, 10)) ax = plt.subplot(projection=submap) submap.plot(axes=ax, clip_interval=(1, 99.99) * u.percent) plt.savefig(f"{save_path}{relevantIndex:04d}.png") # plt.show() plt.close() # smallRegData = subreg_init # TODO : Set up a second map that does not black out submap_base_copy = aia_curr_base_copy.submap(bottom_left=bottom_left, width=500 * u.arcsec, height=500 * u.arcsec) plt.figure(figsize=(10, 10)) ax = plt.subplot(projection=submap_base_copy) submap_base_copy.plot(axes=ax, clip_interval=(1, 99.99) * u.percent) # plt.axis("off") # plt.show() filledFolder = f"{save_path}Filled/" os.makedirs(filledFolder, exist_ok=True) plt.savefig(f"{filledFolder}{relevantIndex:04d}.png") plt.close() # Now select the relevant chunk of the vector and cut it to that size. Then we can append it onto the larger one? sub_time_array = sub_time_array[rel_indexes] sub_lcurve = sub_lcurve[:, :, rel_indexes] sub_pixel_array = sub_pixel_array[:, :, rel_indexes] np.save(f"{save_path}lcurves_{rel_indexes[-1]:04d}.npy", sub_lcurve) np.save(f"{save_path}times_{rel_indexes[-1]:04d}.npy", sub_time_array) np.save(f"{save_path}pix_{rel_indexes[-1]:04d}.npy", sub_pixel_array)
def overplot_aia_coords(aiaimg, pch_obj): plt.rcParams.update({'font.size': 16}) aia = Map(aiaimg) if aia.fits_header['lvl_num'] == 1: aia2 = update_pointing(aia) aia = register(aia2) aia_north = pch_obj['AIA171'][0] #aia_north = pch_obj['AIA171'][0].resample('0.5D').median() n_lat = aia_north[np.abs( pd.to_datetime(aia_north.index).date - aia.date.to_datetime().date() ) <= datetime.timedelta(days=8.25)].N_lower_lat.values * u.rad n_lon = aia_north[np.abs( pd.to_datetime(aia_north.index).date - aia.date.to_datetime().date() ) <= datetime.timedelta(days=8.25)].N_mean_lon.values * u.rad times = aia_north[np.abs( pd.to_datetime(aia_north.index).date - aia.date.to_datetime().date()) <= datetime.timedelta(days=8.25)].index n_pts = coordinates.HeliographicStonyhurst(n_lon, n_lat, obstime=times) n_pts_hpc = n_pts.transform_to(aia.coordinate_frame) #fov = 800 * u.arcsec #mid_pt = n_pts_hpc[int(np.floor((len(n_pts_hpc)-1)/2))] #bottom_left = SkyCoord(mid_pt.Tx - fov/2, mid_pt.Ty - fov/2, frame=aia.coordinate_frame) #smap = aia.submap(bottom_left, width=fov, height=fov) #ax = plt.subplot(projection=smap) #smap.plot() #smap.draw_limb() #ax.grid(False) #ax.plot_coord(n_pts_hpc, 'x', color='deepskyblue', label='North PCH') #plt.legend() #plt.show() #------------------------------------ #aia_south = pch_obj['AIA171'][1].resample('0.5D').median() aia_south = pch_obj['AIA171'][1] s_lat = aia_south[np.abs( pd.to_datetime(aia_south.index).date - aia.date.to_datetime().date() ) <= datetime.timedelta(days=8.25)].S_lower_lat.values * u.rad s_lon = aia_south[np.abs( pd.to_datetime(aia_south.index).date - aia.date.to_datetime().date() ) <= datetime.timedelta(days=8.25)].S_mean_lon.values * u.rad times = aia_south[np.abs( pd.to_datetime(aia_south.index).date - aia.date.to_datetime().date()) <= datetime.timedelta(days=8.25)].index s_pts = coordinates.HeliographicStonyhurst(s_lon, s_lat, obstime=times) s_pts_hpc = s_pts.transform_to(aia.coordinate_frame) # fov = 800 * u.arcsec # mid_pt = s_pts_hpc[int(np.floor((len(s_pts_hpc)-1)/2))] # bottom_left = SkyCoord(mid_pt.Tx - fov/2, mid_pt.Ty - fov/2, frame=aia.coordinate_frame) # smap = aia.submap(bottom_left, width=fov, height=fov) smap = aia n_zoom_coords = SkyCoord(Tx=(-200, 200) * u.arcsec, Ty=(850, 1050) * u.arcsec, frame=smap.coordinate_frame) s_zoom_coords = SkyCoord(Tx=(-500, 500) * u.arcsec, Ty=(-1050, -850) * u.arcsec, frame=smap.coordinate_frame) n_zoom = smap.submap(n_zoom_coords) n_zoom.data[0, 0] = smap.min() n_zoom.data[-1, -1] = smap.max() s_zoom = smap.submap(s_zoom_coords) s_zoom.data[0, 0] = smap.min() s_zoom.data[-1, -1] = smap.max() fig = plt.figure(figsize=(10, 10)) ax = plt.subplot(projection=smap) smap.plot() ax.grid(False) ax.plot_coord(n_pts_hpc, 'x', color='cornflowerblue', label='North PCH') ax.plot_coord(s_pts_hpc, 'x', color='rebeccapurple', label='South PCH') smap.draw_quadrangle(n_zoom_coords, edgecolor="white", linestyle="-", linewidth=2) smap.draw_quadrangle(s_zoom_coords, edgecolor="white", linestyle="-", linewidth=2) plt.legend() plt.savefig('/Users/mskirk/Desktop/SWAP Paper Plots/PCH_AIA171_' + str(aia.date.to_datetime().date()) + '.png') fig2 = plt.figure(figsize=(4, 2), frameon=False) ax2 = plt.subplot(projection=n_zoom) n_zoom.plot() ax2.grid(False) ax2.set_axis_off() ax2.set_title('') ax2.plot_coord(n_pts_hpc, 'x', color='cornflowerblue', label='North PCH') plt.savefig('/Users/mskirk/Desktop/SWAP Paper Plots/PCH_AIA171N_' + str(aia.date.to_datetime().date()) + '.png') fig3 = plt.figure(figsize=(10, 2), frameon=False) ax2 = plt.subplot(projection=s_zoom) s_zoom.plot() ax2.grid(False) ax2.set_axis_off() ax2.set_title('') ax2.plot_coord(s_pts_hpc, 'x', color='rebeccapurple', label='South PCH') plt.savefig('/Users/mskirk/Desktop/SWAP Paper Plots/PCH_AIA171S_' + str(aia.date.to_datetime().date()) + '.png')