Skip to content

Instantly share code, notes, and snippets.

@banesullivan
Created October 27, 2019 01:20
Show Gist options
  • Save banesullivan/9f4434216fb8e9fbc0baaeced1de348d to your computer and use it in GitHub Desktop.
Save banesullivan/9f4434216fb8e9fbc0baaeced1de348d to your computer and use it in GitHub Desktop.
[Using unyt] Use unyt to keep track of physical units when doinng math. In the snippet, I define data in units of centimeters, then peerform math with units of meters and then I can get my final result in the correct units with like no thought!
import unyt as u
import numpy as np
# The following data were collected at a snow station
station = np.array([1,2,3,4,5])
# Collected data
depth = np.array([92,94,105,93,96]) * u.cm
water_eq = np.array([29,30,33,29,32]) * u.cm
temp = np.array([-6, -5, -6, -6, -6]) * u.celcius
# Calculate the snow density
rho_w = 997 * (u.kg / u.m**3)# kg / m^3
density = (water_eq * rho_w / depth).to("kg/m**3")
density
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment