Skip to content

Instantly share code, notes, and snippets.

View settwi's full-sized avatar

William Setterberg settwi

View GitHub Profile
@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