This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imquadfit.cl
226 lines (200 loc) · 9.53 KB
/
imquadfit.cl
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
# IMQUADFIT -- perform a pixel by pixel, polynomial least squares fit
# to a set of images.
#
# This is a modified version of imlinfit.cl
#
#
# The function is: y = a + b*x + c*x**2
#
#
# UPPERCASE are coadded images, lowercase are scalars.
procedure poly (images, coefa, coefb, coefc)
string images {prompt="Images to fit"}
string coefa {prompt="Output coefficient a image"}
string coefb {prompt="Output coefficient b image"}
string coefc {prompt="Output coefficient c image"}
string keyword = " " {prompt="Keyword for x values"}
struct *ilist
struct *xlist
begin
string img, coa, cob, coc, sumy, sumxy, sumx2y
string sumx, sumx2, sumx3, sumx4
string ifile, xfile, x2file, x3file, tfile, tmp1, tmp2
string tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9
string tmp10, tmp11, tmp12, tmp13, tmp14, x4file
real x, delta1, delta2, delta3, delta
real sumpx = 0.0
real sumpx2 = 0.0
real sumpx3 = 0.0
real sumpx4 = 0.0
int nimg = 0
# Make names for temporary images
sumy = mktemp ("tmp$imf")
sumxy = mktemp ("tmp$imf")
sumx2y = mktemp ("tmp$imf")
sumx = mktemp ("tmp$imf")
sumx2 = mktemp ("tmp$imf")
sumx3 = mktemp ("tmp$imf")
sumx4 = mktemp ("tmp$imf")
tmp1 = mktemp ("tmp$imf")
tmp2 = mktemp ("tmp$imf")
tmp3 = mktemp ("tmp$imf")
tmp4 = mktemp ("tmp$imf")
tmp5 = mktemp ("tmp$imf")
tmp6 = mktemp ("tmp$imf")
tmp7 = mktemp ("tmp$imf")
tmp8 = mktemp ("tmp$imf")
tmp9 = mktemp ("tmp$imf")
tmp10 = mktemp ("tmp$imf")
tmp11 = mktemp ("tmp$imf")
tmp12 = mktemp ("tmp$imf")
tmp13 = mktemp ("tmp$imf")
tmp14 = mktemp ("tmp$imf")
# Make names for temporary files
ifile = mktemp ("tmp$imf")
tfile = mktemp ("tmp$imf")
xfile = mktemp ("tmp$imf")
x2file = mktemp ("tmp$imf")
x3file = mktemp ("tmp$imf")
x4file = mktemp ("tmp$imf")
# Prompt for the images and expand the template
sections (images, option="fullname", > ifile)
# Prompt for the other query parameters
coa = coefa
cob = coefb
coc = coefc
# Open the
ilist = ifile; xlist = xfile
while (fscan (ilist, img) != EOF) {
# Temporary images, 1 per input image
print (mktemp ("tmp$imf"), >> tfile)
hselect (img, keyword, yes, >> xfile)
if (fscan (xlist, x) != 1)
error (1, "Keyword `" // keyword // "' not found in " // img)
print (x*x, >> x2file)
print (x**3, >> x3file)
print (x**4, >> x4file)
sumpx += x
sumpx2 += x*x
sumpx3 += x*x*x
sumpx4 += x*x*x*x
nimg += 1
}
ilist = ""; xlist = ""
delta1 = sumpx2*(sumpx*sumpx3 - sumpx2*sumpx2)
delta2 = sumpx3*(nimg*sumpx3 - sumpx2*sumpx)
delta3 = sumpx4*(nimg*sumpx2 - sumpx*sumpx)
delta = delta1 - delta2 + delta3
# Calculate SUMY
imsum ("@" // ifile, sumy, option="sum", pixtype="real",
calctype="real")
# Calculate SUMXY
imarith ("@" // ifile, "*", "@" // xfile, "@" // tfile,
pixtype="real", calctype="real", verbose-, noact-)
imsum ("@" // tfile, sumxy, option="sum", pixtype="real",
calctype="real")
imdelete ("@" // tfile, ver-, >& "dev$null")
# Calculate SUMX2Y
imarith ("@" // ifile, "*", "@" // x2file, "@" // tfile,
pixtype="real", calctype="real", verbose-, noact-)
imsum ("@" // tfile, sumx2y, option="sum", pixtype="real",
calctype="real")
imdelete ("@" // tfile, ver-, >& "dev$null")
# Calculate SUMX
imarith ("@" // ifile, "/", "@" // ifile, "@" // tfile,
pixtype="real", calctype="real", verbose-, noact-)
imarith ("@" // tfile, "*", "@" // xfile, "@" // tfile,
pixtype="real", calctype="real", verbose-, noact-)
imsum ("@" // tfile, sumx, option="sum", pixtype="real",
calctype="real")
imdelete ("@" // tfile, ver-, >& "dev$null")
# Calculate SUMX2
imarith ("@" // ifile, "/", "@" // ifile, "@" // tfile,
pixtype="real", calctype="real", verbose-, noact-)
imarith ("@" // tfile, "*", "@" // x2file, "@" // tfile,
pixtype="real", calctype="real", verbose-, noact-)
imsum ("@" // tfile, sumx2, option="sum", pixtype="real",
calctype="real")
imdelete ("@" // tfile, ver-, >& "dev$null")
# Calculate SUMX3
imarith ("@" // ifile, "/", "@" // ifile, "@" // tfile,
pixtype="real", calctype="real", verbose-, noact-)
imarith ("@" // tfile, "*", "@" // x3file, "@" // tfile,
pixtype="real", calctype="real", verbose-, noact-)
imsum ("@" // tfile, sumx3, option="sum", pixtype="real",
calctype="real")
imdelete ("@" // tfile, ver-, >& "dev$null"
# Calculate SUMX4
imarith ("@" // ifile, "/", "@" // ifile, "@" // tfile,
pixtype="real", calctype="real", verbose-, noact-)
imarith ("@" // tfile, "*", "@" // x4file, "@" // tfile,
pixtype="real", calctype="real", verbose-, noact-)
imsum ("@" // tfile, sumx4, option="sum", pixtype="real",
calctype="real")
imdelete ("@" // tfile, ver-, >& "dev$null"
# Calculate coefa
imarith (sumx, "*", sumx3, tmp1, pix="r", calc="r", verb-, noact-)
imarith (sumx2, "*", sumx2, tmp2, pix="r", calc="r", verb-, noact-)
imarith (tmp1, "-", tmp2, tmp3, pix="r", calc="r", verb-, noact-)
imarith (tmp3, "*", sumx2y, tmp4, pix="r", calc="r", verb-, noact-)
imarith (sumy, "*", sumx3, tmp5, pix="r", calc="r", verb-, noact-)
imarith (sumx2, "*", sumxy, tmp6, pix="r", calc="r", verb-, noact-)
imarith (tmp5, "-", tmp6, tmp7, pix="r", calc="r", verb-, noact-)
imarith (sumx3, "*", tmp7, tmp8, pix="r", calc="r", verb-, noact-)
imarith (sumy, "*", sumx2, tmp9, pix="r", calc="r", verb-, noact-)
imarith (sumx, "*", sumxy, tmp10, pix="r", calc="r", verb-, noact-)
imarith (tmp9, "-", tmp10, tmp11, pix="r", calc="r", verb-, noact-)
imarith (tmp11, "*", sumx4, tmp12, pix="r", calc="r", verb-, noact-)
imarith (tmp4, "-", tmp8, tmp13, pix="r", calc="r", verb-, noact-)
imarith (tmp13, "+", tmp12, tmp14, pix="r", calc="r", verb-, noact-)
imarith (tmp14, "/", delta, coa, pix="r", calc="r", verb-, noact-)
imdelete (tmp1//","//tmp2//","//tmp3//","//tmp4, >& "dev$null")
imdelete (tmp5//","//tmp6//","//tmp7//","//tmp8, ver-, >& "dev$null")
imdelete (tmp9//","//tmp10//","//tmp11, ver-, >& "dev$null")
imdelete (tmp12//","//tmp13//","//tmp14, ver-, >& "dev$null")
# Calculate coefb
imarith (sumy, "*", sumx3, tmp1, pix="r", calc="r", verb-, noact-)
imarith (sumx2, "*", sumxy, tmp2, pix="r", calc="r", verb-, noact-)
imarith (tmp1, "-", tmp2, tmp3, pix="r", calc="r", verb-, noact-)
imarith (tmp3, "*", sumx2, tmp4, pix="r", calc="r", verb-, noact-)
imarith (nimg, "*", sumx3, tmp5, pix="r", calc="r", verb-, noact-)
imarith (sumx, "*", sumx2, tmp6, pix="r", calc="r", verb-, noact-)
imarith (tmp5, "-", tmp6, tmp7, pix="r", calc="r", verb-, noact-)
imarith (sumx2y, "*", tmp7, tmp8, pix="r", calc="r", verb-, noact-)
imarith (nimg, "*", sumxy, tmp9, pix="r", calc="r", verb-, noact-)
imarith (sumy, "*", sumx, tmp10, pix="r", calc="r", verb-, noact-)
imarith (tmp9, "-", tmp10, tmp11, pix="r", calc="r", verb-, noact-)
imarith (tmp11, "*", sumx4, tmp12, pix="r", calc="r", verb-, noact-)
imarith (tmp4, "-", tmp8, tmp13, pix="r", calc="r", verb-, noact-)
imarith (tmp13, "+", tmp12, tmp14, pix="r", calc="r", verb-, noact-)
imarith (tmp14, "/", delta, cob, pix="r", calc="r", verb-, noact-)
imdelete (tmp1//","//tmp2//","//tmp3//","//tmp4, ver-, >& "dev$null")
imdelete (tmp5//","//tmp6//","//tmp7//","//tmp8, ver-, >& "dev$null")
imdelete (tmp9//","//tmp10//","//tmp11, ver-, >& "dev$null")
imdelete (tmp12//","//tmp13//","//tmp14, ver-, >& "dev$null")
# Calculate coefc
imarith (sumx, "*", sumxy, tmp1, pix="r", calc="r", verb-, noact-)
imarith (sumy, "*", sumx2, tmp2, pix="r", calc="r", verb-, noact-)
imarith (tmp1, "-", tmp2, tmp3, pix="r", calc="r", verb-, noact-)
imarith (tmp3, "*", sumx2, tmp4, pix="r", calc="r", verb-, noact-)
imarith (nimg, "*", sumxy, tmp5, pix="r", calc="r", verb-, noact-)
imarith (sumy, "*", sumx, tmp6, pix="r", calc="r", verb-, noact-)
imarith (tmp5, "-", tmp6, tmp7, pix="r", calc="r", verb-, noact-)
imarith (sumx3, "*", tmp7, tmp8, pix="r", calc="r", verb-, noact-)
imarith (nimg, "*", sumx2, tmp9, pix="r", calc="r", verb-, noact-)
imarith (sumx, "*", sumx, tmp10, pix="r", calc="r", verb-, noact-)
imarith (tmp9, "-", tmp10, tmp11, pix="r", calc="r", verb-, noact-)
imarith (tmp11, "*", sumx2y, tmp12, pix="r", calc="r", verb-, noact-)
imarith (tmp4, "-", tmp8, tmp13, pix="r", calc="r", verb-, noact-)
imarith (tmp13, "+", tmp12, tmp14, pix="r", calc="r", verb-, noact-)
imarith (tmp14, "/", delta, coc, pix="r", calc="r", verb-, noact-)
imdelete (tmp1//","//tmp2//","//tmp3//","//tmp4, ver-, >& "dev$null")
imdelete (tmp5//","//tmp6//","//tmp7//","//tmp8, ver-, >& "dev$null")
imdelete (tmp9//","//tmp10//","//tmp11, ver-, >& "dev$null")
imdelete (tmp12//","//tmp13//","//tmp14, ver-, >& "dev$null")
# Clean up
imdelete (sumy//","//sumxy//","//sumx2y//","//sumx//","//sumx2//
","//sumx3//","//sumx4, ver-, >& "dev$null")
delete (ifile//","//tfile//","//xfile//","//x2file//
","//x3file, ver-, >& "dev$null")
end