-
-
Notifications
You must be signed in to change notification settings - Fork 54
docs: add example using reproject to change wcs resolution #893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alasdairwilson
wants to merge
9
commits into
sunpy:main
Choose a base branch
from
alasdairwilson:upscale_example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bfd1c2e
docs: add example using reproject to upscale resolution
alasdairwilson 6d6a28c
tests: added clearer error message for attempting to rebin with scale…
alasdairwilson be3de10
changelog
alasdairwilson 5f27e1e
Update changelog/893.doc.rst
Cadair 8b00120
slight touchups to language
alasdairwilson 9cb90fe
oops
alasdairwilson 1a87194
changed from upsacling to simply changing res
alasdairwilson d08707f
updated c hangelog ref to new file name
alasdairwilson 10c8a54
Merge branch 'main' into upscale_example
alasdairwilson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added an example (:ref:`sphx_glr_generated_gallery_changing_resolution_via_reproject.py`) showing how to change resolution of an NDCube using `~ndcube.NDCube.reproject_to`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,64 @@ | ||||||||||
| """ | ||||||||||
| ===================================== | ||||||||||
| Changing the resolution of an NDCube | ||||||||||
| ===================================== | ||||||||||
|
|
||||||||||
| This example shows how to change the resolution of an NDCube by reprojecting to a finer grid. | ||||||||||
| """ | ||||||||||
|
|
||||||||||
| import matplotlib.pyplot as plt | ||||||||||
|
|
||||||||||
| from astropy.io import fits | ||||||||||
| from astropy.wcs import WCS | ||||||||||
|
|
||||||||||
| from sunpy.data.sample import AIA_171_IMAGE | ||||||||||
| from sunpy.visualization.colormaps import cm | ||||||||||
|
|
||||||||||
| from ndcube import NDCube | ||||||||||
|
|
||||||||||
| ############################################################################ | ||||||||||
| # We start by creating an NDCube from sample solar data provided by SunPy. | ||||||||||
| # Here we use an AIA 171 image, but the same approach can be applied to other datasets, including those with non celestial axes. | ||||||||||
|
|
||||||||||
| image_data = fits.getdata(AIA_171_IMAGE) | ||||||||||
| image_header = fits.getheader(AIA_171_IMAGE) | ||||||||||
|
Comment on lines
+23
to
+24
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| cube = NDCube(image_data, WCS(image_header)) | ||||||||||
|
|
||||||||||
| ########################################################################### | ||||||||||
| # Next, we define a new WCS with a finer pixel scale, note that while it is obvious that the CDELT values are changed to reflect the finer scale, | ||||||||||
| # the CRPIX values also need to be adjusted as the reference pixel position is different on the new pixel scale. | ||||||||||
| # You can scale the axes by any amount, including non-integer values, greater or less than 1. | ||||||||||
| # You can also scale each axis independently. | ||||||||||
| # Here we scale both spatial axes by a factor of 1.5. | ||||||||||
|
|
||||||||||
| scale_factor = 1.5 | ||||||||||
|
|
||||||||||
| new_wcs = cube.wcs.deepcopy() | ||||||||||
| new_wcs.wcs.cdelt /= scale_factor | ||||||||||
| new_wcs.wcs.crpix *= scale_factor | ||||||||||
| new_shape = tuple(int(s * scale_factor) for s in cube.data.shape) | ||||||||||
|
|
||||||||||
| ########################################################################### | ||||||||||
| # We reproject the original cube to the new WCS. | ||||||||||
| reprojected_cube = cube.reproject_to(new_wcs, shape_out=new_shape) | ||||||||||
|
|
||||||||||
| ########################################################################### | ||||||||||
| # Our new NDCube now has a higher resolution. | ||||||||||
| print(cube.data.shape, reprojected_cube.data.shape) | ||||||||||
|
|
||||||||||
| ########################################################################### | ||||||||||
| # We can plot the original and reprojected cubes side by side. | ||||||||||
|
|
||||||||||
| fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 8)) | ||||||||||
|
|
||||||||||
| ax1.imshow(cube.data, origin='lower', cmap=cm.sdoaia171, vmin=10, vmax=10000) | ||||||||||
| ax1.set_title('Original') | ||||||||||
| ax1.set_xlabel('X [pixel]') | ||||||||||
| ax1.set_ylabel('Y [pixel]') | ||||||||||
|
|
||||||||||
| ax2.imshow(reprojected_cube.data, origin='lower', cmap=cm.sdoaia171, vmin=10, vmax=10000) | ||||||||||
| ax2.set_title('Reprojected (Upscaled)') | ||||||||||
| ax2.set_xlabel('X [pixel]') | ||||||||||
| ax2.set_ylabel('Y [pixel]') | ||||||||||
| plt.tight_layout() | ||||||||||
| plt.show() | ||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think to what @hayesla said we should talk about when to use this and when to use
rebinhere, and a big health warning etc.