source: tags/release-1.1.12/verification/createTestImage.cc

Last change on this file was 784, checked in by MatthewWhiting, 13 years ago

Using std::min & std::max!

File size: 4.7 KB
Line 
1#include <iostream>
2#include <stdlib.h>
3#include <time.h>
4#include <math.h>
5#include <Devel/devel.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>
10using std::string;
11
12void addGaussian(float *array, long *dim, float *pos, float *fwhm, float norm);
13void write_header_info(fitsfile *fptr);
14int 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
67void addGaussian(float *array, long *dim, float *pos, float *fwhm, float norm)
68{
69  long size = dim[0] * dim[1] * dim[2];
70  float sigsig = fwhm[0]*fwhm[0] + fwhm[1]*fwhm[1] + fwhm[2]*fwhm[2];
71
72  int x1 = int(std::max(0.,pos[0]-10.));
73  int x2 = int(std::min(float(dim[0]),pos[0]+11));
74  int y1 = int(std::max(0.,pos[1]-10.));
75  int y2 = int(std::min(float(dim[1]),pos[1]+11));
76  int z1 = int(std::max(0.,pos[2]-10.));
77  int z2 = int(std::min(float(dim[2]),pos[2]+11));
78
79  for(int x=x1;x<x2;x++){
80    for(int y=y1;y<y2;y++){
81      for(int z=z1;z<z2;z++){
82       
83        float distX = (x-pos[0])/fwhm[0];
84        float distY = (y-pos[1])/fwhm[1];
85        float distZ = (z-pos[2])/fwhm[2];
86        float gaussFlux = norm * exp( -1.*(distX*distX +
87                                           distY*distY +
88                                           distZ*distZ  ) );
89        long loc = x + y*dim[0] + z*dim[0]*dim[1];
90        array[loc] += gaussFlux;
91      }
92    }
93  }
94
95}
96
97void write_header_info(fitsfile *fptr)
98{
99  int status = 0;
100  float val;
101  string str;
102  val = 0.24;
103  fits_write_key(fptr, TFLOAT,  "BMAJ"  , &val, "", &status);
104  fits_write_key(fptr, TFLOAT,  "BMIN"  , &val, "", &status);
105  val = 0.;
106  fits_write_key(fptr, TFLOAT,  "BPA"   , &val, "", &status);
107  str = "JY/BEAM";
108  fits_write_key(fptr, TSTRING, "BUNIT" , (char *)str.c_str(), "", &status);
109  val = 2000.0;
110  fits_write_key(fptr, TFLOAT,  "EPOCH" , &val, "", &status);
111  val = 180.0;
112  fits_write_key(fptr, TFLOAT, "LONPOLE", &val, "", &status);
113
114  /* First axis -- RA--SIN */
115  str = "RA---SIN";
116  fits_write_key(fptr, TSTRING, "CTYPE1", (char *)str.c_str(), "", &status);
117  val = 90.5;
118  fits_write_key(fptr, TFLOAT,  "CRVAL1", &val, "", &status);
119  val = -6.6666701e-2;
120  fits_write_key(fptr, TFLOAT,  "CDELT1", &val, "", &status);
121  val = 0.;
122  fits_write_key(fptr, TFLOAT,  "CROTA1", &val, "", &status);
123  val = 50.0;
124  fits_write_key(fptr, TFLOAT,  "CRPIX1", &val, "", &status);
125  str = "DEG";
126  fits_write_key(fptr, TSTRING, "CUNIT1", (char *)str.c_str(), "", &status);
127 
128  /* Second axis -- DEC--SIN */
129  str = "DEC--SIN";
130  fits_write_key(fptr, TSTRING, "CTYPE2", (char *)str.c_str(), "", &status);
131  val = -26.0;
132  fits_write_key(fptr, TFLOAT,  "CRVAL2", &val, "", &status);
133  val = 6.6666701e-2;
134  fits_write_key(fptr, TFLOAT,  "CDELT2", &val, "", &status);
135  val = 0.;
136  fits_write_key(fptr, TFLOAT,  "CROTA2", &val, "", &status);
137  val = 50.0;
138  fits_write_key(fptr, TFLOAT,  "CRPIX2", &val, "", &status);
139  str = "DEG";
140  fits_write_key(fptr, TSTRING, "CUNIT2", (char *)str.c_str(), "", &status);
141 
142  /* Third axis -- VELO-HEL */
143  str = "VELO-HEL";
144  fits_write_key(fptr, TSTRING, "CTYPE3", (char *)str.c_str(), "", &status);
145  val = 0.;
146  fits_write_key(fptr, TFLOAT,  "CRVAL3", &val, "", &status);
147  val = 1.3191321e+4;
148  fits_write_key(fptr, TFLOAT,  "CDELT3", &val, "", &status);
149  val = 0.;
150  fits_write_key(fptr, TFLOAT,  "CROTA3", &val, "", &status);
151//   val = 64.0;
152  val = 0.0;
153  fits_write_key(fptr, TFLOAT,  "CRPIX3", &val, "", &status);
154  str = "M/S";
155  fits_write_key(fptr, TSTRING, "CUNIT3", (char *)str.c_str(), "", &status);
156
157  val = 1.420405751786E+09;
158  fits_write_key(fptr, TFLOAT,"RESTFREQ", &val, "", &status);
159
160
161}
Note: See TracBrowser for help on using the repository browser.