GCTAEventList.cpp 32.9 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
/***************************************************************************
 *           GCTAEventList.cpp  -  CTA event atom container class          *
 * ----------------------------------------------------------------------- *
 *  copyright (C) 2010-2011 by Jurgen Knodlseder                           *
 * ----------------------------------------------------------------------- *
 *                                                                         *
 *  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 GCTAEventList.cpp
 * @brief CTA event atom container class implementation.
 * @author J. Knodlseder
 */

/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "GCTAEventList.hpp"
#include "GCTAException.hpp"
#include "GTools.hpp"
#include "GFits.hpp"
#include "GFitsTableBitCol.hpp"
#include "GFitsTableFloatCol.hpp"
#include "GFitsTableDoubleCol.hpp"
#include "GFitsTableULongCol.hpp"
#include "GFitsTableShortCol.hpp"
#include "GFitsTableStringCol.hpp"

/* __ Method name definitions ____________________________________________ */
#define G_OPERATOR                          "GCTAEventList::operator[](int&)"
#define G_ROI                                     "GCTAEventList::roi(GRoi&)"
#define G_READ_DS_EBOUNDS         "GCTAEventList::read_ds_ebounds(GFitsHDU*)"
#define G_READ_DS_ROI                 "GCTAEventList::read_ds_roi(GFitsHDU*)"

/* __ Macros _____________________________________________________________ */

/* __ Coding definitions _________________________________________________ */

/* __ Debug definitions __________________________________________________ */


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

/***********************************************************************//**
 * @brief Void constructor
 ***************************************************************************/
GCTAEventList::GCTAEventList(void) : GEventList()
{
    // Initialise members
    init_members();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Copy constructor
 *
 * @param[in] list Event list.
 ***************************************************************************/
GCTAEventList::GCTAEventList(const GCTAEventList& list) : GEventList(list)
{
    // Initialise members
    init_members();

    // Copy members
    copy_members(list);

    // Return
    return;
}


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

    // Return
    return;
}


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

/***********************************************************************//**
 * @brief Assignment operator
 *
 * @param[in] list Event list.
 ***************************************************************************/
GCTAEventList& GCTAEventList::operator=(const GCTAEventList& list)
{
    // Execute only if object is not identical
    if (this != &list) {

        // Copy base class members
        this->GEventList::operator=(list);

        // Free members
        free_members();

        // Initialise members
        init_members();

        // Copy members
        copy_members(list);

    } // endif: object was not identical

    // Return this object
    return *this;
}


/***********************************************************************//**
 * @brief Event atom access operator
 *
 * @param[in] index Event index [0,...,size()-1].
 *
 * @exception GException::out_of_range
 *            Event index outside valid range.
 *
 * Returns pointer to an event atom.
 ***************************************************************************/
GCTAEventAtom* GCTAEventList::operator[](const int& index)
{
    // Optionally check if the index is valid
    #if defined(G_RANGE_CHECK)
    if (index < 0 || index >= size())
        throw GException::out_of_range(G_OPERATOR, index, 0, size()-1);
    #endif

    // Return pointer
    return (&(m_events[index]));
}


/***********************************************************************//**
 * @brief Event atom access operator
 *
 * @param[in] index Event index [0,...,size()-1].
 *
 * @exception GException::out_of_range
 *            Event index outside valid range.
 *
 * Returns pointer to an event atom.
 ***************************************************************************/
const GCTAEventAtom* GCTAEventList::operator[](const int& index) const
{
    // Optionally check if the index is valid
    #if defined(G_RANGE_CHECK)
    if (index < 0 || index >= size())
        throw GException::out_of_range(G_OPERATOR, index, 0, size()-1);
    #endif

    // Return pointer
    return (&(m_events[index]));
}


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

/***********************************************************************//**
 * @brief Clear object
 *
 * This method properly resets the object to an initial state.
 ***************************************************************************/
void GCTAEventList::clear(void)
{
    // Free class members (base and derived classes, derived class first)
    free_members();
    this->GEventList::free_members();
    this->GEvents::free_members();

    // Initialise members
    this->GEvents::init_members();
    this->GEventList::init_members();
    init_members();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Clone object
***************************************************************************/
GCTAEventList* GCTAEventList::clone(void) const
{
    return new GCTAEventList(*this);
}


/***********************************************************************//**
 * @brief Load events from event FITS file.
 *
 * @param[in] filename Name of FITS file from which events are loaded.
 *
 * Load CTA events from the EVENTS extension EVENTS, read the region of
 * interest and the energy boundaries from the data selection keywords,
 * and read the Good Time Intervals from the GTI extension.
 *
 * The method clears the object before loading, thus any events residing in
 * the object before loading will be lost.
 ***************************************************************************/
void GCTAEventList::load(const std::string& filename)
{
    // Clear object
    clear();

    // Open FITS file
    GFits file(filename);

    // Read event list
    read(file);

    // Close FITS file
    file.close();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Save CTA events into FITS file.
 *
 * @param[in] filename FITS filename.
 * @param[in] clobber Overwrite existing FITS file (default=false).
 *
 * Write the CTA event list into FITS file.
 ***************************************************************************/
void GCTAEventList::save(const std::string& filename, bool clobber) const
{
    // Create empty FITS file
    GFits fits;

    // Write event list
    write(fits);
    
    // Save FITS file
    fits.saveto(filename, clobber);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Read CTA events from FITS file.
 *
 * @param[in] file FITS file.
 *
 * This method read the CTA event list from a FITS file.
 *
 * The method clears the object before loading, thus any events residing in
 * the object before loading will be lost.
 ***************************************************************************/
void GCTAEventList::read(const GFits& file)
{
    // Clear object
    clear();

    // Get HDUs
    GFitsTable* events = file.table("EVENTS");
    GFitsTable* gti    = file.table("GTI");

    // Load event data
    read_events(events);

    // Read region of interest from data selection keyword
    read_ds_roi(events);

    // Read energy boundaries from data selection keyword
    read_ds_ebounds(events);

    // Read Good Time Intervals
    m_gti.read(gti);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Write CTA events into FITS file.
 *
 * @param[in] file FITS file.
 *
 * Write the CTA event list into FITS file.
 *
 * @todo The TELMASK column is allocated with a dummy length of 100.
 * @todo Implement agreed column format
 ***************************************************************************/
void GCTAEventList::write(GFits& file) const
{
    // Allocate FITS binary table HDU
    GFitsBinTable* events = new GFitsBinTable;

    // Write events
    write_events(events);

    // Write data selection keywords
    write_ds_keys(events);

    // Append event table to FITS file
    file.append(*events);

    // Free binary table
    delete events;

    // Append GTI to FITS file
    gti().write(&file);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Set ROI
 *
 * @param[in] roi ROI.
 *
 * @exception GCTAException::bad_roi_type
 *            ROI is not of type GCTARoi.
 ***************************************************************************/
void GCTAEventList::roi(const GRoi& roi)
{
    // Cast ROI dynamically
    const GCTARoi* ptr = dynamic_cast<const GCTARoi*>(&roi);

    // Throw exception if ROI is not of correct type
    if (ptr == NULL)
        throw GCTAException::bad_roi_type(G_ROI);

    // Set ROI
    m_roi = *ptr;

    // Return
    return;
}


/***********************************************************************//**
 * @brief Print event list information
 ***************************************************************************/
std::string GCTAEventList::print(void) const
{
    // Initialise result string
    std::string result;

    // Append header
    result.append("=== GCTAEventList ===");
    result.append("\n"+parformat("Number of events")+str(size()));
    
    // Append GTI intervals
    result.append("\n"+parformat("Time interval"));
    if (gti().size() > 0)
        result.append(str(tstart().met())+" - "+str(tstop().met())+" sec");
    else
        result.append("not defined");
        
    // Append energy intervals
    if (ebounds().size() > 0)
        result.append("\n"+ebounds().print());
    else
        result.append("\n"+parformat("Energy intervals")+"not defined");
    
    // Append ROI
    if (roi().radius() > 0)
        result.append("\n"+roi().print());
    else
        result.append("\n"+parformat("Region of interest")+"not defined");

    // Return result
    return result;
}


/***********************************************************************//**
 * @brief Append event atom to event list
 *
 * @param[in] event Event.
 *
 * Appends an event atom to the event list.
 ***************************************************************************/
void GCTAEventList::append(const GCTAEventAtom& event)
{
    // Append event
    m_events.push_back(event);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Reserves space for events
 *
 * @param[in] number Number of events.
 *
 * Reserves space for number events in the event list.
 ***************************************************************************/
void GCTAEventList::reserve(const int& number)
{
    // Reserve space
    m_events.reserve(number);

    // Return
    return;
}


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

/***********************************************************************//**
 * @brief Initialise class members
 ***************************************************************************/
void GCTAEventList::init_members(void)
{
    // Initialise members
    m_roi.clear();
    m_events.clear();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Copy class members
 *
 * @param[in] list Event list.
 ***************************************************************************/
void GCTAEventList::copy_members(const GCTAEventList& list)
{
    // Copy members
    m_roi    = list.m_roi;
    m_events = list.m_events;

    // Return
    return;
}


/***********************************************************************//**
 * @brief Delete class members
 ***************************************************************************/
void GCTAEventList::free_members(void)
{
    // Return
    return;
}


/***********************************************************************//**
 * @brief Read CTA events from FITS table
 *
 * @param[in] table FITS table pointer.
 *
 * This method reads the CTA event list from a FITS table HDU into memory.
 *
 * @todo Implement agreed column format
 * @todo Declare GFitsTable::column() as const, or at least, add a const
 *       version. Even better, add column access operators instead of the
 *       column() method as GFitsTable is a container for columns, and
 *       each container should have an access operator.
 ***************************************************************************/
void GCTAEventList::read_events(const GFitsTable* table)
{
    // Clear existing events
    m_events.clear();

    // Allocate space for keyword name
    char keyword[10];

    // Continue only if HDU is valid
    if (table != NULL) {

        // Circumvent const correctness. We need this because the column()
        // method is not declared const. This should be corrected.
        //GFitsTable* hdu = (GFitsTable*)table;

        // Extract number of events in FITS file
        int num = table->integer("NAXIS2");

        // If there are events then load them
        if (num > 0) {

            // Reserve data
            m_events.reserve(num);

            // Get column pointers
            GFitsTableULongCol*  ptr_eid        = (GFitsTableULongCol*)&(*table)["EVENT_ID"];
            GFitsTableULongCol*  ptr_oid        = (GFitsTableULongCol*)&(*table)["OBS_ID"];
            GFitsTableDoubleCol* ptr_time       = (GFitsTableDoubleCol*)&(*table)["TIME"];
            GFitsTableShortCol*  ptr_multip     = (GFitsTableShortCol*)&(*table)["MULTIP"];
            GFitsTableFloatCol*  ptr_ra         = (GFitsTableFloatCol*)&(*table)["RA"];
            GFitsTableFloatCol*  ptr_dec        = (GFitsTableFloatCol*)&(*table)["DEC"];
            GFitsTableFloatCol*  ptr_dir_err    = (GFitsTableFloatCol*)&(*table)["DIR_ERR"];
            GFitsTableFloatCol*  ptr_detx       = (GFitsTableFloatCol*)&(*table)["DETX"];
            GFitsTableFloatCol*  ptr_dety       = (GFitsTableFloatCol*)&(*table)["DETY"];
            GFitsTableFloatCol*  ptr_alt        = (GFitsTableFloatCol*)&(*table)["ALT"];
            GFitsTableFloatCol*  ptr_az         = (GFitsTableFloatCol*)&(*table)["AZ"];
            GFitsTableFloatCol*  ptr_corex      = (GFitsTableFloatCol*)&(*table)["COREX"];
            GFitsTableFloatCol*  ptr_corey      = (GFitsTableFloatCol*)&(*table)["COREY"];
            GFitsTableFloatCol*  ptr_core_err   = (GFitsTableFloatCol*)&(*table)["CORE_ERR"];
            GFitsTableFloatCol*  ptr_xmax       = (GFitsTableFloatCol*)&(*table)["XMAX"];
            GFitsTableFloatCol*  ptr_xmax_err   = (GFitsTableFloatCol*)&(*table)["XMAX_ERR"];
            GFitsTableFloatCol*  ptr_shw        = (GFitsTableFloatCol*)&(*table)["SHWIDTH"];
            GFitsTableFloatCol*  ptr_shl        = (GFitsTableFloatCol*)&(*table)["SHLENGTH"];
            GFitsTableFloatCol*  ptr_energy     = (GFitsTableFloatCol*)&(*table)["ENERGY"];
            GFitsTableFloatCol*  ptr_energy_err = (GFitsTableFloatCol*)&(*table)["ENERGY_ERR"];

            // Copy data from columns into GCTAEventAtom objects
            GCTAEventAtom event;
            for (int i = 0; i < num; ++i) {
                event.m_time.met((*ptr_time)(i));
                event.m_dir.radec_deg((*ptr_ra)(i), (*ptr_dec)(i));
                event.m_energy.TeV((*ptr_energy)(i));
                event.m_event_id    = (*ptr_eid)(i);
                event.m_obs_id      = (*ptr_oid)(i);
                event.m_multip      = (*ptr_multip)(i);
                event.m_telmask     = 0;
                event.m_dir_err     = (*ptr_dir_err)(i);
                event.m_detx        = (*ptr_detx)(i);
                event.m_dety        = (*ptr_dety)(i);
                event.m_alt         = (*ptr_alt)(i);
                event.m_az          = (*ptr_az)(i);
                event.m_corex       = (*ptr_corex)(i);
                event.m_corey       = (*ptr_corey)(i);
                event.m_core_err    = (*ptr_core_err)(i);
                event.m_xmax        = (*ptr_xmax)(i);
                event.m_xmax_err    = (*ptr_xmax_err)(i);
                event.m_shwidth     = (*ptr_shw)(i);
                event.m_shlength    = (*ptr_shl)(i);
                event.m_energy_err  = (*ptr_energy_err)(i);
                m_events.push_back(event);
            }

        } // endif: there were events

    } // endif: HDU was valid

    // Return
    return;
}


/***********************************************************************//**
 * @brief Read energy boundary data selection keywords
 *
 * @param[in] hdu FITS HDU
 *
 * @exception GCTAException::no_ebds
 *            Invalid energy data selection encountered
 *
 * Reads the energy boundary data selection keywords by searching for a 
 * DSTYPx keyword named "ENERGY". The data selection information is expected
 * to be in the format "200:50000", where the 2 arguments are the minimum
 * and maximum energy. The energy unit is given by the keyword DSUNIx, which
 * supports keV, MeV, GeV and TeV (case independent). No detailed syntax
 * checking is performed.
 *
 * This method clears the energy boundaries, irrespectively of whether energy
 * boundary information has been found in the HDU.
 ***************************************************************************/
void GCTAEventList::read_ds_ebounds(const GFitsHDU* hdu)
{
    // Reset energy boundaries
    m_ebounds.clear();

    // Continue only if HDU is valid
    if (hdu != NULL) {

        // Get number of data selection keywords
        int ndskeys = 0;
        try {
            ndskeys = hdu->integer("NDSKEYS");
        }
        catch (GException::fits_key_not_found &e) {
            ;
        }

        // Loop over all data selection keys
        for (int i = 1; i <= ndskeys; ++i) {
            std::string type_key  = "DSTYP"+str(i);
            std::string unit_key  = "DSUNI"+str(i);
            std::string value_key = "DSVAL"+str(i);
            try {
                if (hdu->string(type_key) == "ENERGY") {
                    std::string unit                 = toupper(hdu->string(unit_key));
                    std::string value                = hdu->string(value_key);
                    std::vector<std::string> ebounds = split(value, ":");
                    if (ebounds.size() == 2) {
                        double  emin = todouble(ebounds[0]);
                        double  emax = todouble(ebounds[1]);
                        GEnergy e_min;
                        GEnergy e_max;
                        if (unit == "KEV") {
                            e_min.keV(emin);
                            e_max.keV(emax);
                        }
                        else if (unit == "MEV") {
                            e_min.MeV(emin);
                            e_max.MeV(emax);
                        }
                        else if (unit == "GEV") {
                            e_min.GeV(emin);
                            e_max.GeV(emax);
                        }
                        else if (unit == "TEV") {
                            e_min.TeV(emin);
                            e_max.TeV(emax);
                        }
                        else {
                            throw GCTAException::no_ebds(G_READ_DS_EBOUNDS,
                                  "Invalid energy unit \""+unit+
                                  "\" encountered in data selection key \""+
                                  unit_key+"\"");
                        }
                        m_ebounds.append(e_min, e_max);
                    }
                    else {
                        throw GCTAException::no_ebds(G_READ_DS_EBOUNDS,
                              "Invalid energy value \""+value+
                              "\" encountered in data selection key \""+
                              value_key+"\"");
                    }
                } // endif: ENERGY type found
            }
            catch (GException::fits_key_not_found &e) {
                ;
            }
        } // endfor: looped over data selection keys

    } // endif: HDU was valid

    // Return
    return;
}


/***********************************************************************//**
 * @brief Read ROI data selection keywords
 *
 * @param[in] hdu FITS HDU
 *
 * @exception GException::no_roi
 *            Invalid ROI data selection encountered
 *
 * Reads the ROI data selection keywords by searching for a DSTYPx keyword
 * named "POS(RA,DEC)". The data selection information is expected to be
 * in the format "CIRCLE(267.0208,-24.78,4.5)", where the 3 arguments are
 * Right Ascension, Declination and radius in units of degrees. No detailed
 * syntax checking is performed.
 *
 * This method clears the ROI, irrespectively of whether ROI information has
 * been found in the HDU.
 ***************************************************************************/
void GCTAEventList::read_ds_roi(const GFitsHDU* hdu)
{
    // Reset ROI
    m_roi.clear();

    // Continue only if HDU is valid
    if (hdu != NULL) {

        // Get number of data selection keywords
        int ndskeys = 0;
        try {
            ndskeys = hdu->integer("NDSKEYS");
        }
        catch (GException::fits_key_not_found &e) {
            ;
        }

        // Loop over all data selection keys
        for (int i = 1; i <= ndskeys; ++i) {
            std::string type_key  = "DSTYP"+str(i);
            std::string unit_key  = "DSUNI"+str(i);
            std::string value_key = "DSVAL"+str(i);
            try {
                if (hdu->string(type_key) == "POS(RA,DEC)") {
                    std::string unit              = toupper(hdu->string(unit_key));
                    std::string value             = hdu->string(value_key);
                    value                         = strip_chars(value, "CIRCLE(");
                    value                         = strip_chars(value, ")");
                    std::vector<std::string> args = split(value, ",");
                    if (args.size() == 3) {
                        double  ra  = todouble(args[0]);
                        double  dec = todouble(args[1]);
                        double  rad = todouble(args[2]);
                        GCTAInstDir dir;
                        dir.radec_deg(ra, dec);
                        m_roi.centre(dir);
                        m_roi.radius(rad);
                    }
                    else {
                        throw GException::no_roi(G_READ_DS_ROI,
                              "Invalid acceptance cone value \""+value+
                              "\" encountered in data selection key \""+
                              value_key+"\"");
                    }
                } // endif: POS(RA,DEC) type found
            }
            catch (GException::fits_key_not_found &e) {
                ;
            }
        } // endfor: looped over data selection keys

    } // endif: HDU was valid

    // Return
    return;
}


/***********************************************************************//**
 * @brief Write CTA events into FITS table
 *
 * @param[in] hdu FITS table HDU.
 *
 * Write the CTA event list into FITS table.
 *
 * @todo The TELMASK column is allocated with a dummy length of 100.
 * @todo Implement agreed column format
 ***************************************************************************/
void GCTAEventList::write_events(GFitsBinTable* hdu) const
{
    // Continue only if HDU is valid
    if (hdu != NULL) {

        // Set extension name
        hdu->extname("EVENTS");

        // If there are events then write them now
        if (size() > 0) {

            // Allocate columns
            GFitsTableULongCol  col_eid         = GFitsTableULongCol("EVENT_ID", size());
            GFitsTableULongCol  col_oid         = GFitsTableULongCol("OBS_ID", size());
            GFitsTableDoubleCol col_time        = GFitsTableDoubleCol("TIME", size());
            GFitsTableDoubleCol col_live        = GFitsTableDoubleCol("TLIVE", size());
            GFitsTableShortCol  col_multip      = GFitsTableShortCol("MULTIP", size());
            GFitsTableBitCol    col_telmask     = GFitsTableBitCol("TELMASK", size(), 100);
            GFitsTableFloatCol  col_ra          = GFitsTableFloatCol("RA", size());
            GFitsTableFloatCol  col_dec         = GFitsTableFloatCol("DEC", size());
            GFitsTableFloatCol  col_direrr      = GFitsTableFloatCol("DIR_ERR", size());
            GFitsTableFloatCol  col_detx        = GFitsTableFloatCol("DETX", size());
            GFitsTableFloatCol  col_dety        = GFitsTableFloatCol("DETY", size());
            GFitsTableFloatCol  col_alt         = GFitsTableFloatCol("ALT", size());
            GFitsTableFloatCol  col_az          = GFitsTableFloatCol("AZ", size());
            GFitsTableFloatCol  col_corex       = GFitsTableFloatCol("COREX", size());
            GFitsTableFloatCol  col_corey       = GFitsTableFloatCol("COREY", size());
            GFitsTableFloatCol  col_core_err    = GFitsTableFloatCol("CORE_ERR", size());
            GFitsTableFloatCol  col_xmax        = GFitsTableFloatCol("XMAX", size());
            GFitsTableFloatCol  col_xmax_err    = GFitsTableFloatCol("XMAX_ERR", size());
            GFitsTableFloatCol  col_shw         = GFitsTableFloatCol("SHWIDTH", size());
            GFitsTableFloatCol  col_shl         = GFitsTableFloatCol("SHLENGTH", size());
            GFitsTableFloatCol  col_energy      = GFitsTableFloatCol("ENERGY", size());
            GFitsTableFloatCol  col_energy_err  = GFitsTableFloatCol("ENERGY_ERR", size());

            // Fill columns
            for (int i = 0; i < size(); ++i) {
                col_eid(i)         = m_events[i].m_event_id;
                col_oid(i)         = m_events[i].m_obs_id;
                col_time(i)        = m_events[i].time().met();
                col_live(i)        = 0.0;
                col_multip(i)      = 0;
                //col_telmask
                col_ra(i)          = m_events[i].dir().ra_deg();
                col_dec(i)         = m_events[i].dir().dec_deg();
                col_direrr(i)      = m_events[i].m_dir_err;
                col_detx(i)        = m_events[i].m_detx;
                col_dety(i)        = m_events[i].m_dety;
                col_alt(i)         = m_events[i].m_alt;
                col_az(i)          = m_events[i].m_az;
                col_corex(i)       = m_events[i].m_corex;
                col_corey(i)       = m_events[i].m_corey;
                col_core_err(i)    = m_events[i].m_core_err;
                col_xmax(i)        = m_events[i].m_xmax;
                col_xmax_err(i)    = m_events[i].m_xmax_err;
                col_shw(i)         = m_events[i].m_shwidth;
                col_shl(i)         = m_events[i].m_shlength;
                col_energy(i)      = m_events[i].energy().TeV();
                col_energy_err(i)  = m_events[i].m_energy_err;
            } // endfor: looped over rows

            // Append columns to table
            hdu->append_column(col_eid);
            hdu->append_column(col_oid);
            hdu->append_column(col_time);
            hdu->append_column(col_live);
            hdu->append_column(col_multip);
            hdu->append_column(col_telmask);
            hdu->append_column(col_ra);
            hdu->append_column(col_dec);
            hdu->append_column(col_direrr);
            hdu->append_column(col_detx);
            hdu->append_column(col_dety);
            hdu->append_column(col_alt);
            hdu->append_column(col_az);
            hdu->append_column(col_corex);
            hdu->append_column(col_corey);
            hdu->append_column(col_core_err);
            hdu->append_column(col_xmax);
            hdu->append_column(col_xmax_err);
            hdu->append_column(col_shw);
            hdu->append_column(col_shl);
            hdu->append_column(col_energy);
            hdu->append_column(col_energy_err);
        
        } // endif: there were events to write

    } // endif: HDU was valid

    // Return
    return;
}


/***********************************************************************//**
 * @brief Write data selection keywords into FITS HDU
 *
 * @param[in] hdu FITS HDU pointer.
 *
 * This method does nothing if the HDU pointer is NULL.
 *
 * @todo This is a very dumb data selection keyword writing routine that does
 *       not take into account any existing keywords. We definitely want a
 *       more secure logic that checks for existing keywords and possible
 *       conflicts. But for this prototype software, this code does the job.
 ***************************************************************************/
void GCTAEventList::write_ds_keys(GFitsHDU* hdu) const
{
    // Continue only if HDU is valid
    if (hdu != NULL) {

        // Set ROI parameters
        double ra  = roi().centre().ra_deg();
        double dec = roi().centre().dec_deg();
        double rad = roi().radius();

        // Set energy range parameters
        double e_min = emin().TeV();
        double e_max = emax().TeV();

        // Set cone selection string
        std::string dsval2 = "CIRCLE("+str(ra)+","+str(dec)+","+str(rad)+")";

        // Set energy selection string
        std::string dsval3 = str(e_min)+":"+str(e_max);

        // Add time selection keywords
        hdu->card("DSTYP1", "TIME",  "Data selection type");
        hdu->card("DSUNI1", "s",     "Data selection unit");
        hdu->card("DSVAL1", "TABLE", "Data selection value");
        hdu->card("DSREF1", ":GTI",  "Data selection reference");

        // Add acceptance cone selection
        hdu->card("DSTYP2", "POS(RA,DEC)", "Data selection type");
        hdu->card("DSUNI2", "deg",         "Data selection unit");
        hdu->card("DSVAL2", dsval2,        "Data selection value");
        
        // Add energy range selection
        hdu->card("DSTYP3", "ENERGY", "Data selection type");
        hdu->card("DSUNI3", "TeV",    "Data selection unit");
        hdu->card("DSVAL3", dsval3,   "Data selection value");

        // Set number of data selection keys
        hdu->card("NDSKEYS", 3,  "Number of data selections");

    } // endif: HDU was valid

    // Return
    return;
}


/*==========================================================================
 =                                                                         =
 =                                Friends                                  =
 =                                                                         =
 ==========================================================================*/