1 | #include <iostream> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <time.h> |
---|
4 | #include <math.h> |
---|
5 | #include <Utils/utils.hh> |
---|
6 | #define WCSLIB_GETWCSTAB // define this so that we don't try and redefine |
---|
7 | // wtbarr (this is a problem when using gcc v.4+ |
---|
8 | #include <fitsio.h> |
---|
9 | #include <string> |
---|
10 | using std::string; |
---|
11 | |
---|
12 | void addGaussian(float *array, long *dim, float *pos, float *fwhm, float norm); |
---|
13 | void write_header_info(fitsfile *fptr); |
---|
14 | int main() |
---|
15 | { |
---|
16 | |
---|
17 | srandom(37); |
---|
18 | long *dim = new long[3]; |
---|
19 | dim[0] = 100; |
---|
20 | dim[1] = 100; |
---|
21 | dim[2] = 128; |
---|
22 | int size = dim[0]*dim[1]*dim[2]; |
---|
23 | |
---|
24 | float *array = new float[size]; |
---|
25 | for(int i=0;i<size;i++) array[i] = getNormalRV(); |
---|
26 | float xpos[5] = {25.,75.,50.,25.,75.}; |
---|
27 | float ypos[5] = {25.,25.,50.,75.,75.}; |
---|
28 | float zpos[5] = {20.,42.,64.,86.,108.}; |
---|
29 | float fwhm[3] = {5.,5.,5.}; |
---|
30 | float pos[3]; |
---|
31 | for(int i=0;i<5;i++){ |
---|
32 | pos[0] = xpos[i]; |
---|
33 | pos[1] = ypos[i]; |
---|
34 | pos[2] = zpos[i]; |
---|
35 | addGaussian(array, dim, pos, fwhm, 10.); |
---|
36 | } |
---|
37 | |
---|
38 | int status = 0,bitpix; |
---|
39 | long *fpixel = new long[3]; |
---|
40 | for(int i=0;i<3;i++) fpixel[i]=1; |
---|
41 | fitsfile *fptr; |
---|
42 | string cubeName = "!verification/verificationCube.fits"; |
---|
43 | if( fits_create_file(&fptr,cubeName.c_str(),&status) ) |
---|
44 | fits_report_error(stderr, status); |
---|
45 | |
---|
46 | status = 0; |
---|
47 | if( fits_create_img(fptr, FLOAT_IMG, 3, dim, &status) ) |
---|
48 | fits_report_error(stderr, status); |
---|
49 | |
---|
50 | write_header_info(fptr); |
---|
51 | |
---|
52 | status = 0; |
---|
53 | if( fits_write_pix(fptr, TFLOAT, fpixel, size, array, &status) ) |
---|
54 | fits_report_error(stderr, status); |
---|
55 | |
---|
56 | status = 0; |
---|
57 | fits_close_file(fptr, &status); |
---|
58 | if (status){ |
---|
59 | std::cerr << "Error closing file: "; |
---|
60 | fits_report_error(stderr, status); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | template <class T> T min(T x1, T x2) |
---|
69 | { |
---|
70 | if(x1<x2) return x1; |
---|
71 | else return x2; |
---|
72 | } |
---|
73 | |
---|
74 | template <class T> T max(T x1, T x2) |
---|
75 | { |
---|
76 | if(x1>x2) return x1; |
---|
77 | else return x2; |
---|
78 | } |
---|
79 | |
---|
80 | void addGaussian(float *array, long *dim, float *pos, float *fwhm, float norm) |
---|
81 | { |
---|
82 | long size = dim[0] * dim[1] * dim[2]; |
---|
83 | float sigsig = fwhm[0]*fwhm[0] + fwhm[1]*fwhm[1] + fwhm[2]*fwhm[2]; |
---|
84 | |
---|
85 | int x1 = int(max(0.,pos[0]-10.)); |
---|
86 | int x2 = int(min(float(dim[0]),pos[0]+11)); |
---|
87 | int y1 = int(max(0.,pos[1]-10.)); |
---|
88 | int y2 = int(min(float(dim[1]),pos[1]+11)); |
---|
89 | int z1 = int(max(0.,pos[2]-10.)); |
---|
90 | int z2 = int(min(float(dim[2]),pos[2]+11)); |
---|
91 | |
---|
92 | for(int x=x1;x<x2;x++){ |
---|
93 | for(int y=y1;y<y2;y++){ |
---|
94 | for(int z=z1;z<z2;z++){ |
---|
95 | |
---|
96 | float distX = (x-pos[0])/fwhm[0]; |
---|
97 | float distY = (y-pos[1])/fwhm[1]; |
---|
98 | float distZ = (z-pos[2])/fwhm[2]; |
---|
99 | float gaussFlux = norm * exp( -1.*(distX*distX + |
---|
100 | distY*distY + |
---|
101 | distZ*distZ ) ); |
---|
102 | long loc = x + y*dim[0] + z*dim[0]*dim[1]; |
---|
103 | array[loc] += gaussFlux; |
---|
104 | } |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | } |
---|
109 | |
---|
110 | void write_header_info(fitsfile *fptr) |
---|
111 | { |
---|
112 | int status = 0; |
---|
113 | float val; |
---|
114 | string str; |
---|
115 | val = 0.24; |
---|
116 | fits_write_key(fptr, TFLOAT, "BMAJ" , &val, "", &status); |
---|
117 | fits_write_key(fptr, TFLOAT, "BMIN" , &val, "", &status); |
---|
118 | val = 0.; |
---|
119 | fits_write_key(fptr, TFLOAT, "BPA" , &val, "", &status); |
---|
120 | str = "JY/BEAM"; |
---|
121 | fits_write_key(fptr, TSTRING, "BUNIT" , (char *)str.c_str(), "", &status); |
---|
122 | val = 2000.0; |
---|
123 | fits_write_key(fptr, TFLOAT, "EPOCH" , &val, "", &status); |
---|
124 | val = 180.0; |
---|
125 | fits_write_key(fptr, TFLOAT, "LONPOLE", &val, "", &status); |
---|
126 | |
---|
127 | /* First axis -- RA--SIN */ |
---|
128 | str = "RA---SIN"; |
---|
129 | fits_write_key(fptr, TSTRING, "CTYPE1", (char *)str.c_str(), "", &status); |
---|
130 | val = 90.5; |
---|
131 | fits_write_key(fptr, TFLOAT, "CRVAL1", &val, "", &status); |
---|
132 | val = -6.6666701e-2; |
---|
133 | fits_write_key(fptr, TFLOAT, "CDELT1", &val, "", &status); |
---|
134 | val = 0.; |
---|
135 | fits_write_key(fptr, TFLOAT, "CROTA1", &val, "", &status); |
---|
136 | val = 50.0; |
---|
137 | fits_write_key(fptr, TFLOAT, "CRPIX1", &val, "", &status); |
---|
138 | str = "DEG"; |
---|
139 | fits_write_key(fptr, TSTRING, "CUNIT1", (char *)str.c_str(), "", &status); |
---|
140 | |
---|
141 | /* Second axis -- DEC--SIN */ |
---|
142 | str = "DEC--SIN"; |
---|
143 | fits_write_key(fptr, TSTRING, "CTYPE2", (char *)str.c_str(), "", &status); |
---|
144 | val = -26.0; |
---|
145 | fits_write_key(fptr, TFLOAT, "CRVAL2", &val, "", &status); |
---|
146 | val = 6.6666701e-2; |
---|
147 | fits_write_key(fptr, TFLOAT, "CDELT2", &val, "", &status); |
---|
148 | val = 0.; |
---|
149 | fits_write_key(fptr, TFLOAT, "CROTA2", &val, "", &status); |
---|
150 | val = 50.0; |
---|
151 | fits_write_key(fptr, TFLOAT, "CRPIX2", &val, "", &status); |
---|
152 | str = "DEG"; |
---|
153 | fits_write_key(fptr, TSTRING, "CUNIT2", (char *)str.c_str(), "", &status); |
---|
154 | |
---|
155 | /* Third axis -- VELO-HEL */ |
---|
156 | str = "VELO-HEL"; |
---|
157 | fits_write_key(fptr, TSTRING, "CTYPE3", (char *)str.c_str(), "", &status); |
---|
158 | val = 0.; |
---|
159 | fits_write_key(fptr, TFLOAT, "CRVAL3", &val, "", &status); |
---|
160 | val = 1.3191321e+4; |
---|
161 | fits_write_key(fptr, TFLOAT, "CDELT3", &val, "", &status); |
---|
162 | val = 0.; |
---|
163 | fits_write_key(fptr, TFLOAT, "CROTA3", &val, "", &status); |
---|
164 | val = 64.0; |
---|
165 | fits_write_key(fptr, TFLOAT, "CRPIX3", &val, "", &status); |
---|
166 | str = "M/S"; |
---|
167 | fits_write_key(fptr, TSTRING, "CUNIT3", (char *)str.c_str(), "", &status); |
---|
168 | |
---|
169 | val = 1.420405751786E+09; |
---|
170 | fits_write_key(fptr, TFLOAT,"RESTFREQ", &val, "", &status); |
---|
171 | |
---|
172 | |
---|
173 | } |
---|