Removing Cosmic Ray Hits

This example illustrates how to remove cosmic ray hits from a LASCO C2 FITS file. using astroscrappy.detect_cosmics. Astroscrappy is a separate Python package and can be installed separately using pip or conda.

import astroscrappy
import matplotlib.pyplot as plt

from sunpy.io.file_tools import read_file
from sunpy.map import Map
from sunpy.net import Fido
from sunpy.net import attrs as a

For more details on how to download and plot LASCO FITS file see SunPy’s example Downloading and plotting LASCO C3 data. To make this example work you need to have SunPy with all the “net” dependencies installed.

In order to download the required FITS file, we use Fido, SunPy’s downloader client. We need to define two search variables: a time range and the instrument.

time_range = a.Time("2000/11/09 00:06", "2000/11/09 00:07")
instrument = a.Instrument("LASCO")
detector = a.Detector("C2")
result = Fido.search(time_range, instrument)

downloaded_files = Fido.fetch(result[0])
data, header = read_file(downloaded_files[0])[0]

# Add the missing meta information to the header
header["CUNIT1"] = "arcsec"
header["CUNIT2"] = "arcsec"
Files Downloaded:   0%|          | 0/1 [00:00<?, ?file/s]

22080376.fts:   0%|          | 0.00/2.11M [00:00<?, ?B/s]

22080376.fts:   0%|          | 1.02k/2.11M [00:00<04:11, 8.37kB/s]

22080376.fts:  40%|###9      | 841k/2.11M [00:00<00:00, 4.41MB/s]


Files Downloaded: 100%|##########| 1/1 [00:00<00:00,  1.81file/s]
Files Downloaded: 100%|##########| 1/1 [00:00<00:00,  1.81file/s]
/home/docs/checkouts/readthedocs.org/user_builds/sunkit-image/envs/stable/lib/python3.9/site-packages/astropy/io/fits/verify.py:117: VerifyWarning: Verification reported errors:
  warnings.warn(line, VerifyWarning)
/home/docs/checkouts/readthedocs.org/user_builds/sunkit-image/envs/stable/lib/python3.9/site-packages/astropy/io/fits/verify.py:117: VerifyWarning: Card 78:
  warnings.warn(line, VerifyWarning)
/home/docs/checkouts/readthedocs.org/user_builds/sunkit-image/envs/stable/lib/python3.9/site-packages/astropy/io/fits/verify.py:117: VerifyWarning:     Unfixable error: Unprintable string 'offset_bias.pro\t1.20 08/07/00, 584.666'; commentary cards may only contain printable ASCII characters
  warnings.warn(line, VerifyWarning)
/home/docs/checkouts/readthedocs.org/user_builds/sunkit-image/envs/stable/lib/python3.9/site-packages/astropy/io/fits/verify.py:117: VerifyWarning: Card 80:
  warnings.warn(line, VerifyWarning)
/home/docs/checkouts/readthedocs.org/user_builds/sunkit-image/envs/stable/lib/python3.9/site-packages/astropy/io/fits/verify.py:117: VerifyWarning:     Unfixable error: Unprintable string "reduce_level_05.pro\t1.23 11/21/00 reduce_level_05,'001109_001218.img','2"; commentary cards may only contain printable ASCII characters
  warnings.warn(line, VerifyWarning)
/home/docs/checkouts/readthedocs.org/user_builds/sunkit-image/envs/stable/lib/python3.9/site-packages/astropy/io/fits/verify.py:117: VerifyWarning: Note: astropy.io.fits uses zero-based indexing.

  warnings.warn(line, VerifyWarning)

With this fix we can load it into a map and plot the results.

lasco_map = Map(data, header)
fig1 = plt.figure()
lasco_map.plot()
LASCO-C2 Orange white-light 2000-11-09 00:06:05
INFO: Missing metadata for solar radius: assuming the standard radius of the photosphere. [sunpy.map.mapbase]
/home/docs/checkouts/readthedocs.org/user_builds/sunkit-image/envs/stable/lib/python3.9/site-packages/sunpy/map/mapbase.py:627: SunpyMetadataWarning: Missing metadata for observer: assuming Earth-based observer.
For frame 'heliographic_stonyhurst' the following metadata is missing: dsun_obs,hglt_obs,hgln_obs
For frame 'heliographic_carrington' the following metadata is missing: dsun_obs,crlt_obs,crln_obs

  obs_coord = self.observer_coordinate

<matplotlib.image.AxesImage object at 0x7f5e5b7293d0>

Now we will call the astroscrappy.detect_cosmics to remove the cosmic ray hits. This algorithm can perform well with both high and low noise levels in the original data. The function takes a ndarray as input so we only pass the map data. This particular image has lots of high intensity cosmic ray hits which cannot be effectively removed by using the default set of parameters. So we reduce sigclip, the Laplacian to noise ratio from 4.5 to 2 to mark more hits. We also reduce objlim, the contrast between the Laplacian image and the fine structured image to clean the high intensity bright cosmic ray hits. We also modify the readnoise parameter to obtain better results.

mask, clean_data = astroscrappy.detect_cosmics(lasco_map.data, sigclip=2, objlim=2, readnoise=4, verbose=True)
# This returns two variables - mask is a boolean array depicting whether there is
# a cosmic ray hit at that pixel, clean_data is the cleaned image after removing those
# hits.
Starting 4 L.A.Cosmic iterations
Iteration 1:
169952 cosmic pixels this iteration
Iteration 2:
42674 cosmic pixels this iteration
Iteration 3:
13946 cosmic pixels this iteration
Iteration 4:
6735 cosmic pixels this iteration

We can now plot the cleaned image.

clean_map1 = Map(clean_data, lasco_map.meta)

fig2 = plt.figure()
clean_map1.plot()

plt.show()
LASCO-C2 Orange white-light 2000-11-09 00:06:05
INFO: Missing metadata for solar radius: assuming the standard radius of the photosphere. [sunpy.map.mapbase]
/home/docs/checkouts/readthedocs.org/user_builds/sunkit-image/envs/stable/lib/python3.9/site-packages/sunpy/map/mapbase.py:627: SunpyMetadataWarning: Missing metadata for observer: assuming Earth-based observer.
For frame 'heliographic_stonyhurst' the following metadata is missing: dsun_obs,hglt_obs,hgln_obs
For frame 'heliographic_carrington' the following metadata is missing: dsun_obs,crlt_obs,crln_obs

  obs_coord = self.observer_coordinate

Total running time of the script: ( 0 minutes 6.059 seconds)

Gallery generated by Sphinx-Gallery