Skip to content

Instantly share code, notes, and snippets.

View DanPorter's full-sized avatar

Dan Porter DanPorter

  • Diamond Light Source Ltd.
View GitHub Profile
@DanPorter
DanPorter / run.py
Last active June 2, 2025 10:43
run code from gist
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def load_gist(gist_id):
"""read gist source code"""
from json import load
from urllib.request import urlopen
gist_api = urlopen("https://api.github.com/gists/" + gist_id)
@DanPorter
DanPorter / jupyter_notebook_runner.py
Created June 2, 2025 10:18
Add code cell to notebook and run
"""
Example python code to adapt a jupyter notebook template and convert it to html
Uses nbformat and nbconvert
See also: papermill
"""
import os
import shutil
import nbformat
@DanPorter
DanPorter / element-edge-enums.py
Last active May 22, 2025 08:34
Generate a list of XML enums for x-ray absorption edges
"""
Create element-edge enum
X-Ray absorption edges are taken from the X-ray Data Booklet
https://xdb.lbl.gov/Section1/Sec_1-1.html
These are convieniently stored in Dans_Diffraction with other atomic data
$ pip install Dans_Diffraction
For refrences, see: https://github.com/DanPorter/Dans_Diffraction/tree/master/Dans_Diffraction/data
@DanPorter
DanPorter / pdnx_with_tests_i16_example.ipynb
Last active May 15, 2025 15:20
pdnx_with_tests_i16_example
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@DanPorter
DanPorter / hdfmap_example.py
Created October 10, 2024 12:57
HdfMap Example
"""
Example of HdfMap code
GitHub repo: https://github.com/DiamondLightSource/hdfmap
Docs: https://diamondlightsource.github.io/hdfmap/
installation:
$ python -m pip install hdfmap
Dan Porter 2024
@DanPorter
DanPorter / find_ExternalLink_files.py
Last active October 9, 2024 18:32
Find ExternalLink files in NeXus
@DanPorter
DanPorter / find_files_with_links.py
Created October 9, 2024 18:07
Find NeXus Files with external links
@DanPorter
DanPorter / oriented_reciprocal_lattice_vectors.py
Created August 2, 2024 14:51
Get oriented recirpocal lattice vectors
import Dans_Diffraction as dif
xtl = Crystal('filename.cif')
# rotate the crystal basis
xtl.Cell.orientation.rotate_6circle(mu=5) # rotate about x-axis, in degrees
# get return the lab-frame q-vector for a single reflection:
q = xtl.Cell.calculateQ([0, 0, 2])
# or, generate the entire reciprocal lattice
all_hkl = xtl.Scatter.get_hkl(energy_kev=8) # there are lots of other arguments for this!
@DanPorter
DanPorter / i16_read_nxs_files_babelscan.py
Created August 14, 2023 13:21
Read I16 Nexus files in Python with Babelscan
# module load python/3
# or pip install babelscan
import sys
sys.path.insert(0, '/dls_sw/i16/software/python/babelscan')
from babelscan import file_loader, FolderMonitor
# Load scan object
scan = file_loader('/dls/i16/data/2021/mm29566-1/12345.nxs')
print(scan)
@DanPorter
DanPorter / i16_read_nxs_files_h5py.py
Last active August 15, 2024 12:12
Read I16 Nexus Files in Python using h5py
# module load python/3
# or pip install h5py (h5py is often included in common python distrobutions)
import h5py
with h5py.File('example_nexus/794932.nxs', 'r') as nx:
print(nx['entry1']['measurement'].keys())
dataset = nx['entry1/measurement/eta']