Skip to content

Instantly share code, notes, and snippets.

View settwi's full-sized avatar

William Setterberg settwi

View GitHub Profile
import numpy as np
import astropy.constants as con
import astropy.units as u
# In the corona, the Coulomb logarithm is approximately 20.
# Note: Kontar+ calls Lambda the Coulomb logarithm, although
# Lambda usually refers to the plasma parameter, i.e. the argument to that logarithm.
# Don't get confused by that notation.
COULOMB_LOG = 20
ELECTRON_MASS_ENERGY = (con.m_e * con.c**2).to(u.keV)
import matplotlib.pyplot as plt
import numpy as np
'''
Here we show an example pileup rejection algorithm based on splitting an integral in half.
The algorithm is very simple: assume a uniform pulse shape across time,
and then say how much time it takes to integrate half of the pulse area.
Look at the `pileup_detect` function for the implementation.
W Setterberg 2025
import logging
logging.basicConfig()
logger = logging.getLogger(__name__)
import asdf
import astropy.units as u
import astropy.constants as c
import fiasco
from fiasco.util.exceptions import MissingDatasetException
import numpy as np
@settwi
settwi / area_contours.py
Created January 28, 2024 22:22
Contour a 2D array based on area
import numpy as np
import matplotlib.pyplot as plt
def area_contour_image(img: np.ndarray, quantiles: list[float], ax=None, x=None, y=None, **contour_kwds):
normalized_img = (img - np.min(img)) / (np.max(img) - np.min(img))
flattened_img = normalized_img.flatten()
# sort pixels: small areas first, then large areas
sorted_values = np.sort(flattened_img)[::-1]
@settwi
settwi / stix_pixel_loader.py
Last active September 15, 2023 21:53
Code to load STIX pixel data into some arrays/dicts and then sum it into a spectrogram
from astropy.io import fits
import astropy.table as atab
import astropy.time as atime
import astropy.units as u
import numpy as np
from stixdcpy import instrument as inst
def load_stix_pixel_data_to_spectrogram(fn: str) -> dict:
pdat = load_stix_pixel_data(fn)
import astropy.units as u
import numpy as np
from sklearn import linear_model
import typing
@u.quantity_input
def decompose(
counts: u.ct,
energy_bins: u.keV,
@settwi
settwi / hinode_scraper.py
Created February 17, 2023 16:39
Python script to scrape all the fits filels from a Hinode/XRT DARTS directory. I'm using it to get XRT data specifically but it could probably work with other instruments. Pass it the URL to scrape via command line arguments.
import requests
import parse
import os
import sys
verbose = True
def vp(*args, **kw):
@settwi
settwi / rebin_flux.py
Last active September 21, 2023 17:21
Perform a flux- (area-) conserving rebinning of a histogram given its edges and a set of new edges. (REBIN A HISTOGRAM IN PYTHON)
import numpy as np
import numpy.typing
'''
Perform a flux- (i.e. area-) conserving rebinning of a histogram,
given its edges and a set of new edges.
The __name__ ... section has an example using a power law.
@settwi
settwi / make_aia_movie.py
Last active January 10, 2023 21:52
Make an AIA movie out of a bunch of FITS files using sunpy
from datetime import datetime
import functools
import multiprocessing as mp
import os
import pathlib
import sys
import imageio
import numpy as np
import sunpy.map