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

Last change on this file was 1350, checked in by MatthewWhiting, 10 years ago

Getting an include statement right.

File size: 6.1 KB
Line 
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 <duchamp/Utils/utils.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>
37using std::string;
38
39void addGaussian(float *array, long *dim, float *pos, float *fwhm, float norm);
40void write_header_info(fitsfile *fptr);
41int 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
94void addGaussian(float *array, long *dim, float *pos, float *fwhm, float norm)
95{
96  long size = dim[0] * dim[1] * dim[2];
97  float sigma[3];
98  for(int i=0;i<3;i++) sigma[i] = fwhm[i] / (2 *sqrt(2*log(2)));
99  float sigsig = sigma[0]*sigma[0] + sigma[1]*sigma[1] + sigma[2]*sigma[2];
100
101  int x1 = int(std::max(0.,pos[0]-10.));
102  int x2 = int(std::min(float(dim[0]),pos[0]+11));
103  int y1 = int(std::max(0.,pos[1]-10.));
104  int y2 = int(std::min(float(dim[1]),pos[1]+11));
105  int z1 = int(std::max(0.,pos[2]-10.));
106  int z2 = int(std::min(float(dim[2]),pos[2]+11));
107
108  for(int x=x1;x<x2;x++){
109    for(int y=y1;y<y2;y++){
110      for(int z=z1;z<z2;z++){
111       
112        float distX = (x-pos[0])/sigma[0];
113        float distY = (y-pos[1])/sigma[1];
114        float distZ = (z-pos[2])/sigma[2];
115        float gaussFlux = norm * exp( -0.5*(distX*distX +
116                                            distY*distY +
117                                            distZ*distZ  ) );
118        long loc = x + y*dim[0] + z*dim[0]*dim[1];
119        array[loc] += gaussFlux;
120      }
121    }
122  }
123
124}
125
126void write_header_info(fitsfile *fptr)
127{
128  int status = 0;
129  float val;
130  string str;
131  val = 0.24;
132  fits_write_key(fptr, TFLOAT,  "BMAJ"  , &val, "", &status);
133  fits_write_key(fptr, TFLOAT,  "BMIN"  , &val, "", &status);
134  val = 0.;
135  fits_write_key(fptr, TFLOAT,  "BPA"   , &val, "", &status);
136  str = "JY/BEAM";
137  fits_write_key(fptr, TSTRING, "BUNIT" , (char *)str.c_str(), "", &status);
138  val = 2000.0;
139  fits_write_key(fptr, TFLOAT,  "EPOCH" , &val, "", &status);
140  val = 180.0;
141  fits_write_key(fptr, TFLOAT, "LONPOLE", &val, "", &status);
142
143  /* First axis -- RA--SIN */
144  str = "RA---SIN";
145  fits_write_key(fptr, TSTRING, "CTYPE1", (char *)str.c_str(), "", &status);
146  val = 90.5;
147  fits_write_key(fptr, TFLOAT,  "CRVAL1", &val, "", &status);
148  val = -6.6666701e-2;
149  fits_write_key(fptr, TFLOAT,  "CDELT1", &val, "", &status);
150  val = 0.;
151  fits_write_key(fptr, TFLOAT,  "CROTA1", &val, "", &status);
152  val = 50.0;
153  fits_write_key(fptr, TFLOAT,  "CRPIX1", &val, "", &status);
154  str = "DEG";
155  fits_write_key(fptr, TSTRING, "CUNIT1", (char *)str.c_str(), "", &status);
156 
157  /* Second axis -- DEC--SIN */
158  str = "DEC--SIN";
159  fits_write_key(fptr, TSTRING, "CTYPE2", (char *)str.c_str(), "", &status);
160  val = -26.0;
161  fits_write_key(fptr, TFLOAT,  "CRVAL2", &val, "", &status);
162  val = 6.6666701e-2;
163  fits_write_key(fptr, TFLOAT,  "CDELT2", &val, "", &status);
164  val = 0.;
165  fits_write_key(fptr, TFLOAT,  "CROTA2", &val, "", &status);
166  val = 50.0;
167  fits_write_key(fptr, TFLOAT,  "CRPIX2", &val, "", &status);
168  str = "DEG";
169  fits_write_key(fptr, TSTRING, "CUNIT2", (char *)str.c_str(), "", &status);
170 
171  /* Third axis -- VELO-HEL */
172  str = "VELO-HEL";
173  fits_write_key(fptr, TSTRING, "CTYPE3", (char *)str.c_str(), "", &status);
174  val = 0.;
175  fits_write_key(fptr, TFLOAT,  "CRVAL3", &val, "", &status);
176  val = 1.3191321e+4;
177  fits_write_key(fptr, TFLOAT,  "CDELT3", &val, "", &status);
178  val = 0.;
179  fits_write_key(fptr, TFLOAT,  "CROTA3", &val, "", &status);
180//   val = 64.0;
181  val = 0.0;
182  fits_write_key(fptr, TFLOAT,  "CRPIX3", &val, "", &status);
183  str = "M/S";
184  fits_write_key(fptr, TSTRING, "CUNIT3", (char *)str.c_str(), "", &status);
185
186  val = 1.420405751786E+09;
187  fits_write_key(fptr, TFLOAT,"RESTFREQ", &val, "", &status);
188
189
190}
Note: See TracBrowser for help on using the repository browser.