"rm -rf resources/region-data/raw-images && mkdir resources/region-data/raw-images" ) os.system( "rm -rf resources/region-data/r-masked-images && mkdir resources/region-data/r-masked-images" ) os.system( "rm -rf resources/region-data/e-masked-images && mkdir resources/region-data/e-masked-images" ) os.system( "rm -rf resources/region-data/c-masked-images && mkdir resources/region-data/c-masked-images" ) ##### import data RECORDER.info_text("Importing AIA171 data") MAPCUBE_AIA_171 = smap.Map("/Volumes/Nicholas Data/AIA171/*.fits", sequence=True) # MAPCUBE_AIA_171 = smap.Map("%s/resources/aia-fits-files/*.171.image_lev1.fits" % MAIN_DIR, sequence = True) RECORDER.info_text("Importing AIA304 data") MAPCUBE_AIA_304 = smap.Map("/Volumes/Nicholas Data/AIA304/*.fits", sequence=True) # MAPCUBE_AIA_304 = smap.Map("%s/resources/aia-fits-files/*.304.image_lev1.fits" % MAIN_DIR, sequence = True) RECORDER.info_text("Importing HMI magnetogram data\n") MAPCUBE_HMI = smap.Map("/Volumes/Nicholas Data/HMI/*.fits", sequence=True) # MAPCUBE_HMI = smap.Map("%s/resources/hmi-fits-files/*.fits" % MAIN_DIR, sequence = True) ##### prepare mapcube DATA = { "AIA171": MAPCUBE_AIA_171, "AIA304": MAPCUBE_AIA_304, "HMI": MAPCUBE_HMI
data_aia = client.get(result_aia, methods=('URL-FILE_Rice', 'URL-FILE')).wait() print('\n' + str(data_hmi)) print(str(data_aia) + '\n') # Cropping into the active region within the HMI map str_vol_filepath = data_hmi[0][0:-5] + '_Bxyz.npy' xrange = u.Quantity([50, 300] * u.arcsec) yrange = u.Quantity([-350, -100] * u.arcsec) zrange = u.Quantity([0, 250] * u.arcsec) xrangeextended = u.Quantity([xrange.value[0] - 50, xrange.value[1] + 50] * xrange.unit) yrangeextended = u.Quantity([yrange.value[0] - 50, yrange.value[1] + 50] * yrange.unit) # Open the map and create a cropped version for the extrapolation. map_hmi = mp.Map(data_hmi[0]) map_hmi_cropped = map_hmi.submap(xrange, yrange) dimensions = u.Quantity([20, 20] * u.pixel) map_hmi_cropped_resampled = map_hmi_cropped.resample(dimensions, method='linear') # Open the map and create a cropped version for the visualisation. map_boundary = mp.Map(data_hmi[0]) map_boundary_cropped = map_boundary.submap(xrangeextended, yrangeextended) aPotExt = PotentialExtrapolator(map_hmi_cropped_resampled, filepath=str_vol_filepath, zshape=20, zrange=zrange) aMap3D = aPotExt.extrapolate()
from PolarCoronalHoles import PCH_Tools, PCH_Detection, PCH_stats, PCH_series from sunpy import map import sunpy.data.sample import numpy as np from skimage import measure from sunpy.coordinates.utils import GreatArc import matplotlib.pyplot as plt from astropy import units as u import pandas as pd test_map = map.Map(sunpy.data.sample.AIA_171_IMAGE) def assertEquals(var1, var2): if var1 == var2: return True else: return False def test_rsun_pix(): pixels = PCH_Detection.rsun_pix(test_map) if assertEquals(np.sum(pixels), 786.95568651283929): print('Pass') else: print('rsun_pix fails') def test_pch_mask(): PCH_Detection.pch_mask(test_map)
def aia171_test_map(): return smap.Map(test.get_test_filepath('aia_171_level1.fits'))
def test_mapcube_coalign_by_match_template(): # take the AIA image and shift it # Pixel displacements have the y-displacement as the first entry nx = layer_shape[1] ny = layer_shape[0] pixel_displacements = np.asarray([1.6, 10.1]) known_displacements = { 'x': np.asarray([0.0, pixel_displacements[1] * testmap.scale['x']]), 'y': np.asarray([0.0, pixel_displacements[0] * testmap.scale['y']]) } # Create a map that has been shifted a known amount. d1 = sp_shift(testmap.data, pixel_displacements) m1 = map.Map((d1, testmap.meta)) # Create the mapcube mc = map.Map([testmap, m1], cube=True) # Test to see if the code can recover the displacements. Do the coalignment # using the "return_displacements_only" option test_displacements = mapcube_coalign_by_match_template( mc, return_displacements_only=True) # Assert assert_allclose(test_displacements['x'], known_displacements['x'], rtol=5e-2, atol=0) assert_allclose(test_displacements['y'], known_displacements['y'], rtol=5e-2, atol=0) # Test setting the template as a ndarray template_ndarray = testmap.data[ny / 4:3 * ny / 4, nx / 4:3 * nx / 4] test_displacements = mapcube_coalign_by_match_template( mc, template=template_ndarray, return_displacements_only=True) # Assert assert_allclose(test_displacements['x'], known_displacements['x'], rtol=5e-2, atol=0) assert_allclose(test_displacements['y'], known_displacements['y'], rtol=5e-2, atol=0) # Test setting the template as GenericMap submap = testmap.submap([nx / 4, 3 * nx / 4], [ny / 4, 3 * ny / 4], units='pixels') test_displacements = mapcube_coalign_by_match_template( mc, template=submap, return_displacements_only=True) # Assert assert_allclose(test_displacements['x'], known_displacements['x'], rtol=5e-2, atol=0) assert_allclose(test_displacements['y'], known_displacements['y'], rtol=5e-2, atol=0) # Test setting the template as something other than a ndarray and a # GenericMap. This should throw a ValueError. try: test_displacements = mapcube_coalign_by_match_template( mc, template='broken') except ValueError: pass # Test passing in displacements test_apply_displacements = { 'x': -test_displacements['x'], 'y': -test_displacements['y'] } test_displacements = mapcube_coalign_by_match_template( mc, apply_displacements=test_apply_displacements, return_displacements_only=True) assert_allclose(test_displacements['x'], test_apply_displacements['x'], rtol=5e-2, atol=0) assert_allclose(test_displacements['y'], test_apply_displacements['y'], rtol=5e-2, atol=0) # Test returning using the "with_displacements" option test_output = mapcube_coalign_by_match_template(mc, with_displacements=True) # Assert assert (isinstance(test_output[0], map.MapCube)) assert_allclose(test_output[1]['x'], known_displacements['x'], rtol=5e-2, atol=0) assert_allclose(test_output[1]['y'], known_displacements['y'], rtol=5e-2, atol=0) # Test returning with no extra options - the code returns a mapcube only test_output = mapcube_coalign_by_match_template(mc) assert (isinstance(test_output, map.MapCube)) # Test returning with no clipping. Output layers should have the same size # as the original input layer. test_mc = mapcube_coalign_by_match_template(mc, clip=False) assert (test_mc[0].data.shape == testmap.data.shape) assert (test_mc[1].data.shape == testmap.data.shape)
import matplotlib.pyplot as plt from sunpy import map from astropy.io import fits import datetime import os from astropy import units as u date_search = datetime.datetime.utcnow() base_path = '/Users/admin/Documents/solarmonitor_2_0/sol_mon/data/'+date_search.strftime('%Y/%m/%d/')+'fits/' mapy = map.Map(base_path + 'AIA/*.fits') def plot_map(mapy): fig = plt.figure(figsize = (10, 10)) ax = fig.add_subplot(111,projection = mapy) plt.grid(False) mapy.plot(axes = ax, vmin = 0, vmax = mapy.mean() + 6*mapy.std()) mapy.draw_grid(grid_spacing = 10*u.deg, axes = ax)#, ls = 'dashed') ax.set_xlabel('X (arcsec)') ax.set_ylabel('Y (arcsec)') ax.set_title('AIA Tests', pad = 0.005) ax.text(0.76, 0.018, 'SolarMonitor.org', color = 'w', transform = ax.transAxes, fontsize = 'xx-large')
def find_carr_coords(cme_root, craft, c1cme, eucme): """ Function to calculate carrington longitude and latitude of events in eucme and add these to the dataframe. These are used to create plots of each event. """ # Find the euvi fits files if craft == 'A': fn_tag = '*eua.fts' elif craft == 'B': fn_tag = '*eub.fts' # Calculate Carrington Longitudes and Latitudes of source locations eucme['carrington_longitude'] = pd.Series(np.zeros(len(eucme)) * np.NaN, index=eucme.index) eucme['carrington_latitude'] = pd.Series(np.zeros(len(eucme)) * np.NaN, index=eucme.index) for i in range(len(eucme)): print i cme_root_new = r'/home/sq112614/SS/Images/EUVI/ST' + craft cme_root2 = cme_root_new + '/cme_' + '{0:02d}'.format(i + 1) euvi_files = find_files(cme_root2, fn_tag) print len(euvi_files) if len(euvi_files) > 0: print '**********' print cme_root2 print euvi_files # Files exist, load in the subpy map to work out carrington coordinates euvi = smap.Map(euvi_files[0]) # Convert the 256x256 coordinates into correctly scaled coordinates xn = eucme['l'][i] + eucme['w'][i] / 2 yn = eucme['b'][i] + eucme['h'][i] / 2 xn = (xn / 256) * euvi.dimensions[0] yn = euvi.dimensions[1] - (yn / 256) * euvi.dimensions[1] # Get HPC and HPR coords lon, lat = HPC_LONLAT(euvi) el, pa = HPC_to_HPR(lon.to('deg').value, lat.to('deg').value) # Get Carrington coords: cf = euvi.coordinate_frame hpc = spc.Helioprojective(Tx=lon, Ty=lat, D0=cf.D0, B0=cf.B0, L0=cf.L0, representation=cf.representation) hcc = spc.Heliocentric(D0=cf.D0, B0=cf.B0, L0=cf.L0) hcc = spc.hpc_to_hcc(hpc, hcc) hgs = spc.HeliographicStonyhurst(dateobs=cf.dateobs) hgc = spc.HeliographicCarrington(dateobs=cf.dateobs) hgs = spc.hcc_to_hgs(hcc, hgs) hgc = spc.hgs_to_hgc(hgs, hgc) # Try looking up carrington coordinates idy = np.int(np.round(yn.value)) idx = np.int(np.round(xn.value)) hgc_lon = hgc.lon[idy, idx] hgc_lat = hgc.lat[idy, idx] # If returned NaNs extrapolate to limb if np.logical_or(np.isnan(hgc_lon), np.isnan(hgc_lat)): print('Limb Extrapolation') # Get elongation of Limb el_limb = np.arctan(0.99 * euvi.rsun_meters / euvi.dsun).to('deg') # Get position angle of point ppa = pa[idy, idx] # Find pixel best matching this elongation and pa pa_diff = np.abs(pa - ppa) el_diff = np.abs(el - el_limb) diff = np.sqrt(pa_diff**2 + el_diff**2) idy, idx = np.unravel_index(np.argmin(diff), diff.shape) hgc_lon = hgc.lon[idy, idx] hgc_lat = hgc.lat[idy, idx] # Make sure hgc lon runs between 0 and 360, rather than -180, 180 if hgc_lon.value < 0: hgc_lon = 360 * u.deg + hgc_lon euvi.peek() plt.contour(pa.value, levels=[c1cme['pa'][i]], colors=['r']) plt.plot(xn.value, yn.value, 'yo', markersize=12) plt.plot(idx, idy, 'y*', markersize=12) plt.contour(hgc.lon.value, colors=['white']) plt.contour(hgc.lat.value, colors=['m']) plt.draw() name = cme_root2 + r'\euvi_eruption_id.png' plt.savefig(name) plt.close('all') # Mark out the coords for chucking in the array eucme.loc[eucme.index == i, 'carrington_longitude'] = hgc_lon.value eucme.loc[eucme.index == i, 'carrington_latitude'] = hgc_lat.value return eucme
def clean_significance(workdir='', xstart=None, xend=None, ystart=None, yend=None): #recognize signal's area automaticlly(distance from brightest point to center point) #workdir='/srg/ywei/data/eovsa/sep6_dofinal/slfcal/images' fitslist = [] fitslist.append( makelist(tdir=workdir, keyword1='0testing', keyword2='fits')) fitslist.append( makelist(tdir=workdir, keyword1='1testing', keyword2='fits')) fitslist.append( makelist(tdir=workdir, keyword1='2testing', keyword2='fits')) fitslist.append( makelist(tdir=workdir, keyword1='3testing', keyword2='fits')) round_loop = [] for roun in range(4): sp_loop = [] background_list = [] briest_list = [] significance = [] mask = [] dist = [] cur_list = fitslist[roun] for sp in range(30): cur_emap = smap.Map(cur_list[sp]) sz = cur_emap.data.shape backregion = cur_emap.data[0, 0, xstart:xend, ystart:yend] background_list.append(np.sqrt(np.nanmean(np.power(backregion, 2)))) briest_list.append(np.nanmax(cur_emap.data)) significance.append( np.nanmax(cur_emap.data) / np.sqrt(np.nanmean(np.power(backregion, 2)))) cur_mask = seed_grow(im=cur_emap.data[0, 0, :, :], x=0, y=0, threshold=60000.0) #print 'for spw' + str(sp)+', in round ' +str(roun)+' significance is '+ str(np.nanmax(cur_emap.data[0,0,:,:])/np.sqrt(np.nanmean(np.power(backregion,2)))) #print 'for spw' + str(sp)+', in round ' +str(roun)+' significance is '+ str(np.nanmax(cur_emap.data[0,0,:,2])) mask.append(np.nanmean(cur_mask)) cur_im = cur_emap.data[0, 0, :, :] loc = np.where(cur_im == np.nanmax(cur_im)) distance = np.sqrt( abs(loc[0][0] - 128)**2 + abs(loc[1][0] - 128)**2) dist.append(distance) print ' mask now is ' + str(distance) sp_loop.append(background_list) sp_loop.append(briest_list) sp_loop.append(significance) sp_loop.append(mask) sp_loop.append(dist) round_loop.append(sp_loop) #return round_loop print 'shape now is ' + str(round_loop[0][2][0]) rx = [0, 1, 2, 3] #plt.xlabel('nround') #plt.ylabel('significance') #plt.title('spw 10') #plt.plot(rx,[round_loop[0][2][9],round_loop[1][2][9],round_loop[2][2][9],round_loop[3][2][9]],'ro') rsp = np.empty(30) yax = np.empty(30) for nn in range(30): yax[nn] = abs(round_loop[0][4][nn] - round_loop[3][4][nn]) rsp[nn] = nn print yax[nn] plt.plot(rsp, yax, 'ro') #plt.plot(rx,[round_loop[0][2][9],round_loop[1][2][9],round_loop[2][2][9],round_loop[3][2][9]],'ro') plt.show()
def plot_results(): fits_data = fits.open(output_path2) map_halpha = map.Map(fits_data[0].data, fits_data[0].header) map_halpha.plot()