/*
 * pxmagic.c
 *
 * Copyright (C) 2009 IfA
 *
 * 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; version 2 of the License.
 *
 * 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
 * program; see the file COPYING. If not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// #include <stdlib.h>
// #include <ippdb.h>
// #include <string.h>

#include "pxtools.h"

bool pxspaceAddWhere(pxConfig *config, psString *pQuery, psString table)
{
    PS_ASSERT_PTR_NON_NULL(config, NULL);
    PS_ASSERT_PTR_NON_NULL(table, NULL);

    PXOPT_LOOKUP_F64(radius, config->args, "-radius", false, false);

    if (radius > 0) {
        PXOPT_LOOKUP_F64(ra, config->args, "-ra", false, false);
        PXOPT_LOOKUP_F64(decl, config->args, "-decl", false, false);

        ra   *= PS_RAD_DEG;
        decl *= PS_RAD_DEG;
        radius *= PS_RAD_DEG;

        psStringAppend(pQuery, " AND (ACOS((cos(%s.decl) * cos(%lf) * cos(%s.ra - %lf)) +(sin(%s.decl) * sin(%lf))) < %lf)",
                table, decl, table, ra, table, decl, radius);
    }

    return true;
}

void pxspaceAddArguments(psMetadata *md)
{
    psMetadataAddF64(md, PS_LIST_TAIL, "-radius", 0,           "search for exposures within radius RA DEC (degrees)", false);
    psMetadataAddF64(md, PS_LIST_TAIL, "-ra", 0,               "RA value for radius search (degrees)", false);
    psMetadataAddF64(md, PS_LIST_TAIL, "-decl", 0,             "DEC value for radius search (degrees)", false);
}
