GFitsTableCol.cpp 34.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
/***************************************************************************
 *        GFitsTableCol.cpp - FITS table column abstract base class        *
 * ----------------------------------------------------------------------- *
 *  copyright (C) 2008-2013 by Juergen Knoedlseder                         *
 * ----------------------------------------------------------------------- *
 *                                                                         *
 *  This program is free software: you can redistribute it and/or modify   *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation, either version 3 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 *                                                                         *
 ***************************************************************************/
/**
 * @file GFitsTableCol.cpp
 * @brief Abstract FITS table column class implementation
 * @author Juergen Knoedlseder
 */

/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cstdlib>
#include "GException.hpp"
#include "GFitsCfitsio.hpp"
#include "GFitsTableCol.hpp"
#include "GTools.hpp"

/* __ Method name definitions ____________________________________________ */
#define G_ELEMENTS1                     "GFitsTableCol::elements(int&, int&)"
#define G_ELEMENTS2                           "GFitsTableCol::elements(int&)"
#define G_LOAD_COLUMN_FIXED              "GFitsTableCol::load_column_fixed()"
#define G_LOAD_COLUMN_VARIABLE        "GFitsTableCol::load_column_variable()"
#define G_SAVE_COLUMN_FIXED              "GFitsTableCol::save_column_fixed()"
#define G_SAVE_COLUMN_VARIABLE        "GFitsTableCol::save_column_variable()"
#define G_OFFSET                          "GFitsTableCol::offset(int&, int&)"

/* __ Macros _____________________________________________________________ */

/* __ Coding definitions _________________________________________________ */

/* __ Debug definitions __________________________________________________ */
//#define G_CALL_GRAPH                        //!< Dump call graph in console
#define G_PRINT_CONTENT                           //!< Print column content


/*==========================================================================
 =                                                                         =
 =                         Constructors/destructors                        =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Void constructor
 ***************************************************************************/
GFitsTableCol::GFitsTableCol(void)
{
    // Initialise class members for clean destruction
    init_members();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Column constructor
 *
 * @param[in] name Name of column.
 * @param[in] length Length of column (number of rows).
 * @param[in] number Number of elements in column (negative for variable-length).
 * @param[in] width Width of one column element.
 *
 * Construct column instance from @p name, @p length, @p number of elements
 * and @p width (or size) of one element. If @p number is negative, a
 * variable-length column will be allocated. The repeat value, which is
 * required for binary tables, is computed internally by multiplying the
 * @p number by the @p width.
 ***************************************************************************/
GFitsTableCol::GFitsTableCol(const std::string& name,
                             const int&         length,
                             const int&         number,
                             const int&         width)
{
    // Initialise class members for clean destruction
    init_members();

    // Store attributes
    if (number >= 0) { // fixed-length column
        m_name     = name;
        m_length   = length;
        m_number   = number;
        m_width    = width;
        m_variable = false;
    }
    else {             // variable-length column
        m_name     = name;
        m_length   = length;
        m_number   = 1;
        m_width    = width;
        m_variable = true;
        m_rowstart.assign(length+1, 0);
    }

    // Calculate repeat value (only used for binary table!)
    m_repeat = m_number * m_width;

    // Return
    return;
}


/***********************************************************************//**
 * @brief Copy constructor
 *
 * @param[in] column Column.
 ***************************************************************************/
GFitsTableCol::GFitsTableCol(const GFitsTableCol& column)
{
    // Initialise class members for clean destruction
    init_members();

    // Copy members
    copy_members(column);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Destructor
 ***************************************************************************/
GFitsTableCol::~GFitsTableCol(void)
{
    // Free members
    free_members();

    // Return
    return;
}


/*==========================================================================
 =                                                                         =
 =                                 Operators                               =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Assignment operator
 *
 * @param[in] column Column.
 * @return Column.
 ***************************************************************************/
GFitsTableCol& GFitsTableCol::operator=(const GFitsTableCol& column)
{
    // Execute only if object is not identical
    if (this != &column) {

        // Free members
        free_members();

        // Initialise private members for clean destruction
        init_members();

        // Copy members
        copy_members(column);

    } // endif: object was not identical

    // Return this object
    return *this;
}


/*==========================================================================
 =                                                                         =
 =                             Public methods                              =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Set number of column elements for specific row
 *
 * @param[in] row Row index.
 * @param[in] elements Number of elements in @p row.
 *
 * @exception GException::out_of_range
 *            Invalid row index specified.
 * @exception GException::invalid_argument
 *            Invalid number of elements specified.
 *
 * Sets the number of elements in column for a specific @p row.
 ***************************************************************************/
void GFitsTableCol::elements(const int& row, const int& elements)
{
    // Check row value
    if (row < 0 || row >= length()) {
        throw GException::out_of_range(G_ELEMENTS1, "Row index", row, length());
    }

    // Check that elements is non-negative
    if (elements < 0) {
        std::string msg = "Number of elements " + gammalib::str(elements) +
                          " can not be negative.\nPlease specify a"
                          " non-negative number of elements.";
        throw GException::invalid_argument(G_ELEMENTS1, msg);
    }

    // First handle the case of a variable-length column
    if (is_variable()) {

        // Determine number of elements to add or to remove
        int difference = elements - (m_rowstart[row+1] - m_rowstart[row]);

        // If difference is positive, then add elements at the end of
        // the vector. This is done using resize_data() which will insert
        // "difference" elements at the index specified as first argument
        if (difference > 0) {
            resize_data(m_rowstart[row+1], difference);
        }

        // ... else if difference is negative then remove elements from
        // the end of the array. This is done using resize_data() which
        // will remove "difference" element from the index on specified as
        // first argument
        else if (difference < 0) {
            resize_data(m_rowstart[row+1]+difference, difference);
        }

        // Update row start indices
        for (int i = row + 1; i <= length(); ++i) {
            m_rowstart[i] += difference;
        }

        // Update maximum column length
        m_varlen = 0;
        for (int i = 0; i < length(); ++i) {
            int len = m_rowstart[row+1] - m_rowstart[row];
            if (len > m_varlen) {
                m_varlen = len;
            }
        }
        
    } // endif: we had a variable-length column

    // Return
    return;
}


/***********************************************************************//**
 * @brief Returns number of elements in column for specific row
 *
 * @param[in] row Row index.
 * @return Number of elements in column at @p row.
 *
 * @exception GException::out_of_range
 *            Row index out of valid range.
 *
 * Returns the number of elements in the column for a specific @p row. For a
 * fixed-length column the returned number is independent of the row. For a
 * variable-length column the returned number is then length of the column
 * for the specified row.
 ***************************************************************************/
int GFitsTableCol::elements(const int& row) const
{
    // Check row value
    if (row < 0 || row >= length()) {
        throw GException::out_of_range(G_ELEMENTS2, "Row index", row, length());
    }

    // Get number of elements
    int number = (m_variable) ? m_rowstart[row+1] - m_rowstart[row]
                              : m_number; 

    // Return number
    return number;
}


/***********************************************************************//**
 * @brief Returns TFORM code for binary table column
 *
 * Constructs the TFORM code for a binary table column, supporting
 * fixed-length and variable-length column types.
 ***************************************************************************/
std::string GFitsTableCol::tform_binary(void) const
{
    // Set type code string
    std::string typecode = "";
    switch (std::abs(type())) {
    case __TBIT:
        typecode = "X";
        break;
    case __TBYTE:
        typecode = "B";
        break;
    case __TLOGICAL:
        typecode = "L";
        break;
    case __TSTRING:
        // If there are substrings then add width of substring
        typecode = "A";
        if (repeat() > width()) {
            typecode.append(gammalib::str(width()));
        }
        break;
    case __TUSHORT:
        typecode = "U";
        break;
    case __TSHORT:
        typecode = "I";
        break;
    case __TULONG:
        typecode = "V";
        break;
    case __TLONG:
        typecode = "J";
        break;
    case __TFLOAT:
        typecode = "E";
        break;
    case __TLONGLONG:
        typecode = "K";
        break;
    case __TDOUBLE:
        typecode = "D";
        break;
    case __TCOMPLEX:
        typecode = "C";
        break;
    case __TDBLCOMPLEX:
        typecode = "M";
        break;
    default:
        break;
    }

    // Build TFORM code
    std::string tform;
    if (std::abs(type()) == __TSTRING) {
        tform.append(gammalib::str(repeat()));
    }
    else {
        tform.append(gammalib::str(number()));
    }
    if (is_variable()) {
        tform.append("P");
    }
    tform.append(typecode);
    if (is_variable() && m_varlen > 0) {
        tform.append("("+gammalib::str(m_varlen)+")");
    }
    
    // Return TFORM code
    return tform;
}


/***********************************************************************//**
 * @brief Print column information
 *
 * @param[in] chatter Chattiness (defaults to NORMAL).
 * @return String containing column information.
 *
 * @todo Format and cfitsio information is mainly for debugging. This could
 * vanish in a more stable version of the code, or it could be compiled-in
 * conditionally using a debug option. Alternatively, a higher chatter level
 * may be required to see this information.
 ***************************************************************************/
std::string GFitsTableCol::print(const GChatter& chatter) const
{
    // Initialise result string
    std::string result;

    // Continue only if chatter is not silent
    if (chatter != SILENT) {

        // Append formatted column name. Optionally add units
        if (unit().length() > 0) {
            result.append(gammalib::parformat(name()+" ("+unit()+")"));
        }
        else {
            result.append(gammalib::parformat(name()));
        }

        // Append column number. This will be "-" if the column does not exist
        // in the FITS file.
        if (m_colnum > 0) {
            result.append(gammalib::right(gammalib::str(m_colnum),4)+" ");
        }
        else {
            result.append(gammalib::right("[-]",4)+" ");
        }

        // Append loading information
        if (is_loaded()) {
            result.append("[loaded]     ");
        }
        else {
            result.append("[not loaded] ");
        }

        // Append format information
        result.append("["+tform_binary()+","+ascii_format()+"]");

        // Append dimensions (if available)
        if (!dim().empty()) {
    
            // Build TDIM string
            std::string value = "("+gammalib::str(dim()[0]);
            for (int k = 1; k < dim().size(); ++k) {
                value += ","+gammalib::str(dim()[k]);
            }
            value += ")";
        
            // Append
            result.append(" "+value);
        }

        // Append cfitsio information
        if (is_variable()) {
            result.append(" repeat=" + gammalib::str(repeat()));
            result.append(" width="  + gammalib::str(width()));
            result.append(" number=" + gammalib::str(number()));
            result.append(" length=" + gammalib::str(length()));
            result.append(" size="   + gammalib::str(m_size));
            result.append(" varlen=");
            if (m_varlen > 0) {
                result.append(gammalib::str(m_varlen));
            }
            else {
                result.append("undetermined");
            }
        }
        else {
            result.append(" repeat=" + gammalib::str(repeat()));
            result.append(" width="  + gammalib::str(width()));
            result.append(" number=" + gammalib::str(number()));
            result.append(" length=" + gammalib::str(length()));
            result.append(" size="   + gammalib::str(m_size));
        }

    } // endif: chatter was not silent

    // Compile option: print content
    #if defined(G_PRINT_CONTENT)
    if (chatter != SILENT) {

        // Fetch data if necessary
        if (!is_loaded()) fetch_data();

        // Loop over all rows
        for (int row = 0; row < length(); ++row) {
            result.append("\n");
            if (is_variable()) {
                result.append("start=");
                result.append(gammalib::str(m_rowstart[row]));
                result.append(":");
            }
            for (int inx = 0; inx < elements(row); ++inx) {
                result.append(" "+string(row, inx));
            }
        }
        if (is_variable()) {
            result.append("\nend=");
            result.append(gammalib::str(m_rowstart[length()]));
        }
    }
    #endif

    // Return result
    return result;
}


/*==========================================================================
 =                                                                         =
 =                            Protected methods                            =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Save table column into FITS file
 *
 * Refer to GFitsTableCol::save_column() for more information.
 ***************************************************************************/
void GFitsTableCol::save(void)
{
    // Save column
    save_column();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Load table column from FITS file
 *
 * Loads table column from FITS file by calling the load_column_variable()
 * method for variable-length columns and load_column_fixed() for fixed-
 * length columns.
 ***************************************************************************/
void GFitsTableCol::load_column(void)
{
    // Load variable-length or fixed-length column from FITS file
    if (is_variable()) {
        load_column_variable();
    }
    else {
        load_column_fixed();
    }

    // Return
    return;
}


/***********************************************************************//**
 * @brief Load fixed-length column from FITS file
 *
 * @exception GException::fits_hdu_not_found
 *            Specified HDU not found in FITS file.
 * @exception GException::fits_error
 *            An error occured while loading column data from FITS file.
 *
 * If a FITS file is attached to the column the data are loaded into memory
 * from the FITS file. If no FITS file is attached, memory is allocated
 * to hold the column data and all cells are set to 0.
 *
 * The method makes use of the virtual methods 
 * GFitsTableCol::alloc_data,
 * GFitsTableCol::init_data,
 * GFitsTableCol::ptr_data, and
 * GFitsTableCol::ptr_nulval.
 * These methods are implemented by the derived column classes which 
 * implement a specific storage class (i.e. float, double, short, ...).
 ***************************************************************************/
void GFitsTableCol::load_column_fixed(void)
{
    // Calculate size of memory
    m_size = m_number * m_length;

    // Load only if the column has a positive size
    if (m_size > 0) {

        // Allocate and initialise fresh memory
        alloc_data();
        init_data();

        // If a FITS file is attached then try loading column data from the
        // FITS file. This may fail in case that no data has yet been written
        // to the FITS file. In that case we just skip loading and return
        // the initalised column ... 
        if (FPTR(m_fitsfile)->Fptr != NULL) {

            // Move to the HDU
            int status = 0;
            status     = __ffmahd(FPTR(m_fitsfile),
                                  (FPTR(m_fitsfile)->HDUposition)+1,
                                  NULL, &status);
            
            // If this failed because:
            // - the primary HDU was not found (status 252)
            // - we moved past the file (status 107)
            // we assume that no data have yet been written to the file and
            // we skip the loading.
            if (status != 252 && status != 107) {
            
                // Break on any other cfitsio error
                if (status != 0) {
                    throw GException::fits_hdu_not_found(G_LOAD_COLUMN_FIXED,
                                  (FPTR(m_fitsfile)->HDUposition)+1,
                                  status);
                }

                // Load data
                status = __ffgcv(FPTR(m_fitsfile), m_type, m_colnum, 
                                 1, 1, m_size, ptr_nulval(), ptr_data(),
                                 &m_anynul, &status);
                if (status != 0) {
                    throw GException::fits_error(G_LOAD_COLUMN_FIXED, status,
                                    "for column '"+m_name+"'.");
                }
        
            } // endif: no primary HDU found

        } // endif: there was a FITS file attached

    } // endif: column has a positive size

    // Return
    return;
}


/***********************************************************************//**
 * @brief Load variable-length column from FITS file
 *
 * @exception GException::fits_hdu_not_found
 *            Specified HDU not found in FITS file.
 * @exception GException::fits_error
 *            An error occured while loading column data from FITS file.
 *
 * If a FITS file is attached to the column the data are loaded into memory
 * from the FITS file. If no FITS file is attached, memory is allocated
 * to hold the column data and all cells are set to 0.
 *
 * The method makes use of the virtual methods 
 * GFitsTableCol::alloc_data,
 * GFitsTableCol::init_data,
 * GFitsTableCol::ptr_data, and
 * GFitsTableCol::ptr_nulval.
 * These methods are implemented by the derived column classes which 
 * implement a specific storage class (i.e. float, double, short, ...).
 ***************************************************************************/
void GFitsTableCol::load_column_variable(void)
{
    // If a FITS file is attached then try loading column data from the
    // FITS file. This may fail in case that no data has yet been written
    // to the FITS file. In that case we just skip loading and return
    // the initalised column ... 
    if (FPTR(m_fitsfile)->Fptr != NULL) {

        // Move to the HDU
        int status = 0;
        status     = __ffmahd(FPTR(m_fitsfile),
                              (FPTR(m_fitsfile)->HDUposition)+1,
                              NULL,
                              &status);
            
        // If this failed because:
        // - the primary HDU was not found (status 252)
        // - we moved past the file (status 107)
        // we assume that no data have yet been written to the file and
        // we skip the loading.
        if (status != 252 && status != 107) {
            
            // Break on any other cfitsio error
            if (status != 0) {
                throw GException::fits_hdu_not_found(G_LOAD_COLUMN_VARIABLE,
                                  (FPTR(m_fitsfile)->HDUposition)+1,
                                  status);
            }

            // Allocate rowstart array
            m_rowstart.assign(m_length+1, 0);

            // Determine the column length for each row by looping over
            // all rows and derive the total memory requirement
            m_size        = 0;
            m_varlen      = 0;
            m_rowstart[0] = 0;
            for (int row = 0; row < m_length; ++row) {

                // Initialise offset and repeat
                long offset(0);
                long repeat(0);

                // Get variable-length of row in repeat
                status = __ffgdes(FPTR(m_fitsfile),
                                  m_colnum,
                                  row+1,
                                  &repeat,
                                  &offset,
                                  &status);
                if (status != 0) {
                    std::string msg = "Unable to get descriptor of row "+
                                      gammalib::str(row+1)+" of column '"+
                                      name()+"' from FITS file.";
                    throw GException::fits_error(G_LOAD_COLUMN_VARIABLE, status,
                                                 msg);
                }

                // Store start of next row
                m_rowstart[row+1] = m_rowstart[row] + repeat;
                m_size           += repeat;
                if (repeat > m_varlen) {
                    m_varlen = repeat;
                }

            } // endfor: looped over all rows

            // Allocate and initialise fresh memory
            alloc_data();
            init_data();

            // Load data for each row
            for (int row = 0; row < m_length; ++row) {

                // Initialise anynul
                int anynul(0);

                // Load data
                status = __ffgcv(FPTR(m_fitsfile),
                                 std::abs(m_type),
                                 m_colnum, 
                                 row+1,
                                 1,
                                 elements(row),
                                 ptr_nulval(),
                                 ptr_data(m_rowstart[row]),
                                 &anynul,
                                 &status);
                if (status != 0) {
                    std::string msg = "Unable to load row "+gammalib::str(row+1)+""
                                      " of column '"+name()+"' from FITS file.";
                    throw GException::fits_error(G_LOAD_COLUMN_VARIABLE, status,
                                                 msg);
                }

                // Increment anynul
                m_anynul += anynul;

            } // endfor: looped over all rows

        } // endif: no primary HDU found

    } // endif: there was a FITS file attached

    // Return
    return;
}


/***********************************************************************//**
 * @brief Save table column into FITS file
 *
 * Save table column into FITS file by calling the save_column_variable()
 * method for variable-length columns and save_column_fixed() for fixed-
 * length columns.
 ***************************************************************************/
void GFitsTableCol::save_column(void)
{
    // Save variable-length or fixed-length column into FITS file
    if (is_variable()) {
        save_column_variable();
    }
    else {
        save_column_fixed();
    }

    // Return
    return;
}


/***********************************************************************//**
 * @brief Save table column into FITS file
 *
 * @exception GException::fits_hdu_not_found
 *            Specified HDU not found in FITS file.
 * @exception GException::fits_error
 *            Error occured during writing of the column data.
 *
 * The table column is only saved if it is linked to a FITS file and if the
 * data are indeed present in the class instance. This avoids saving of data
 * that have not been modified.
 *
 * The method make use of the virtual methods 
 *   GFitsTableCol::ptr_data and
 *   GFitsTableCol::ptr_nulval.
 * These methods are implemented by the derived column classes which 
 * implement a specific storage class (i.e. float, double, short, ...).
 ***************************************************************************/
void GFitsTableCol::save_column_fixed(void)
{
    // Continue only if a FITS file is connected and data have been loaded
    if (FPTR(m_fitsfile)->Fptr != NULL && m_colnum > 0 && ptr_data() != NULL) {

        // Move to the HDU
        int status = 0;
        status     = __ffmahd(FPTR(m_fitsfile),
                              (FPTR(m_fitsfile)->HDUposition)+1, NULL,
                              &status);
        if (status != 0) {
            throw GException::fits_hdu_not_found(G_SAVE_COLUMN_FIXED,
                              (FPTR(m_fitsfile)->HDUposition)+1,
                              status);
        }

        // Save the column data
        status = __ffpcn(FPTR(m_fitsfile), m_type, m_colnum, 1, 1,
                         m_size, ptr_data(), ptr_nulval(), &status);
        if (status != 0) {
            std::string msg = "Unable to save column '"+name()+"' to"
                              " FITS file.";
            throw GException::fits_error(G_SAVE_COLUMN_FIXED, status, msg);
        }

    } // endif: FITS file was connected

    // Return
    return;
}


/***********************************************************************//**
 * @brief Save table column into FITS file
 *
 * @exception GException::fits_hdu_not_found
 *            Specified HDU not found in FITS file.
 * @exception GException::fits_error
 *            Error occured during writing of the column data.
 *
 * The table column is only saved if it is linked to a FITS file and if the
 * data are indeed present in the class instance. This avoids saving of data
 * that have not been modified.
 *
 * The method make use of the virtual methods 
 *   GFitsTableCol::ptr_data and
 *   GFitsTableCol::ptr_nulval.
 * These methods are implemented by the derived column classes which 
 * implement a specific storage class (i.e. float, double, short, ...).
 ***************************************************************************/
void GFitsTableCol::save_column_variable(void)
{
    // Continue only if a FITS file is connected and data have been loaded
    if (FPTR(m_fitsfile)->Fptr != NULL && m_colnum > 0 && ptr_data() != NULL) {

        // Move to the HDU
        int status = 0;
        status     = __ffmahd(FPTR(m_fitsfile),
                              (FPTR(m_fitsfile)->HDUposition)+1, NULL,
                              &status);
        if (status != 0) {
            throw GException::fits_hdu_not_found(G_SAVE_COLUMN_VARIABLE,
                              (FPTR(m_fitsfile)->HDUposition)+1,
                              status);
        }

        // Save the column data row-by-row
        for (int row = 0; row < m_length; ++row) {

            // Save row data
            status = __ffpcn(FPTR(m_fitsfile),
                             std::abs(m_type),
                             m_colnum, 
                             row+1,
                             1,
                             elements(row),
                             ptr_data(m_rowstart[row]),
                             ptr_nulval(),
                             &status);
            if (status != 0) {
                std::string msg = "Unable to save row "+gammalib::str(row+1)+""
                                  " of column '"+name()+"' to FITS file.";
                throw GException::fits_error(G_SAVE_COLUMN_VARIABLE, status,
                                             msg);
            }

        } // endfor: looped over rows

    } // endif: FITS file was connected

    // Return
    return;
}


/***********************************************************************//**
 * @brief Compute offset of column element in memory
 *
 * @param[in] row Row of column.
 * @param[in] inx Vector index in column row.
 *
 * @exception GException::out_of_range
 *            Table row or vector index are out of valid range.
 *
 * Computes the offset of a column element in the storage array from the
 * @p row number and the vector index. The method also supports both
 * variable-length and fixed-length columns.
 ***************************************************************************/
int GFitsTableCol::offset(const int& row, const int& inx) const
{
    // Check row value
    #if defined(G_RANGE_CHECK)
    if (row < 0 || row >= m_length) {
        throw GException::out_of_range(G_OFFSET, row, 0, m_length-1);
    }
    #endif

    // Check inx value
    #if defined(G_RANGE_CHECK)
    if (inx < 0 || inx >= elements(row)) {
        throw GException::out_of_range(G_OFFSET, inx, 0, elements(row));
    }
    #endif

    // Calculate pixel offset
    int offset = (is_variable()) ? m_rowstart[row] + inx : row * m_number + inx;

    // Return offset
    return offset;
}


/*==========================================================================
 =                                                                         =
 =                             Private methods                             =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Initialise class members
 ***************************************************************************/
void GFitsTableCol::init_members(void)
{
    // Optionally print call graph
    #if defined(G_CALL_GRAPH)
    printf("GFitsTableCol::init_members\n");
    #endif

    // Allocate FITS file pointer
    m_fitsfile = new __fitsfile;
    FPTR(m_fitsfile)->HDUposition = 0;
    FPTR(m_fitsfile)->Fptr        = NULL;

    // Initialise members
    m_name.clear();
    m_unit.clear();
    m_dim.clear();
    m_rowstart.clear();
    m_colnum   = 0;
    m_type     = 0;
    m_repeat   = 0;
    m_width    = 0;
    m_number   = 0;
    m_length   = 0;
    m_variable = false;
    m_varlen   = 0;
    m_size     = 0;
    m_anynul   = 0;

    // Optionally print call graph
    #if defined(G_CALL_GRAPH)
    printf("exit GFitsTableCol::init_members\n");
    #endif

    // Return
    return;
}


/***********************************************************************//**
 * @brief Copy class members
 *
 * @param[in] column Column to be copied.
 ***************************************************************************/
void GFitsTableCol::copy_members(const GFitsTableCol& column)
{
    // Copy attributes
    m_name     = column.m_name;
    m_unit     = column.m_unit;
    m_dim      = column.m_dim;
    m_colnum   = column.m_colnum;
    m_type     = column.m_type;
    m_repeat   = column.m_repeat;
    m_width    = column.m_width;
    m_number   = column.m_number;
    m_length   = column.m_length;
    m_variable = column.m_variable;
    m_varlen   = column.m_varlen;
    m_rowstart = column.m_rowstart;
    m_size     = column.m_size;
    m_anynul   = column.m_anynul;
    FPTR_COPY(m_fitsfile, column.m_fitsfile);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Delete class members
 ***************************************************************************/
void GFitsTableCol::free_members(void)
{
    // Optionally print call graph
    #if defined(G_CALL_GRAPH)
    printf("GFitsTableCol::free_members\n");
    #endif

    // Free memory
    if (m_fitsfile != NULL) delete FPTR(m_fitsfile);

    // Mark memory as free
    m_fitsfile = NULL;

    // Optionally print call graph
    #if defined(G_CALL_GRAPH)
    printf("exit GFitsTableCol::free_members\n");
    #endif

    // Return
    return;
}


/***********************************************************************//**
 * @brief Connect table column to FITS file
 *
 * @param[in] vptr Column file void pointer.
 *
 * A table column is connected to a FITS file when the m_fitsfile holds a
 * FITS file pointer. In that way, the column knows to which file it belongs.
 ***************************************************************************/
void GFitsTableCol::connect(void* vptr)
{
    // Connect table column by copying the column file pointer
    FPTR_COPY(m_fitsfile, vptr);

    // Return
    return;
}