<jupyter_start><jupyter_text>Script to load in and plot the RHESSI image archive fits[https://hesperia.gsfc.nasa.gov/rhessi_extras/flare_images/hsi_flare_image_archive.html](https://hesperia.gsfc.nasa.gov/rhessi_extras/flare_images/hsi_flare_image_archive.html)21-Jul-2020 IGH<jupyter_code>from astropy.io import fits
import numpy as np
import astropy.time
import astropy.units as u
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.colors as colors
%matplotlib inline

from PIL import Image

import sunpy.visualization.colormaps as cm

import glob
import warnings
warnings.filterwarnings("ignore")<jupyter_output><empty_output><jupyter_text>File we are using here is from[https://hesperia.gsfc.nasa.gov/rhessi_extras/flare_images/2012/10/26/20121026_0812_0818/hsi_20121026_0812_0818.html](https://hesperia.gsfc.nasa.gov/rhessi_extras/flare_images/2012/10/26/20121026_0812_0818/hsi_20121026_0812_0818.html)<jupyter_code># Use glob to find more files
# mdir='/scratch/mokusei/iain/rfia/hesperia.gsfc.nasa.gov/rhessi_extras/imagecube_fits/'
# subdir='2012/10/26/'
# ff=glob.glob(mdir+subdir+'**/hsi_imagecube_clean_*.fits')
ff=glob.glob('hsi_imagecube_clean_*.fits')
print(ff)
fitsin=fits.open(ff[0])
# Images in [0]
imgs=fitsin[0].data
# Set negative to 0
imgs[imgs < 0]=0.
hdr=fitsin[0].header
print(imgs.shape)
nt,ne,nx,ny=imgs.shape
# So done as (t,e,x,y)
# for key in hdr:
#     print(key)
print(hdr['date'])
print(fitsin[2].columns.names)
print(fitsin[2].data['image_atten_state'])
snr=fitsin[2].data['SNR_VALUE']
print(snr)
tims=fitsin[2].data['ABSOLUTE_TIME_RANGE']
print(tims.shape,tims)
engs=fitsin[2].data['IM_EBAND_USED']
print(engs.shape,engs)
# Remeber to close the fits file after done with it
fitsin.close()
# Need scale tai as that's what the ssw anytim use
ref79=astropy.time.Time('1979-01-01 00:00:00',format='iso',scale='tai')
print(ref79.isot)
times=ref79+tims*u.s
print(times.iso)
# Work out the Max per enery for scaling
# maxs=[,np.max(imgs[:,1,:,:]),np.max(imgs[:,2,:,:])]
maxs=np.zeros(ne)
for ee in np.arange(ne):
    maxs[ee]=np.max(imgs[:,ee,:,:])
# Work out the Rel Flux for each one
relflux=np.zeros(shape=(nt,ne))
for tt in np.arange(nt):
    for ee in np.arange(ne):
        relflux[tt,ee]=np.max(imgs[tt,ee,:,:])/maxs[ee]
print(maxs)
print(relflux)
# Sort out the time labels
timlab=[]
for t in np.arange(nt):
    timlab.append(times[t*ne][0].isot[11:19]+' - '+times[t*ne][1].isot[11:19])
# Sort out the energy labels
englab=[]
for ee in np.arange(ne):
    englab.append(str(engs[ee][0])+' - '+str(engs[ee][1])+' keV')
    
# Plot all the maps    
plt.rcParams.update({'font.size': 12,'font.family':"sans-serif",\
                     'font.sans-serif':"Arial",'mathtext.default':"regular"})
# plt.rcParams.update({'font.size': 12,'font.family':"serif",\
#                      'font.serif':"STIXGeneral",'mathtext.default':"regular"})
fig, ax = plt.subplots(nrows=ne, ncols=nt,figsize=(nt*3, ne*3))
# Load in the RHESSI color table (maybe not the best here?)
cmap = plt.get_cmap('rhessi')
# Loop over all the maps and plot
for tt in np.arange(nt):
    for ee in np.arange(ne):
        ax[ee,tt].set_xticks([])
        ax[ee,tt].set_yticks([])
        ax[ee,tt].imshow(imgs[tt,ee,:,:],origin='lower',vmin=0.,vmax=maxs[ee],cmap=cmap)
        if (ee == 0):
            ax[ee,tt].set_title(timlab[tt])
        if (tt == 0):
            ax[ee,tt].set_ylabel(englab[ee]) 
        ax[ee,tt].set_xlabel('SNR: '+"%0.1f" % snr[tt*ne+ee]+\
                             ', Rel Flux: '+"%0.2f" % relflux[tt,ee])    
plt.show()
# Plot all the maps    
plt.rcParams.update({'font.size': 12,'font.family':"sans-serif",\
                     'font.sans-serif':"Arial",'mathtext.default':"regular"})
# plt.rcParams.update({'font.size': 12,'font.family':"serif",\
#                      'font.serif':"STIXGeneral",'mathtext.default':"regular"})
fig, ax = plt.subplots(nrows=ne, ncols=nt,figsize=(nt*3, ne*3))
# Load in the RHESSI color table (maybe not the best here?)
cmap = plt.get_cmap('rhessi')
# Loop over all the maps and plot
for tt in np.arange(nt):
    for ee in np.arange(ne):
        ax[ee,tt].set_xticks([])
        ax[ee,tt].set_yticks([])
        ax[ee,tt].imshow(imgs[tt,ee,:,:],origin='lower',\
                         norm=colors.PowerNorm(0.5,vmin=maxs[ee]/100.,vmax=maxs[ee]),cmap=cmap)
        if (ee == 0):
            ax[ee,tt].set_title(timlab[tt])
        if (tt == 0):
            ax[ee,tt].set_ylabel(englab[ee]) 
        ax[ee,tt].set_xlabel('SNR: '+"%0.1f" % snr[tt*ne+ee]+\
                             ', Rel Flux: '+"%0.2f" % relflux[tt,ee])    
plt.show()
img_two=np.zeros([nx,2*ny])
img_two[:,0:nx]=imgs[1,0,:,:]/maxs[0]
img_two[:,nx:]=imgs[1,2,:,:]/maxs[2]
fig= plt.figure(figsize=(8,4))
cmap = plt.get_cmap('rhessi')
plt.imshow(img_two,origin='lower',vmin=0.,vmax=1,cmap='Greys')
plt.xticks([], [])
plt.yticks([], [])
plt.show()
# Need to rescale to 8bit integer from 0-1
img_two_255=(255.*img_two).astype(np.uint8)
test_img= Image.fromarray(img_two_255)
# Then save it out
test_img.save('test_img.png')
reload_img=Image.open('test_img.png')
print(reload_img.format,reload_img.size,reload_img.mode)
# load the png outside python
# reload_img.show()
# Convert to numpy array
reload_img_np=np.asarray(reload_img)

# Then plot it again
plt.rcParams.update({'font.size': 12,'font.family':"sans-serif",\
                     'font.sans-serif':"Arial",'mathtext.default':"regular"})
fig= plt.figure(figsize=(8,4))
plt.imshow(reload_img_np,origin='lower',vmin=0,vmax=255,cmap='Greys')
plt.xticks([], [])
plt.yticks([], [])
plt.show()
# worth doing more zoomed in?
img_two60=np.zeros([60,120])
img_two60[:,0:60]=imgs[1,0,20:80,20:80]/maxs[0]
img_two60[:,60:]=imgs[1,2,20:80,20:80]/maxs[2]
fig= plt.figure(figsize=(8,4))
plt.imshow(img_two60,origin='lower',vmin=0.,vmax=1,cmap='Greys')
plt.xticks([], [])
plt.yticks([], [])
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1931453655242276, 'fraction_numerical': 0.041701840283640046, 'mean_word_length': 4.620493358633776, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '187912', 'license': 'MIT', 'n_tokens_mistral': 2669, 'n_tokens_neox': 2478, 'n_words': 377, 'path': 'image_archive_fits.ipynb', 'repo_name': 'ianan/rhsi_flstat', 'size': '5923'}