LCOV - code coverage report
Current view: top level - source/ioutils/fits - fitsUtils.cpp (source / functions) Coverage Total Hit
Test: mxlib Lines: 65.6 % 64 42
Test Date: 2026-08-30 22:23:37 Functions: 75.0 % 8 6

            Line data    Source code
       1              : /** \file fitsUtils.cpp
       2              :  * \brief Implementation of utilities to work with FITS files
       3              :  * \ingroup fits_processing_files
       4              :  * \author Jared R. Males (jaredmales@gmail.com)
       5              :  *
       6              :  */
       7              : 
       8              : //***********************************************************************//
       9              : // Copyright 2015-2022 Jared R. Males (jaredmales@gmail.com)
      10              : //
      11              : // This file is part of mxlib.
      12              : //
      13              : // mxlib is free software: you can redistribute it and/or modify
      14              : // it under the terms of the GNU General Public License as published by
      15              : // the Free Software Foundation, either version 3 of the License, or
      16              : // (at your option) any later version.
      17              : //
      18              : // mxlib is distributed in the hope that it will be useful,
      19              : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      20              : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      21              : // GNU General Public License for more details.
      22              : //
      23              : // You should have received a copy of the GNU General Public License
      24              : // along with mxlib.  If not, see <http://www.gnu.org/licenses/>.
      25              : //***********************************************************************//
      26              : 
      27              : #include "ioutils/fits/fitsUtils.hpp"
      28              : 
      29              : namespace mx
      30              : {
      31              : namespace fits
      32              : {
      33              : 
      34            0 : int fitsStripApost( std::string &s )
      35              : {
      36            0 :     int stripped = 0;
      37            0 :     int p = s.find_first_not_of( " \t\n\t" );
      38            0 :     if( s[p] == '\'' )
      39              :     {
      40            0 :         s.erase( 0, p + 1 );
      41            0 :         ++stripped;
      42              :     }
      43              : 
      44            0 :     p = s.find_last_not_of( " \t\n\t" );
      45            0 :     if( s[p] == '\'' )
      46              :     {
      47            0 :         s.erase( p );
      48            0 :         ++stripped;
      49              :     }
      50              : 
      51            0 :     --p;
      52              : 
      53            0 :     while( s[p] == ' ' && p >= 0 )
      54              :     {
      55            0 :         s.erase( p );
      56            0 :         --p;
      57            0 :         ++stripped;
      58              :     }
      59              : 
      60            0 :     return stripped;
      61              : }
      62              : 
      63            1 : void fitsPopulateCard( char headStr[81], char *keyword, char *value, char *comment )
      64              : {
      65            1 :     memset( headStr, ' ', 80 );
      66            1 :     headStr[80] = '\0';
      67              : 
      68            1 :     int rpos = 0;
      69              : 
      70            1 :     if( strlen( keyword ) > 8 )
      71              :     {
      72            0 :         rpos += snprintf( headStr, 81, "HIERARCH " );
      73              : 
      74            0 :         rpos += snprintf( headStr + rpos, 81 - rpos, "%s =", keyword );
      75              :     }
      76              :     else
      77              :     {
      78            1 :         rpos += snprintf( headStr, 81, "%-8s=", keyword );
      79              :     }
      80              : 
      81            1 :     if( strlen( value ) < stdValWidth )
      82              :     {
      83              :         char fstr[10];
      84            1 :         snprintf( fstr, 10, "%%+%ds", stdValWidth );
      85            1 :         rpos += snprintf( headStr + rpos, 81 - rpos, fstr, value );
      86              :     }
      87              :     else
      88              :     {
      89            0 :         rpos += snprintf( headStr + rpos, 81 - rpos, "%s", value );
      90              :     }
      91              : 
      92            1 :     headStr[rpos] = ' ';
      93            1 :     ++rpos;
      94              : 
      95            1 :     headStr[rpos] = '/';
      96            1 :     ++rpos;
      97              : 
      98            1 :     headStr[rpos] = ' ';
      99            1 :     ++rpos;
     100              : 
     101            1 :     snprintf( headStr + rpos, 81 - rpos, "%s", comment );
     102            1 : }
     103              : 
     104              : template <>
     105            1 : mx::error_t fits_write_key<char *>( fitsfile *fptr, char *keyword, void *value, char *comment )
     106              : {
     107            1 :     int fstatus = 0;
     108              : 
     109            1 :     fits_write_key_longwarn( fptr, &fstatus );
     110              : 
     111            1 :     if(fstatus != 0)
     112              :     {
     113            0 :         return fits_status2error_t(fstatus);
     114              :     }
     115              : 
     116            1 :     fits_write_key_longstr( fptr, keyword, (const char *)value, comment, &fstatus );
     117              : 
     118            1 :     return fits_status2error_t(fstatus);
     119              : }
     120              : 
     121              : template <>
     122            0 : mx::error_t fits_write_key<std::string>( fitsfile *fptr, char *keyword, void *value, char *comment )
     123              : {
     124            0 :     return fits_write_key<char *>( fptr, keyword, value, comment );
     125              : }
     126              : 
     127              : template <>
     128            4 : mx::error_t fits_write_key<bool>( fitsfile *fptr, char *keyword, void *value, char *comment )
     129              : {
     130            4 :     unsigned char bc = *( (bool *)value );
     131              : 
     132            4 :     int fstatus = 0;
     133              : 
     134            4 :     fits_write_key( fptr, fitsType<unsigned char>(), keyword, &bc, comment, &fstatus );
     135              : 
     136            8 :     return fits_status2error_t(fstatus);
     137              : }
     138              : 
     139              : template <>
     140            1 : mx::error_t fits_write_key<fitsUnknownType>( fitsfile *fptr, char *keyword, void *value, char *comment )
     141              : {
     142              : 
     143            1 :     int fstatus = 0;
     144              : 
     145            1 :     char *vstr = (char *)value;
     146              : 
     147              :     // If type is unkown, do it verbatim
     148              :     char headStr[81];
     149              : 
     150            1 :     fitsPopulateCard( headStr, keyword, vstr, comment );
     151              : 
     152            1 :     fits_write_record( fptr, headStr, &fstatus );
     153              : 
     154            2 :     return fits_status2error_t(fstatus);
     155              : }
     156              : 
     157            4 : mx::error_t fits_write_comment( fitsfile *fptr, char *comment )
     158              : {
     159            4 :     int fstatus = 0;
     160              : 
     161            4 :     fits_write_comment( fptr, comment, &fstatus );
     162              : 
     163            8 :     return fits_status2error_t(fstatus);
     164              : }
     165              : 
     166            4 : mx::error_t fits_write_history( fitsfile *fptr, char *history )
     167              : {
     168            4 :     int fstatus = 0;
     169              : 
     170            4 :     fits_write_history( fptr, history, &fstatus );
     171              : 
     172            8 :     return fits_status2error_t(fstatus);
     173              : }
     174              : 
     175              : /*void fitsErrText( std::string &explan, const std::string &filename, int fstatus )
     176              : {
     177              :     char emnem[31];
     178              : 
     179              :     fits_get_errstatus( fstatus, emnem );
     180              : 
     181              :     explan += ": ";
     182              :     explan += filename;
     183              :     explan += ". CFITSIO: ";
     184              : 
     185              :     explan += emnem;
     186              :     explan += " (";
     187              :     explan += std::format( fstatus );
     188              :     explan += ")";
     189              : }*/
     190              : 
     191              : } // namespace fits
     192              : } // namespace mx
        

Generated by: LCOV version 2.0-1