From 37e8b11a24b0c3148d757851292c7358274ea281 Mon Sep 17 00:00:00 2001
From: Dario Barghini <dario.barghini@inaf.it>
Date: Thu, 1 Feb 2024 10:11:41 +0100
Subject: [PATCH] Solved calibration_image.pro bug when reading empty or
 corrupted FITS files

---
 code/calibration/calibration.pro        |  2 +-
 code/calibration/calibration_image.pro  | 18 ++++++++++++------
 code/event/event.pro                    |  2 +-
 code/functions/common/check_path.pro    |  2 +-
 code/functions/fits/ispar.pro           | 10 ++++++++++
 code/functions/fits/read_fits.pro       |  8 ++++++--
 code/functions/orbit/orbital_vector.pro |  1 -
 7 files changed, 31 insertions(+), 12 deletions(-)
 create mode 100644 code/functions/fits/ispar.pro

diff --git a/code/calibration/calibration.pro b/code/calibration/calibration.pro
index 140074f..bd9c3ee 100644
--- a/code/calibration/calibration.pro
+++ b/code/calibration/calibration.pro
@@ -17,7 +17,7 @@ useful_vars
 
 ; checking for input, if not use these ones
 if ~isa(camera) then camera = 'ITPI01'
-if ~isa(target) then target = '201903'
+if ~isa(target) then target = '202001'
 
 ; configuration file path, if not present using standard one defined in the code
 if ~isa(config_file) then config_file = config_path_filename()
diff --git a/code/calibration/calibration_image.pro b/code/calibration/calibration_image.pro
index 6e53d10..1263d58 100644
--- a/code/calibration/calibration_image.pro
+++ b/code/calibration/calibration_image.pro
@@ -65,14 +65,20 @@ for i=0, n-1 do begin
   img = read_fits(captures[i], rotate = par.fits.rotate, header = h)
   s = size(img)
   
-  ; if dimensions match, adding to the stack
+  ; if dimensions match
   if s[0] eq 2 and s[1] eq rotdim[0] and s[2] eq rotdim[1] then begin
     
-    stack[*,*,i] = img
-    headers.add, h
-    ii_dim = [ii_dim, i]
-    
-  endif 
+    ; if needed keywords are provided in the fits header
+    if ispar(h, 'EXPOSURE') and ispar(h, 'DATE-OBS') then begin
+      
+      ; adding to the stack
+      stack[*,*,i] = img
+      headers.add, h
+      ii_dim = [ii_dim, i]
+      
+    endif
+
+  endif
   
 endfor
 
diff --git a/code/event/event.pro b/code/event/event.pro
index 72a3549..3db2f38 100644
--- a/code/event/event.pro
+++ b/code/event/event.pro
@@ -14,7 +14,7 @@ compile_opt idl2
 
 useful_vars
 
-if ~isa(event) then event = '20190415T194950_UT'
+if ~isa(event) then event = '20200101T182654_UT'
 
 ; configuration file path
 if ~isa(config_file) then config_file = config_path_filename()
diff --git a/code/functions/common/check_path.pro b/code/functions/common/check_path.pro
index fcae6a7..704258e 100644
--- a/code/functions/common/check_path.pro
+++ b/code/functions/common/check_path.pro
@@ -6,7 +6,7 @@ compile_opt idl2
 
 ; checking if last character is the path separator
 sep = strmid(path, strlen(path) - 1, 1)
-if sep eq path_sep() then retv = strmid(path, 0, strlen(path) - 2) else retv = path
+if sep eq path_sep() then retv = strmid(path, 0, strlen(path) - 1) else retv = path
    
 return, retv
 
diff --git a/code/functions/fits/ispar.pro b/code/functions/fits/ispar.pro
new file mode 100644
index 0000000..78ee63c
--- /dev/null
+++ b/code/functions/fits/ispar.pro
@@ -0,0 +1,10 @@
+; checking the presence of a keyword in a FITS header
+
+function ispar, header, keyword
+
+compile_opt idl2
+
+check = isa(sxpar(header, keyword, /null))
+return, check
+
+end
\ No newline at end of file
diff --git a/code/functions/fits/read_fits.pro b/code/functions/fits/read_fits.pro
index aff8e8c..8eb6565 100644
--- a/code/functions/fits/read_fits.pro
+++ b/code/functions/fits/read_fits.pro
@@ -9,8 +9,12 @@ compile_opt idl2
 ; standard rotate value
 if ~isa(rotate) then rotate = 0
 
-; reading and rotate fits image
-retv = rotate(readfits(nomefile, header, /silent), rotate)
+; reading fits image
+retv = readfits(nomefile, header, /silent)
+
+; rotate the image (if twodimensional)
+s = size(retv)
+if s[0] eq 2 then retv = rotate(retv, rotate)
 
 ; making it float precision
 retv = float(retv)
diff --git a/code/functions/orbit/orbital_vector.pro b/code/functions/orbit/orbital_vector.pro
index c806a3c..73176aa 100644
--- a/code/functions/orbit/orbital_vector.pro
+++ b/code/functions/orbit/orbital_vector.pro
@@ -59,7 +59,6 @@ if isa(covar_elements) then begin
   so = sin(omega)
   cnu = cos(nu)
   snu = sin(nu)
-
   
   Jrx = [ $
          cl*((2.*h*cnu*co)/(!mu*(e*cnu + 1.)) - (2.*h*snu*so)/(!mu*(e*cnu + 1.))) - sl*ci*((2.*h*cnu*so)/(!mu*(e*cnu + 1.)) + (2.*h*co*snu)/(!mu*(e*cnu + 1.))), $
-- 
GitLab