From d7308f663de3e5e9da29cd855308cbe1cd15f986 Mon Sep 17 00:00:00 2001
From: Robert Butora <robert.butora@inaf.it>
Date: Tue, 3 Sep 2024 14:41:59 +0300
Subject: [PATCH] vlkb: header subcommands now actually writes/updates the
 header in the given fits-file

---
 .../src/common/include/ast_frameset.hpp       |  1 +
 .../common/{src => include}/fits_header.hpp   |  1 +
 .../engine/src/common/src/ast_frameset.cpp    | 24 +++++++++++++++++++
 .../engine/src/common/src/fits_header.cpp     | 18 ++++++++++++++
 data-access/engine/src/vlkb/src/ast.cpp       | 15 +++++++++---
 5 files changed, 56 insertions(+), 3 deletions(-)
 rename data-access/engine/src/common/{src => include}/fits_header.hpp (98%)

diff --git a/data-access/engine/src/common/include/ast_frameset.hpp b/data-access/engine/src/common/include/ast_frameset.hpp
index 542c963..b1c570a 100644
--- a/data-access/engine/src/common/include/ast_frameset.hpp
+++ b/data-access/engine/src/common/include/ast_frameset.hpp
@@ -40,6 +40,7 @@ class frameset
       overlap_ranges overlap(coordinates coord);
 
       void write(std::ostream& ostrm, std::string header);
+      void write2(std::string fits_pathname, int hdunum);
 
       std::ostream& serialize(std::ostream& strm) const;
 
diff --git a/data-access/engine/src/common/src/fits_header.hpp b/data-access/engine/src/common/include/fits_header.hpp
similarity index 98%
rename from data-access/engine/src/common/src/fits_header.hpp
rename to data-access/engine/src/common/include/fits_header.hpp
index 928e2dc..7779387 100644
--- a/data-access/engine/src/common/src/fits_header.hpp
+++ b/data-access/engine/src/common/include/fits_header.hpp
@@ -47,6 +47,7 @@ class header
       int read_record(int keynum, char *card, int *status);
       int get_nkeys();
       void update(const std::vector<fits_card> additional_cards);
+      void update(const std::string card);
       std::string get_header(bool apply_fixes = false);
       std::string read_card(std::string keyname);
       bool contains_card(std::string keyname);
diff --git a/data-access/engine/src/common/src/ast_frameset.cpp b/data-access/engine/src/common/src/ast_frameset.cpp
index d557629..b540ea6 100644
--- a/data-access/engine/src/common/src/ast_frameset.cpp
+++ b/data-access/engine/src/common/src/ast_frameset.cpp
@@ -2,6 +2,7 @@
 
 
 #include "ast_frameset.hpp"
+#include "fits_header.hpp"
 #include "io.hpp"
 #include "my_assert.hpp"
 
@@ -650,6 +651,29 @@ void ast::frameset::write(std::ostream& ostrm, std::string header)
 }
 
 
+void ast::frameset::write2(std::string fits_pathname, int hdunum)
+{
+   LOG_trace(__func__);
+
+   AstFitsChan * fchan = astFitsChan( NULL, NULL, "Encoding=FITS-WCS" );
+
+   astWrite(fchan, m_hdr_fs);
+   if ( !astOK )
+      throw runtime_error(failed_with_status(__FILE__,__LINE__,"astWrite(fchan, m_hdr_fs)"));
+
+   fits::header hdr(fits_pathname, hdunum, READWRITE);
+
+   char card[ 81 ];
+   astClear( fchan, "Card" );
+   while ( astFindFits( fchan, "%f", card, 1 ) )
+   {
+      if(0 ==string(card).compare(0,7,"WCSAXES")) continue; // writes this key after other existing CRxxx keys, which is illegal
+      cout << string(card) << endl;
+      hdr.update(card);
+   }
+}
+
+
 
 
 
diff --git a/data-access/engine/src/common/src/fits_header.cpp b/data-access/engine/src/common/src/fits_header.cpp
index 6149acf..c569d0c 100644
--- a/data-access/engine/src/common/src/fits_header.cpp
+++ b/data-access/engine/src/common/src/fits_header.cpp
@@ -445,6 +445,24 @@ void fits::header::update_card(const struct fits_card new_card)
 }
 
 
+void fits::header::update(const std::string card)
+{
+   LOG_trace(__func__);
+
+   int status = 0;
+   int keylength;
+   char ccard[FLEN_CARD];
+   char keyname[FLEN_KEYWORD];
+
+   strcpy(ccard ,card.c_str());
+
+   if(fits_get_keyname(ccard, keyname, &keylength, &status))
+      throw runtime_error(cfitsio_errmsg(__FILE__,__LINE__,status) + " card: " + card);
+
+   if(fits_update_card(fptr, keyname, ccard, &status))
+      throw runtime_error(cfitsio_errmsg(__FILE__,__LINE__,status) + " card: " + card);
+}
+
 
 
 void fits::header::update(const vector<fits_card> additional_cards)
diff --git a/data-access/engine/src/vlkb/src/ast.cpp b/data-access/engine/src/vlkb/src/ast.cpp
index 03cc610..5cdeeea 100644
--- a/data-access/engine/src/vlkb/src/ast.cpp
+++ b/data-access/engine/src/vlkb/src/ast.cpp
@@ -94,6 +94,15 @@ int vlkb_listbounds(const string& skysys_str, const string& specsys_str, const s
 const string VELOLSRK{"System=VELO,StdOfRest=LSRK,Unit=km/s"};
 const string WAVEBARY{"System=WAVE,StdOfRest=Bary,Unit=m"};
 */
+
+void write_previous(string header, string filename)
+{
+   std::ofstream out(filename);
+   out << header;
+}
+
+
+
 int header_modif_coordsys(const string& skysys_str, const string& specsys_str, const string& pathname)
 {
    LOG_trace(__func__);
@@ -109,20 +118,20 @@ int header_modif_coordsys(const string& skysys_str, const string& specsys_str, c
 
       fitsfiles::Hdu hd = allHdus.at(i);
 
+      write_previous(hd.m_header, "backup.fitshdu"+ to_string(i) +"header.orig");
       ast::frameset frm_set(hd.m_header);
       frm_set.set_skysystem(skysys_str); 
       if(frm_set.has_specframe())
          frm_set.set_specsystem(specsys_str);
 
-      frm_set.write(cout, hd.m_header);
+      //frm_set.write(cout, hd.m_header);
+      frm_set.write2(pathname, i+1);
    }
 
    return 0;
 }
 
 
-
-
 //---------------------------------------------------------------------
 // overlap with area given in query-string form (name=value&...)
 //---------------------------------------------------------------------
-- 
GitLab