Changeset: a2bf85b1b910 for MonetDB URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=a2bf85b1b910 Modified Files: sql/backends/monet5/LSST/80_lsst.mal sql/backends/monet5/LSST/Makefile.ag sql/backends/monet5/LSST/lsst.c sql/backends/monet5/LSST/lsst.h sql/backends/monet5/LSST/lsst.mal sql/backends/monet5/LSST/lsst.sql sql/backends/monet5/Makefile.ag sql/backends/monet5/UDF/80_udf.mal sql/backends/monet5/UDF/Makefile.ag sql/backends/monet5/UDF/README sql/backends/monet5/UDF/udf.c sql/backends/monet5/UDF/udf.h sql/backends/monet5/UDF/udf.mal sql/backends/monet5/UDF/udf.sql sql/test/LSST/Tests/ptSphPoly.sql Branch: Apr2011 Log Message:
User defined functions and lsst A template for user defined functions is added, which is also documented on the new website. In addition the lsst extensions are used as a test case. diffs (truncated from 976 to 300 lines): diff --git a/sql/backends/monet5/LSST/80_lsst.mal b/sql/backends/monet5/LSST/80_lsst.mal new file mode 100644 --- /dev/null +++ b/sql/backends/monet5/LSST/80_lsst.mal @@ -0,0 +1,19 @@ +# The contents of this file are subject to the MonetDB Public License +# Version 1.1 (the "License"); you may not use this file except in +# compliance with the License. You may obtain a copy of the License at +# http://monetdb.cwi.nl/Legal/MonetDBLicense-1.1.html +# +# Software distributed under the License is distributed on an "AS IS" +# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +# License for the specific language governing rights and limitations +# under the License. +# +# The Original Code is the MonetDB Database System. +# +# The Initial Developer of the Original Code is CWI. +# Portions created by CWI are Copyright (C) 1997-July 2008 CWI. +# Copyright August 2008-2011 MonetDB B.V. +# All Rights Reserved. + +# This loads the MonetDB/SQL module +include lsst; diff --git a/sql/backends/monet5/LSST/Makefile.ag b/sql/backends/monet5/LSST/Makefile.ag new file mode 100644 --- /dev/null +++ b/sql/backends/monet5/LSST/Makefile.ag @@ -0,0 +1,63 @@ +# The contents of this file are subject to the MonetDB Public License +# Version 1.1 (the "License"); you may not use this file except in +# compliance with the License. You may obtain a copy of the License at +# http://monetdb.cwi.nl/Legal/MonetDBLicense-1.1.html +# +# Software distributed under the License is distributed on an "AS IS" +# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +# License for the specific language governing rights and limitations +# under the License. +# +# The Original Code is the MonetDB Database System. +# +# The Initial Developer of the Original Code is CWI. +# Portions created by CWI are Copyright (C) 1997-July 2008 CWI. +# Copyright August 2008-2011 MonetDB B.V. +# All Rights Reserved. + +INCLUDES = ../../../include \ + ../../../common \ + ../../../storage \ + ../../../server .. \ + ../../../../monetdb5/modules/atoms \ + ../../../../monetdb5/compiler \ + ../../../../monetdb5/modules/kernel \ + ../../../../monetdb5/mal \ + ../../../../monetdb5/modules/mal \ + ../../../../monetdb5/optimizer \ + ../../../../monetdb5/scheduler \ + ../../../../clients/mapilib \ + ../../../../common/options \ + ../../../../common/stream \ + ../../../../common/utils \ + ../../../../gdk \ + $(READLINE_INCS) $(MSEED_CFLAGS) $(cfitsio_CFLAGS) $(curl_CFLAGS) + +lib__lsst = { + MODULE + DIR = libdir/monetdb5 + SOURCES = lsst.c + LIBS = ../../../../monetdb5/tools/libmonetdb5 \ + ../../../../gdk/libbat $(curl_LIBS) +} + +headers_mal = { + HEADERS = mal + DIR = libdir/monetdb5 + SOURCES = lsst.mal +} + +headers_sql = { + HEADERS = sql + DIR = libdir/monetdb5 + SOURCES = lsst.sql +} + +headers_autoload = { + HEADERS = mal + DIR = libdir/monetdb5/autoload + SOURCES = 80_lsst.mal +} + +EXTRA_DIST_DIR = Tests +EXTRA_DIST = 80_lsst.mal diff --git a/sql/backends/monet5/LSST/lsst.c b/sql/backends/monet5/LSST/lsst.c new file mode 100644 --- /dev/null +++ b/sql/backends/monet5/LSST/lsst.c @@ -0,0 +1,433 @@ +/* + * The contents of this file are subject to the MonetDB Public License + * Version 1.1 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://monetdb.cwi.nl/Legal/MonetDBLicense-1.1.html + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the + * License for the specific language governing rights and limitations + * under the License. + * + * The Original Code is the MonetDB Database System. + * + * The Initial Developer of the Original Code is CWI. + * Portions created by CWI are Copyright (C) 1997-July 2008 CWI. + * Copyright August 2008-2011 MonetDB B.V. + * All Rights Reserved. +*/ + +#include "lsst.h" + +/* The remainder of the code base is taken over from MySQLSpatialUdf.c */ + +static double const QSERV_DEG_PER_RAD = 180.0 / M_PI; +static double const QSERV_RAD_PER_DEG = M_PI / 180.0; +static double const QSERV_ARCSEC_PER_DEG = 3600.0; + + +/* -- Angular separation -------- */ + +/** Returns D^2/4, where D is the euclidian distance between the two + input points on the unit sphere. */ +static double _qserv_dist(double ra1, double dec1, double ra2, double dec2) { + double x, y, z, dist; + x = sin((ra1 - ra2) * QSERV_RAD_PER_DEG * 0.5); + x *= x; + y = sin((dec1 - dec2) * QSERV_RAD_PER_DEG * 0.5); + y *= y; + z = cos((dec1 + dec2) * QSERV_RAD_PER_DEG * 0.5); + z *= z; + dist = x * (z - y) + y; + return dist < 0.0 ? 0.0 : (dist > 1.0 ? 1.0 : dist); +} + +static double _qserv_angSep(double ra1, double dec1, double ra2, double dec2) { + double dist; + dist = _qserv_dist(ra1, dec1, ra2, dec2); + return 2.0 * QSERV_DEG_PER_RAD * asin(sqrt(dist)); +} + +/** Returns the angular separation in degrees between two spherical + * coordinate pairs (ra1, dec1) and (ra2, dec2). + * + * Consumes 4 arguments ra1, dec1, ra2 and dec2 all of type REAL: + * @li ra1: right ascension of the first position (deg) + * @li dec1: declination of the first position (deg) + * @li ra2: right ascension of the second position (deg) + * @li dec2: declination of the second position (deg) + * + * Also: + * @li If any parameter is NULL, NULL is returned. + * @li If dec1 or dec2 lies outside of [-90, 90], this is an error + * and NULL is returned. + */ +sql5_export str +qserv_angSep(dbl *sep, dbl *ra1, dbl *dec1, dbl *ra2, dbl *dec2) +{ + /* If any input is null, the result is null. */ + if ( *ra1 == dbl_nil || *dec1 == dbl_nil || *ra2 == dbl_nil || *dec2 == dbl_nil){ + *sep = dbl_nil; + return MAL_SUCCEED; + } + + /* Check that dec lies in range. */ + if (*dec1 < -90.0 || *dec1 > 90.0 || *dec2 < -90.0 || *dec2 > 90.0) + throw(MAL,"lsst.qserv_angSep", "Illegal angulars"); + + *sep = _qserv_dist(*ra1, *dec1, *ra2, *dec2); + return MAL_SUCCEED; +} + + +/* -- Point in spherical box test -------- */ + +/** Range reduces the given angle to lie in the range [0.0, 360.0). */ +static double _qserv_reduceRa(double theta) { + if (theta < 0.0 || theta >= 360.0) { + theta = fmod(theta, 360.0); + if (theta < 0.0) { + theta += 360.0; + } + } + return theta; +} + +/** Returns 1 if the given spherical longitude/latitude box contains + * the given position, and 0 otherwise. + * + * Consumes 6 arguments ra, dec, ra_min, dec_min, ra_max and dec_max, in + * that order, all of type REAL and in units of degrees. (ra, dec) is the + * position to test - the remaining parameters specify the spherical box. + * + * Note that: + * @li If any parameter is NULL, the return value is 0. + * @li If dec, dec_min or dec_max lies outside of [-90, 90], + * this is an error and NULL is returned. + * @li If dec_min is greater than dec_max, the spherical box is empty + * and 0 is returned. + * @li If both ra_min and ra_max lie in the range [0, 360], then ra_max + * can be less than ra_min. For example, a box with ra_min = 350 + * and ra_max = 10 includes points with right ascensions in the ranges + * [350, 360) and [0, 10]. + * @li If either ra_min or ra_max lies outside of [0, 360], then ra_min + * must be = ra_max (otherwise, NULL is returned), though the values + * can be arbitrary. If the two are separated by 360 degrees or more, + * then the box spans [0, 360). Otherwise, both values are range reduced. + * For example, a spherical box with ra_min = 350 and ra_max = 370 + * includes points with right ascensions in the rnages [350, 360) and + * [0, 10]. + */ +str qserv_ptInSphBox(int *ret, dbl *ra, dbl *dec, dbl *ra_min, dbl *dec_min, dbl *ra_max, dbl *dec_max) +{ + dbl lra, lra_min, lra_max; + + if (*ra == dbl_nil || *dec == dbl_nil || *ra_min == dbl_nil || *dec_min == dbl_nil || *ra_max == dbl_nil || *dec_max == dbl_nil){ + *ret = int_nil; + return MAL_SUCCEED; + } + /* Check arguments. */ + if (*dec < -90.0 || *dec_min < -90.0 || *dec_max < -90.0 || + *dec > 90.0 || *dec_min > 90.0 || *dec_max > 90.0) { + *ret = int_nil; + return MAL_SUCCEED; + } + if ( *ra_max < *ra_min && ( *ra_max < 0.0 || *ra_min > 360.0)) { + *ret = int_nil; + return MAL_SUCCEED; + } + if ( *dec_min > *dec_max || *dec < *dec_min || *dec > *dec_max) { + *ret = int_nil; + return MAL_SUCCEED; + } + /* Range-reduce longitude angles */ + lra = _qserv_reduceRa(*ra); + if ( *ra_max - *ra_min >= 360.0) { + lra_min = 0.0; + lra_max = 360.0; + } else { + lra_min = _qserv_reduceRa(*ra_min); + lra_max = _qserv_reduceRa(*ra_max); + } + if (lra_min == lra_max) + *ret = lra >= lra_min && lra == lra_max; + else + *ret = lra >= lra_min || lra == lra_max; + return MAL_SUCCEED; +} + + +/* -- Point in spherical circle test -------- */ + +/** Returns 1 if the given circle on the unit sphere contains + * the specified position and 0 otherwise. + * + * Consumes 5 arguments, all of type REAL: + * @li ra: right ascension of position to test (deg) + * @li dec: declination of position to test (deg) + * @li ra_cen: right ascension of circle center (deg) + * @li dec_cen: declination of circle center (deg) + * @li radius: radius (opening angle) of circle (deg) + * + * Note that: + * @li If any parameter is NULL, the return value is 0. + * @li If dec or dec_cen lies outside of [-90, 90], + * this is an error and NULL is returned. + * @li If radius is negative or greater than 180, this is + * an error and NULL is returned. + */ +str +qserv_ptInSphCircle(int *ret, dbl *ra, dbl *dec, dbl *ra_cen, dbl *dec_cen, dbl *radius) +{ + if (*ra == dbl_nil || *dec == dbl_nil || *ra_cen == dbl_nil || *dec_cen == dbl_nil || *radius == dbl_nil ){ + *ret = int_nil; + return MAL_SUCCEED; + } + /* Check arguments */ + if ( *dec < -90.0 || *dec > 90.0 || *dec_cen < -90.0 || *dec_cen > 90.0) { + *ret = int_nil; + return MAL_SUCCEED; + } + if (*radius < 0.0 || *radius > 180.0) { + *ret = int_nil; + return MAL_SUCCEED; + } + /* Fail-fast if declination delta exceeds the radius. */ + if ( fabs(*dec - *dec_cen) > *radius) { + *ret = 0; + return MAL_SUCCEED; + } + *ret = _qserv_angSep(*ra, *dec, *ra_cen, *dec_cen) <= *radius; + return MAL_SUCCEED; +} + _______________________________________________ Checkin-list mailing list [email protected] http://mail.monetdb.org/mailman/listinfo/checkin-list
