Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-8467: Wrap lsst_distrib with pybind11 #57

Merged
merged 1 commit into from
Mar 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 0 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ doc/html
doc/doxygen.conf
doc/xml
examples/refSources
python/lsst/meas/astrom/net/netLib.py
python/lsst/meas/astrom/net/netLib_wrap.cc
python/lsst/meas/astrom/sip/sipLib.py
python/lsst/meas/astrom/sip/sipLib_wrap.*
python/lsst/meas/astrom/*Lib.py
python/lsst/meas/astrom/*Lib_wrap.*
tests/.cache
tests/.tests
tests/testConvertCheby
Expand All @@ -26,6 +20,4 @@ tests/testlsf2d
doc/html/
doc/*.tag
version.py
python/lsst/meas/astrom/astrometry_net.py
python/lsst/meas/astrom/astrometry_net_wrap.cc
bin/generateANetCache.py
21 changes: 0 additions & 21 deletions examples/imsimPlots.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
from __future__ import print_function
from optparse import OptionParser
from math import hypot

import lsst.pex.policy as policy
import lsst.meas.astrom as measAstrom
import lsst.afw.image as afwImage
from lsst.afw.coord import DEGREES
from lsst.log import Log
import lsst.meas.algorithms.utils as maUtils

Expand Down Expand Up @@ -73,19 +69,10 @@ def plotsForField(inButler, keys, fixup, plots=None, prefix=''):
print('Got sources', psources)

pmatches = inButler.get('icMatch', **keys)
# print 'Got matches', pmatches
#matchmeta = pmatches.getSourceMatchMetadata()
#matches = pmatches.getSourceMatches()
# print 'Match metadata:', matchmeta
sources = psources.getSources()

calexp = inButler.get('calexp', **keys)
# print 'Got calexp', calexp
wcs = calexp.getWcs()
# print 'Got wcs', wcs
# print wcs.getFitsMetadata().toString()
wcs = afwImage.cast_TanWcs(wcs)
# print 'After cast:', wcs

photocal = calexp.getCalib()
zp = photocal.getMagnitude(1.)
Expand Down Expand Up @@ -152,14 +139,6 @@ def plotsForField(inButler, keys, fixup, plots=None, prefix=''):
print('matches...')
wcsPlots.plotMatches(sources, ref, matches, wcs, W, H, prefix)

if 'corr' in plots:
# print 'corr...'
# requires astrometry.libkd (not available in 0.30)
#wcsPlots.plotCorrespondences2(sources, ref, matches, wcs, W, H, prefix)
# print 'corr...'
#wcsPlots.plotCorrespondences(sources, ref, matches, wcs, W, H, prefix)
pass

if 'distortion' in plots:
print('distortion...')
wcsPlots.plotDistortion(wcs, W, H, 400, prefix,
Expand Down
237 changes: 237 additions & 0 deletions include/lsst/meas/astrom/astrometry_net.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
// -*- lsst-C++ -*-

// Astrometry.net include files...
extern "C" {
#include "astrometry/solver.h"
#include "astrometry/index.h"
#include "astrometry/multiindex.h"
#include "astrometry/starkd.h"
#include "astrometry/fitsioutils.h"
#include "astrometry/fitstable.h"
#include "astrometry/log.h"
#include "astrometry/tic.h"
#include "astrometry/healpix.h"

#undef ATTRIB_FORMAT
#undef FALSE
#undef TRUE

#undef logdebug
#undef debug
}

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "boost/format.hpp"

#include "lsst/utils/python.h"
#include "lsst/meas/astrom/detail/utils.h"
#include "lsst/base.h"
#include "lsst/log/Log.h"
#include "lsst/daf/persistence.h"
#include "lsst/daf/base/Persistable.h"
#include "lsst/daf/base/PropertyList.h"
#include "lsst/afw/table.h"
#include "lsst/afw/image/Wcs.h"
#include "lsst/afw/image/TanWcs.h"
#include "lsst/afw/geom.h"
#include "lsst/afw/coord.h"

namespace lsst {
namespace meas {
namespace astrom {

/*
* A thin C++ wrapper around astrometry_net's multiindex_t struct.
*
* This provide memory management and a few methods used by LSST.
*/
class MultiIndex {

public:

/**
* Construct a MultiIndex from an astrometry.net multi-index file
*/
MultiIndex(std::string const & filepath);

~MultiIndex() {}

/**
* Get the specified index
*/
index_t * operator[](int i) const {
return multiindex_get(_multiindex.get(), i);
}

/**
* Add an index read from a file
*/
void addIndex(std::string const & filepath, bool metadataOnly);

/**
* Is this multi-index in range of the specified cone?
*/
int isWithinRange(double ra, double dec, double radius_deg);

/**
* Unload the indices
*/
void unload() {
multiindex_unload(_multiindex.get());
}

std::string getName() const {
return _multiindex->fits->filename;
}

/**
* Get the number of indices
*/
int getLength() const {
return multiindex_n(_multiindex.get());
}

/**
* Reload the indices
*/
void reload();

private:

struct _Deleter {
void operator()(multiindex_t* m) {
multiindex_free(m);
}
};

std::unique_ptr<multiindex_t, _Deleter> _multiindex;
};


/**
* A thin C++ wrapper around astrometry.net's solver_t struct.
*
* This provide memory management and methods used by LSST.
*/
class Solver {

public:

explicit Solver();

~Solver();

/**
Load reference objects in a region of the sky described by a center coordinate and a radius

@param[in] inds list of star kd-trees from astrometry.net
@param[in] ctrCoord center of search region
@param[in] radius search radius
@param[in] idCol name of ID column in astrometry.net data
@param[in] filterNameList names of filters in astrometry.net data
@param[in] magColList names of magnitude columns in astrometry.net data
@param[in] magErrColList names of magnitude uncertainty (sigma) columns in astrometry.net data
@param[in] starGalCol name of "starGal" column (true if object is a star) in astrometry.net data
@param[in] varCol name of "var" column (true if brightness is variable) in astrometry.net data
@param[in] uniqueIds if true then only return unique IDs (the first of each seen)

Returned schema:
- id
- coord: sky position (an lsst::afw::coord::IcrsCoord)
- centroid: centroid on some exposure, if relevant (an lsst::afw::geom::Point2D);
returned value is not set
- hasCentroid: if true then centroid has been set; returned value is false
- <filterName>_flux: flux in the specified filter (double)
- <filterName>_fluxSigma: flux uncertainty in the specified filter (double)
- resolved (if starGalCol specified): true if object is not resolved
- variable (if varCol specified): true if brightness is variable
- photometric: true if not resolved (or starGalCol blank) and not variable (or varCol blank);
note that if starGalCol and varCol both blank then all objects are claimed to be photometric
*/
lsst::afw::table::SimpleCatalog getCatalog(
std::vector<index_t*> inds,
lsst::afw::coord::Coord const &ctrCoord,
lsst::afw::geom::Angle const &radius,
const char* idCol,
std::vector<std::string> const& filterNameList,
std::vector<std::string> const& magColList,
std::vector<std::string> const& magErrColList,
const char* starGalCol,
const char* varCol,
bool uniqueIds=true);

std::shared_ptr<lsst::daf::base::PropertyList> getSolveStats() const;

std::shared_ptr<lsst::afw::image::Wcs> getWcs();

bool didSolve() const {
return solver_did_solve(_solver.get());
}

void run(double cpulimit);

std::pair<double, double> getQuadSizeRangeArcsec() const {
double qlo,qhi;
solver_get_quad_size_range_arcsec(_solver.get(), &qlo, &qhi);
return std::make_pair(qlo, qhi);
}

/**
* Add indices to the solver
*
* The indices are bare pointers whose memory is managed by the caller.
* Typically the indices are owned by a MultiIndex object owned by the caller.
*/
void addIndices(std::vector<index_t*> inds);

/**
* Set parity to flipped (if true) or normal (if false)
*/
void setParity(bool flipped) {
_solver->parity = flipped ? PARITY_FLIP : PARITY_NORMAL;
}

void setMatchThreshold(double threshold) {
solver_set_keep_logodds(_solver.get(), threshold);
}

void setPixelScaleRange(double low, double high) {
_solver->funits_lower = low;
_solver->funits_upper = high;
}

void setRaDecRadius(double ra, double dec, double radius_deg) {
solver_set_radec(_solver.get(), ra, dec, radius_deg);
}

void setImageSize(int width, int height);

void setMaxStars(int maxStars) {
_solver->endobj = maxStars;
}

void setStars(lsst::afw::table::SourceCatalog const & srcs, int x0, int y0);

private:

struct _Deleter {
void operator()(solver_t* m) {
solver_free(m);
}
};

std::unique_ptr<solver_t, _Deleter> _solver;
};

/**
* Calculate the distance from coordinates to a healpix
*
* Note that this assumes that the astrometry.net catalog reference system is ICRS.
*/
lsst::afw::geom::Angle healpixDistance(int hp, int nside, lsst::afw::coord::Coord const& coord);

}}} // namespace lsst::meas::astrom
10 changes: 8 additions & 2 deletions python/lsst/meas/astrom/SConscript
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
# -*- python -*-
from lsst.sconsUtils import scripts
scripts.BasicSConscript.python(["astromLib", "astrometry_net"],
swigSrc={"astrometry_net": ["utils.cc"]})
scripts.BasicSConscript.pybind11([
"astrometry_net/astrometry_net",
"makeMatchStatistics",
"matchOptimisticB/matchOptimisticB",
"polynomialTransform",
"scaledPolynomialTransformFitter",
"sipTransform",
], addUnderscore=False)
15 changes: 10 additions & 5 deletions python/lsst/meas/astrom/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from __future__ import absolute_import
#
# LSST Data Management System
# Copyright 2008, 2009, 2010 LSST Corporation.
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
# See the COPYRIGHT file
#
# 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
Expand All @@ -20,23 +19,29 @@
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#
# import lsst.utils
from __future__ import absolute_import

from .makeMatchStatistics import *
from .matchOptimisticB import *
from .polynomialTransform import *
from .scaledPolynomialTransformFitter import *
from .sipTransform import *

from . import sip

from .ref_match import *
from .astrometryNetDataConfig import *
from .anetBasicAstrometry import *
from .astrometry import *
from .anetAstrometry import *
from .approximateWcs import *
from .loadAstrometryNetObjects import *
from .matchOptimisticB import *
from .setMatchDistance import *
from .display import *
from .approximateWcs import *
from .createMatchMetadata import *
from .astromLib import *
from .catalogStarSelector import *
from .directMatch import *
from .fitSipDistortion import *

from .version import *