Table model example

The following Python example sets up a table model grid with two parameters. The parameters, energies and fluxes are given arbitrary values, in practice these could be read from text files.

import os
from heasp import *
import numpy as np

test = table()

# set table descriptors and the energy array
test.setModelName("Test")
test.setModelUnits(" ")
test.setisRedshift(True)
test.setisAdditive(True)
test.setisError(False)

# set up the energies. note that the size is one greater
# than that of the array for the model fluxes
test.setEnergies(np.linspace(0.1,10.0,100))

test.setNumIntParams(2)
test.setNumAddParams(0)

# define first parameter and give it 11 values ranging from
# 0.0 to 2.0 in steps of 0.2.

testpar = tableParameter()
testpar.setName("param1")
testpar.setInterpolationMethod(0)
testpar.setInitialValue(1.0)
testpar.setDelta(0.1)
testpar.setMinimum(0.0)
testpar.setBottom(0.0)
testpar.setTop(2.0)
testpar.setMaximum(2.0)
testpar.setTabulatedValues(np.linspace(0.0,2.0,11))

# and push it onto the vector of parameters
test.pushParameter(testpar)

# define the second parameter and give it 5 values ranging from
# 4.6 to 5.4 in steps of 0.2. As an example this parameter is set up
# using a create method which allows information to be passed in one call

testpar2 = tableParameter("param2", 0, 5.0, 0.1, 4.6, 4.6, 5.4, 5.4) 
testpar2.setTabulatedValues(np.linspace(4.6,5.4,5)

# and push it onto the vector of parameters
test.pushParameter(testpar2);

# now set up the spectra. these are arbitrarily calculated, in a real program 
# this step would read a file or call a routine.

for i1 in range(11):
  for i2 in range(5):
    flux = empty((99))
    for j in range(99): flux[j] = 0.2*i1+10*(4.6+0.2*i2)+j*0.1
    testspec = tableSpectrum()
    testspec.setParameterValues(np.array([0.2*i1,4.6+0.2*i2]))
    testspec.setFlux(flux)
    test.pushSpectrum(testspec)

# now write out the table.
tablefile = "test.mod"
if (os.path.exists(tablefile)): os.remove(tablefile)
status = test.write(tablefile);
if status != 0: print("Failed to write test.mod: status = ", status)




HEASARC Home | Observatories | Archive | Calibration | Software | Tools | Students/Teachers/Public

Last modified: Wednesday, 28-Aug-2024 16:49:37 EDT

HEASARC Staff Scientist Position - Applications are now being accepted for a Staff Scientist with significant experience and interest in the technical aspects of astrophysics research, to work in the High Energy Astrophysics Science Archive Research Center (HEASARC) at NASA Goddard Space Flight Center (GSFC) in Greenbelt, MD. Refer to the AAS Job register for full details.