1 /*
2  # This file is part of the Astrometry.net suite.
3  # Licensed under a 3-clause BSD style license - see LICENSE
4  */
5 
6 #include "scamp.h"
7 #include "scamp-catalog.h"
8 #include "sip_qfits.h"
9 #include "errors.h"
10 #include "log.h"
11 
scamp_write_field(const qfits_header * imageheader,const sip_t * wcs,const starxy_t * xy,const char * filename)12 int scamp_write_field(const qfits_header* imageheader,
13                       const sip_t* wcs,
14                       const starxy_t* xy,
15                       const char* filename) {
16     scamp_cat_t* scamp;
17     qfits_header* hdr;
18     int i;
19 
20     if (!imageheader)
21         hdr = qfits_table_prim_header_default();
22     else
23         hdr = qfits_header_copy(imageheader);
24 
25     sip_add_to_header(hdr, wcs);
26 
27     scamp = scamp_catalog_open_for_writing(filename, FALSE);
28     if (!scamp) {
29         return -1;
30     }
31 
32     if (scamp_catalog_write_field_header(scamp, hdr)) {
33         return -1;
34     }
35     qfits_header_destroy(hdr);
36 
37     for (i=0; i<starxy_n(xy); i++) {
38         scamp_obj_t obj;
39         obj.x = starxy_getx(xy, i);
40         obj.y = starxy_gety(xy, i);
41         obj.err_a = 1.0;
42         obj.err_b = 1.0;
43         obj.err_theta = 0.0;
44         if (xy->flux)
45             obj.flux = xy->flux[i];
46         else
47             obj.flux = 1000.0;
48         obj.err_flux = 1.0;
49         obj.flags = 0;
50         if (scamp_catalog_write_object(scamp, &obj)) {
51             return -1;
52         }
53     }
54 
55     if (scamp_catalog_close(scamp)) {
56         return -1;
57     }
58     return 0;
59 }
60 
scamp_write_config_file(const char * refcatfn,const char * outfn)61 int scamp_write_config_file(const char* refcatfn, const char* outfn) {
62     FILE* fid;
63     char* text;
64     int res;
65     fid = fopen(outfn, "w");
66     if (!fid) {
67         SYSERROR("Failed to open file %s to write Scamp config file", outfn);
68         return -1;
69     }
70     text = scamp_get_config_options(refcatfn);
71     res = fprintf(fid, "%s", text);
72     free(text);
73     if (res < 0) {
74         SYSERROR("Failed to write to Scamp config file %s", outfn);
75         return -1;
76     }
77     if (fclose(fid)) {
78         SYSERROR("Failed to close to Scamp config file %s", outfn);
79         return -1;
80     }
81     return 0;
82 }
83 
scamp_get_config_options(const char * refcatfn)84 char* scamp_get_config_options(const char* refcatfn) {
85     char* res;
86     char* fmt =
87         "## These are SCAMP config file entries to get Scamp to read the\n"
88         "## catalogs generated by the Astrometry.net Scamp integration code.\n"
89         "#\n"
90         "# The following decribe the reference catalog:\n"
91         "# Read from a local file.\n"
92         "ASTREF_CATALOG    FILE\n"
93         "# You can also set this to: USNO-B1, SDSS-R7, 2MASS, etc\n"
94         "# The reference catalog file name is:\n"
95         "ASTREFCAT_NAME    %s\n"
96         "# The reference catalog (RA,Dec) in degrees: column names in file\n"
97         "ASTREFCENT_KEYS   RA, DEC\n"
98         "# The reference catalog (RA,Dec) error ellipse: column names\n"
99         "ASTREFERR_KEYS    ERR_A, ERR_B\n"
100         "# The reference catalog magnitude: column names\n"
101         "ASTREFMAG_KEY     MAG\n"
102         "#\n"
103         "# The following decribe the input catalog:\n"
104         "#\n"
105         "# The (x,y) source positions in pixels: column names\n"
106         "CENTROID_KEYS     X_IMAGE, Y_IMAGE\n"
107         "# The source positions should get distortions\n"
108         "DISTORT_KEYS      X_IMAGE, Y_IMAGE\n"
109         "# The error ellipse in source positions: column names\n"
110         "CENTROIDERR_KEYS  ERR_A, ERR_B\n"
111         "# The flux: column name\n"
112         "PHOTFLUX_KEY      FLUX\n"
113         "# The flux error: column name\n"
114         "PHOTFLUXERR_KEY   FLUX_ERR\n"
115         "# Don't run Scamp's matching procedure to get initial WCS\n"
116         "# (use the Astrometry.net solution!)\n"
117         "MATCH N\n"
118         "#\n"
119         "## End of Astrometry.net Scamp integration config items\n"
120         ;
121     asprintf_safe(&res, fmt, refcatfn);
122     return res;
123 }
124 
125