Skip to content

Instantly share code, notes, and snippets.

@dpshelio
Forked from Cadair/gist:5893586
Last active August 29, 2015 14:27
Show Gist options
  • Save dpshelio/e8741c9c29072cabb1a8 to your computer and use it in GitHub Desktop.
Save dpshelio/e8741c9c29072cabb1a8 to your computer and use it in GitHub Desktop.
How to compensate for differential rotation in the default sunpy image. This is based upon a combination of pr/452 and pr/482 my branch for which can be found here: https://github.com/Cadair/sunpy/tree/map_refactor_diff_rot
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 29 21:31:58 2013
@author: stuart
"""
import numpy as np
from datetime import timedelta
import matplotlib.pyplot as plt
from skimage import transform
from skimage.util import img_as_float
from astropy import units as u
import sunpy.data.sample
import sunpy.map
import sunpy.physics.transforms.differential_rotation as diffrot
from sunpy.time import parse_time
#Scikit-image needs float arrays between 0 and 1
def to_norm(arr):
'''
Normalises an array.
'''
arr = img_as_float(arr, force_copy=True)
if arr.min() < 0:
arr += np.abs(arr.min())
arr /= arr.max()
return arr
def un_norm(arr, ori):
'''
Un-normalises an array based in the original.
'''
level = 0 if ori.min() > 0 else np.abs(ori.min())
arr *= ori.max() + level
arr -= level
return arr
#Define a function that returns a new list of coordinates for each input coord.
def warp_sun(xy, smap):
#Calculate the hpc coords
x = np.arange(0, smap.dimensions.x.value)
y = np.arange(0, smap.dimensions.y.value)
xx, yy = np.meshgrid(x, y)
hpc_coords = smap.pixel_to_data(xx * u.pix, yy * u.pix)
#Do the diff rot
rotted = diffrot.rot_hpc(hpc_coords[1], hpc_coords[0], smap.date,
smap.date - timedelta(days=2))
#Go back to pixel coords
x2,y2 = smap.data_to_pixel(rotted[0], rotted[1])
#Restack the data to make it correct output form
xy2 = np.column_stack([x2.value.flat,y2.value.flat])
#Remove NaNs
mask = np.isnan(xy2)
xy2[mask] = 0.0
return xy2
if __name__ == '__main__':
data = sunpy.map.Map(sunpy.data.sample.AIA_171_IMAGE)
#Do the warp:
out = transform.warp(to_norm(data.data), inverse_map=warp_sun, map_args={'smap':data})
#de norm
out = un_norm(out, data.data)
plt.figure()
plt.subplot(121)
plt.imshow(data.data, cmap=data.plot_settings['cmap'], origin='lower',norm=data.plot_settings['norm'])
plt.subplot(122)
plt.imshow(out, cmap=data.plot_settings['cmap'], origin='lower',norm=data.plot_settings['norm'])
plt.show()
import pytest
import numpy as np
from numpy.testing import assert_allclose
from sunpy.physics.transforms import diff_rot
def test_toNorm():
array_simple = np.array([10., 20., 30., 100.])
assert_allclose(diff_rot.to_norm(array_simple), np.array([0.1, 0.2, 0.3, 1.]))
array_simple_neg = np.array([-10., 0., 10., 90.])
assert_allclose(diff_rot.to_norm(array_simple_neg), np.array([0, 0.1, 0.2, 1.]))
def test_unNorm():
array_simple = np.array([10, 20, 30, 100.])
assert_allclose(diff_rot.un_norm(np.array([0.1, 0.2, 0.3, 1.]), array_simple), array_simple)
array_simple_neg = np.array([-10, 0, 10, 90])
assert_allclose(diff_rot.un_norm(np.array([0, 0.1, 0.2, 1.]), array_simple_neg), array_simple_neg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment