Repository: soar-telescope/goodman_focus

Issue:
Add log report when no files are found
For older data the keyword OBSTYPE does not contain the vale FOCUS which causes the program to exit without doing anything and without any error messages.

Discussion:

Patch:
diff --git a/goodman_focus/goodman_focus.py b/goodman_focus/goodman_focus.py
index f975ec9..04523bc 100644
--- a/goodman_focus/goodman_focus.py
+++ b/goodman_focus/goodman_focus.py
@@ -102,9 +102,12 @@ def get_args(arguments=None):
     args = parser.parse_args(args=arguments)
 
     if not os.path.isdir(args.data_path):
-        parser.error("Data location {} does not exist".format(args.data_path))
+        log.error("Data location {} does not exist".format(args.data_path))
+        sys.exit(0)
     elif len(glob.glob(os.path.join(args.data_path, args.file_pattern))) == 0:
-        parser.error("There are no files matching \"{}\" in the folder \"{}\"".format(args.file_pattern, args.data_path))
+        log.error("There are no files matching \"{}\" in the folder \"{}\""
+                  "".format(args.file_pattern, args.data_path))
+        sys.exit(0)
 
     return args
 
@@ -325,32 +328,45 @@ class GoodmanFocus(object):
         _ifc = ImageFileCollection(self.full_path, keywords=self.keywords)
 
         self.ifc = _ifc.summary.to_pandas()
+        self.log.debug("Found {} FITS files".format(self.ifc.shape[0]))
         self.ifc = self.ifc[(self.ifc['OBSTYPE'] == self.args.obstype)]
-        self.focus_groups = []
-        configs = self.ifc.groupby(['CAM_TARG',
-                                    'GRT_TARG',
-                                    'FILTER',
-                                    'FILTER2',
-                                    'GRATING',
-                                    'SLIT',
-                                    'WAVMODE',
-                                    'RDNOISE',
-                                    'GAIN',
-                                    'ROI']).size().reset_index().rename(
-            columns={0: 'count'})
-        # print(configs.to_string())
-        for i in configs.index:
-            focus_group = self.ifc[((self.ifc['CAM_TARG'] == configs.iloc[i]['CAM_TARG']) &
-                                    (self.ifc['GRT_TARG'] == configs.iloc[i]['GRT_TARG']) &
-                                    (self.ifc['FILTER'] == configs.iloc[i]['FILTER']) &
-                                    (self.ifc['FILTER2'] == configs.iloc[i]['FILTER2']) &
-                                    (self.ifc['GRATING'] == configs.iloc[i]['GRATING']) &
-                                    (self.ifc['SLIT'] == configs.iloc[i]['SLIT']) &
-                                    (self.ifc['WAVMODE'] == configs.iloc[i]['WAVMODE']) &
-                                    (self.ifc['RDNOISE'] == configs.iloc[i]['RDNOISE']) &
-                                    (self.ifc['GAIN'] == configs.iloc[i]['GAIN']) &
-                                    (self.ifc['ROI'] == configs.iloc[i]['ROI']))]
-            self.focus_groups.append(focus_group)
+        if self.ifc.shape[0] != 0:
+            self.log.debug("Found {} FITS files with OBSTYPE = FOCUS".format(
+                self.ifc.shape[0]))
+
+            self.focus_groups = []
+            configs = self.ifc.groupby(['CAM_TARG',
+                                        'GRT_TARG',
+                                        'FILTER',
+                                        'FILTER2',
+                                        'GRATING',
+                                        'SLIT',
+                                        'WAVMODE',
+                                        'RDNOISE',
+                                        'GAIN',
+                                        'ROI']).size().reset_index().rename(
+                columns={0: 'count'})
+            # print(configs.to_string())
+            for i in configs.index:
+                focus_group = self.ifc[((self.ifc['CAM_TARG'] == configs.iloc[i]['CAM_TARG']) &
+                                        (self.ifc['GRT_TARG'] == configs.iloc[i]['GRT_TARG']) &
+                                        (self.ifc['FILTER'] == configs.iloc[i]['FILTER']) &
+                                        (self.ifc['FILTER2'] == configs.iloc[i]['FILTER2']) &
+                                        (self.ifc['GRATING'] == configs.iloc[i]['GRATING']) &
+                                        (self.ifc['SLIT'] == configs.iloc[i]['SLIT']) &
+                                        (self.ifc['WAVMODE'] == configs.iloc[i]['WAVMODE']) &
+                                        (self.ifc['RDNOISE'] == configs.iloc[i]['RDNOISE']) &
+                                        (self.ifc['GAIN'] == configs.iloc[i]['GAIN']) &
+                                        (self.ifc['ROI'] == configs.iloc[i]['ROI']))]
+                self.focus_groups.append(focus_group)
+        else:
+            self.log.critical('Focus files must have OBSTYPE keyword equal to '
+                              '"FOCUS", none found.')
+            self.log.info('Please use "--obstype" to change the value though '
+                          'it is not recommended to use neither "OBJECT" nor '
+                          '"FLAT" because it may contaminate the sample with '
+                          'non focus images.')
+            sys.exit(0)
 
     @property
     def fwhm(self):


Test patch:
diff --git a/goodman_focus/tests/test_goodman_focus.py b/goodman_focus/tests/test_goodman_focus.py
index e06ba7d..73c162b 100644
--- a/goodman_focus/tests/test_goodman_focus.py
+++ b/goodman_focus/tests/test_goodman_focus.py
@@ -166,3 +166,56 @@ class GoodmanFocusTests(TestCase):
     def tearDown(self):
         for _file in self.file_list:
             os.unlink(_file)
+
+
+class DirectoryAndFilesTest(TestCase):
+
+    def setUp(self):
+        self.arguments = [
+            '--data-path', os.path.join(os.getcwd(), 'nonexisting'),
+            '--file-pattern', '*.fits',
+            '--obstype', 'FOCUS',
+            '--features-model', 'gaussian']
+
+        os.mkdir(os.path.join(os.getcwd(), 'test_dir_empty'))
+        os.mkdir(os.path.join(os.getcwd(), 'test_dir_no_focus'))
+        for i in range(3):
+            ccd = CCDData(data=np.ones((100, 100)),
+                          meta=fits.Header(),
+                          unit='adu')
+            ccd.header['obstype'] = 'OBJECT'
+            ccd.header['cam_foc'] = 0
+            ccd.header['cam_targ'] = 0
+            ccd.header['grt_targ'] = 0
+            ccd.header['filter'] = 'filter'
+            ccd.header['filter2'] = 'filter2'
+            ccd.header['grating'] = 'grating'
+            ccd.header['slit'] = '0.4 slit'
+            ccd.header['wavmode'] = '400m2'
+            ccd.header['rdnoise'] = 1
+            ccd.header['gain'] = 1
+            ccd.header['roi'] = 'user-defined'
+
+            ccd.write(os.path.join(os.getcwd(),
+                                   'test_dir_no_focus',
+                                   'file_{}.fits'.format(i+1)))
+
+    def test_directory_does_not_exists(self):
+
+        # goodman_focus = GoodmanFocus(arguments=arguments)
+        self.assertRaises(SystemExit, GoodmanFocus, self.arguments)
+
+    def test_directory_exists_but_empty(self):
+        self.arguments[1] = os.path.join(os.getcwd(), 'test_dir_empty')
+        self.assertRaises(SystemExit, GoodmanFocus, self.arguments)
+
+    def test_no_focus_files(self):
+        self.arguments[1] = os.path.join(os.getcwd(), 'test_dir_no_focus')
+        self.assertRaises(SystemExit, GoodmanFocus, self.arguments)
+
+
+    def tearDown(self):
+        os.rmdir(os.path.join(os.getcwd(), 'test_dir_empty'))
+        for _file in os.listdir(os.path.join(os.getcwd(), 'test_dir_no_focus')):
+            os.unlink(os.path.join(os.getcwd(), 'test_dir_no_focus', _file))
+        os.rmdir(os.path.join(os.getcwd(), 'test_dir_no_focus'))