source: trunk/MagicSoft/Simulation/Detector/Starfield/starfield.cxx@ 343

Last change on this file since 343 was 343, checked in by petry, 25 years ago
One cosmetic change and a change in the naming of the cer and sta output file: the sign of the DEC is now ignored. (Have to think of something better later).
File size: 7.8 KB
Line 
1/////////////////////////////////////////////////////////////////////////
2// Starfield Generator
3//
4// (c) 2000 D. Petry
5//
6/////////////////////////////////////////////////////////////////////////
7
8
9#include "starfield.h"
10
11#define PROGRAMID "$Id: starfield.cxx,v 1.2 2000-01-21 16:39:25 petry Exp $"
12
13/////////////////////////////////////////////////////////////////////////
14
15main(int argc, char **argv)
16{
17
18 parameters pars;
19 star stars[iMAXSTARS];
20 photon photons[iMAXPHOT];
21
22 ifstream in;
23 FILE *catfile;
24 char parfilename[160];
25 char catfilename[160];
26
27
28 int i, j, k, numstars, subnumstars, starnumber, totalnumphot, photinside;
29 int totalphotinside, idum;
30 int istart_ra_h, iend_ra_h;
31 int nph[4]; // numbers of photons in the four wavebands
32 float lmin_nm[4] = {ULMIN_nm, BLMIN_nm, VLMIN_nm, RLMIN_nm}; // wave band definitions
33 float lmax_nm[4] = {ULMAX_nm, BLMAX_nm, VLMAX_nm, RLMAX_nm};
34 float theta_rad, costheta, phi_rad, randtime, lambda_nm, xdum_m, ydum_m;
35 float angdist;
36
37 //welcome
38
39 cout << "This is STARFIELD. (c) 2000 D. Petry\n";
40 cout << PROGRAMID << "\n";
41
42 // check command line arguments
43
44 if(argc == 1){
45 sprintf(parfilename, "starfield.par");
46 }
47 else{ // a filename was given
48 sprintf(parfilename, "%s", argv[1]);
49 }
50
51 // read parameter file
52
53 in.open(parfilename, ios::in); // open the parameter file
54
55 if(!in){
56 cout << "Failed to open " << parfilename << "\n"
57 << "Value of stream \"in\" was " << in << "\n";
58 cout << "\nThere shoud be a parameter file "<< parfilename
59 <<" in the working directory with the following format:\n-----\n";
60 pars.usage(&cout);
61 cout << "-----\nExiting.\n";
62 exit(1);
63 }
64
65 cout << "Opened " << parfilename << " for reading ...\n";
66
67 if( !(pars.readparameters(&in)) ){ // read not OK?
68 if(!in.eof()){ // was the error not due to EOF?
69 cout << "Error: rdstate = " << in.rdstate() << "\n";
70 }
71 else{
72 cout << "Error: premature EOF in parameter file.\n";
73 }
74 exit(1);
75 }
76
77 in.close();
78
79
80 // prepare loop over star catalog files
81
82 cout << "SKY2000 - Master Star Catalog - Star Catalog Database, Version 2\n";
83 cout << "Sande C.B., Warren W.H.Jr., Tracewell D.A., Home A.T., Miller A.C.\n";
84 cout << "<Goddard Space Flight Center, Flight Dynamics Division (1998)>\n";
85
86 angdist = fmod( pars.catalog_fov_deg / cos( pars.ct_dec_rad ), 360.);
87
88 if(angdist > 180.){ // too near to the pole, have to loop over all files
89
90 istart_ra_h = 0;
91 iend_ra_h = 23;
92
93 }
94 else{ // can loop over selected files only
95
96 istart_ra_h = (int) (pars.ct_ra_h - angdist / 360. * 24.) - 1;
97 iend_ra_h = (int) (pars.ct_ra_h + angdist / 360. * 24. ) + 1;
98
99 }
100
101 //read catalog
102
103 i = 0;
104 for (j = istart_ra_h; j <= iend_ra_h; j++){
105
106 subnumstars = 0;
107
108 if ( j < 0){
109 idum = j + 24;
110 }
111 else if ( j > 23 ){
112 idum = j - 24;
113 }
114 else {
115 idum = j;
116 }
117
118 sprintf(catfilename, "%s/sky%02d.dat", pars.datapath, idum);
119
120 if((catfile = fopen(catfilename, "r")) == NULL){ // open the star catalog
121 cout << "Failed to open " << catfilename << "\n";
122 exit(1);
123 }
124 cout << "Opened file " << catfilename << " for reading ...\n";
125
126 while( stars[i].readstar(catfile, pars.verbose) ){ // read next star OK
127
128 angdist = acos( cos( pars.ct_dec_rad ) * cos( stars[i].dec_rad ) *
129 cos( stars[i].ra_rad - pars.ct_ra_rad ) +
130 sin( pars.ct_dec_rad ) * sin( stars[i].dec_rad ) );
131
132 if( angdist < pars.catalog_fov_deg / 180. * PI ){ // star in Field Of View?
133
134 stars[i].calcmissingmags(pars.verbose);
135 if (pars.verbose) stars[i].printstar();
136
137 if( stars[i].umag > -100. ){
138 i++; // accept star
139 subnumstars++;
140 }
141 else{
142 cout << "Star rejected.\n";
143 }
144
145 if( i > iMAXSTARS ){
146 i--;
147 cout << "Error: Star memory full. Accepted " << i << " stars.\n";
148 break;
149 }
150 }
151 }
152 if( feof(catfile) ){ // was EOF reached?
153 cout << "EOF reached; accepted "<< subnumstars << " stars from this segment.\n";
154 }
155 if( ferror(catfile) ){ // did an error occur?
156 cout << "Error while reading catalog file.\n";
157 exit(1);
158 }
159 fclose(catfile);
160 if(i == iMAXSTARS){
161 break;
162 }
163 }
164
165 cout << "Accepted "<< i << " stars in total.\n";
166 numstars = i;
167
168 // loop over all photons from all stars, filling their fields
169
170 totalnumphot=0;
171
172 totalphotinside=0;
173
174 for(i=0; i<numstars;i++){
175
176 starnumber=stars[i].icatnum;
177
178 // calculate director cosines (see Montenbruck & Pfleger, 1989, p. 196)
179
180 costheta = cos( pars.ct_dec_rad ) * cos( stars[i].dec_rad ) *
181 cos( stars[i].ra_rad - pars.ct_ra_rad ) +
182 sin( pars.ct_dec_rad ) * sin( stars[i].dec_rad );
183
184 if(costheta == 0.){
185 cout << "Star number " << i << " (catalog number " << stars[i].icatnum <<
186 ") seems to be at 90 degrees distance from optical axis.\n ... will ignore it.";
187 continue;
188 }
189
190 stars[i].u = -1. * cos( stars[i].dec_rad ) * sin( stars[i].ra_rad - pars.ct_ra_rad ) / costheta;
191
192 stars[i].v = -1. * ( sin( pars.ct_dec_rad ) * cos( stars[i].dec_rad ) *
193 cos( stars[i].ra_rad - pars.ct_ra_rad ) -
194 cos( pars.ct_dec_rad ) * sin( stars[i].dec_rad )
195 ) / costheta;
196
197 // calculate the "zenith angle" theta and "azimuth" phi of the star assuming
198 // the telecope points at the zenith
199 // take into account the ambiguity of acos()
200
201 theta_rad = acos(costheta);
202
203 if( stars[i].v >= 0. ){
204 phi_rad = acos(stars[i].u);
205 }
206 else{
207 phi_rad = 2.*PI - acos(stars[i].u);
208 }
209
210 // calculate number of photons
211
212 // mag_nphot() translates the star magnitude into number of photons,
213 // using the expression log(flux)=-0.4*m-22.42 for each waveband
214 // the resulting numbers ar stored in the array nph
215
216 stars[i].mag_nphot(nph, pars.integtime_s, pars.mirr_radius_m, pars.verbose);
217
218 // loop over all photons
219
220 photinside=0;
221
222 for(k=0; k < 4; k++){ // loop over wavebands
223
224 for(j=0; j<nph[k]; j++){ // loop over photons of this waveband
225
226 // for every photon, a pair of uniform random x,y coordinates ( x*x+y*y<300 ) is generated
227 // and a uniform random arrival time inside a time window given by the integration time.
228
229 xdum_m=rand_coord(pars.mirr_radius_m);
230 ydum_m=rand_coord(pars.mirr_radius_m);
231
232 if((xdum_m*xdum_m+ydum_m*ydum_m)<pars.mirr_radius_m*pars.mirr_radius_m){
233
234 randtime = rand_time(pars.integtime_s);
235 lambda_nm = rand_lambda(lmin_nm[k], lmax_nm[k]);
236
237 //fill the photon fields by using the member functions defined in photon.hxx
238
239 photons[totalphotinside].starnum = starnumber;
240 photons[totalphotinside].arrtime_sec = randtime;
241 photons[totalphotinside].x_m = xdum_m;
242 photons[totalphotinside].y_m = ydum_m;
243 photons[totalphotinside].u = stars[i].u;
244 photons[totalphotinside].v = stars[i].v;
245 photons[totalphotinside].lambda_nm = lambda_nm;
246
247 if(pars.verbose > 2)
248 cout << "PH " << starnumber << " " << randtime << " " << xdum_m << " "
249 << ydum_m << " " << stars[i].u << " " << stars[i].v << " " << lambda_nm
250 << "\n";
251
252 photinside++;
253
254 totalphotinside++;
255
256 if(totalphotinside >= iMAXPHOT){
257 cout << "Error: photon memory full. Can only store " << iMAXPHOT << " photons.\n";
258 exit(1);
259 }
260
261 totalnumphot++;
262
263 }
264 else{
265
266 totalnumphot++;
267 continue;
268
269 } // end if
270
271 } // end photon loop
272
273 } // end waveband loop
274
275 stars[i].numphot = photinside;
276
277 if (pars.verbose) cout<<"Star number= "<<i<< " (catalog number " <<
278 starnumber << ") Number of photons accepted = "<< photinside<<endl;
279
280 } // end star loop
281
282 cout << "Total number of photons accepted = " << totalphotinside << "\n";
283
284 convertcorsika(
285 ((int)pars.ct_ra_h*10)*1000 + (int)(abs(pars.ct_dec_deg)*10), // the file id
286 totalphotinside, photons, pars.integtime_s, pars.verbose);
287
288 return 0;
289
290} // end main
Note: See TracBrowser for help on using the repository browser.