1***************************************
2Building Index Files for Astrometry.net
3***************************************
4
5Astrometry.net searches the sky using *index files*.  These contain a
6set of "stars" (maybe also galaxies), selected so that they are
7relatively bright and cover the sky uniformly.  They also contain a
8large number of *features* or *quads* that describe the local shape of
9sets of (usually four) stars.  Each *feature* points back to the stars
10it is composed of.  The Astrometry.net engine works by detecting stars
11in your image, and then looking at sets of (usually four) stars,
12computing their local shape, and searching in the index files for
13features with similar shapes.  For each similar shape that is found,
14it retrieves *other* stars in the area and checks whether other
15reference stars are aligned with other stars in your image.
16
17While we distribute index files based on the 2MASS and Tycho-2
18astrometric catalogs that should work for most purposes, some people
19want to use other reference catalogs for their particular purpsose.
20This document explains how to build custom index files from a
21reference catalog.
22
23The steps are:
24  * :ref:`convert-to-fits`
25  * :ref:`prepare-fits`
26  * :ref:`hpsplit`
27  * :ref:`build-index`
28  * :ref:`use`
29
30Here are some pictures of the index-building process itself:
31
32+-------------------------------+----------------------------+----------------------------------------------+
33|A reference catalog:           |We lay down a healpix grid: | And select the brightest stars in each cell: |
34+-------------------------------+----------------------------+----------------------------------------------+
35| .. image:: usnob.jpg          | .. image:: usnob-grid.jpg  | .. image:: cut.png                           |
36|  :height: 200px               |  :height: 200px            |  :height: 200px                              |
37+-------------------------------+----------------------------+----------------------------------------------+
38|And then try to build a 4-star | And again...               | And again, until the sky is densely tiled    |
39|feature centered in each cell  |                            | in features.                                 |
40+-------------------------------+----------------------------+----------------------------------------------+
41| .. image:: quads1b.png        | .. image:: quads2b.png     | .. image:: quads3b.jpg                       |
42|  :height: 200px               |  :height: 200px            |  :height: 200px                              |
43+-------------------------------+----------------------------+----------------------------------------------+
44
45
46.. _convert-to-fits:
47
48Convert your reference catalog to FITS tables
49=============================================
50
51The Astrometry.net index files are FITS tables, and the index-building
52process take FITS tables as inputs.
53
54Many astrometric reference catalogs are available in FITS format.  For
55those that aren't, here are a few options for converting to FITS
56BINTABLE (binary table) format:
57* *text2fits.py* in the Astrometry.net package---useful for CSV
58(comma-separated values) and other ASCII text inputs; this is a
59simple parser and takes a huge amount of memory to process big
60files.  It would be possible to make it "stream" the inputs and
61outputs, but I haven't done that (yet).
62* Custom format converters, including *2masstofits*, *nomadtofits*,
63*ucac3tofits*, and *usnobtofits* (all in the Astrometry.net
64package).
65* Check the `Vizier <http://vizier.u-strasbg.fr/viz-bin/VizieR-2>`_
66service to see if your catalog is available there; sometimes you
67can download it as FITS binary table (in the "Preferences" box for
68output format).  I find the Vizier search engine impossible to
69use; just use your favorite web search engine to query, say,
70"vizier ucac4".
71* Write your own custom converter.  If I had to do this again, I
72would rewrite all the *Xtofits* converters above in python,
73probably using the `struct module <http://docs.python.org/2/library/struct.html>`_.
74But if you are converting a format that is very similar to one of
75the above, the fastest may be to copy-n-edit one of the existing
76ones.  If you do this, please consider contributing your code back
77to the Astrometry.net codebase.
78
79As for python FITS table handling, the *best* option is
80`fitsio <https://github.com/esheldon/fitsio>`_.
81The *most popular* option is probably
82`pyfits <http://www.stsci.edu/institute/software_hardware/pyfits/Download>`_.
83The Astrometry.net package includes a wrapper that can use either of
84those; util/fits.py.
85
86The *cfitsio* package includes some tools for handling FITS tables, in
87particular *liststruc* (list the structure of a FITS file), *listhead*
88(print the headers), *fitscopy* (copy files, possible with manipulations; see
89`extended filename syntax <http://heasarc.gsfc.nasa.gov/fitsio/c/c_user/node81.html>`_.).
90
91.. _prepare-fits:
92
93Prepare your FITS tables
94========================
95
96You may want to make some cuts, remove irrelevant columns, or
97otherwise prepare your FITS tables before feeding them into the
98index-building stage.  At the very least, you want your FITS tables to
99contain *RA* and *DEC* columns, as well as a column that defines the
100brightness ordering of your stars: probably a *MAG*.
101
102Any other columns you include can optionally be propagated into the
103index files, so that after getting an astrometric match you will also
104have access to this "tag-along" data.  This is useful for, for
105example, doing an initial photometric calibration by tagging-along one
106or more bands of photometric data for each astrometric star.
107
108As an example, the file
109`2mass-cut.py <https://github.com/dstndstn/astrometry.net/blob/master/solver/2mass-cut.py>`_
110implements the cut we used to build our 2MASS-based index files.  It
111removes any stars that are flagged in the 2MASS catalog (low quality,
112contaminated, etc), and writes out just the RA,Dec, and J-magnitude
113columns.
114
115.. _hpsplit:
116
117Split the sky into pieces
118=========================
119
120Optionally, you can split the sky into slightly overlapping pieces.
121
122Why split the sky into pieces?  First, it results in smaller files
123that can be easier to handle.  Second, if you have an initial guess of
124where your image is on the sky, the Astrometry.net engine can avoid
125loading sky tiles that don't overlap, so it results in faster and less
126memory-intensive searches.
127
128If you don't split the sky into pieces, at this point you should
129combine your input catalog files into a single FITS table, if you
130haven't done that already.  You can use the *tabmerge* program for
131that.
132
133Splitting the sky into pieces is done using the *hpsplit* program.  It
134takes a number of input FITS tables and produces one output table for
135each *healpix* tile::
136
137    > hpsplit -h
138    This program is part of the Astrometry.net suite.
139    For details, visit  http://astrometry.net .
140    Subversion URL svn+ssh://astrometry.net/svn/trunk/src/astrometry/util/
141    Revision 22921, date 2013-06-02 15:07:59 -0400 (Sun, 02 Jun 2013).
142
143    Usage: hpsplit [options] <input-FITS-catalog> [...]
144        -o <output-filename-pattern>  with %i printf-pattern
145        [-r <ra-column-name>]: name of RA in FITS table (default RA)
146        [-d <dec-column-name>]: name of DEC in FITS table (default DEC)
147        [-n <healpix Nside>]: default is 1
148        [-m <margin in deg>]: add a margin of this many degrees around the healpixes; default 0
149        [-g]: gzip'd inputs
150        [-c <name>]: copy given column name to the output files
151        [-t <temp-dir>]: use the given temp dir; default is /tmp
152        [-b <backref-file>]: save the filenumber->filename map in this file; enables writing backreferences too
153        [-v]: +verbose
154
155The number of healpix tiles is determined by the *Nside* (-n) option.
156``-n 1`` means split the sky into 12 pieces.  ``-n 2`` means split the sky
157into 48 pieces.  You probably don't want to go any finer than that.
158
159For reference, maps of the healpix tiles are here:
160`Nside=1 healpixes <https://github.com/dstndstn/astrometry.net/blob/master/util/hp.png>`_;
161`Nside=2 healpixes <http://trac.astrometry.net/browser/trunk/src/astrometry/util/hp2.png>`_.
162
163You probably want to set ``-m`` for the *margin* -- extra overlapping
164area around each healpix tile.  You probably want to set this about
165half as big as the images you are going to solve.  This will mean that
166in the margin areas, multiple healpix tiles will contain the same
167stars.
168
169If you want to "tag-along" extra information into the index files,
170include those columns with the ``-c`` option.
171
172Example hpsplit command::
173
174    hpsplit -o 2mass-hp%02i.fits -n 2 -m 1 2mass/2mass-*.fits
175
176Notice the ``%02i`` in the output filename; that's a "printf string"
177that says, write an integer, using 2 digits, padding with zeros.  The
178outputs will be named 2mass-hp00.fits through 2mass-hp11.fits (for ``-n
1791``).
180
181At the end of this, you will have 12 or 48 FITS tables (assuming your
182input catalog was all-sky; fewer if not).  You will build several
183index file for each of these (each one covering one scale).
184
185.. _build-index:
186
187Building Index Files
188====================
189
190Finally!  The real deal.
191
192*build-astrometry-index* has a daunting number of options, but don't panic::
193
194    > build-astrometry-index
195    You must specify input & output filenames.
196    This program is part of the Astrometry.net suite.
197    For details, visit  http://astrometry.net .
198    Subversion URL svn+ssh://astrometry.net/svn/trunk/src/astrometry/util/
199    Revision 22921, date 2013-06-02 15:07:59 -0400 (Sun, 02 Jun 2013).
200
201    Usage: build-astrometry-index
202          (
203             -i <input-FITS-catalog>  input: source RA,DEC, etc
204        OR,
205             -1 <input-index>         to share another index's stars
206          )
207          -o <output-index>        output filename for index
208          (
209             -P <scale-number>: use 'preset' values for '-N', '-l', and '-u'
210                   (the scale-number is the last two digits of the pre-cooked
211                    index filename -- eg, index-205 is  "-P 5".
212                    -P 0  should be good for images about 6 arcmin in size
213                        and it goes in steps of sqrt(2), so:
214                    -P 2  should work for images about 12 arcmin across
215                    -P 4  should work for images about 24 arcmin across
216                    -P 6  should work for images about 1 degree across
217                    -P 8  should work for images about 2 degree across
218                    -P 10 should work for images about 4 degree across
219                     etc... up to -P 19
220      OR,
221             -N <nside>            healpix Nside for quad-building
222             -l <min-quad-size>    minimum quad size (arcminutes)
223             -u <max-quad-size>    maximum quad size (arcminutes)
224          )
225          [-S <column>]: sort column (default: assume the input file is already sorted)
226          [-f]: sort in descending order (eg, for FLUX); default ascending (eg, for MAG)
227          [-A <column>]: specify the RA  column name in the input FITS table (default "RA")
228          [-D <column>]: specify the Dec column name in the input FITS table (default "Dec")
229          [-B <val>]: cut any object whose sort-column value is less than 'val'; for mags this is a bright limit
230          [-U]: healpix Nside for uniformization (default: same as -n)
231          [-H <big healpix>]; default is all-sky
232          [-s <big healpix Nside>]; default is 1
233          [-m <margin>]: add a margin of <margin> healpixels; default 0
234          [-n <sweeps>]    (ie, number of stars per fine healpix grid cell); default 10
235          [-r <dedup-radius>]: deduplication radius in arcseconds; default no deduplication
236          [-j <jitter-arcsec>]: positional error of stars in the reference catalog (in arcsec; default 1)
237
238          [-d <dimquads>] number of stars in a "quad" (default 4).
239          [-p <passes>]   number of rounds of quad-building (ie, # quads per healpix cell, default 16)
240          [-R <reuse-times>] number of times a star can be used (default: 8)
241          [-L <max-reuses>] make extra passes through the healpixes, increasing the "-r" reuse
242                         limit each time, up to "max-reuses".
243          [-E]: scan through the catalog, checking which healpixes are occupied.
244
245          [-I <unique-id>] set the unique ID of this index
246
247          [-M]: in-memory (don't use temp files)
248          [-T]: don't delete temp files
249          [-t <temp-dir>]: use this temp direcotry (default: /tmp)
250          [-v]: add verbosity.
251
252
253I will list them from most important to least (and roughly
254top-to-bottom).
255
256**Input file**::
257
258      (
259         -i <input-FITS-catalog>  input: source RA,DEC, etc
260    OR,
261         -1 <input-index>         to share another index's stars
262      )
263
264The ``-1`` version is only used in the LSST index files; everyone else
265should probably use ``-i``.  This will be the FITS file you have
266carefully created as detailed above.
267
268**Output filename**::
269
270    -o <output-index>        output filename for index
271
272Easy!  I usually just name mine with a number, the healpix tile, and
273scale, but you can do anything that makes sense to you.  These will be
274FITS tables, so the suffix .fits would be appropriate, but none of the
275code cares about the filenames, so do what you like.
276
277**Index scale**::
278
279        (
280           -P <scale-number>: use 'preset' values for '-N', '-l', and '-u'
281                 (the scale-number is the last two digits of the pre-cooked
282                  index filename -- eg, index-205 is  "-P 5".
283                  -P 0  should be good for images about 6 arcmin in size
284                      and it goes in steps of sqrt(2), so:
285                  -P 2  should work for images about 12 arcmin across
286                  -P 4  should work for images about 24 arcmin across
287                  -P 6  should work for images about 1 degree across
288                  -P 8  should work for images about 2 degree across
289                  -P 10 should work for images about 4 degree across
290                   etc... up to -P 19
291    OR,
292           -N <nside>            healpix Nside for quad-building
293           -l <min-quad-size>    minimum quad size (arcminutes)
294           -u <max-quad-size>    maximum quad size (arcminutes)
295        )
296        ...
297        [-U]: healpix Nside for uniformization (default: same as -n)
298
299
300This determines the scale on which stars are selected
301uniformly on the sky, the scale at which features are selected, and
302the angular size of the features to create.  In Astrometry.net land,
303we use a "preset" number of scales, each one covering a range of about
304square-root-of-2.  Totally arbitrarily, the range 2.0-to-2.4
305arcminutes is called scale zero.  You want to have features that are
306maybe 25% to 75% of the size of your image, so you probably want to
307build a range of scales.  For reference, for most of the experiments
308in my thesis I used scale 2 (4 to 5.6 arcmin features) to recognize
309Sloan Digital Sky Survey images, which are 13-by-9 arcminutes.  Scales
3103, 4, and 1 also yielded solutions when they were included.
311
312You will run build-astrometry-index once for each scale.
313
314Presets in the range -5 to 19 are available.  The scales for the presets are listed in the `Getting Index Files <http://astrometry.net/doc/readme.html#getting-index-files>`_ documentation.
315
316Rather than use the ``-P`` option it is possible to specify separately
317the different scales using ``-N``, ``-l``, ``-u``, ``-U``, but I wouldn't
318recommend it.  The presets are listed in
319`<build-index-main.c healpixes https://github.com/dstndstn/astrometry.net/blob/master/solver/build-index-main.c>`_.
320
321**Sort column**::
322
323    [-S <column>]: sort column (default: assume the input file is already sorted)
324    [-f]: sort in descending order (eg, for FLUX); default ascending (eg, for MAG)
325    [-B <val>]: cut any object whose sort-column value is less than 'val'; for mags this is a bright limit
326
327Which column in your FITS table input should we use to
328determine which stars are bright?  (We preferentially select bright
329stars to include in the index files.)  Typically this will be something like::
330
331    build-astrometry-index -S J_mag [...]
332
333By default, we assume that SMALL values of the sorting column are
334bright -- that is, it works for MAGs.  If you have linear FLUX-like
335units, then use the ``-f`` flag to reverse the sorting direction.
336
337It is also possible to *cut* objects whose sort-column value is less
338than a lower limit, using the ``-B`` flag.
339
340**Which part of the sky is this?**::
341
342    [-H <big healpix>]; default is all-sky
343    [-s <big healpix Nside>]; default is 1
344
345You need to tell build-astrometry-index which
346part of the sky it is indexing.  By default, it assumes you are
347building an all-sky index.
348
349If you have split your reference catalog into 12 pieces (healpix
350Nside = 1) using *hpsplit* as described above, then you will run
351*build-astrometry-index* once for each healpix tile FITS table and scale,
352specifying the tile number with ``-H`` and the Nside with ``-s`` (default
353is 1), and specifying the scale with ``-P``::
354
355    # Healpix 0, scales 2-4
356    build-astrometry-index -i catalog-hp00.fits -H 0 -s 1 -P 2 -o myindex-02-00.fits [...]
357    build-astrometry-index -i catalog-hp00.fits -H 0 -s 1 -P 3 -o myindex-03-00.fits [...]
358    build-astrometry-index -i catalog-hp00.fits -H 0 -s 1 -P 4 -o myindex-04-00.fits [...]
359    # Healpix 1, scales 2-4
360    build-astrometry-index -i catalog-hp01.fits -H 1 -s 1 -P 2 -o myindex-02-01.fits [...]
361    build-astrometry-index -i catalog-hp01.fits -H 1 -s 1 -P 3 -o myindex-03-01.fits [...]
362    build-astrometry-index -i catalog-hp01.fits -H 1 -s 1 -P 4 -o myindex-04-01.fits [...]
363
364    ...
365    # Healpix 11, scales 2-4
366    build-astrometry-index -i catalog-hp11.fits -H 1 -s 1 -P 2 -o myindex-02-11.fits [...]
367    build-astrometry-index -i catalog-hp11.fits -H 1 -s 1 -P 3 -o myindex-03-11.fits [...]
368    build-astrometry-index -i catalog-hp11.fits -H 1 -s 1 -P 4 -o myindex-04-11.fits [...]
369
370You probably want to do that using a loop in your shell; for example, in bash::
371
372    for ((HP=0; HP<12; HP++)); do
373      for ((SCALE=2; SCALE<=4; SCALE++)); do
374        HH=$(printf %02i $HP)
375        SS=$(printf %02i $SCALE)
376        build-astrometry-index -i catalog-hp${HH}.fits -H $HP -s 1 -P $SCALE -o myindex-${HH}-${SS}.fits [...]
377      done
378    done
379
380**Sparse catalog?**::
381
382    [-E]: scan through the catalog, checking which healpixes are occupied.
383
384If your catalog only covers a small part of the sky, be sure to set
385the ``-E`` flag, so that ``build-astrometry-index`` only tries to select features in
386the part of the sky that your index covers.
387
388**Unique ID**::
389
390    [-I <unique-id>] set the unique ID of this index
391
392Select an identifier for your index files.  I usually encode the date
393and scale: eg 2013-08-01, scale 2, becomes 13080102.  Or I keep a
394running number, like the 4100-series and 4200-series files.  The
395different healpixes at a scale do not need unique IDs.
396
397**Triangles?**::
398
399    [-d <dimquads>] number of stars in a "quad" (default 4).
400
401Normally we use four-star featurse.  This allows you to build 3- or
4025-star features instead.  3-star features are useful for wide-angle
403images.  5-star features are probably not useful for most purposes.
404
405
406You probably don't need to set any of the options below here
407------------------------------------------------------------
408
409**RA,Dec column names**::
410
411    [-A <column>]: specify the RA  column name in the input FITS table (default "RA")
412    [-D <column>]: specify the Dec column name in the input FITS table (default "Dec")
413
414I would recommend naming your RA and Dec columns "RA" and "DEC", but
415if for some reason you don't want to do that, you need to tell
416``build-astrometry-index`` what they're called at this point, using the ``-A``
417and ``-D`` options::
418
419    build-astrometry-index -A Alpha_J2000 -D Delta_J2000 [...]
420
421**Indexing Details**::
422
423    [-m <margin>]: add a margin of <margin> healpixels; default 0
424
425Try to create features in a margin around each healpix tile.  Not
426normally necessary: the healpix tiles can contain overlapping margins
427*stars*, so each one can recognize images that straddle its boundary.
428There's no need to also cover the margin regions with (probably
429duplicate) features.
430
431::
432
433    [-n <sweeps>]    (ie, number of stars per fine healpix grid cell); default 10
434
435We try to select a bright, uniform subset of stars from your reference
436catalog by laying down a fine healpix grid and selecting ``-n`` stars
437from each.  This allows you to select fewer or more.  With fewer, you
438risk being unable to recognize some images.  With more, file sizes
439will be bigger.
440
441::
442
443    [-r <dedup-radius>]: deduplication radius in arcseconds; default no deduplication
444
445We can remove stars that are within a radius of exclusion of each
446other (eg, double stars, or problems with the reference catalog).
447
448::
449
450    [-j <jitter-arcsec>]: positional error of stars in the reference catalog (in arcsec; default 1)
451
452The index files contain a FITS header card saying what the typical
453astrometric error is.  This is used when "verifying" a proposed match;
454I don't think the system is very sensitive to this value.
455
456::
457
458    [-p <passes>]   number of rounds of quad-building (ie, # quads per healpix cell, default 16)
459
460We try to build a uniform set of features by laying down a fine
461healpix grid and trying to build a feature in each cell.  We run
462multiple passes of this, building a total of ``-p`` features in each
463cell.
464
465::
466
467    [-R <reuse-times>] number of times a star can be used (default: 8)
468
469By default, any star can be used in at most 8 features.  This prevents
470us from relying too heavily on any one star.
471
472::
473
474    [-L <max-reuses>] make extra passes through the healpixes, increasing the "-r" reuse
475                      limit each time, up to "max-reuses".
476
477Sometimes the ``-R`` option means that we "use up" all the stars in an
478area and can't build as many features as we would like.  This option
479enables a second pass where we loosen up with ``-R`` value, trying to
480build extra features.
481
482**Runtime details**::
483
484    [-M]: in-memory (don't use temp files)
485    [-T]: don't delete temp files
486    [-t <temp-dir>]: use this temp direcotry (default: /tmp)
487    [-v]: add verbosity.
488
489The help messages are all pretty self-explanatory, no?
490
491
492.. _use:
493
494Using your shiny new index files
495================================
496
497In order to use your new index files, you need to create a *backend
498config* file that tells the astrometry engine where to find them.
499
500The default backend config file is in
501/usr/local/astrometry/etc/backend.cfg
502
503You can either edit that file, or create a new .cfg file.  Either way,
504you need to add lines like::
505
506    # In which directories should we search for indices?
507    add_path /home/dstn/astrometry/data
508
509    # Load any indices found in the directories listed above.
510    autoindex
511
512    ## Or... explicitly list the indices to load.
513    #index index-4200-00.fits
514    #index index-4200-01.fits
515
516
517It is safe to include multiple sets of index files that cover the same
518region of sky, mix and match, or whatever.  The astrometry engine will
519just use whatever you give it.
520
521If you edited the default backend.cfg file, ``solve-field`` and
522``backend`` will start using your new index files right away.  If you
523create a new index file (I often put one in the directory containing
524the index files themselves), you need to tell ``solve-field`` where it
525is::
526
527    solve-field --backend-config /path/to/backend.cfg [...]
528
529
530That's it!  Report successes, failures, frustrations, missing documentation, spelling errors, and such at the `Astrometry.net google group <http://astrometry.net/group>`_.
531
532