-
Notifications
You must be signed in to change notification settings - Fork 1
/
cubestats.c
177 lines (157 loc) · 4.61 KB
/
cubestats.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
/** @file cubestats.c
*/
#include <math.h>
#include "CommandLineInterface/CLIcore.h"
#include "COREMOD_memory/COREMOD_memory.h"
// ==========================================
// Forward declaration(s)
// ==========================================
imageID info_cubestats(const char *ID_name,
const char *IDmask_name,
const char *outfname);
// ==========================================
// Command line interface wrapper function(s)
// ==========================================
errno_t info_cubestats_cli()
{
if(CLI_checkarg(1, CLIARG_IMG) + CLI_checkarg(2, CLIARG_IMG) +
CLI_checkarg(3, CLIARG_STR_NOT_IMG) ==
0)
{
info_cubestats(data.cmdargtoken[1].val.string,
data.cmdargtoken[2].val.string,
data.cmdargtoken[3].val.string);
return CLICMD_SUCCESS;
}
else
{
return CLICMD_INVALID_ARG;
}
}
// ==========================================
// Register CLI command(s)
// ==========================================
errno_t cubestats_addCLIcmd()
{
RegisterCLIcommand("cubestats",
__FILE__,
info_cubestats_cli,
"image cube stats",
"<3Dimage> <mask> <output file>",
"cubestats imc immask imc_stats.txt",
"long info_cubestats(const char *ID_name, const char "
"*IDmask_name, const char *outfname)");
return RETURN_SUCCESS;
}
// mask pixel values are 0 or 1
// prints:
// index
// min
// max
// total
// average
// tot power
// RMS
imageID info_cubestats(const char *ID_name,
const char *IDmask_name,
const char *outfname)
{
imageID ID, IDm;
float min, max, tot, tot2;
uint64_t xysize;
FILE *fp;
int init = 0;
float mtot;
float val;
int COMPUTE_CORR = 1;
long kcmax = 100;
double valn1, valn2, v1, v2, valxp, vcorr;
long k1, k2, kc;
ID = image_ID(ID_name);
if(data.image[ID].md[0].naxis != 3)
{
printf("ERROR: info_cubestats requires 3D image\n");
exit(0);
}
IDm = image_ID(IDmask_name);
xysize = data.image[ID].md[0].size[0] * data.image[ID].md[0].size[1];
mtot = 0.0;
for(unsigned long ii = 0; ii < xysize; ii++)
{
mtot += data.image[IDm].array.F[ii];
}
fp = fopen(outfname, "w");
for(unsigned long kk = 0; kk < data.image[ID].md[0].size[2]; kk++)
{
init = 0;
tot = 0.0;
tot2 = 0.0;
for(unsigned long ii = 0; ii < xysize; ii++)
{
if(data.image[IDm].array.F[ii] > 0.5)
{
val = data.image[ID].array.F[kk * xysize + ii];
if(init == 0)
{
init = 1;
min = val;
max = val;
}
if(val > max)
{
max = val;
}
if(val < min)
{
min = val;
}
tot += val;
tot2 += val * val;
}
}
fprintf(fp,
"%5ld %20f %20f %20f %20f %20f %20f\n",
kk,
min,
max,
tot,
tot / mtot,
tot2,
sqrt((tot2 - tot * tot / mtot) / mtot));
}
fclose(fp);
if(COMPUTE_CORR == 1)
{
fp = fopen("corr.txt", "w");
for(kc = 1; kc < kcmax; kc++)
{
vcorr = 0.0;
for(unsigned long kk = 0;
kk < (unsigned long)(data.image[ID].md[0].size[2] - kc);
kk++)
{
k1 = kk;
k2 = kk + kc;
valn1 = 0.0;
valn2 = 0.0;
valxp = 0.0;
for(unsigned long ii = 0; ii < xysize; ii++)
{
if(data.image[IDm].array.F[ii] > 0.5)
{
v1 = data.image[ID].array.F[k1 * xysize + ii];
v2 = data.image[ID].array.F[k2 * xysize + ii];
valn1 += v1 * v1;
valn2 += v2 * v2;
valxp += v1 * v2;
}
}
vcorr += valxp / sqrt(valn1 * valn2);
}
vcorr /= data.image[ID].md[0].size[2] - kc;
fprintf(fp, "%3ld %g\n", kc, vcorr);
}
fclose(fp);
}
return (ID);
}