From 830788272d9e10d76e4939327e7eb90fd7398248 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Wed, 17 Feb 2016 21:22:01 +0100 Subject: [PATCH] Use filename including extension name and expression in GFits::open(). --- src/fits/GFits.cpp | 8 ++++---- test/data/file.fits.gz | Bin 0 -> 35437 bytes test/test_GFits.py | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------ 3 files changed, 89 insertions(+), 46 deletions(-) create mode 100644 test/data/file.fits.gz diff --git a/src/fits/GFits.cpp b/src/fits/GFits.cpp index e0e72e8..90e83fe 100644 --- a/src/fits/GFits.cpp +++ b/src/fits/GFits.cpp @@ -992,7 +992,7 @@ int GFits::extno(const std::string& extname) const * @brief Open or (optionally) create FITS file * * @param[in] filename Name of FITS file to be opened. - * @param[in] create Create FITS file if it does not exist (default=false) + * @param[in] create Create FITS file if it does not exist (default: false) * * @exception GException::invalid_argument * Class instance contains already an opened FITS file. @@ -1037,12 +1037,12 @@ void GFits::open(const GFilename& filename, const bool& create) // Try opening FITS file with readwrite access int status = 0; - status = __ffopen(FHANDLE(m_fitsfile), filename.url().c_str(), 1, &status); + status = __ffopen(FHANDLE(m_fitsfile), std::string(filename).c_str(), 1, &status); // If failed then try opening as readonly if (status == 104 || status == 112) { status = 0; - status = __ffopen(FHANDLE(m_fitsfile), filename.url().c_str(), 0, &status); + status = __ffopen(FHANDLE(m_fitsfile), std::string(filename).c_str(), 0, &status); m_readwrite = false; } @@ -1050,7 +1050,7 @@ void GFits::open(const GFilename& filename, const bool& create) // FITS file now if (create && status == 104) { status = 0; - status = __ffinit(FHANDLE(m_fitsfile), filename.url().c_str(), &status); + status = __ffinit(FHANDLE(m_fitsfile), std::string(filename).c_str(), &status); m_readwrite = true; m_created = true; } diff --git a/test/data/file.fits.gz b/test/data/file.fits.gz new file mode 100644 index 0000000..56f1fc0 Binary files /dev/null and b/test/data/file.fits.gz differ diff --git a/test/test_GFits.py b/test/test_GFits.py index 294a5df..56228f0 100755 --- a/test/test_GFits.py +++ b/test/test_GFits.py @@ -1,7 +1,7 @@ # ========================================================================== # This module performs unit tests for the GammaLib FITS module. # -# Copyright (C) 2012-2015 Juergen Knoedlseder +# Copyright (C) 2012-2016 Juergen Knoedlseder # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -18,10 +18,10 @@ # # ========================================================================== import math -import os import sys import gammalib + # =================================== # # Test class for GammaLib FITS module # # =================================== # @@ -48,6 +48,8 @@ class Test(gammalib.GPythonTestSuite): # Append tests self.append(self.test_fits, "Test GFits") + self.append(self.test_fits_image, "Test GFitsImage") + self.append(self.test_fits_table, "Test GFitsTable") self.append(self.test_fits_table_columns, "Test FITS table columns") # Return @@ -55,26 +57,56 @@ class Test(gammalib.GPythonTestSuite): def test_fits(self): """ - Test FITS interface. + Test GFile class interface. """ - # Set filenames - file1 = "test_python_fits_v1.fits" - file2 = "test_python_fits_v2.fits" + # Set test file names + file = gammalib.GFilename("data/file.fits") - # Remove test files + # Test creation of FITS file + self.test_try("Test GFits file constructor") try: - os.remove(file1) - os.remove(file2) + fits = gammalib.GFits(file, True) + self.test_try_success() except: - pass + self.test_try_failure("Unable to create file.") + + # Open FITS file + fits = gammalib.GFits(file) + self.test_value(fits["EVENTS"].nrows(), 1231, "Check number of rows") + + # Open FITS file with event selection + fits = gammalib.GFits(file+"[EVENTS][ENERGY>1.0]") + self.test_value(fits["EVENTS"].nrows(), 152, "Check number of rows") + + # Open FITS file with non-existing extension name + self.test_try("Test non-existing extension name") + try: + fits = gammalib.GFits(file+"[DUMMY][ENERGY>1.0]") + self.test_try_failure() + except: + self.test_try_success() + + # Return + return + + def test_fits_image(self): + """ + Test GFitsImage class interface. + """ + # Set test file names + file1 = gammalib.GFilename("test_python_fits_image_v1.fits") + file2 = gammalib.GFilename("test_python_fits_image_v2.fits") + + # Remove test files + file1.remove() + file2.remove() # Create FITS file fits = gammalib.GFits(file1, True) - sys.stdout.write(".") # Create images - nx = 10 - ny = 10 + nx = 10 + ny = 10 img1 = gammalib.GFitsImageByte(nx, ny) img2 = gammalib.GFitsImageDouble(nx, ny) img3 = gammalib.GFitsImageFloat(nx, ny) @@ -104,7 +136,6 @@ class Test(gammalib.GPythonTestSuite): img7.extname("Short") img8.extname("ULong") img9.extname("UShort") - sys.stdout.write(".") # Append images to FITS file fits.append(img1) @@ -116,14 +147,52 @@ class Test(gammalib.GPythonTestSuite): fits.append(img7) fits.append(img8) fits.append(img9) - sys.stdout.write(".") # Set header keywords img_byte = fits.image(0) img_byte.card("test", "test-value", "this is for testing") img_byte.card("real", 3.1415, "a real value") img_byte.card("int", 41, "an integer value") - sys.stdout.write(".") + + # Save FITS file + fits.save() + + # Close FITS file + fits.close() + + # Re-open FITS file + fits = gammalib.GFits(file1) + + # Get double precision image, take square root of pixel and save in + # another file + img_double = fits.image("Double") + for x in range(nx): + for y in range(ny): + img_double[x, y] = math.sqrt(img_double[x, y]) + + # Save into another FITS file + fits.saveto(file2) + + # Close FITS file + fits.close() + + # Return + return + + def test_fits_table(self): + """ + Test GFitsTable class interface. + """ + # Set test file names + file1 = gammalib.GFilename("test_python_fits_table_v1.fits") + file2 = gammalib.GFilename("test_python_fits_table_v2.fits") + + # Remove test files + file1.remove() + file2.remove() + + # Create FITS file + fits = gammalib.GFits(file1, True) # Create table columns nrows = 10 @@ -152,7 +221,6 @@ class Test(gammalib.GPythonTestSuite): col9[i] = str(i * 100) col10[i] = i * 100 col11[i] = i * 100 - sys.stdout.write(".") # Set ASCII table tbl_ascii = gammalib.GFitsAsciiTable() @@ -169,7 +237,6 @@ class Test(gammalib.GPythonTestSuite): tbl_ascii.append(col11) tbl_ascii.extname("ASCII table") fits.append(tbl_ascii) - sys.stdout.write(".") # Set binary table tbl_bin = gammalib.GFitsBinTable() @@ -186,36 +253,12 @@ class Test(gammalib.GPythonTestSuite): tbl_bin.append(col11) tbl_bin.extname("Binary table") fits.append(tbl_bin) - sys.stdout.write(".") # Save FITS file - # sys.stdout.write(fits+"\n") fits.save() - sys.stdout.write(".") - - # Close FITS file - fits.close() - sys.stdout.write(".") - - # Re-open FITS file - fits = gammalib.GFits(file1) - sys.stdout.write(".") - - # Get double precision image, take square root of pixel and save in - # another file - img_double = fits.image("Double") - for x in range(nx): - for y in range(ny): - img_double[x, y] = math.sqrt(img_double[x, y]) - sys.stdout.write(".") - - # Save into another FITS file - fits.saveto(file2) - sys.stdout.write(".") # Close FITS file fits.close() - sys.stdout.write(".") # Return return -- libgit2 0.21.4