aboutsummaryrefslogtreecommitdiff
path: root/noao/twodspec/apextract/apresize.x
blob: 8443223a9ff1cbcd038c1a3d1f2100929f3d2386 (plain) (blame)
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
include	"apertures.h"

# AP_RESIZE -- Resize apertures.

procedure ap_resize (image, line, nsum, aps, naps, apedit)

char	image[SZ_FNAME]		# Image name
int	line			# Image dispersion line
int	nsum			# Number of dispersion lines to sum
int	apedit			# Called from apedit?

pointer	aps[ARB]		# Aperture pointers
int	naps			# Number of apertures

real	llimit, ulimit		# Maximum aperture limits
real	ylevel			# Fraction of intensity for resize
bool	peak			# Is ylevel a fraction of the peak?
bool	bkg			# Subtract background?
real	grow			# Expand limits by this factor
bool	avglimits		# Average limits?

real	center, low, high
int	i, na, npts, apaxis
pointer	sp, str, im, imdata, title

bool	clgetb(), ap_answer(), apgetb()
real	apgetr(), ap_cveval()
errchk	ap_getdata

begin
	# Check if apertures are defined.
	na = 0
	do i = 1, naps
	    if (AP_SELECT(aps[i]) == YES)
		na = na + 1
	if (na == 0)
	    return

	call smark (sp)
	call salloc (str, SZ_LINE, TY_CHAR)
	if (apedit == NO) {
	    call sprintf (Memc[str], SZ_LINE, "Resize apertures for %s?")
	        call pargstr (image)
	    if (!ap_answer ("ansresize", Memc[str])) {
	        call sfree (sp)
	        return
	    }

	    if (clgetb ("verbose"))
	        call printf ("Resizing apertures ...\n")
	}

	# Map the image and get the image data.
	call ap_getdata (image, line, nsum, im, imdata, npts, apaxis, title)

	# Resize the apertures.
	llimit = apgetr ("llimit")
	ulimit = apgetr ("ulimit")
	ylevel = apgetr ("ylevel")
	bkg = apgetb ("bkg")
	peak = apgetb ("peak")
	grow = apgetr ("r_grow")
	avglimits = apgetb ("avglimits")

	if (IS_INDEF(llimit))
	    llimit = -npts
	if (IS_INDEF(ulimit))
	    ulimit = npts
	
	high = max (llimit, ulimit)
	llimit = min (llimit, ulimit)
	ulimit = high

	if (IS_INDEF (ylevel)) {
	    do i = 1, naps {
		if (AP_SELECT(aps[i]) == YES) {
		    AP_LOW(aps[i], apaxis) = llimit
		    AP_HIGH(aps[i], apaxis) = ulimit
		}
	    }
	    avglimits = true
	} else {
	    do i = 1, naps {
		if (AP_SELECT(aps[i]) == YES) {
		    low = llimit
		    high = ulimit
		    center = AP_CEN(aps[i], apaxis) +
			ap_cveval (AP_CV(aps[i]), real (line))
		    call ap_ylevel (Memr[imdata], npts, ylevel, peak, bkg, grow,
			center, low, high)
		    AP_LOW(aps[i], apaxis) = min (low, high)
		    AP_HIGH(aps[i], apaxis) = max (low, high)
		}
	    }

	    if (avglimits) {
		low = 0.
		high = 0.
		do i = 1, naps {
		    if (AP_SELECT(aps[i]) == YES) {
			low = low + AP_LOW(aps[i], apaxis)
			high = high + AP_HIGH(aps[i], apaxis)
		    }
		}
		low = low / na
		high = high / na
		do i = 1, naps {
		    if (AP_SELECT(aps[i]) == YES) {
			AP_LOW(aps[i], apaxis) = low
			AP_HIGH(aps[i], apaxis) = high
		    }
		}
	    }
	}

	# Log the operation, write the apertures to the database,
	# unmap the image and free memory.
	if (na == 1 || avglimits) {
	    call sprintf (Memc[str], SZ_LINE,
        	"APRESIZE  - %d apertures resized for %s (%.2f, %.2f)")
	        call pargi (na)
	        call pargstr (image)
		call pargr (AP_LOW(aps[1], apaxis))
		call pargr (AP_HIGH(aps[1], apaxis))
	} else {
	    call sprintf (Memc[str], SZ_LINE,
	        "RESIZE - %d apertures resized for %s")
	        call pargi (na)
	        call pargstr (image)
	}
	if (apedit == NO)
	    call ap_log (Memc[str], YES, YES, NO)
	else
	    call ap_log (Memc[str], YES, NO, NO)

	call appstr ("ansdbwrite1", "yes")

	call mfree (imdata, TY_REAL)
	call mfree (title, TY_CHAR)
	call imunmap (im)
	call sfree (sp)
end