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
/
closure.cl
218 lines (198 loc) · 9.1 KB
/
closure.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
# CLOSURE: 06APR94 KMM
# CLOSURE: 10APR91 KMM 10APR92
# CLOSURE determine overlap region and mininum enclosure
# 06APR94 KMM replace "type" with "concatenate"
procedure closure (infofile,gxshift,gyshift)
file infofile {prompt="file produced by XYGET|XYLAP|XYADOPT|IRCOMBINE"}
real gxshift {prompt="global xshift for final image"}
real gyshift {prompt="global yshift for final image"}
# trim values applied to final image
string trimlimits {"[0:0,0:0]",
prompt="added trim limits for input subrasters"}
string interp_shift {"linear",enum="nearest|linear|poly3|poly5|spline3",
prompt="IMSHIFT interpolant (nearest,linear,poly3,poly5,spline3)"}
bool origin {no, prompt="Move origin to lower left corner?"}
bool format {no, prompt="Format output table"}
bool verbose {yes, prompt="Verbose output?"}
struct *list1, *list2, *l_list
begin
int i,stat,nim,slen,slenmax,
nxhi, nxlo, nyhi, nylo, nxhisrc, nxlosrc, nyhisrc, nylosrc,
nxlotrim,nxhitrim,nylotrim,nyhitrim,nxspan,nyspan,
nxhiref,nxloref,nyhiref,nyloref,nxhimat,nxlomat,nyhimat,nylomat,
nxlovig,nxhivig,nylovig,nyhivig,nxlolap,nxhilap,nylolap,nyhilap,
nxlonew,nxhinew,nylonew,nyhinew,
nxmat0, nymat0, nxmos0, nymos0, ixs, iys, ncolsout,nrowsout
real zoff, rjunk, xin, yin, xmat, ymat, fxs, fys,
xs, ys, xoff, yoff, g_xshift, g_yshift,
xmax, xmin, ymax, ymin,oxmin,oymin,
xoffset,yoffset, xlo, xhi, ylo, yhi, xshift, yshift
bool firsttime
string uniq,imname,sjunk,soffset,encsub,info,
src,mat,ref,outsec,lapsec,vigsec,sformat
file out,l_log,tmp1,matinfo,newinfo,task
struct line = ""
info = infofile
g_xshift = gxshift
g_yshift = gyshift
uniq = mktemp ("_Tclo")
task = uniq // ".tsk"
newinfo = uniq // ".mat"
matinfo = mktemp("tmp$clo")
tmp1 = mktemp("tmp$clo")
l_log = mktemp("tmp$clo")
l_list = l_log
if (! access(info)) { # Exit if can't find info
print ("Cannot access info_file: ",info)
goto err
}
# Set initial values
xmin = 10000.; xmax = -10000.; oxmin = 10000.
ymin = 10000.; ymax = -10000.; oymin = 10000.
print (trimlimits) | translit ("", "[:,]", " ", >> l_log)
stat = (fscan(l_list,nxlotrim,nxhitrim,nylotrim,nyhitrim))
# Read in data along direction of imcopy
list1 = info
# print ("COM_"//pathpos," ",src," ",nxlosrc,nxhisrc,nylosrc,nyhisrc,
# nxmat0,nymat0,xs,ys,soffset)
slenmax = 0
firsttime = yes
for (i = 0; fscan(list1,imname,src,nxlosrc,nxhisrc,nylosrc,nyhisrc,
nxmat0,nymat0,xs,ys,soffset) != EOF; i += 1) {
# Get sizes of source images
if (stridx("#",imname) != 0) {
print (imname," ",src," ",nxlosrc,nxhisrc,nylosrc,nyhisrc,
nxmat0,nymat0,fxs,fys,soffset," ",line,>> newinfo)
next
}
print (src) | translit ("", "[:,]", " ", >> l_log)
stat = (fscan(l_list,sjunk,nxloref,nxhiref,nyloref,nyhiref))
# Establish image span
nxspan = nxhiref - nxloref + 1
nyspan = nyhiref - nyloref + 1
nxhiref = nxhiref - nxloref + 1; nxloref = 1
nyhiref = nyhiref - nyloref + 1; nyloref = 1
# Put in additional global trims
nxlosrc += nxlotrim; nxhisrc -= nxhitrim
nylosrc += nylotrim; nyhisrc -= nyhitrim
# Put in global shifts
xs = xs + g_xshift; ys = ys + g_yshift
ixs = nint(xs); iys = nint(ys)
fxs = xs - ixs; fys = ys - iys
fxs = 0.01*real(nint(100.0*fxs))
fys = 0.01*real(nint(100.0*fys))
xshift = xs + nxmat0; yshift = ys + nymat0
xoff = ixs + nxmat0; yoff = iys + nymat0
nxlomat = nxlosrc + xoff; nxhimat = nxhisrc + xoff
nylomat = nylosrc + yoff; nyhimat = nyhisrc + yoff
slenmax = max(slenmax,strlen(src))
# Recode MAT table including global trims and shifts
nxmat0 = int(xoff); nymat0 = int(yoff)
print (imname," ",src," ",nxlosrc,nxhisrc,nylosrc,nyhisrc,
nxmat0,nymat0,fxs,fys,soffset," ",line,>> newinfo)
# Determine size of composite image. Note that trims are not included.
xlo = nxloref + xshift; xhi = nxhiref + xshift
ylo = nyloref + yshift; yhi = nyhiref + yshift
xmin = min(xmin,xlo); xmax = max(xmax,xhi)
ymin = min(ymin,ylo); ymax = max(ymax,yhi)
oxmin = min (nxhiref, oxmin); oymin = min (nyhiref, oymin)
# Determine overlap region. Note that trims are included.
xlo = nxlosrc + xshift; xhi = nxhisrc + xshift
ylo = nylosrc + yshift; yhi = nyhisrc + yshift
nxlonew = int (xlo); if (xlo > nxlonew) nxlonew += 1 # round up
nxhinew = int (xhi); if (xhi < nxhinew) nxhinew -= 1 # round down
nylonew = int (ylo); if (ylo > nylonew) nylonew += 1 # round up
nyhinew = int (yhi); if (yhi < nyhinew) nyhinew -= 1 # round down
if (firsttime) {
nxlo = nxlonew; nxhi = nxhinew
nylo = nylonew; nyhi = nyhinew
xmin = nxhiref; ymin = nyhiref
# xmin = nxhisrc; ymin = nyhisrc
firsttime = no
} else {
nxlo = max (nxlo, nxlonew); nxhi = min (nxhi, nxhinew)
nylo = max (nylo, nylonew); nyhi = min (nyhi, nyhinew)
}
}
# Determine corners of minimum rectangle enclosing region
nxlomat = int (xmin); if (xmin < nxlomat) nxlomat -= 1 # round down
nxhimat = int (xmax); if (xmax > nxhimat) nxhimat += 1 # round up
nylomat = int (ymin); if (ymin < nylomat) nylomat -= 1 # round down
nyhimat = int (ymax); if (ymax > nyhimat) nyhimat += 1 # round up
print (nxlomat,nxhimat,nylomat,nyhimat)
xoffset = -nxlomat
yoffset = -nylomat
print("APPLIED_OFFSETS: ",xoffset,yoffset)
nxlolap = nxlo; nxhilap = nxhi
nylolap = nylo; nyhilap = nyhi
# Compute conservative overlap region
# Vignetting is possible downstream since IMSHIFT (and
# other tasks) preserve the size of the input image.
# correct for boundary extension "contamination"
if (interp_shift == "poly3")
{ nxlolap += 1; nxhilap -= 1; nylolap += 1; nyhilap -= 1 }
else if (interp_shift == "poly5" || interp_shift == "spline3")
{ nxlolap += 2; nxhilap -= 2; nylolap += 2; nyhilap -= 2 }
# Calculate vignetted overlap vs. smallest image
nxlovig = max (1, min (nxlolap, int(oxmin)))
nxhivig = max (1, min (nxhilap, int(oxmin)))
nylovig = max (1, min (nylolap, int(oymin)))
nyhivig = max (1, min (nyhilap, int(oymin)))
if (1 <= nxlovig && nxlovig <= nxhivig &&
1 <= nylovig && nylovig <= nyhivig) {
vigsec = "["//nxlovig//":"//nxhivig//","//nylovig//":"//nyhivig//"]"
} else {
# nxlovig = 0; nxhivig = 0; nylovig = 0; nyhivig = 0
vigsec = "[0:0,0:0]"
}
if (origin) { # Recode info with null offset
list1 = newinfo
for (i = 0; fscan(list1,imname,src,nxlosrc,nxhisrc,nylosrc,nyhisrc,
nxmat0,nymat0,xs,ys,soffset) != EOF; i += 1) {
nxmat0 += xoffset
nymat0 += yoffset
print (imname," ",src," ",nxlosrc,nxhisrc,nylosrc,nyhisrc,
nxmat0,nymat0,xs,ys,soffset,>> tmp1)
}
delete (newinfo,ver-,>& "dev$null")
rename (tmp1, newinfo)
nxlomat += xoffset; nxhimat += xoffset
nylomat += yoffset; nyhimat += yoffset
nxlolap += xoffset; nxhilap += xoffset
nylolap += yoffset; nyhilap += yoffset
xoffset = 0
yoffset = 0
}
if (1 <= nxlolap && nxlolap <= nxhilap &&
1 <= nylolap && nylolap <= nyhilap) {
lapsec = "["//nxlolap//":"//nxhilap//","//nylolap//":"//nyhilap//"]"
} else {
lapsec = "["//nxlolap//":"//nxhilap//","//nylolap//":"//nyhilap//"]"
print ("#WARNING: overlap section: ",lapsec," is unphysical!")
}
encsub = "["//nxlomat//":"//nxhimat//","//nylomat//":"//nyhimat//"]"
# Establishes origin at (0,0)
ncolsout = nxhimat - nxlomat + 1
nrowsout = nyhimat - nylomat + 1
outsec = "[1:"// ncolsout //",1:"// nrowsout //"]"
print("ENCLOSED_REGION: ",encsub)
print("ENCLOSED_SIZE: ",outsec)
print("UNAPPLIED_OFFSETS: ",xoffset,yoffset)
print("OVERLAP: ",lapsec," ",vigsec)
if (format) { # fancy formatter
sformat = '{printf("%-7s %'//-slenmax//
's %3d %3d %3d %3d %4d %4d %5.2f %5.2f %8.2f'
if ((slenmax + 57) == 80) # dodge 80 char no lf
sformat = sformat //' |\\n"'
else
sformat = sformat //' |\\n"'
sformat = sformat // ',$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)}'
print(sformat, > task); print("!awk -f ",task," ",newinfo) | cl
} else
concatenate (newinfo)
err:
# Finish up
list1 = ""; list2 = ""; l_list = ""
delete (tmp1//","//matinfo//","//l_log,>& "dev$null")
delete (newinfo//","//task,ver-,>& "dev$null")
end