Skip to content

Commit

Permalink
Added New xarray wrapper and Test Files
Browse files Browse the repository at this point in the history
  • Loading branch information
NVJY committed Dec 4, 2024
1 parent 84179be commit 5837f37
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 0 deletions.
60 changes: 60 additions & 0 deletions autoreact/ktp_xarray/xarray_demo_DataArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import xarray
import numpy

temps = [1000, 1500, 2000, 2500]
press = [1, 10, numpy.inf]

# Define the array of rates (note: it makes sense to structure it as a list
# of rates at a given P based on how the MESS parser is written, though it
# doesn't really matter)
ktp_vals = [[1e1, 1e2, 1e3, 1e4], [1e5, 1e6, 1e7, 1e8], [1e9, 1e10, 1e11, 1e12]]

# Create the DataArray
ktp = xarray.DataArray(ktp_vals, [("pres", press), ("temp", temps)])
print(ktp)

# Get a slice at a selected pressure or temperature value
print('\nPressure slice by value')
print(ktp.sel(pres=numpy.inf)) # the fact that you can use np.inf is very convenient!
print('\nTemperature slice by value')
print(ktp.sel(temp=1500))

# Get a specific value at a selected temperature value and pressure value
print('\nTemperature slice by value')
print(ktp.sel(temp=1500, pres=1))

# Get a slice at a selected pressure or temperature index
print('\nPressure slice by index')
print(ktp.isel(pres=0))
print('\nTemperature slice by index')
print(ktp.isel(temp=0))

# Can also add some metadata (probably unnecessary for ktp objects, but cool)
ktp.attrs["units"] = "s^-1" # any number of arbitrary attributes can be added
ktp.name = "N2O=N2+O"

# Can do various arithmetic operations
print('\nVarious operations')
print(ktp + ktp)
print(3 * ktp)

# To get the actual values, use the values OR data attribute
print('\nGet the values, which should be a numpy array')
print(ktp.values)
print('type of ktp.values: ', type(ktp.values))
print('\nTemperature slice by value, as an array')
print(ktp.sel(temp=1500, pres=1).data)
print(type(ktp.sel(temp=1500, pres=1).data))

# Can get all dimensions associated with an array usings the dims attribute
print('\nNames of all dimensions, as a tuple')
print(ktp.dims)

# Can get the coordinates associated with a dimension by using that coordinate name
print('\nPressure values: ', ktp.pres.data)
print('\nTemperature values: ', ktp.temp.data)

# Can do lookup on values with the nearest value
# This is giving an error even though I thought I copied it from the website?
#print('\nTemperature slice by nearest lookup')
#print(ktp.sel(temp=1600), method='nearest')
26 changes: 26 additions & 0 deletions autoreact/ktp_xarray/xarray_demo_DataSet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import xarray
import numpy

temps = [1000, 1500, 2000, 2500]
enthalpy = [60, 70, 80, 90]
gibbs = [10, 15, 20, 25]
c_p = [1, 1.1, 1.2, 1.3]
entropy = [3, 4, 5, 6]

therm = xarray.Dataset(
{
"enthalpy": (["temp"], enthalpy),
"gibbs": (["temp"], gibbs),
"c_p": (["temp"], c_p),
"entropy": (["temp"], entropy),
},
coords={
"temp": temps,
},
)

print(therm)
print(therm.temp)
print(therm.temp.values)


92 changes: 92 additions & 0 deletions autoreact/ktp_xarray/xarray_wrappers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""
Wrappers for the new xarray system. Constructors, Getters, then Setters.
"""

import xarray

# Constructors
def from_data(temps, press, rates):
"""
Construct a KTP DataArray from data
"""

ktp = xarray.DataArray(rates, [("pres", press), ("temp", temps)])

return ktp



# Getters
def get_pressures(ktp):
"""
Gets the pressure values
"""

return ktp.pres.data


def get_temperatures(ktp):
"""
Gets the temperature values
"""

return ktp.temp.data


def get_values(ktp):
"""
Gets the KTP values
"""

return ktp.values


def get_pslice(ktp, ip):
"""
Get a slice at a selected pressure value
"""

return ktp.sel(pres=ip)


def get_tslice(ktp, it):
"""
Get a slice at a selected temperature value
"""

return ktp.sel(temp=it)


def get_spec_vals(ktp, it, ip):
"""
Get a specific value at a selected temperature and pressure value
"""

return ktp.sel(temp=it, pres=ip)


def get_ipslice(ktp, ip):
"""
Get a slice at a selected pressure index
"""

return ktp.isel(pres=ip)


def get_itslice(ktp, it):
"""
Get a slice at a selected temperature index
"""

return ktp.isel(temp=it)



# Setters
def set_rates(ktp, rates, pres, temp):
"""
Sets the KTP values
"""

ktp.loc[dict(pres=pres, temp=temp)] = rates
return ktp
67 changes: 67 additions & 0 deletions autoreact/ktp_xarray/xarray_wrappers_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import xarray_wrappers
import xarray
import numpy

Temps = [1000, 1500, 2000, 2500]
Press = [1, 10, numpy.inf]
Rates = [[1e1, 1e2, 1e3, 1e4], [1e5, 1e6, 1e7, 1e8], [1e9, 1e10, 1e11, 1e12]]

Ktp = xarray_wrappers.from_data(Temps, Press, Rates)
print(Ktp)

def test_set_rates():
ktp = xarray_wrappers.set_rates(Ktp, Rates)
print(ktp)

def test_get_temperatures():
temp = xarray_wrappers.get_temperatures(Ktp)
print(temp)


def test_get_pressures():
pres = xarray_wrappers.get_pressures(Ktp)
print(pres)


def test_get_values():
vals = xarray_wrappers.get_values(Ktp)
print(vals)


def test_get_pslice():
pslice = xarray_wrappers.get_pslice(Ktp, numpy.inf)
print(pslice)


def test_get_tslice():
tslice = xarray_wrappers.get_tslice(Ktp, 1500)
print(tslice)


def test_get_spec_vals():
vals = xarray_wrappers.get_spec_vals(Ktp, 1500, 1)
print(vals)


def test_get_ipslice():
ipslice = xarray_wrappers.get_ipslice(Ktp, 0)
print(ipslice)


def test_get_itslice():
itslice = xarray_wrappers.get_itslice(Ktp, 0)
print(itslice)

def test_set_rates():
new_rates = xarray_wrappers.set_rates(Ktp, 1e11, 10, 2000)
print(new_rates)

test_get_pressures()
test_get_temperatures()
test_get_values()
test_get_pslice()
test_get_tslice()
test_get_spec_vals()
test_get_ipslice()
test_get_itslice()
test_set_rates()

0 comments on commit 5837f37

Please sign in to comment.