Commit 00e492dad792a0e80915dd1e61e9247e6dd46d1b
1 parent
24a7b892
renamed from test_ to example_
Showing
3 changed files
with
88 additions
and
42 deletions
test/example_model_xml_io.py
0 → 100755
| 1 | +#!/usr/bin/env python | |
| 2 | +# ===========================================================================================# | |
| 3 | +# This script builds a set of models, saves the models into an XML file and loads the | |
| 4 | +# models back into memory. | |
| 5 | +# ===========================================================================================# | |
| 6 | + | |
| 7 | +"""IO example: GModels <--> XML file""" | |
| 8 | + | |
| 9 | +from gammalib import * | |
| 10 | + | |
| 11 | +# | |
| 12 | +# Set the location / centre of the source model to | |
| 13 | +# Right Ascension = 42.0 deg and Declination = 43 deg | |
| 14 | +# =================================================== | |
| 15 | +center = GSkyDir() | |
| 16 | +center.radec_deg(42.0, 43.0) | |
| 17 | + | |
| 18 | +# | |
| 19 | +# Create a point source with power law spectral shape | |
| 20 | +# Normalization: 1.0e-7 ph/cm2/s/MeV @ 100 MeV | |
| 21 | +# Spectral index: -2.1 | |
| 22 | +# =================================================== | |
| 23 | +point_spatial = GModelSpatialPtsrc(center) | |
| 24 | +point_spectrum = GModelSpectralPlaw(1.0e-7, -2.1) | |
| 25 | +point = GModelPointSource(point_spatial, point_spectrum) | |
| 26 | +point.name('My point source') | |
| 27 | + | |
| 28 | +# | |
| 29 | +# Create a 2D Gaussian source with power law spectral shape | |
| 30 | +# Gaussian sigma: 3.0 deg | |
| 31 | +# Normalization: 4.2e-7 ph/cm2/s/MeV @ 100 MeV | |
| 32 | +# Spectral index: -2.4 | |
| 33 | +# =================================================== | |
| 34 | +gauss_spatial = GModelRadialGauss(center, 3.0) | |
| 35 | +gauss_spectrum = GModelSpectralPlaw(4.2e-7, -2.4) | |
| 36 | +gauss = GModelExtendedSource(gauss_spatial, gauss_spectrum) | |
| 37 | +gauss.name('My Gaussian source') | |
| 38 | + | |
| 39 | +# | |
| 40 | +# Allocate a GModels model container and append both source | |
| 41 | +# models to the model container. Print the content of the | |
| 42 | +# container | |
| 43 | +# ========================================================= | |
| 44 | +models = GModels() | |
| 45 | +models.append(point) | |
| 46 | +models.append(gauss) | |
| 47 | +print models | |
| 48 | + | |
| 49 | +# | |
| 50 | +# Write the model container to XML file and delete the container | |
| 51 | +# in-memory | |
| 52 | +# ============================================================== | |
| 53 | +filename = 'models.xml' | |
| 54 | +models.save(filename) | |
| 55 | +del models | |
| 56 | + | |
| 57 | +# | |
| 58 | +# Now read the models back from the XML file into the container. | |
| 59 | +# Print again the content of the container. | |
| 60 | +# ============================================================== | |
| 61 | +models2 = GModels(filename) | |
| 62 | ||
| 63 | +print models2 | ... | ... |
test/test_radial_models.py renamed to test/example_radial_models.py
| 1 | 1 | #! /usr/bin/env python |
| 2 | +# ===========================================================================================# | |
| 3 | +# This script makes an image from a radial model and verifies that the model | |
| 4 | +# is correctly normalized. | |
| 5 | +# ===========================================================================================# | |
| 2 | 6 | from gammalib import * |
| 3 | 7 | |
| 8 | + | |
| 9 | +# ============================ # | |
| 10 | +# Radial model testing routine # | |
| 11 | +# ============================ # | |
| 4 | 12 | def test_radial_model(name, model, clobber=True): |
| 5 | 13 | """Make an image of a spatial model |
| 6 | 14 | and check that it integrates to 1""" |
| 7 | - # Make a 2D FITS image | |
| 8 | - ra, dec, binsz, npix = 0, 0, 0.01, 200 | |
| 15 | + # Make a 2D FITS image (cartesian projection, celestial coordinates) | |
| 16 | + ra, dec, binsz, npix = 0, 0, 0.01, 300 | |
| 9 | 17 | image = GSkymap("CAR", "CEL", ra, dec, -binsz, binsz, npix, npix, 1) |
| 10 | - | |
| 18 | + | |
| 11 | 19 | # Fill the image |
| 12 | 20 | for pix in range(image.npix()): |
| 13 | - dir = image.pix2dir(pix) | |
| 14 | - theta = center.dist(dir) | |
| 15 | - image[pix, 0] = model.eval(theta); | |
| 21 | + dir = image.pix2dir(pix) | |
| 22 | + theta = center.dist(dir) | |
| 23 | + image[pix] = model.eval(theta); | |
| 16 | 24 | |
| 17 | 25 | # Write it to file |
| 18 | 26 | filename = name + '.fits' |
| ... | ... | @@ -20,18 +28,25 @@ def test_radial_model(name, model, clobber=True): |
| 20 | 28 | image.save(filename, clobber) |
| 21 | 29 | |
| 22 | 30 | # Check that it integrates to 1 |
| 23 | - integral = 0 | |
| 31 | + integral = 0.0 | |
| 24 | 32 | for pix in range(image.npix()): |
| 25 | 33 | integral += image.omega(pix) * image[pix] |
| 26 | 34 | print 'integral = %g (should be 1)' % integral |
| 27 | 35 | print 'integral error = %g (should be 0)' % (integral - 1) |
| 28 | 36 | |
| 29 | 37 | |
| 38 | +# ================ # | |
| 39 | +# Main entry point # | |
| 40 | +# ================ # | |
| 30 | 41 | if __name__ == '__main__': |
| 31 | - | |
| 42 | + """ | |
| 43 | + Test radial model integration. | |
| 44 | + """ | |
| 45 | + # Set model location / centre | |
| 32 | 46 | center = GSkyDir() |
| 33 | - center.radec_deg(0, 0) | |
| 47 | + center.radec_deg(0.3, 0.1) | |
| 34 | 48 | |
| 49 | + # Test models | |
| 35 | 50 | test_radial_model(name='gauss', model=GModelRadialGauss(center, 0.3)) |
| 36 | - test_radial_model(name='disk', model=GModelRadialDisk(center, 0.8)) | |
| 51 | + test_radial_model(name='disk', model=GModelRadialDisk(center, 0.8)) | |
| 37 | 52 | test_radial_model(name='shell', model=GModelRadialShell(center, 0.5, 0.1)) | ... | ... |
test/test_model_xml_io.py deleted
| 1 | -#!/usr/bin/env python | |
| 2 | -"""IO example: GModels <--> XML file""" | |
| 3 | - | |
| 4 | -from gammalib import * | |
| 5 | - | |
| 6 | -# Make a GModels object containing two models | |
| 7 | -center = GSkyDir() | |
| 8 | -center.radec_deg(42, 43) | |
| 9 | - | |
| 10 | -point_spatial = GModelSpatialPtsrc(center) | |
| 11 | -point_spectrum = GModelSpectralPlaw(1.0e-7, -2.1) | |
| 12 | -point = GModelPointSource(point_spatial, point_spectrum) | |
| 13 | -point.name('point') | |
| 14 | - | |
| 15 | -gauss_spatial = GModelRadialGauss(center, 3) | |
| 16 | -gauss_spectrum = GModelSpectralPlaw(4.2e-7, -2.4) | |
| 17 | -gauss = GModelExtendedSource(gauss_spatial, gauss_spectrum) | |
| 18 | -gauss.name('gauss') | |
| 19 | - | |
| 20 | -models = GModels() | |
| 21 | -models.append(point) | |
| 22 | -models.append(gauss) | |
| 23 | -print models | |
| 24 | - | |
| 25 | -# Write GModels to file and delete it in-memory | |
| 26 | -filename = 'models.xml' | |
| 27 | -models.save(filename) | |
| 28 | -del models | |
| 29 | - | |
| 30 | -# Now read it back | |
| 31 | -models2 = GModels(filename) | |
| 32 | -print models2 |