-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadradecz.c
267 lines (242 loc) · 7.43 KB
/
readradecz.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "header.h"
extern gsl_rng *rngen;
//ra and dec input must be in radians already!!!
//not shifting by originpos anymore!!!
void radecztopos(double ra, double dec, float z, int unitsMpc, int angopt, cosmo_params cosmopfid, real pos[3], real *chi) {
//change -- send back the real chi for Hogg method use, even if angopt = 1.
if(unitsMpc == 1) {
(*chi) = comoving_distMpc_lcdm((double) z, cosmopfid);
}
else {
(*chi) = comoving_disthinvMpc_lcdm((double) z, cosmopfid);
}
if(angopt == 1) {
(pos)[0] = cos(dec)*cos(ra);
(pos)[1] = cos(dec)*sin(ra);
(pos)[2] = sin(dec);
} //angular clustering option.
else {
(pos)[0] = (*chi)*cos(dec)*cos(ra);
(pos)[1] = (*chi)*cos(dec)*sin(ra);
(pos)[2] = (*chi)*sin(dec);
} //end angopt = 0 (3d correlation option)
} //end radecztopos.
float getcatzmax(char *infilename, int ftype) {
FILE *ifp;
if(!(ifp = open_file_read(infilename))) {
fprintf(stderr,"Exiting!\n");
exit(1);
}
int header, linestot;
get_file_length(ifp,&header,&linestot);
double ra, dec;
float z,finalwgt;
int i;
char line[MAXLINELEN];
float myzmax;
z = -1.; //value if no input z.
for(i=0;i<header;i++) {
fgets(line,MAXLINELEN,ifp);
}
for(i=0;i<linestot-header;i++) {
switch(ftype) {
case(1):
fscanf(ifp,"%lf %lf %f %f\n",&ra,&dec,&z,&finalwgt);
break;
case(2):
fscanf(ifp,"%lf %lf %f\n",&ra,&dec,&z);
finalwgt = 1.;
break;
case(3):
fscanf(ifp,"%lf %lf %f\n",&ra,&dec,&finalwgt);
// z = 0.5*(zmin + zmax);
break;
case(4):
fscanf(ifp,"%lf %lf\n",&ra,&dec);
// z = 0.5*(zmin + zmax);
finalwgt = 1.;
break;
default:
fprintf(stderr,"your ftype is not supported. Exiting!\n");
exit(1);
break;
} //end ftype switch.
if(i==0) {
myzmax = z;
}
else {
myzmax = max(z,myzmax);
}
} //end loop to count gals and weights in redshift range.
return myzmax;
} //end getcatzmax
particle *readcat(char *infilename, int ftype, int unitsMpc, int angopt, cosmo_params cosmopfid, float zmin, float zmax, int DorR, float ndownRR, int *ntot, long double *wgttot, long double *wgtSI, real *mindist, real *maxdist) {
printf("hello beth, %d %e\n",DorR,ndownRR);
FILE *ifp;
ifp = open_file_read(infilename);
int header, linestot;
get_file_length(ifp,&header,&linestot);
double ra, dec;
float z,finalwgt;
(*wgttot) = 0.;
(*wgtSI) = 0.; //this is for the Hogg S/I cross correlation.
int i,ii;
char line[MAXLINELEN];
int ipos[3];
float catzmax = -1;
particle *gallist;
float mychimin = 20000.;
int dosubsample = 1;
if(fabs(ndownRR - 1.0) < 1.0e-3 || ndownRR < 0.) {
dosubsample = 0;
}
double ndownthresh = 1./ndownRR;
int nRmax = -1;
(*ntot) = 0;
(*wgttot) = 0.;
(*wgtSI) = 0.;
for(i=0;i<header;i++) {
fgets(line,MAXLINELEN,ifp);
}
for(i=0;i<linestot-header;i++) {
switch(ftype) {
case(1):
fscanf(ifp,"%lf %lf %f %f\n",&ra,&dec,&z,&finalwgt);
break;
case(2):
fscanf(ifp,"%lf %lf %f\n",&ra,&dec,&z);
finalwgt = 1.;
break;
case(3):
fscanf(ifp,"%lf %lf %f\n",&ra,&dec,&finalwgt);
z = 0.5*(zmin + zmax);
break;
case(4):
fscanf(ifp,"%lf %lf\n",&ra,&dec);
z = 0.5*(zmin + zmax);
finalwgt = 1.;
break;
default:
fprintf(stderr,"your ftype is not supported. Exiting!\n");
exit(1);
break;
} //end ftype switch.
catzmax = max(catzmax,z);
if(z >= zmin && z <= zmax) {
(*ntot) += 1;
}
} //end loop to count gals and weights in redshift range.
rewind(ifp);
if(dosubsample == 0) {
gallist = (particle *) malloc(sizeof(particle)*(*ntot));
}
else {
double mybuffer = 1.+30./sqrt((*ntot));
nRmax = ((int) floor(((*ntot)/ndownRR*mybuffer))) + 1;
gallist = (particle *) malloc(sizeof(particle)*nRmax);
}
if(((zmax - catzmax) > 0.1*max(zmax,catzmax)) && (angopt == 0)) {
fprintf(stderr,"You should update your zmax to be closer to the zmax of the input catalog, %f\n",catzmax);
exit(1);
}
real posmin[3];
real posmax[3];
for(ii=0;ii<=2;ii++) {
if(unitsMpc == 1) {
posmin[ii] = comoving_distMpc_lcdm((double) zmax, cosmopfid);
posmax[ii] = -comoving_distMpc_lcdm((double) zmax, cosmopfid);
}
else {
posmin[ii] = comoving_disthinvMpc_lcdm((double) zmax, cosmopfid);
posmax[ii] = -comoving_disthinvMpc_lcdm((double) zmax, cosmopfid);
}
}
real chi;
int gindx = 0;
for(i=0;i<header;i++) {
fgets(line,MAXLINELEN,ifp);
}
for(i=0;i<linestot-header;i++) {
switch(ftype) {
case(1):
fscanf(ifp,"%lf %lf %f %f\n",&ra,&dec,&z,&finalwgt);
break;
case(2):
fscanf(ifp,"%lf %lf %f\n",&ra,&dec,&z);
finalwgt = 1.;
break;
case(3):
fscanf(ifp,"%lf %lf %f\n",&ra,&dec,&finalwgt);
z = 0.5*(zmin + zmax);
break;
case(4):
fscanf(ifp,"%lf %lf\n",&ra,&dec);
z = 0.5*(zmin + zmax);
finalwgt = 1.;
break;
default:
fprintf(stderr,"your ftype is not supported. Exiting!\n");
exit(1);
break;
} //end ftype switch.
if(z >= zmin && z <= zmax) {
if(dosubsample == 1) {
//draw a random uniform number.
if(gsl_rng_uniform(rngen) > ndownthresh) {
continue;
}
}
ra = ra*M_PI/180.;
dec = dec*M_PI/180.;
radecztopos(ra,dec,z, unitsMpc, angopt, cosmopfid, gallist[gindx].pos, &chi); //chi used to be saved in gallist, now its not. and now it is again!!
mychimin = min(mychimin,chi);
gallist[gindx].chi = chi;
//void radecztopos(double ra, double dec, float z, int unitsMpc, int angopt, cosmo_params cosmopfid, real pos[3], real *chi) {
for(ii=0;ii<=2;ii++) {
//ipos[ii] = ((int) floor(gallist[gindx].pos[ii]*Ncell/Lbox));
posmin[ii] = min(posmin[ii],gallist[gindx].pos[ii]);
posmax[ii] = max(posmax[ii],gallist[gindx].pos[ii]);
}
gallist[gindx].weight = finalwgt;
(*wgttot) += finalwgt;
(*wgtSI) += finalwgt/(chi*chi);
//this is now set in the countpairs part.
//gallist[gindx].DorR = DorR;
//gallist[gindx].cell = i2n(ipos); //going to assign this in the countpairs section.
gindx += 1;
if(gindx == nRmax+1) {
printf("nR alloc error for subsample case! exiting!!\n");
exit(1);
}
}
} //end loop over gals.
if(dosubsample == 0) {
assert(gindx == (*ntot));
}
else {
assert(gindx < nRmax);
#ifdef REALLYVERBOSE
printf("downsample produced this many, this expected: %d %d %e\n",(*ntot), gindx, ((float) (*ntot))/ndownRR);
#endif
(*ntot) = gindx;
gallist = (particle *) realloc(gallist,sizeof(particle)*(*ntot));
}
fclose(ifp);
/*
for(ii=0;ii<=2;ii++) {
assert(posmin[ii] >= 0. && posmin[ii] <= Lbox);
}
*/
#ifdef REALLYVERBOSE
printf("catalog mins: %e %e %e %e %e\n",comoving_distMpc_lcdm((double) zmax, cosmopfid),comoving_disthinvMpc_lcdm((double) zmax, cosmopfid),posmin[0],posmin[1],posmin[2]);
printf("catalog maxs: %e %e %e %e %e\n",comoving_distMpc_lcdm((double) zmax, cosmopfid),comoving_disthinvMpc_lcdm((double) zmax, cosmopfid),posmax[0],posmax[1],posmax[2]);
#endif
//compute the maximum distance away to set Lbox for this data set.
(*mindist) = mychimin;
(*maxdist) = -1000.;
for(ii=0;ii<=2;ii++) {
(*maxdist) = max(fabs(posmin[ii]),(*maxdist));
(*maxdist) = max(fabs(posmax[ii]),(*maxdist));
}
return gallist;
} //end readcat