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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
|
include <mach.h>
include <imhdr.h>
include "vt.h"
include "numeric.h"
define LEN_HISTO 1025
define SPACING 1
# RMAP -- Project a full disk solar image [2048x2048] into a square
# image [180x180] such that lines of latitude and longitude are
# perpendicular straight lines.
procedure t_rmap()
char inputimage[SZ_FNAME] # input image
char outputimage[SZ_FNAME] # output data image
char outweight[SZ_FNAME] # output weight image
char outabs[SZ_FNAME] # output absolute value image
char histoname[SZ_FNAME] # output histogram name
real bzero # latitude of sub-earth point
real el[LEN_ELSTRUCT] # ellipse parameters data structure
pointer inputim, outputim, outw, outa, sp
pointer inim_subras_ptr, outim_subras_ptr
pointer outwei_subras_ptr, outav_subras_ptr
int outputrow, wvlngth
int inim_subras_bottom
double meanf, meanaf, zcm, muzero
int numpix
bool skip, helium
real tempr
real imgetr()
int imgeti()
pointer immap()
pointer imgs2s(), imps2s(), imps2i()
double rmap_mode()
errchk immap, imgs2s, imps2s, imps2i, checkimmem, rowmap
begin
# Get parameters from the cl.
# Image names.
call clgstr ("inputimage", inputimage, SZ_FNAME)
call clgstr ("outputimage", outputimage, SZ_FNAME)
call clgstr ("outweight", outweight, SZ_FNAME)
call clgstr ("outabs", outabs, SZ_FNAME)
call clgstr ("histoname", histoname, SZ_FNAME)
# Open images.
inputim = immap (inputimage, READ_ONLY, 0)
wvlngth = imgeti (inputim, "wv_lngth")
helium = false
if (wvlngth == 10830)
helium = true
outputim = immap (outputimage, NEW_COPY, inputim)
outw = immap (outweight, NEW_COPY, inputim)
if (!helium)
outa = immap (outabs, NEW_COPY, inputim)
# Compute mode estimate from the input image.
muzero = rmap_mode (inputim, histoname, helium)
# Define some parameters for output images.
IM_LEN(outputim, 1) = DIM_SQUAREIM
IM_LEN(outputim, 2) = DIM_SQUAREIM
IM_PIXTYPE(outputim) = TY_INT
IM_LEN(outw, 1) = DIM_SQUAREIM
IM_LEN(outw, 2) = DIM_SQUAREIM
if (!helium) {
IM_LEN(outa, 1) = DIM_SQUAREIM
IM_LEN(outa, 2) = DIM_SQUAREIM
IM_PIXTYPE(outa) = TY_INT
}
# Get latitude of sub-earth point from input image header.
bzero = imgetr (inputim, "B_ZERO")
# Ellipse parameters.
E_XCENTER(el) = imgetr (inputim, "E_XCEN")
E_YCENTER(el) = imgetr (inputim, "E_YCEN")
E_XSEMIDIAMETER(el) = imgetr (inputim, "E_XSMD")
E_YSEMIDIAMETER(el) = imgetr (inputim, "E_YSMD")
# Remove the elipse parameters from the header records of the
# output images
call imdelf (outputim, "E_XCEN")
call imdelf (outputim, "E_YCEN")
call imdelf (outputim, "E_XSMD")
call imdelf (outputim, "E_YSMD")
call imdelf (outw, "E_XCEN")
call imdelf (outw, "E_YCEN")
call imdelf (outw, "E_XSMD")
call imdelf (outw, "E_YSMD")
call imaddb (outw, "WEIGHTS", YES)
if (!helium) {
call imdelf (outa, "E_XCEN")
call imdelf (outa, "E_YCEN")
call imdelf (outa, "E_XSMD")
call imdelf (outa, "E_YSMD")
call imaddb (outa, "ABS_VALU", YES)
}
# Set the variable that keeps track of where in the input image the
# bottom of the subraster is, map in the initial subraster.
inim_subras_bottom = 1
inim_subras_ptr = imgs2s (inputim, 1, DIM_VTFD, inim_subras_bottom,
inim_subras_bottom+DIM_IN_RAS-1)
# Map the outputimages into memory.
outim_subras_ptr = imps2i (outputim, 1, DIM_SQUAREIM, 1, DIM_SQUAREIM)
outwei_subras_ptr = imps2s (outw, 1, DIM_SQUAREIM, 1, DIM_SQUAREIM)
if (!helium)
outav_subras_ptr = imps2i (outa, 1, DIM_SQUAREIM, 1, DIM_SQUAREIM)
else {
call smark (sp)
call salloc (outav_subras_ptr, DIM_SQUAREIM*DIM_SQUAREIM, TY_INT)
}
# Initialize meanf, meanaf, numpix.
meanf = 0.0
meanaf = 0.0
numpix = 0
# Map the input image into the output image by output image rows.
do outputrow = 1, DIM_SQUAREIM {
# Check the current input subraster to see if it covers
# the next output row to be mapped and map in a new subraster
# if necessary.
call checkimmem (inim_subras_bottom, bzero, inputim, outputrow,
inim_subras_ptr, el, skip)
# If checkimmem returns skip = true then this row is not contained
# in the input image so fill it with zeros and skip it.
if (skip) {
# Fill the empty row with zeros.
call emptyrow (outputrow, Memi[outim_subras_ptr],
Mems[outwei_subras_ptr], Memi[outav_subras_ptr])
next
}
# Map this pixel row.
call rowmap (inim_subras_bottom, Mems[inim_subras_ptr], bzero,
outputrow, Memi[outim_subras_ptr], Mems[outwei_subras_ptr],
Memi[outav_subras_ptr], el, muzero, meanf, meanaf, numpix,
helium)
}
# Put the mean field, the number of pixels, the zero corrected mean
# absolute field, the mode estimate, the zero corrected mean field,
# and the standard deviation in the output image header.
meanaf = meanaf/double(numpix)
meanf = meanf/double(numpix)
zcm = meanf - muzero
tempr = real(meanf)
call imaddr (outputim, "MEAN_FLD", tempr)
call imaddi (outputim, "NUMPIX", numpix)
if (!helium) {
tempr = real(meanaf)
call imaddr (outputim, "MEANAFLD", tempr)
tempr = real(muzero)
call imaddr (outputim, "MUZERO", tempr)
tempr = real(zcm)
call imaddr (outputim, "ZCM", tempr)
}
# Close images.
call imunmap (inputim)
call imunmap (outputim)
call imunmap (outw)
if (!helium)
call imunmap (outa)
if (helium)
call sfree (sp)
end
# CHECKIMMEM -- Check this row to see if the input subraster in memory
# covers it and if it doesn't, map in a new subraster.
procedure checkimmem (inim_subras_bottom, bzero, inputim, outputrow,
inim_subras_ptr, el, skip)
int inim_subras_bottom # current bottom of the loaded subraster
real bzero # latitude of sub-earth point for this image
pointer inputim # pointer to input image
int outputrow # which output row to map
pointer inim_subras_ptr # input image subraster pointer
real el[LEN_ELSTRUCT] # ellipse parameters data structure
bool skip # returned flag saying to skip this line
real x ,y
int ymax, ymin
real uplat, downlat, lminusl0, latitude
pointer imgs2s()
errchk imgs2s
begin
skip = false
# Find values for the latitudes of the upper and lower edges of this
# pixel row.
uplat = 180./3.1415926*asin(float(outputrow - 90)/90.)
downlat = 180./3.1415926*asin(float(outputrow - 91)/90.)
# Check to see if this row is either completely off the image or
# partially off the image. If it is off the image then return
# skip = true. If it is partially off the image then truncate
# the appropriate boundary latitude at the image boundary.
if (bzero > 0) {
if ( downlat < (-90 + bzero) && uplat < (-90 + bzero)) {
# This row is not on the image.
skip = true
return
}
if (downlat < (-90 + bzero))
downlat = -90 + bzero
} else {
if ( downlat > (90 - bzero) && uplat > (90 - bzero)) {
# This row is not on the image.
skip = true
return
}
if (uplat > (90 - bzero))
uplat = 90 - bzero
}
# Calculate the minimum and maximum values of y in the input image that
# we will need to map this output row of pixels and check these
# values against the value of the current bottom of the subraster.
if (bzero > 0) {
# Calculate y position in image.
lminusl0 = 90.
latitude = uplat
call getxy (latitude, lminusl0, bzero, el, x, y, skip)
ymax = int(y + .5)
lminusl0 = -90.
latitude = uplat
call getxy (latitude, lminusl0, bzero, el, x, y, skip)
if (int(y + .5) > ymax)
ymax = int(y + .5)
# Calculate min y position.
lminusl0 = 0.
latitude = downlat
call getxy (latitude, lminusl0, bzero, el, x, y, skip)
ymin = int(y + .5)
} else {
# Calculate y position in image.
lminusl0 = 90.
latitude = downlat
call getxy (latitude, lminusl0, bzero, el, x, y, skip)
ymin = int(y + .5)
lminusl0 = -90.
latitude = downlat
call getxy (latitude, lminusl0, bzero, el, x, y, skip)
if (int(y + .5) < ymin) ymin = int(y + .5)
# Calculate max y position.
lminusl0 = 0.
latitude = uplat
call getxy (latitude, lminusl0, bzero, el, x, y, skip)
ymax = int(y + .5)
}
# If ymin or ymax is outside the current subraster, then map in
# an appropriate subraster.
if ((ymin < (inim_subras_bottom + 5)) ||
(ymax > (inim_subras_bottom + 140))) {
if ((ymax - ymin) > 150) {
call printf ("Subraster too small(ymax-ymin > 150), bye")
}
if ((ymin + 144) > 2048) {
ymin = 2048 - 144
}
if ((ymin - 5) < 1) {
skip = true
return
}
inim_subras_ptr = imgs2s (inputim, 1, DIM_VTFD, (ymin - 5),
(ymin + 144))
inim_subras_bottom = ymin - 5
}
end
# ROWMAP -- Map this output row pixel by pixel.
procedure rowmap (inim_subras_bottom, in_subraster, bzero, outputrow,
out_subraster, outw_subraster, outa_subraster, el, muzero, meanf,
meanaf, numpix helium)
real bzero # lat of sub-earth
real el[LEN_ELSTRUCT] # ellipse parameters data structure
int inim_subras_bottom # bottom of current
int outputrow # output row
short in_subraster[DIM_VTFD, DIM_IN_RAS] # subraster
int out_subraster[DIM_SQUAREIM, DIM_SQUAREIM] # output image
short outw_subraster[DIM_SQUAREIM, DIM_SQUAREIM] # output weights
int outa_subraster[DIM_SQUAREIM, DIM_SQUAREIM] # output abs. value
double muzero # mode estimate
double meanf # mean field
double meanaf # mean absolute field
int numpix # number of pixels
bool helium # 10830 flag
int pixel
errchk pixelmap
begin
# Do all 180 pixels in this output row.
do pixel = 1,180 {
call pixelmap (pixel, in_subraster, inim_subras_bottom,
bzero, outputrow, out_subraster, outw_subraster, outa_subraster,
el, muzero, meanf, meanaf, numpix, helium)
}
end
# PIXELMAP -- Sum up and count the input pixels contained inside the
# given output pixel. The sum is carried out in the following way:
#
# Calculate, on the input image, the position of the center of the
# output pixel to be mapped.
# Calculate the values of the partial derivitives of latitude and
# longitude with respect to x and y.
# Calculate the boundaries of the pixel in the input image and
# sum and count all the pixels inside, assign the value
# to the output pixel = sum/count.
procedure pixelmap (pixel, in_subraster, inim_subras_bottom,
bzero, outputrow, out_subraster, outw_subraster, outa_subraster,
el, muzero, meanf, meanaf, numpix, helium)
int pixel # which pixel
short in_subraster[DIM_VTFD, DIM_IN_RAS] # subraster
int inim_subras_bottom # bottom of current
real bzero # lat of sub-earth
int outputrow # output row
int out_subraster[DIM_SQUAREIM, DIM_SQUAREIM] # output image
short outw_subraster[DIM_SQUAREIM, DIM_SQUAREIM] # output weights
int outa_subraster[DIM_SQUAREIM, DIM_SQUAREIM] # output abs. value
real el[LEN_ELSTRUCT] # ellipse parameters data structure
double muzero # mode estimate
double meanf # first moment accum.
double meanaf # mean absolute field
int numpix # number of pixels
bool helium # helium flag
real lat_mid, long_mid, lat_bot, lat_top
real long_rite, long_left
double sum, sumabs
int count
real xpixcenter, ypixcenter
real dlongdx, dlatdy
int xleft,xright,ybottom,ytop,x,y
int num_pix_vert, num_pix_horz
pointer sp
pointer num # numeric structure pointer
real dat
begin
call smark (sp)
call salloc (num, VT_LENNUMSTRUCT, TY_STRUCT)
# First obtain the parameters necessary from numeric.
call numeric (bzero, el, outputrow, pixel, xpixcenter, ypixcenter, num)
dlongdx = VT_DLODX(num)
dlatdy = VT_DLATDY(num)
lat_top = VT_LATTOP(num)
lat_bot = VT_LATBOT(num)
long_left = VT_LOLEFT(num)
long_rite = VT_LORITE(num)
lat_mid = VT_LATMID(num)
long_mid = VT_LOMID(num)
if (lat_top == 10000.) {
out_subraster[pixel,outputrow] = 0
outw_subraster[pixel,outputrow] = 0
outa_subraster[pixel,outputrow] = 0
call sfree (sp)
return
}
# Calculate the box of pixels we want.
num_pix_horz = int((1.0 / dlongdx) + .5)
xleft = xpixcenter - int((.5 / dlongdx) + .5)
xright = xleft + num_pix_horz - 1
num_pix_vert = int((abs(abs(lat_top) - abs(lat_bot)) / dlatdy) + .5)
ybottom = ypixcenter - int(((abs(abs(lat_mid) - abs(lat_bot))) /
dlatdy) + .5) - (inim_subras_bottom - 1)
ytop = ybottom + num_pix_vert - 1
# Sum up the pixels inside this box.
count = 0
sum = 0.0
sumabs = 0.0
do x = xleft, xright {
do y = ybottom, ytop {
if (and(int(in_subraster[x,y]),17B) >= THRESHOLD+1) {
count = count + 1
# Divide by 16 to remove squibby brightness
# Accumulate the various moment data.
dat = real(in_subraster[x,y]/16)
sum = sum + double(dat)
sumabs = sumabs + double(abs(dat - muzero))
}
}
}
outw_subraster[pixel,outputrow] = short(count)
out_subraster[pixel,outputrow] = int(sum - double(count*muzero) + .5)
if (!helium)
outa_subraster[pixel,outputrow] = int(sumabs + .5)
meanf = meanf + sum
meanaf = meanaf + sumabs
numpix = numpix + count
call sfree (sp)
end
# EMPTYROW -- Set this row in the output image to zero.
procedure emptyrow (outputrow, out_subraster, outw_subraster, outa_subraster)
int outputrow
int out_subraster[DIM_SQUAREIM, DIM_SQUAREIM]
short outw_subraster[DIM_SQUAREIM, DIM_SQUAREIM]
int outa_subraster[DIM_SQUAREIM, DIM_SQUAREIM]
int pixel
begin
# Do all 180 pixels in this output row.
do pixel = 1,180 {
out_subraster[pixel, outputrow] = 0
outw_subraster[pixel, outputrow] = 0
outa_subraster[pixel, outputrow] = 0
}
end
double procedure rmap_mode (inputim, histoname, helium)
pointer inputim # Input image
char histoname[SZ_FNAME] # Histogram name
bool helium
int count, i, j
int dati, hist_middle
pointer imline, histim, hiptr
int histo[LEN_HISTO]
# Stuff for mrqmin.
real a[3], x[LEN_HISTO], y[LEN_HISTO], sig[LEN_HISTO]
int lista[3]
real alambda, chisq, covar[3,3], alpha[3,3]
short k
pointer imgl2s(), impl1i(), immap()
short shifts()
extern gauss
begin
# Initialize.
count = 0
k = -4
do i = 1, LEN_HISTO
histo[i] = 0
do i = 1, DIM_VTFD, SPACING{
imline = imgl2s (inputim, i)
do j = 1, DIM_VTFD, SPACING {
if (and(int(Mems[imline+j-1]),17B) >= THRESHOLD+1) {
count = count + 1
dati = shifts(Mems[imline+j-1], k)
# Put the data into a histogram.
hist_middle = (LEN_HISTO-1)/2 + 1
if (abs(dati) <= hist_middle-1)
histo[dati+hist_middle] = histo[dati+hist_middle] + 1
}
}
}
# Write this histogram out to an image.
histim = immap (histoname, NEW_COPY, inputim)
IM_NDIM(histim) = 1
IM_LEN(histim, 1) = LEN_HISTO
IM_PIXTYPE(histim) = TY_INT
hiptr = impl1i (histim)
# Put the histogram into this image.
do i = 1, LEN_HISTO
Memi[hiptr+i-1] = histo[i]
if (!helium) {
# Set up arrays, etc. for gaussian fit.
a[2] = 1.0
a[1] = real(histo[1])
do i = 1, LEN_HISTO {
x[i] = real(i)
y[i] = real(histo[i])
sig[i] = 1.0
if (histo[i] > a[1]) {
a[1] = real(histo[i])
a[2] = real(i)
}
}
a[3] = 15.0
do i = 1, 3
lista[i] = i
# Fit the gaussian.
alambda = -1.0
call mrqmin (x, y, sig, LEN_HISTO, a, 3, lista, 3, covar, alpha, 3,
chisq, gauss, alambda)
do i = 1, 10 {
call mrqmin (x, y, sig, LEN_HISTO, a, 3, lista, 3, covar,
alpha, 3, chisq, gauss, alambda)
}
call imaddr (histim, "GSS_AMPL", a[1])
call imaddr (histim, "GSS_CNTR", a[2])
call imaddr (histim, "GSS_WDTH", a[3])
# Put the mode estimate in the header.
call imaddr (histim, "MUZERO", (a[2] - real(hist_middle)))
}
call imunmap (histim)
if (helium)
return (0.0)
else
return (double(a[2] - real(hist_middle)))
end
|