-
Notifications
You must be signed in to change notification settings - Fork 7
/
ioutils.c
188 lines (162 loc) · 5.36 KB
/
ioutils.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
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <mpi.h>
#include <gsl/gsl_sort_long.h>
#include <ctype.h>
#include <string.h>
#include "raytrace.h"
//routine to do case insens. string comp.
int strcmp_caseinsens(const char *s1, const char *s2)
{
int N,i,equal;
if(strlen(s1) != strlen(s2))
return 1;
N = strlen(s1);
#ifdef DEBUG
#if DEBUG_LEVEL > 2
if(ThisTask == 0)
fprintf(stderr,"s1 = '%s', s2 = '%s'\n",s1,s2);
#endif
#endif
equal = 0;
for(i=0;i<N;++i)
{
#ifdef DEBUG
#if DEBUG_LEVEL > 2
if(ThisTask == 0)
fprintf(stderr,"s1[i] = '%c', s2[i] = '%c'\n",tolower(s1[i]),tolower(s2[i]));
#endif
#endif
if(tolower(s1[i]) != tolower(s2[i]))
{
equal = 1;
break;
}
}
#ifdef DEBUG
#if DEBUG_LEVEL > 2
if(ThisTask == 0)
fprintf(stderr,"equal %d (0 is true, 1 is false, weird)\n",equal);
#endif
#endif
return equal;
}
/* do M to N on peano inds for reading lens planes */
void getPeanoIndsToReadFromFile(long HEALPixOrder, long *PeanoIndsToRead, long NumPeanoIndsToRead,
long FileHEALPixOrder, long **FilePeanoIndsToRead, long *NumFilePeanoIndsToRead)
{
/********************** get inds to read from the file ********************
* If the order of the file differs from the order of the requested cells, then we either
* 1) have to read in all cells in file which are in the requested cells
* 2) read in the cell in the file which contains the requested cell and then cull the particles
*/
long FileNPix = order2npix(FileHEALPixOrder);
long i,j,BaseNest,FileNest,OrderDiff,ind;
long NinPix,Nextra=1000,*tmp;
*FilePeanoIndsToRead = (long*)malloc(sizeof(long)*FileNPix);
assert(*FilePeanoIndsToRead != NULL);
*NumFilePeanoIndsToRead = FileNPix;
if(FileHEALPixOrder > HEALPixOrder) /* Case #1 above: file cells are smaller, so just need to figure out which file cells are in the large cells we have requested */
{
OrderDiff = FileHEALPixOrder - HEALPixOrder;
NinPix = 1;
NinPix = (1 << (2*OrderDiff));
ind = 0;
for(i=0;i<NumPeanoIndsToRead;++i)
{
BaseNest = peano2nest(PeanoIndsToRead[i],HEALPixOrder);
FileNest = (BaseNest << (2*OrderDiff));
for(j=0;j<NinPix;++j)
{
(*FilePeanoIndsToRead)[ind] = nest2peano(FileNest+j,FileHEALPixOrder);
++ind;
if(ind >= (*NumFilePeanoIndsToRead))
{
tmp = (long*)realloc(*FilePeanoIndsToRead,sizeof(long)*((*NumFilePeanoIndsToRead)+Nextra));
assert(tmp != NULL);
*FilePeanoIndsToRead = tmp;
(*NumFilePeanoIndsToRead) = (*NumFilePeanoIndsToRead) + Nextra;
}
}
}
*NumFilePeanoIndsToRead = ind;
tmp = (long*)realloc(*FilePeanoIndsToRead,sizeof(long)*(*NumFilePeanoIndsToRead));
assert(tmp != NULL);
*FilePeanoIndsToRead = tmp;
}
else if(FileHEALPixOrder < HEALPixOrder) /* Case #2 above: file cells are larger, so need to convert requested cells to file cells */
{
OrderDiff = HEALPixOrder - FileHEALPixOrder;
ind = 0;
for(i=0;i<NumPeanoIndsToRead;++i)
{
BaseNest = peano2nest(PeanoIndsToRead[i],HEALPixOrder);
FileNest = BaseNest >> (2*OrderDiff);
(*FilePeanoIndsToRead)[ind] = nest2peano(FileNest,FileHEALPixOrder);
++ind;
if(ind >= (*NumFilePeanoIndsToRead))
{
tmp = (long*)realloc(*FilePeanoIndsToRead,sizeof(long)*((*NumFilePeanoIndsToRead)+Nextra));
assert(tmp != NULL);
*FilePeanoIndsToRead = tmp;
(*NumFilePeanoIndsToRead) = (*NumFilePeanoIndsToRead) + Nextra;
}
}
*NumFilePeanoIndsToRead = ind;
tmp = (long*)realloc(*FilePeanoIndsToRead,sizeof(long)*(*NumFilePeanoIndsToRead));
assert(tmp != NULL);
*FilePeanoIndsToRead = tmp;
}
else /* order is the same, so just copy the inds */
{
*NumFilePeanoIndsToRead = NumPeanoIndsToRead;
*FilePeanoIndsToRead = (long*)realloc(*FilePeanoIndsToRead,sizeof(long)*(*NumFilePeanoIndsToRead));
assert(*FilePeanoIndsToRead != NULL);
for(j=0;j<NumPeanoIndsToRead;++j)
(*FilePeanoIndsToRead)[j] = PeanoIndsToRead[j];
}
/* remove duplicates */
gsl_sort_long(*FilePeanoIndsToRead,(size_t) 1,(size_t) (*NumFilePeanoIndsToRead));
ind = 1;
for(j=1;j<(*NumFilePeanoIndsToRead);++j)
{
if((*FilePeanoIndsToRead)[j] != (*FilePeanoIndsToRead)[ind-1])
{
(*FilePeanoIndsToRead)[ind] = (*FilePeanoIndsToRead)[j];
++ind;
}
}
*NumFilePeanoIndsToRead = ind;
tmp = (long*)realloc(*FilePeanoIndsToRead,sizeof(long)*(*NumFilePeanoIndsToRead));
assert(tmp != NULL);
*FilePeanoIndsToRead = tmp;
}
/*gets the number of lines in a file*/
long fnumlines(FILE *fp)
{
long i=-1;
char c[5000];
while(!feof(fp))
{
++i;
fgets(c,5000,fp);
}
rewind(fp);
return i;
}
FILE *fopen_retry(const char *filename, const char *mode)
{
int try,Ntry = 10;
FILE *fp;
//try Ntry times, if opens, return fp
for(try=0;try<Ntry;++try)
{
fp = fopen(filename,mode);
if(fp != NULL)
return fp;
}
//if we get to here, return NULL
return NULL;
}