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 qfits_header_destroy(hdr); //# Modified by Robert Lancaster for the StellarSolver Internal Library, to prevent leak
30 return -1;
31 }
32
33 if (scamp_catalog_write_field_header(scamp, hdr)) {
34 scamp_catalog_close(scamp); //# Modified by Robert Lancaster for the StellarSolver Internal Library, to prevent leaks
35 qfits_header_destroy(hdr);
36 return -1;
37 }
38 qfits_header_destroy(hdr);
39
40 for (i=0; i<starxy_n(xy); i++) {
41 scamp_obj_t obj;
42 obj.x = starxy_getx(xy, i);
43 obj.y = starxy_gety(xy, i);
44 obj.err_a = 1.0;
45 obj.err_b = 1.0;
46 obj.err_theta = 0.0;
47 if (xy->flux)
48 obj.flux = xy->flux[i];
49 else
50 obj.flux = 1000.0;
51 obj.err_flux = 1.0;
52 obj.flags = 0;
53 if (scamp_catalog_write_object(scamp, &obj)) {
54 return -1;
55 }
56 }
57
58 if (scamp_catalog_close(scamp)) {
59 return -1;
60 }
61 return 0;
62 }
63
scamp_write_config_file(const char * refcatfn,const char * outfn)64 int scamp_write_config_file(const char* refcatfn, const char* outfn) {
65 FILE* fid;
66 char* text;
67 int res;
68 fid = fopen(outfn, "w");
69 if (!fid) {
70 SYSERROR("Failed to open file %s to write Scamp config file", outfn);
71 return -1;
72 }
73 text = scamp_get_config_options(refcatfn);
74 res = fprintf(fid, "%s", text);
75 free(text);
76 if (res < 0) {
77 SYSERROR("Failed to write to Scamp config file %s", outfn);
78 return -1;
79 }
80 if (fclose(fid)) {
81 SYSERROR("Failed to close to Scamp config file %s", outfn);
82 return -1;
83 }
84 return 0;
85 }
86
scamp_get_config_options(const char * refcatfn)87 char* scamp_get_config_options(const char* refcatfn) {
88 char* res;
89 char* fmt =
90 "## These are SCAMP config file entries to get Scamp to read the\n"
91 "## catalogs generated by the Astrometry.net Scamp integration code.\n"
92 "#\n"
93 "# The following decribe the reference catalog:\n"
94 "# Read from a local file.\n"
95 "ASTREF_CATALOG FILE\n"
96 "# You can also set this to: USNO-B1, SDSS-R7, 2MASS, etc\n"
97 "# The reference catalog file name is:\n"
98 "ASTREFCAT_NAME %s\n"
99 "# The reference catalog (RA,Dec) in degrees: column names in file\n"
100 "ASTREFCENT_KEYS RA, DEC\n"
101 "# The reference catalog (RA,Dec) error ellipse: column names\n"
102 "ASTREFERR_KEYS ERR_A, ERR_B\n"
103 "# The reference catalog magnitude: column names\n"
104 "ASTREFMAG_KEY MAG\n"
105 "#\n"
106 "# The following decribe the input catalog:\n"
107 "#\n"
108 "# The (x,y) source positions in pixels: column names\n"
109 "CENTROID_KEYS X_IMAGE, Y_IMAGE\n"
110 "# The source positions should get distortions\n"
111 "DISTORT_KEYS X_IMAGE, Y_IMAGE\n"
112 "# The error ellipse in source positions: column names\n"
113 "CENTROIDERR_KEYS ERR_A, ERR_B\n"
114 "# The flux: column name\n"
115 "PHOTFLUX_KEY FLUX\n"
116 "# The flux error: column name\n"
117 "PHOTFLUXERR_KEY FLUX_ERR\n"
118 "# Don't run Scamp's matching procedure to get initial WCS\n"
119 "# (use the Astrometry.net solution!)\n"
120 "MATCH N\n"
121 "#\n"
122 "## End of Astrometry.net Scamp integration config items\n"
123 ;
124 asprintf_safe(&res, fmt, refcatfn);
125 return res;
126 }
127
128