Commit 830788272d9e10d76e4939327e7eb90fd7398248

Authored by Jürgen Knödlseder
1 parent 8cc8f849

Use filename including extension name and expression in GFits::open().

src/fits/GFits.cpp
@@ -992,7 +992,7 @@ int GFits::extno(const std::string& extname) const @@ -992,7 +992,7 @@ int GFits::extno(const std::string& extname) const
992 * @brief Open or (optionally) create FITS file 992 * @brief Open or (optionally) create FITS file
993 * 993 *
994 * @param[in] filename Name of FITS file to be opened. 994 * @param[in] filename Name of FITS file to be opened.
995 - * @param[in] create Create FITS file if it does not exist (default=false) 995 + * @param[in] create Create FITS file if it does not exist (default: false)
996 * 996 *
997 * @exception GException::invalid_argument 997 * @exception GException::invalid_argument
998 * Class instance contains already an opened FITS file. 998 * Class instance contains already an opened FITS file.
@@ -1037,12 +1037,12 @@ void GFits::open(const GFilename& filename, const bool& create) @@ -1037,12 +1037,12 @@ void GFits::open(const GFilename& filename, const bool& create)
1037 1037
1038 // Try opening FITS file with readwrite access 1038 // Try opening FITS file with readwrite access
1039 int status = 0; 1039 int status = 0;
1040 - status = __ffopen(FHANDLE(m_fitsfile), filename.url().c_str(), 1, &status); 1040 + status = __ffopen(FHANDLE(m_fitsfile), std::string(filename).c_str(), 1, &status);
1041 1041
1042 // If failed then try opening as readonly 1042 // If failed then try opening as readonly
1043 if (status == 104 || status == 112) { 1043 if (status == 104 || status == 112) {
1044 status = 0; 1044 status = 0;
1045 - status = __ffopen(FHANDLE(m_fitsfile), filename.url().c_str(), 0, &status); 1045 + status = __ffopen(FHANDLE(m_fitsfile), std::string(filename).c_str(), 0, &status);
1046 m_readwrite = false; 1046 m_readwrite = false;
1047 } 1047 }
1048 1048
@@ -1050,7 +1050,7 @@ void GFits::open(const GFilename& filename, const bool& create) @@ -1050,7 +1050,7 @@ void GFits::open(const GFilename& filename, const bool& create)
1050 // FITS file now 1050 // FITS file now
1051 if (create && status == 104) { 1051 if (create && status == 104) {
1052 status = 0; 1052 status = 0;
1053 - status = __ffinit(FHANDLE(m_fitsfile), filename.url().c_str(), &status); 1053 + status = __ffinit(FHANDLE(m_fitsfile), std::string(filename).c_str(), &status);
1054 m_readwrite = true; 1054 m_readwrite = true;
1055 m_created = true; 1055 m_created = true;
1056 } 1056 }
test/data/file.fits.gz 0 → 100644
No preview for this file type
test/test_GFits.py
1 # ========================================================================== 1 # ==========================================================================
2 # This module performs unit tests for the GammaLib FITS module. 2 # This module performs unit tests for the GammaLib FITS module.
3 # 3 #
4 -# Copyright (C) 2012-2015 Juergen Knoedlseder 4 +# Copyright (C) 2012-2016 Juergen Knoedlseder
5 # 5 #
6 # This program is free software: you can redistribute it and/or modify 6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by 7 # it under the terms of the GNU General Public License as published by
@@ -18,10 +18,10 @@ @@ -18,10 +18,10 @@
18 # 18 #
19 # ========================================================================== 19 # ==========================================================================
20 import math 20 import math
21 -import os  
22 import sys 21 import sys
23 import gammalib 22 import gammalib
24 23
  24 +
25 # =================================== # 25 # =================================== #
26 # Test class for GammaLib FITS module # 26 # Test class for GammaLib FITS module #
27 # =================================== # 27 # =================================== #
@@ -48,6 +48,8 @@ class Test(gammalib.GPythonTestSuite): @@ -48,6 +48,8 @@ class Test(gammalib.GPythonTestSuite):
48 48
49 # Append tests 49 # Append tests
50 self.append(self.test_fits, "Test GFits") 50 self.append(self.test_fits, "Test GFits")
  51 + self.append(self.test_fits_image, "Test GFitsImage")
  52 + self.append(self.test_fits_table, "Test GFitsTable")
51 self.append(self.test_fits_table_columns, "Test FITS table columns") 53 self.append(self.test_fits_table_columns, "Test FITS table columns")
52 54
53 # Return 55 # Return
@@ -55,26 +57,56 @@ class Test(gammalib.GPythonTestSuite): @@ -55,26 +57,56 @@ class Test(gammalib.GPythonTestSuite):
55 57
56 def test_fits(self): 58 def test_fits(self):
57 """ 59 """
58 - Test FITS interface. 60 + Test GFile class interface.
59 """ 61 """
60 - # Set filenames  
61 - file1 = "test_python_fits_v1.fits"  
62 - file2 = "test_python_fits_v2.fits" 62 + # Set test file names
  63 + file = gammalib.GFilename("data/file.fits")
63 64
64 - # Remove test files 65 + # Test creation of FITS file
  66 + self.test_try("Test GFits file constructor")
65 try: 67 try:
66 - os.remove(file1)  
67 - os.remove(file2) 68 + fits = gammalib.GFits(file, True)
  69 + self.test_try_success()
68 except: 70 except:
69 - pass 71 + self.test_try_failure("Unable to create file.")
  72 +
  73 + # Open FITS file
  74 + fits = gammalib.GFits(file)
  75 + self.test_value(fits["EVENTS"].nrows(), 1231, "Check number of rows")
  76 +
  77 + # Open FITS file with event selection
  78 + fits = gammalib.GFits(file+"[EVENTS][ENERGY>1.0]")
  79 + self.test_value(fits["EVENTS"].nrows(), 152, "Check number of rows")
  80 +
  81 + # Open FITS file with non-existing extension name
  82 + self.test_try("Test non-existing extension name")
  83 + try:
  84 + fits = gammalib.GFits(file+"[DUMMY][ENERGY>1.0]")
  85 + self.test_try_failure()
  86 + except:
  87 + self.test_try_success()
  88 +
  89 + # Return
  90 + return
  91 +
  92 + def test_fits_image(self):
  93 + """
  94 + Test GFitsImage class interface.
  95 + """
  96 + # Set test file names
  97 + file1 = gammalib.GFilename("test_python_fits_image_v1.fits")
  98 + file2 = gammalib.GFilename("test_python_fits_image_v2.fits")
  99 +
  100 + # Remove test files
  101 + file1.remove()
  102 + file2.remove()
70 103
71 # Create FITS file 104 # Create FITS file
72 fits = gammalib.GFits(file1, True) 105 fits = gammalib.GFits(file1, True)
73 - sys.stdout.write(".")  
74 106
75 # Create images 107 # Create images
76 - nx = 10  
77 - ny = 10 108 + nx = 10
  109 + ny = 10
78 img1 = gammalib.GFitsImageByte(nx, ny) 110 img1 = gammalib.GFitsImageByte(nx, ny)
79 img2 = gammalib.GFitsImageDouble(nx, ny) 111 img2 = gammalib.GFitsImageDouble(nx, ny)
80 img3 = gammalib.GFitsImageFloat(nx, ny) 112 img3 = gammalib.GFitsImageFloat(nx, ny)
@@ -104,7 +136,6 @@ class Test(gammalib.GPythonTestSuite): @@ -104,7 +136,6 @@ class Test(gammalib.GPythonTestSuite):
104 img7.extname("Short") 136 img7.extname("Short")
105 img8.extname("ULong") 137 img8.extname("ULong")
106 img9.extname("UShort") 138 img9.extname("UShort")
107 - sys.stdout.write(".")  
108 139
109 # Append images to FITS file 140 # Append images to FITS file
110 fits.append(img1) 141 fits.append(img1)
@@ -116,14 +147,52 @@ class Test(gammalib.GPythonTestSuite): @@ -116,14 +147,52 @@ class Test(gammalib.GPythonTestSuite):
116 fits.append(img7) 147 fits.append(img7)
117 fits.append(img8) 148 fits.append(img8)
118 fits.append(img9) 149 fits.append(img9)
119 - sys.stdout.write(".")  
120 150
121 # Set header keywords 151 # Set header keywords
122 img_byte = fits.image(0) 152 img_byte = fits.image(0)
123 img_byte.card("test", "test-value", "this is for testing") 153 img_byte.card("test", "test-value", "this is for testing")
124 img_byte.card("real", 3.1415, "a real value") 154 img_byte.card("real", 3.1415, "a real value")
125 img_byte.card("int", 41, "an integer value") 155 img_byte.card("int", 41, "an integer value")
126 - sys.stdout.write(".") 156 +
  157 + # Save FITS file
  158 + fits.save()
  159 +
  160 + # Close FITS file
  161 + fits.close()
  162 +
  163 + # Re-open FITS file
  164 + fits = gammalib.GFits(file1)
  165 +
  166 + # Get double precision image, take square root of pixel and save in
  167 + # another file
  168 + img_double = fits.image("Double")
  169 + for x in range(nx):
  170 + for y in range(ny):
  171 + img_double[x, y] = math.sqrt(img_double[x, y])
  172 +
  173 + # Save into another FITS file
  174 + fits.saveto(file2)
  175 +
  176 + # Close FITS file
  177 + fits.close()
  178 +
  179 + # Return
  180 + return
  181 +
  182 + def test_fits_table(self):
  183 + """
  184 + Test GFitsTable class interface.
  185 + """
  186 + # Set test file names
  187 + file1 = gammalib.GFilename("test_python_fits_table_v1.fits")
  188 + file2 = gammalib.GFilename("test_python_fits_table_v2.fits")
  189 +
  190 + # Remove test files
  191 + file1.remove()
  192 + file2.remove()
  193 +
  194 + # Create FITS file
  195 + fits = gammalib.GFits(file1, True)
127 196
128 # Create table columns 197 # Create table columns
129 nrows = 10 198 nrows = 10
@@ -152,7 +221,6 @@ class Test(gammalib.GPythonTestSuite): @@ -152,7 +221,6 @@ class Test(gammalib.GPythonTestSuite):
152 col9[i] = str(i * 100) 221 col9[i] = str(i * 100)
153 col10[i] = i * 100 222 col10[i] = i * 100
154 col11[i] = i * 100 223 col11[i] = i * 100
155 - sys.stdout.write(".")  
156 224
157 # Set ASCII table 225 # Set ASCII table
158 tbl_ascii = gammalib.GFitsAsciiTable() 226 tbl_ascii = gammalib.GFitsAsciiTable()
@@ -169,7 +237,6 @@ class Test(gammalib.GPythonTestSuite): @@ -169,7 +237,6 @@ class Test(gammalib.GPythonTestSuite):
169 tbl_ascii.append(col11) 237 tbl_ascii.append(col11)
170 tbl_ascii.extname("ASCII table") 238 tbl_ascii.extname("ASCII table")
171 fits.append(tbl_ascii) 239 fits.append(tbl_ascii)
172 - sys.stdout.write(".")  
173 240
174 # Set binary table 241 # Set binary table
175 tbl_bin = gammalib.GFitsBinTable() 242 tbl_bin = gammalib.GFitsBinTable()
@@ -186,36 +253,12 @@ class Test(gammalib.GPythonTestSuite): @@ -186,36 +253,12 @@ class Test(gammalib.GPythonTestSuite):
186 tbl_bin.append(col11) 253 tbl_bin.append(col11)
187 tbl_bin.extname("Binary table") 254 tbl_bin.extname("Binary table")
188 fits.append(tbl_bin) 255 fits.append(tbl_bin)
189 - sys.stdout.write(".")  
190 256
191 # Save FITS file 257 # Save FITS file
192 - # sys.stdout.write(fits+"\n")  
193 fits.save() 258 fits.save()
194 - sys.stdout.write(".")  
195 -  
196 - # Close FITS file  
197 - fits.close()  
198 - sys.stdout.write(".")  
199 -  
200 - # Re-open FITS file  
201 - fits = gammalib.GFits(file1)  
202 - sys.stdout.write(".")  
203 -  
204 - # Get double precision image, take square root of pixel and save in  
205 - # another file  
206 - img_double = fits.image("Double")  
207 - for x in range(nx):  
208 - for y in range(ny):  
209 - img_double[x, y] = math.sqrt(img_double[x, y])  
210 - sys.stdout.write(".")  
211 -  
212 - # Save into another FITS file  
213 - fits.saveto(file2)  
214 - sys.stdout.write(".")  
215 259
216 # Close FITS file 260 # Close FITS file
217 fits.close() 261 fits.close()
218 - sys.stdout.write(".")  
219 262
220 # Return 263 # Return
221 return 264 return