Skip to content

Instantly share code, notes, and snippets.

View ZGainsforth's full-sized avatar

Zack Gainsforth ZGainsforth

  • University of California at Berkeley
  • Los Angeles, CA, USA
View GitHub Profile
@ZGainsforth
ZGainsforth / Quant Bruker spx.ipynb
Last active March 13, 2024 09:24
Read in a Bruker spx spectrum and do basic TEM quantification to get area under the peaks.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ZGainsforth
ZGainsforth / Plot EDAX Stack.ipynb
Last active September 23, 2022 06:34
Read an EDAX spd file into hyperspy, browse, plot maps of the elements, and make RGBs.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ZGainsforth
ZGainsforth / ExtractSpectrumFromBruker.py
Created March 4, 2022 09:04
Script to manually extract spectra out of a bruker bcf file by using a mask image. Written by Michael and Roger.
#1/5/2022 Michael Ofengenden & Roger Yu & A LOT A LOT of help from Zack Gainsforth
import sys, os
import hyperspy.api as hs
import numpy as np
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore")
import tifffile as tif
import skimage
from skimage.transform import resize
@ZGainsforth
ZGainsforth / DrawSpec.py
Created November 8, 2021 22:33
Draw a spectrum and compare it against another. Using streamlit enable rescaling, shifting and gaussian convolution of spectra.
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
import os, sys
from scipy.ndimage.filters import gaussian_filter1d
from scipy.interpolate import interp1d
def ConvolveGaussian(Eraw, Iraw, Sigma):
E = np.linspace(Eraw[0], Eraw[-1], 10000)
dE = E[1] - E[0]
@ZGainsforth
ZGainsforth / PlotIR.py
Last active March 13, 2024 09:23
A quick way to plot some IR spectra extracted from a NanoIR stack.
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import medfilt, convolve
from scipy.signal.windows import gaussian
import os, sys
SpectraPath = os.path.join(".")
SpectraFiles = {
"Top Crystal" : os.path.join(SpectraPath, "Top Crystal.txt"),
@ZGainsforth
ZGainsforth / PlotSTXMLinescan.py
Created August 16, 2021 20:25
Plot a STXM linescan. It will be a 2D image file with one dimension spatial and the other energy.
import numpy as np
import matplotlib.pyplot as plt
from skimage.io import imread, imsave
from icecream import ic
# from QuickPlot import PrettyPlot
E = np.genfromtxt('532_210619074_energies.txt')
x = imread('532_210619074_CPX_Linescan.tif')
# The pixels are 100 nm in length, and the image was rotated 65 degrees. Therefore, the length of a pixel in this raster is (about 110 nm):
@ZGainsforth
ZGainsforth / PlotFeL.py
Created August 14, 2021 02:49
A simple script to plot some STXM spectra for comparison.
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import medfilt, convolve
from scipy.signal.windows import gaussian
import os, sys
STXMPath = os.path.join("..", "210628", "NS_210628066 Fe-L", "Spectra")
PtychoPath = os.path.join("..", "210629", "NS_210629000 - Fe-L ptycho", "Spectra Abs")
SpectraFiles = {"3+ inclusion (ptycho)": os.path.join(PtychoPath, "3+ inclusion.txt"),
@ZGainsforth
ZGainsforth / BackgroundSubtractBrukerSpectra.ipynb
Created August 12, 2021 22:36
Subtract the elements from one Bruker quant from another after normalizing one element. Useful for getting the composition of an inclusion or one phase out of a mixed phase where the other phase is known.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ZGainsforth
ZGainsforth / fluxedto2col.py
Created July 23, 2021 19:05
Convert Newton/XMM RGS Fluxed spectrum into a text file which can be plotted easily.
from astropy.constants import c, h, e
from astropy.io import fits
import numpy as np
from sys import argv
def LoadSpectrum(SpectrumName='0090340201.fluxed'):
spectrumfits = fits.open(SpectrumName)
spec = spectrumfits[1]
angstrom = np.nan_to_num(spec.data.field('CHANNEL').copy())
@ZGainsforth
ZGainsforth / CombineRGSObsIDs.sh
Last active June 15, 2022 18:51
Combine RGS1 and RGS2 spectra from two different Newton/XMM observations.
# Filenames are: RxxxxxxxxxxRyS00zAAAAAA1001.FTZ
# xxxxxxxxxx is the observation ID.
# Ry = R1 or R2 depending on whether it is RGS instrument 1 or 2.
# S00z can be s004 or 5 depending on which RGS.
# AAAAAA is SRSPEC for the spectrum, RSPMAT for the response matrix, BGSPEC for the background spectrum.
# Also need fluxedto2col.py to do the conversion of the fluxed file into a two column output.
# For each ObsID, combine the RGS 1 and RGS 2 first order spectrum, produce a fluxed output, and convert that into a 2 column file.
for OBSID in 0693490201 0090340201