def map_rot_correct(mmap, refx, refy, reftime): """ Parameters ---------- Returns ------- Notes ----- Example ------- """ t = mmap.date if type(refx) == float: refx *= u.arcsec if type(refy) == float: refy *= u.arcsec x, y = rot_hpc(refx, refy, reftime, t) sx = x - refx sy = y - refy smap = mmap.shift(-sx, -sy) return smap
def calculate_solar_rotate_shift(mc, layer_index=0, **kwargs): """ Calculate the shift that must be applied to each layer of a mapcube in order to compensate for solar rotation. The center of the map is used to calculate the position of each mapcube layer. Shifts are calculated relative to a specified layer in the mapcube. When using this functionality, it is a good idea to check that the shifts that were applied to were reasonable and expected. One way of checking this is to animate the original mapcube, animate the derotated mapcube, and compare the differences you see to the calculated shifts. An example use is as follows. If you select data from the SDO cutout service, it is common to not use the solar tracking implemented by this service. This is because (at time of writing) the solar tracking implemented by that service moves the image by single pixels at a time. This is not optimal for many use cases, as it introduces artificial jumps in the data. So with solar tracking not chosen, the selected area is like a window through which you can see the Sun rotating underneath. mc : `sunpy.map.MapCube` The input mapcube. layer_index : int The index layer. Shifts are calculated relative to the time of this layer. ``**kwargs`` These keywords are passed to the function `sunpy.physics.differential_rotation.rot_hpc`. Returns ------- x, y : `~astropy.units.Quantity`, ~astropy.units.Quantity` The shifts relative to the index layer that can be applied to the input mapcube in order to compensate for solar rotation. The shifts are given in helioprojective co-ordinates. """ # Size of the data nt = len(mc.maps) # Storage for the shifts in arcseconds xshift_arcseconds = np.zeros((nt)) * u.arcsec yshift_arcseconds = np.zeros_like(xshift_arcseconds) # Calculate the rotations and the shifts for i, m in enumerate(mc): # Calculate the rotation of the center of the map 'm' at its # observation time to the observation time of the reference layer # indicated by "layer_index". newx, newy = rot_hpc(m.center.Tx, m.center.Ty, m.date, mc.maps[layer_index].date, **kwargs) # Calculate the shift in arcseconds xshift_arcseconds[i] = newx - mc.maps[layer_index].center.Tx yshift_arcseconds[i] = newy - mc.maps[layer_index].center.Ty return {"x": xshift_arcseconds, "y": yshift_arcseconds}
def calculate_solar_rotate_shift(mc, layer_index=0, **kwargs): """ Calculate the shift that must be applied to each layer of a mapcube in order to compensate for solar rotation. The center of the map is used to calculate the position of each mapcube layer. Shifts are calculated relative to a specified layer in the mapcube. When using this functionality, it is a good idea to check that the shifts that were applied to were reasonable and expected. One way of checking this is to animate the original mapcube, animate the derotated mapcube, and compare the differences you see to the calculated shifts. An example use is as follows. If you select data from the SDO cutout service, it is common to not use the solar tracking implemented by this service. This is because (at time of writing) the solar tracking implemented by that service moves the image by single pixels at a time. This is not optimal for many use cases, as it introduces artificial jumps in the data. So with solar tracking not chosen, the selected area is like a window through which you can see the Sun rotating underneath. mc : `sunpy.map.MapCube` The input mapcube. layer_index : int The index layer. Shifts are calculated relative to the time of this layer. ``**kwargs`` These keywords are passed to the function `sunpy.physics.differential_rotation.rot_hpc`. Returns ------- x, y : `~astropy.units.Quantity`, ~astropy.units.Quantity` The shifts relative to the index layer that can be applied to the input mapcube in order to compensate for solar rotation. The shifts are given in helioprojective co-ordinates. """ # Size of the data nt = len(mc.maps) # Storage for the shifts in arcseconds xshift_arcseconds = np.zeros((nt)) * u.arcsec yshift_arcseconds = np.zeros_like(xshift_arcseconds) # Calculate the rotations and the shifts for i, m in enumerate(mc): # Calculate the rotation of the center of the map 'm' at its # observation time to the observation time of the reference layer # indicated by "layer_index". newx, newy = rot_hpc(m.center.x, m.center.y, m.date, mc.maps[layer_index].date, **kwargs) # Calculate the shift in arcseconds xshift_arcseconds[i] = newx - mc.maps[layer_index].center.x yshift_arcseconds[i] = newy - mc.maps[layer_index].center.y return {"x": xshift_arcseconds, "y": yshift_arcseconds}
def test_rot_hpc(): # testing along the Sun-Earth line, observer is on the Earth x, y = rot_hpc(451.4 * u.arcsec, -108.9 * u.arcsec, '2012-06-15', '2012-06-15 16:05:23') np.testing.assert_almost_equal(x.to(u.arcsec).value, 574.2, decimal=1) np.testing.assert_almost_equal(y.to(u.arcsec).value, -108.4, decimal=1) # Test that astropy Angles are returned and that they have the expected # units isinstance(x, Angle) x.unit == u.arcsec isinstance(y, Angle) y.unit == u.arcsec
############################################################################## # Next let's show how to this looks like on the Sun. # Load in an AIA map: aia_map = sunpy.map.Map(sunpy.data.sample.AIA_171_IMAGE) ############################################################################## # Let's define our starting coordinates hpc_y = u.Quantity(np.arange(-700, 800, 100), u.arcsec) hpc_x = np.zeros_like(hpc_y) ############################################################################## # Let's define how many days in the future we want to rotate to dt = timedelta(days=4) future_date = aia_map.date + dt ############################################################################## # Now let's plot the original and rotated positions on the AIA map. fig = plt.figure() ax = plt.subplot() aia_map.plot() ax.set_title('The effect of {0} days of differential rotation'.format(dt.days)) aia_map.draw_grid() for this_hpc_x, this_hpc_y in zip(hpc_x, hpc_y): new_hpc_x, new_hpc_y = rot_hpc(this_hpc_x, this_hpc_y, aia_map.date, future_date) plt.plot([this_hpc_x.value, new_hpc_x.value], [this_hpc_y.value, new_hpc_y.value], 'o-') plt.ylim(-1000, 1000) plt.xlim(-1000, 1000) plt.show()
def update_fiss_header(file, alignfile, **kwargs): """ Update the header of FISS data Parameters ---------- Returns ------- Notes ----- """ sil = kwargs.pop('sil', False) sol_rot = kwargs.pop('sol_rot', False) if not sil: print('Add the align information to the haeder.') level = alignfile[-8:-4] inform = np.load(alignfile) fissht = [getheader(i) for i in file] fissh = [fits.getheader(i) for i in file] tlist = [i['date'] for i in fissht] time = Time(tlist, format='isot', scale='ut1') angle = inform['angle'] ny = fissht[0]['naxis2'] nx = fissht[0]['naxis3'] x = np.array((0, nx - 1, nx - 1, 0)) y = np.array((0, 0, ny - 1, ny - 1)) xc = inform['xc'].item() yc = inform['yc'].item() dx = inform['dx'] dy = inform['dy'] xt1, yt1 = rot_trans(x, y, xc, yc, angle.max()) xt2, yt2 = rot_trans(x, y, xc, yc, angle.min()) tmpx = np.concatenate((xt1, xt2)) tmpy = np.concatenate((yt1, yt2)) xmargin = int(np.abs(np.round(tmpx.min() + dx.min()))) + 1 ymargin = int(np.abs(np.around(tmpy.min() + dy.min()))) + 1 if level == 'lev0': for i, h in enumerate(fissh): h['alignl'] = (0, 'Alignment level') h['reflect'] = (False, 'Mirror reverse') h['reffr'] = (inform['reffr'].item(), 'Reference frame in alignment') h['reffi'] = (inform['reffi'].item(), 'Reference file name in alignment') h['cdelt2'] = (0.16, 'arcsec per pixel') h['cdelt3'] = (0.16, 'arcsec per pixel') h['crota2'] = (angle[i], 'Roation angle about reference pixel') h['crpix3'] = (inform['xc'].item(), 'Reference pixel in data axis 3') h['shift3'] = (inform['dx'][i], 'Shifting pixel value along data axis 2') h['crpix2'] = (inform['yc'].item(), 'Reference pixel in data axis 2') h['shift2'] = (inform['dy'][i], 'Shifting pixel value along data axis 3') h['margin2'] = (ymargin, 'Rotation margin in axis 2') h['margin3'] = (xmargin, 'Rotation margin in axis 3') h['history'] = 'FISS aligned (lev0)' elif level == 'lev1': wcsx = inform['wcsx'] wcsy = inform['wcsy'] xref = wcsx * u.arcsec yref = wcsy * u.arcsec reffr = inform['reffr'] for i, h in enumerate(fissh): if sol_rot: wcsx, wcsy = rot_hpc(xref, yref, time[reffr], time[i]) h['crval3'] = (wcsx.value, 'Location of ref pixel x (arcsec)') h['crval2'] = (wcsy.value, 'Location of ref pixel y (arcsec)') else: h['crval3'] = ( wcsx.item(), 'Location of ref pixel for ref frame x (arcsec)') h['crval2'] = ( wcsy.item(), 'Location of ref pixel for ref frame y (arcsec)') h['alignl'] = (1, 'Alignment level') h['reflect'] = (inform['reflect'].item(), 'Mirror reverse') h['reffr'] = (inform['reffr'].item(), 'Reference frame in alignment') h['reffi'] = (inform['reffi'].item(), 'Reference file name in alignment') h['cdelt2'] = (0.16, 'arcsec per pixel') h['cdelt3'] = (0.16, 'arcsec per pixel') h['crota1'] = (inform['sdo_angle'].item(), 'Rotation angle of reference frame (radian)') h['crota2'] = (inform['angle'][i], 'Rotation angle about reference pixel (radian)') h['crpix3'] = (inform['xc'].item(), 'Reference pixel in data axis 3') h['shift3'] = (inform['dx'][i], 'Shifting pixel value along data axis 3') h['crpix2'] = (inform['yc'].item(), 'Reference pixel in data axis 2') h['shift2'] = (inform['dy'][i], 'Shifting pixel value along data axis 2') h['margin2'] = (ymargin, 'Rotation margin in axis 2') h['margin3'] = (xmargin, 'Rotation margin in axis 3') h['srot'] = (True, 'Solar Rotation correction') h['history'] = 'FISS aligned and matched wcs (lev1)' else: raise ValueError('The level of alignfile is neither lev0 or lev1.') data = [fits.getdata(i) for i in file] odirname = os.path.dirname(file[0]) if not odirname: odirname = os.getcwd() dirname = odirname + os.sep + 'match' try: os.mkdir(dirname) except: pass for i, oname in enumerate(file): name = 'm' + os.path.basename(oname) fits.writeto(dirname + os.sep + name, data[i], fissh[i]) try: pfilelist = [i['pfile'] for i in fissh] pfileset = set(pfilelist) for i in pfileset: copy2(odirname + os.sep + i, dirname + os.sep + i) except: pass if not sil: print("The align information is updated to the header, " "and new fts file is locate %s the file name is '*_cm.fts'" % dirname)