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

Last change on this file since 5077 was 432, checked in by harald, 24 years ago
An additional change in starfield.cxx was neccessary to compile the starfield adder on the alpha machines.
File size: 8.0 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.3 2000-09-21 10:31:36 harald Exp $"
12
13/////////////////////////////////////////////////////////////////////////
14
15main(int argc, char **argv)
16{
17
18 parameters pars;
19 star stars[iMAXSTARS];
20 photon *photons;
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 // Allocate memory for photons
80
81 photons=new photon[iMAXPHOT];
82
83
84 // prepare loop over star catalog files
85
86 cout << "SKY2000 - Master Star Catalog - Star Catalog Database, Version 2\n";
87 cout << "Sande C.B., Warren W.H.Jr., Tracewell D.A., Home A.T., Miller A.C.\n";
88 cout << "<Goddard Space Flight Center, Flight Dynamics Division (1998)>\n";
89
90 angdist = fmod( pars.catalog_fov_deg / cos( pars.ct_dec_rad ), 360.);
91
92 if(angdist > 180.){ // too near to the pole, have to loop over all files
93
94 istart_ra_h = 0;
95 iend_ra_h = 23;
96
97 }
98 else{ // can loop over selected files only
99
100 istart_ra_h = (int) (pars.ct_ra_h - angdist / 360. * 24.) - 1;
101 iend_ra_h = (int) (pars.ct_ra_h + angdist / 360. * 24. ) + 1;
102
103 }
104
105 //read catalog
106
107 i = 0;
108 for (j = istart_ra_h; j <= iend_ra_h; j++){
109
110 subnumstars = 0;
111
112 if ( j < 0){
113 idum = j + 24;
114 }
115 else if ( j > 23 ){
116 idum = j - 24;
117 }
118 else {
119 idum = j;
120 }
121
122 sprintf(catfilename, "%s/sky%02d.dat", pars.datapath, idum);
123
124 if((catfile = fopen(catfilename, "r")) == NULL){ // open the star catalog
125 cout << "Failed to open " << catfilename << "\n";
126 exit(1);
127 }
128 cout << "Opened file " << catfilename << " for reading ...\n";
129
130 while( stars[i].readstar(catfile, pars.verbose) ){ // read next star OK
131
132 angdist = acos( cos( pars.ct_dec_rad ) * cos( stars[i].dec_rad ) *
133 cos( stars[i].ra_rad - pars.ct_ra_rad ) +
134 sin( pars.ct_dec_rad ) * sin( stars[i].dec_rad ) );
135
136 if( angdist < pars.catalog_fov_deg / 180. * PI ){ // star in Field Of View?
137
138 stars[i].calcmissingmags(pars.verbose);
139 if (pars.verbose) stars[i].printstar();
140
141 if( stars[i].umag > -100. ){
142 i++; // accept star
143 subnumstars++;
144 }
145 else{
146 cout << "Star rejected.\n";
147 }
148
149 if( i > iMAXSTARS ){
150 i--;
151 cout << "Error: Star memory full. Accepted " << i << " stars.\n";
152 break;
153 }
154 }
155 }
156 if( feof(catfile) ){ // was EOF reached?
157 cout << "EOF reached; accepted "<< subnumstars << " stars from this segment.\n";
158 }
159 if( ferror(catfile) ){ // did an error occur?
160 cout << "Error while reading catalog file.\n";
161 exit(1);
162 }
163 fclose(catfile);
164 if(i == iMAXSTARS){
165 break;
166 }
167 }
168
169 cout << "Accepted "<< i << " stars in total.\n";
170 numstars = i;
171
172 // loop over all photons from all stars, filling their fields
173
174 totalnumphot=0;
175
176 totalphotinside=0;
177
178 for(i=0; i<numstars;i++){
179
180 starnumber=stars[i].icatnum;
181
182 // calculate director cosines (see Montenbruck & Pfleger, 1989, p. 196)
183
184 costheta = cos( pars.ct_dec_rad ) * cos( stars[i].dec_rad ) *
185 cos( stars[i].ra_rad - pars.ct_ra_rad ) +
186 sin( pars.ct_dec_rad ) * sin( stars[i].dec_rad );
187
188 if(costheta == 0.){
189 cout << "Star number " << i << " (catalog number " << stars[i].icatnum <<
190 ") seems to be at 90 degrees distance from optical axis.\n ... will ignore it.";
191 continue;
192 }
193
194 stars[i].u = -1. * cos( stars[i].dec_rad ) * sin( stars[i].ra_rad - pars.ct_ra_rad ) / costheta;
195
196 stars[i].v = -1. * ( sin( pars.ct_dec_rad ) * cos( stars[i].dec_rad ) *
197 cos( stars[i].ra_rad - pars.ct_ra_rad ) -
198 cos( pars.ct_dec_rad ) * sin( stars[i].dec_rad )
199 ) / costheta;
200
201 // calculate the "zenith angle" theta and "azimuth" phi of the star assuming
202 // the telecope points at the zenith
203 // take into account the ambiguity of acos()
204
205 theta_rad = acos(costheta);
206
207 if( stars[i].v >= 0. ){
208 phi_rad = acos(stars[i].u);
209 }
210 else{
211 phi_rad = 2.*PI - acos(stars[i].u);
212 }
213
214 // calculate number of photons
215
216 // mag_nphot() translates the star magnitude into number of photons,
217 // using the expression log(flux)=-0.4*m-22.42 for each waveband
218 // the resulting numbers ar stored in the array nph
219
220 stars[i].mag_nphot(nph, pars.integtime_s, pars.mirr_radius_m, pars.verbose);
221
222 // loop over all photons
223
224 photinside=0;
225
226 for(k=0; k < 4; k++){ // loop over wavebands
227
228 for(j=0; j<nph[k]; j++){ // loop over photons of this waveband
229
230 // Check if we have overflowed the alllowed ph number
231
232 if(totalphotinside >= iMAXPHOT){
233 cout << "Warning: photon memory full. Can only store " << iMAXPHOT << " photons.\n";
234 break;
235 //exit(1);
236 }
237
238 // for every photon, a pair of uniform random x,y coordinates ( x*x+y*y<300 ) is generated
239 // and a uniform random arrival time inside a time window given by the integration time.
240
241 xdum_m=rand_coord(pars.mirr_radius_m);
242 ydum_m=rand_coord(pars.mirr_radius_m);
243
244 if((xdum_m*xdum_m+ydum_m*ydum_m)<pars.mirr_radius_m*pars.mirr_radius_m){
245
246 randtime = rand_time(pars.integtime_s);
247 lambda_nm = rand_lambda(lmin_nm[k], lmax_nm[k]);
248
249 //fill the photon fields by using the member functions defined in photon.hxx
250
251 photons[totalphotinside].starnum = starnumber;
252 photons[totalphotinside].arrtime_sec = randtime;
253 photons[totalphotinside].x_m = xdum_m;
254 photons[totalphotinside].y_m = ydum_m;
255 photons[totalphotinside].u = stars[i].u;
256 photons[totalphotinside].v = stars[i].v;
257 photons[totalphotinside].lambda_nm = lambda_nm;
258
259 if(pars.verbose > 2)
260 cout << "PH " << starnumber << " " << randtime << " " << xdum_m << " "
261 << ydum_m << " " << stars[i].u << " " << stars[i].v << " " << lambda_nm
262 << "\n";
263
264 photinside++;
265
266 totalphotinside++;
267
268 totalnumphot++;
269
270 }
271 else{
272
273 totalnumphot++;
274 continue;
275
276 } // end if
277
278 } // end photon loop
279 } // end waveband loop
280
281 stars[i].numphot = photinside;
282 //if (i>81) break;
283 if (pars.verbose) cout<<"Star number= "<<i<< " (catalog number " <<
284 starnumber << ") Number of photons accepted = "<< photinside<<endl;
285
286 } // end star loop
287
288 cout << "Total number of photons accepted = " << totalphotinside << "\n";
289
290 convertcorsika(
291 ((int)pars.ct_ra_h*10)*1000 + (int)(abs(pars.ct_dec_deg)*10), // the file id
292 totalphotinside, photons, pars.integtime_s, pars.verbose, pars.output_file);
293
294 return 0;
295
296} // end main
Note: See TracBrowser for help on using the repository browser.