1 | // ----------------------------------------------------------------------- |
---|
2 | // createTestImage.cc: Code to create the test image used by the verification scripts |
---|
3 | // ----------------------------------------------------------------------- |
---|
4 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
5 | // |
---|
6 | // This program is free software; you can redistribute it and/or modify it |
---|
7 | // under the terms of the GNU General Public License as published by the |
---|
8 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
9 | // option) any later version. |
---|
10 | // |
---|
11 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
13 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
14 | // for more details. |
---|
15 | // |
---|
16 | // You should have received a copy of the GNU General Public License |
---|
17 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
18 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
19 | // |
---|
20 | // Correspondence concerning Duchamp may be directed to: |
---|
21 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
22 | // Postal address: Dr. Matthew Whiting |
---|
23 | // Australia Telescope National Facility, CSIRO |
---|
24 | // PO Box 76 |
---|
25 | // Epping NSW 1710 |
---|
26 | // AUSTRALIA |
---|
27 | // ----------------------------------------------------------------------- |
---|
28 | #include <iostream> |
---|
29 | #include <stdlib.h> |
---|
30 | #include <time.h> |
---|
31 | #include <math.h> |
---|
32 | #include <Devel/devel.hh> |
---|
33 | #define WCSLIB_GETWCSTAB // define this so that we don't try and redefine |
---|
34 | // wtbarr (this is a problem when using gcc v.4+ |
---|
35 | #include <fitsio.h> |
---|
36 | #include <string> |
---|
37 | using std::string; |
---|
38 | |
---|
39 | void addGaussian(float *array, long *dim, float *pos, float *fwhm, float norm); |
---|
40 | void write_header_info(fitsfile *fptr); |
---|
41 | int main() |
---|
42 | { |
---|
43 | |
---|
44 | srandom(37); |
---|
45 | long *dim = new long[3]; |
---|
46 | dim[0] = 100; |
---|
47 | dim[1] = 100; |
---|
48 | dim[2] = 128; |
---|
49 | int size = dim[0]*dim[1]*dim[2]; |
---|
50 | |
---|
51 | float *array = new float[size]; |
---|
52 | for(int i=0;i<size;i++) array[i] = getNormalRV(); |
---|
53 | float xpos[5] = {25.,75.,50.,25.,75.}; |
---|
54 | float ypos[5] = {25.,25.,50.,75.,75.}; |
---|
55 | float zpos[5] = {20.,42.,64.,86.,108.}; |
---|
56 | float fwhm[3] = {5.,5.,5.}; |
---|
57 | float pos[3]; |
---|
58 | for(int i=0;i<5;i++){ |
---|
59 | pos[0] = xpos[i]; |
---|
60 | pos[1] = ypos[i]; |
---|
61 | pos[2] = zpos[i]; |
---|
62 | addGaussian(array, dim, pos, fwhm, 10.); |
---|
63 | } |
---|
64 | |
---|
65 | int status = 0,bitpix; |
---|
66 | long *fpixel = new long[3]; |
---|
67 | for(int i=0;i<3;i++) fpixel[i]=1; |
---|
68 | fitsfile *fptr; |
---|
69 | string cubeName = "!verification/verificationCube.fits"; |
---|
70 | if( fits_create_file(&fptr,cubeName.c_str(),&status) ) |
---|
71 | fits_report_error(stderr, status); |
---|
72 | |
---|
73 | status = 0; |
---|
74 | if( fits_create_img(fptr, FLOAT_IMG, 3, dim, &status) ) |
---|
75 | fits_report_error(stderr, status); |
---|
76 | |
---|
77 | write_header_info(fptr); |
---|
78 | |
---|
79 | status = 0; |
---|
80 | if( fits_write_pix(fptr, TFLOAT, fpixel, size, array, &status) ) |
---|
81 | fits_report_error(stderr, status); |
---|
82 | |
---|
83 | status = 0; |
---|
84 | fits_close_file(fptr, &status); |
---|
85 | if (status){ |
---|
86 | std::cerr << "Error closing file: "; |
---|
87 | fits_report_error(stderr, status); |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | |
---|
92 | } |
---|
93 | |
---|
94 | void addGaussian(float *array, long *dim, float *pos, float *fwhm, float norm) |
---|
95 | { |
---|
96 | long size = dim[0] * dim[1] * dim[2]; |
---|
97 | float sigsig = fwhm[0]*fwhm[0] + fwhm[1]*fwhm[1] + fwhm[2]*fwhm[2]; |
---|
98 | |
---|
99 | int x1 = int(std::max(0.,pos[0]-10.)); |
---|
100 | int x2 = int(std::min(float(dim[0]),pos[0]+11)); |
---|
101 | int y1 = int(std::max(0.,pos[1]-10.)); |
---|
102 | int y2 = int(std::min(float(dim[1]),pos[1]+11)); |
---|
103 | int z1 = int(std::max(0.,pos[2]-10.)); |
---|
104 | int z2 = int(std::min(float(dim[2]),pos[2]+11)); |
---|
105 | |
---|
106 | for(int x=x1;x<x2;x++){ |
---|
107 | for(int y=y1;y<y2;y++){ |
---|
108 | for(int z=z1;z<z2;z++){ |
---|
109 | |
---|
110 | float distX = (x-pos[0])/fwhm[0]; |
---|
111 | float distY = (y-pos[1])/fwhm[1]; |
---|
112 | float distZ = (z-pos[2])/fwhm[2]; |
---|
113 | float gaussFlux = norm * exp( -1.*(distX*distX + |
---|
114 | distY*distY + |
---|
115 | distZ*distZ ) ); |
---|
116 | long loc = x + y*dim[0] + z*dim[0]*dim[1]; |
---|
117 | array[loc] += gaussFlux; |
---|
118 | } |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | } |
---|
123 | |
---|
124 | void write_header_info(fitsfile *fptr) |
---|
125 | { |
---|
126 | int status = 0; |
---|
127 | float val; |
---|
128 | string str; |
---|
129 | val = 0.24; |
---|
130 | fits_write_key(fptr, TFLOAT, "BMAJ" , &val, "", &status); |
---|
131 | fits_write_key(fptr, TFLOAT, "BMIN" , &val, "", &status); |
---|
132 | val = 0.; |
---|
133 | fits_write_key(fptr, TFLOAT, "BPA" , &val, "", &status); |
---|
134 | str = "JY/BEAM"; |
---|
135 | fits_write_key(fptr, TSTRING, "BUNIT" , (char *)str.c_str(), "", &status); |
---|
136 | val = 2000.0; |
---|
137 | fits_write_key(fptr, TFLOAT, "EPOCH" , &val, "", &status); |
---|
138 | val = 180.0; |
---|
139 | fits_write_key(fptr, TFLOAT, "LONPOLE", &val, "", &status); |
---|
140 | |
---|
141 | /* First axis -- RA--SIN */ |
---|
142 | str = "RA---SIN"; |
---|
143 | fits_write_key(fptr, TSTRING, "CTYPE1", (char *)str.c_str(), "", &status); |
---|
144 | val = 90.5; |
---|
145 | fits_write_key(fptr, TFLOAT, "CRVAL1", &val, "", &status); |
---|
146 | val = -6.6666701e-2; |
---|
147 | fits_write_key(fptr, TFLOAT, "CDELT1", &val, "", &status); |
---|
148 | val = 0.; |
---|
149 | fits_write_key(fptr, TFLOAT, "CROTA1", &val, "", &status); |
---|
150 | val = 50.0; |
---|
151 | fits_write_key(fptr, TFLOAT, "CRPIX1", &val, "", &status); |
---|
152 | str = "DEG"; |
---|
153 | fits_write_key(fptr, TSTRING, "CUNIT1", (char *)str.c_str(), "", &status); |
---|
154 | |
---|
155 | /* Second axis -- DEC--SIN */ |
---|
156 | str = "DEC--SIN"; |
---|
157 | fits_write_key(fptr, TSTRING, "CTYPE2", (char *)str.c_str(), "", &status); |
---|
158 | val = -26.0; |
---|
159 | fits_write_key(fptr, TFLOAT, "CRVAL2", &val, "", &status); |
---|
160 | val = 6.6666701e-2; |
---|
161 | fits_write_key(fptr, TFLOAT, "CDELT2", &val, "", &status); |
---|
162 | val = 0.; |
---|
163 | fits_write_key(fptr, TFLOAT, "CROTA2", &val, "", &status); |
---|
164 | val = 50.0; |
---|
165 | fits_write_key(fptr, TFLOAT, "CRPIX2", &val, "", &status); |
---|
166 | str = "DEG"; |
---|
167 | fits_write_key(fptr, TSTRING, "CUNIT2", (char *)str.c_str(), "", &status); |
---|
168 | |
---|
169 | /* Third axis -- VELO-HEL */ |
---|
170 | str = "VELO-HEL"; |
---|
171 | fits_write_key(fptr, TSTRING, "CTYPE3", (char *)str.c_str(), "", &status); |
---|
172 | val = 0.; |
---|
173 | fits_write_key(fptr, TFLOAT, "CRVAL3", &val, "", &status); |
---|
174 | val = 1.3191321e+4; |
---|
175 | fits_write_key(fptr, TFLOAT, "CDELT3", &val, "", &status); |
---|
176 | val = 0.; |
---|
177 | fits_write_key(fptr, TFLOAT, "CROTA3", &val, "", &status); |
---|
178 | // val = 64.0; |
---|
179 | val = 0.0; |
---|
180 | fits_write_key(fptr, TFLOAT, "CRPIX3", &val, "", &status); |
---|
181 | str = "M/S"; |
---|
182 | fits_write_key(fptr, TSTRING, "CUNIT3", (char *)str.c_str(), "", &status); |
---|
183 | |
---|
184 | val = 1.420405751786E+09; |
---|
185 | fits_write_key(fptr, TFLOAT,"RESTFREQ", &val, "", &status); |
---|
186 | |
---|
187 | |
---|
188 | } |
---|