This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cimtest.c
331 lines (310 loc) · 8.56 KB
/
cimtest.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "fitsio.h"
#include "jsfitsio.h"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#define EMDIR "/mydir"
#endif
/*
cimtest img2d.fits processes entire 2D image
cimtest cube3d.fits processes slice 0 of 3D cube
cimtest -c "*:*:1234" cube3d.fits processes slice 1234 of 3D cube
cimtest -c "*:*:all" cube3d.fits processes entire 3D cube
*/
#define EXTLIST "EVENTS STDEVT"
#define IMDIM 2048
#define NDIM 4
void errchk(int status) {
if (status){
fits_report_error(stderr, status);
exit(status);
}
}
char *FileContents(char *path, size_t *osize)
{
FILE *fd;
char *tbuf;
struct stat buf;
int get;
int got;
/* start pessimisticly */
if( osize != NULL )
*osize = 0;
/* make sure the file exists */
if( stat(path, &buf) <0 ){
return(NULL);
}
/* open the file */
if( (fd=fopen(path, "r")) == NULL ){
return(NULL);
}
get = buf.st_size;
/* get contents */
tbuf = (char *)malloc(get+1);
got = fread(tbuf, sizeof(char), get, fd);
fclose(fd);
tbuf[got] = '\0';
/* user may want to know how much was read */
if( osize != NULL )
*osize = got;
return(tbuf);
}
void imstat(void *buf, int idim1, int idim2, int bitpix, int n){
int ii, totpix;
double sum = 0.0, meanval = 0.0, minval = 1.E33, maxval = -1.E33;
char *cbuf;
short *sbuf;
unsigned short *usbuf;
int *ibuf;
long long *lbuf;
float *fbuf;
double *dbuf;
/* process image section */
totpix = idim1 * idim2;
for (ii = 0; ii < totpix; ii++) {
// allocate space for the pixel array
switch(bitpix){
case 8:
cbuf = (char *)buf;
sum += cbuf[ii];
if (cbuf[ii] < minval) minval = cbuf[ii]; /* find min and */
if (cbuf[ii] > maxval) maxval = cbuf[ii]; /* max values */
break;
case 16:
sbuf = (short *)buf;
sum += sbuf[ii];
if (sbuf[ii] < minval) minval = sbuf[ii]; /* find min and */
if (sbuf[ii] > maxval) maxval = sbuf[ii]; /* max values */
break;
case -16:
usbuf = (unsigned short *)buf;
sum += usbuf[ii];
if (usbuf[ii] < minval) minval = usbuf[ii]; /* find min and */
if (usbuf[ii] > maxval) maxval = usbuf[ii]; /* max values */
break;
case 32:
ibuf = (int *)buf;
sum += ibuf[ii];
if (ibuf[ii] < minval) minval = ibuf[ii]; /* find min and */
if (ibuf[ii] > maxval) maxval = ibuf[ii]; /* max values */
break;
case 64:
lbuf = (long long *)buf;
sum += lbuf[ii];
if (lbuf[ii] < minval) minval = lbuf[ii]; /* find min and */
if (lbuf[ii] > maxval) maxval = lbuf[ii]; /* max values */
break;
case -32:
fbuf = (float *)buf;
sum += fbuf[ii];
if (fbuf[ii] < minval) minval = fbuf[ii]; /* find min and */
if (fbuf[ii] > maxval) maxval = fbuf[ii]; /* max values */
break;
case -64:
dbuf = (double *)buf;
sum += dbuf[ii];
if (dbuf[ii] < minval) minval = dbuf[ii]; /* find min and */
if (dbuf[ii] > maxval) maxval = dbuf[ii]; /* max values */
break;
default:
return;
}
}
if (totpix > 0) meanval = sum / totpix;
if( n >= 0 ){
if( n == 0 ){
printf("imstat: %d x %d [bitpix %d] image\n", idim1, idim2, bitpix);
}
printf("%4d: sum=%.3f mean=%.3f min=%.3f max=%.3f\n",
n+1, sum, meanval, minval, maxval);
} else {
printf("imstat for %d x %d [bitpix %d] image\n", idim1, idim2, bitpix);
printf(" sum of pixels = %.3f\n", sum);
printf(" mean value = %.3f\n", meanval);
printf(" minimum value = %.3f\n", minval);
printf(" maximum value = %.3f\n", maxval);
}
}
int main(int argc, char *argv[])
{
char *colname = "X Y";
char *ifile = NULL;
char *ifilter=NULL;
char *tfilter=NULL;
char *tfilt=NULL;
char *imem=NULL;
char *cardstr=NULL;
char *cube=NULL;
char tbuf[81];
int c, i, args, hdutype, ncard, ioff;
int domem = 0;
int dohdr = 0;
int status = 0; /* CFITSIO status value MUST be initialized to zero! */
int binMode = 0;
int start[NDIM];
int stop[NDIM];
int dims[] = {IMDIM, IMDIM};
double bin = 1;
void *buf;
int idim1, idim2, idim3, bitpix;
size_t ilen=0;
fitsfile *fptr, *ofptr;
/* we want the args in the same order in which they arrived, and
gnu getopt sometimes changes things without this */
putenv("POSIXLY_CORRECT=true");
// mount the current folder as a NODEFS instance
// inside of emscripten
#ifdef NODEJS
EM_ASM(
FS.mkdir('/mydir');
FS.mount(NODEFS, { root: '.' }, '/mydir');
);
#endif
/* process switch arguments */
while ((c = getopt(argc, argv, "b:c:hm")) != -1){
switch(c){
case 'b':
bin = atof(optarg);
break;
case 'c':
cube = optarg;
break;
case 'h':
dohdr = 1;
break;
case 'm':
domem = 1;
break;
}
}
// filename is required, filter is optional
args = argc - optind;
if( args < 1 ){
ifile = strdup("test/snr.ev.gz");
} else {
// emscripten: if path is relative, make it relative to the virtual dir
#if NODEJS
if( argv[optind + 0][0] == '/' ){
ifile = strdup(argv[optind + 0]);
} else {
ifile = malloc(strlen(EMDIR) + 1 + strlen(argv[optind + 0]) + 1);
strcpy(ifile, EMDIR);
strcat(ifile, "/");
strcat(ifile, argv[optind + 0]);
}
#else
ifile = strdup(argv[optind + 0]);
#endif
}
// check for a filter
if( args > 1 ){
ifilter = argv[optind + 1];
}
if( domem ){
// read and open fits file in memory for reading, go to a useful HDU
imem = FileContents(ifile, &ilen);
fptr = openFITSMem((void **)&imem, &ilen, EXTLIST, NULL, &hdutype, &status);
} else {
// open fits file for reading, go to a useful HDU
fptr = openFITSFile(ifile, READONLY, EXTLIST, NULL, &hdutype, &status);
}
errchk(status);
fprintf(stdout, "File: %s\n", ifile);
// display header, if necessary
if( dohdr ){
// get cards as a string
getHeaderToString(fptr, &cardstr, &ncard, &status);
errchk(status);
if( ncard && cardstr ){
// print cards individually to make it easier to diff
fprintf(stdout, "Cards [%d]:\n", ncard);
tbuf[80] = '\0';
for(i=0; i<ncard; i++){
memcpy(tbuf, &cardstr[i*80], 80);
fprintf(stdout, "%s\n", tbuf);
}
free(cardstr);
}
}
// process based on hdu type
switch(hdutype){
case IMAGE_HDU:
fprintf(stdout, "HDU: image\n");
// get image array
buf = getImageToArray(fptr, NULL, NULL, bin, binMode, cube, NULL,
start, stop, &bitpix, &status);
idim1 = stop[0] - start[0] + 1;
idim2 = stop[1] - start[1] + 1;
if( cube && strstr(cube, "all") ){
idim3 = stop[2] - start[2] + 1;
} else {
idim3 = 1;
}
errchk(status);
// size in bytes of one slice
ioff = idim1 * idim2 * abs(bitpix)/8;
// image statistics on image section
for(i=0; i<idim3; i++){
imstat(buf+(i * ioff), idim1, idim2, bitpix, i);
}
// clean up
free(buf);
break;
default:
fprintf(stdout, "HDU: binary table\n");
// image statistics through a filter
if( ifilter && *ifilter ){
tfilter = (char *)strdup(ifilter);
for(tfilt=(char *)strtok(tfilter, ";"); tfilt != NULL;
tfilt=(char *)strtok(NULL, ";")){
// image from table
fprintf(stdout, "Filter: %s\n", tfilt);
ofptr = filterTableToImage(fptr, tfilt, colname, dims, NULL, 1, NULL,
&status);
errchk(status);
// get image array
buf = getImageToArray(ofptr, NULL, NULL, bin, binMode, cube, NULL,
start, stop, &bitpix, &status);
errchk(status);
idim1 = stop[0] - start[0] + 1;
idim2 = stop[1] - start[1] + 1;
// image statistics on image section
imstat(buf, idim1, idim2, bitpix, -1);
// clean up
free(buf);
closeFITSFile(ofptr, &status);
errchk(status);
}
} else {
// image from table
ofptr = filterTableToImage(fptr, NULL, colname, dims, NULL, 1, NULL,
&status);
errchk(status);
// get image array
buf = getImageToArray(ofptr, dims, NULL, bin, binMode, cube, NULL,
start, stop, &bitpix, &status);
errchk(status);
idim1 = stop[0] - start[0] + 1;
idim2 = stop[1] - start[1] + 1;
// image statistics on image section
imstat(buf, idim1, idim2, bitpix, -1);
// clean up
free(buf);
closeFITSFile(ofptr, &status);
errchk(status);
}
free(tfilter);
break;
}
// close fits file
closeFITSFile(fptr, &status);
errchk(status);
// clean up
if( imem ) free(imem);
if( ifile ) free(ifile);
return(0);
}