aboutsummaryrefslogtreecommitdiff
path: root/noao/nproto/ace/sky.x
blob: c713a4371f00a7acfd3ffd66d5ed10c33ecf3486 (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
include	<error.h>
include	"sky.h"


# SKY -- Determine sky and sky sigma in an image.
#
# Get the sky and sigma map pointers.  This is layered on the MAPIO routines
# and lower level sky algorithms.  The sky parameter structure will be
# allocated if needed and must be freed by the calling program.
#
# If they are not defined compute an initial
# sky and/or sky sigma surface fit using a subset of the input lines.
# Whether the sky and/or the sigma are fit is determined by whether the input
# sky and sky sigma pointers are NULL.  The initial data for the surface fit
# is measured at a subset of lines with any masked pixels excluded.  Objects
# are removed by fitting a 1D curve to each line, rejection points with large
# residuals and iterating until only sky is left.  The sky points are then
# accumulated for a 2D surface fit and the residuals are added to a
# histogram.  The absolute deviations, scaled by 0.7979 to convert to an
# gausian sigma, are accumulated for a sky sigma surface fit.  After all the
# sample lines are accumulated the surface fits are computed.  The histogram
# of residuals is then fit by a gaussian to estimate an offset from the sky
# fit to the sky mode caused by unrejected object light.  The offset is
# applied to the sky surface.

procedure sky (par, im, bpm, expmap, skyname, signame, skymap, sigmap,
	dosky, dosig, logfd)

pointer	par			#I Parameters
pointer	im			#I Input image
pointer	bpm			#I Input mask
pointer	expmap			#I Exposure map
char	skyname[ARB]		#I Sky map name
char	signame[ARB]		#I Sigma map name
pointer	skymap			#O Sky map
pointer	sigmap			#O Sigma map
bool	dosky			#O Sky computed?
bool	dosig			#O Sigma computed?
int	logfd			#I Verbose?

real	rval
pointer	sp, namesky, namesig

int	errcode()
pointer	map_open()
errchk	map_open, sky_fit, sky_block

begin
	call smark (sp)
	call salloc (namesky, SZ_FNAME, TY_CHAR)
	call salloc (namesig, SZ_FNAME, TY_CHAR)

	if (logfd != NULL)
	    call fprintf (logfd, "  Set sky and sigma:\n")

	# Check whether to compute a sky.
	skymap = NULL
	if (skyname[1] != EOS) {
	    iferr (skymap = map_open (skyname, im)) {
		skymap = NULL
		if (errcode() != 2)
		   call erract (EA_ERROR)
	    }
	    if (logfd != NULL && skymap != NULL) {
		ifnoerr (call map_getr (skymap, "constant", rval)) {
		    call fprintf (logfd, "    Use constant input sky: %g\n")
			call pargr (rval)
		} else {
		    call fprintf (logfd, "    Use input sky: %s\n")
			call pargstr (skyname)
		}
	    }
	}
	dosky = (skymap == NULL)

	# Check whether to compute a sky sigma.
	sigmap = NULL
	if (signame[1] != EOS) {
	    iferr (sigmap = map_open (signame, im)) {
		sigmap = NULL
		if (errcode() != 2)
		   call erract (EA_ERROR)
	    }
	    if (logfd != NULL && sigmap != NULL) {
		ifnoerr (call map_getr (sigmap, "constant", rval)) {
		    call fprintf (logfd, "    Use constant input sigma: %g\n")
			call pargr (rval)
		} else {
		    call fprintf (logfd, "    Use input sigma: %s\n")
			call pargstr (signame)
		}
	    }
	}
	dosig = (sigmap == NULL)

	# Compute the sky.
	if (dosky || dosig) {
	    # Set parameters.
	    call sky_pars ("open", "", par)

	    switch (SKY_TYPE(par)) {
	    case SKY_FIT:
		call sky_fit (SKY_SKF(par), dosky, dosig, im, bpm, expmap,
		    skyname, signame, skymap, sigmap, logfd)
	    case SKY_BLOCK:
		call sky_fit (SKY_SKF(par), dosky, dosig, im, bpm, expmap,
		    "", "", skymap, sigmap, logfd)
		call map_seti (skymap, "sample", 5)
		call map_seti (sigmap, "sample", 5)
		call sky_block (SKY_SKB(par), dosky, dosig, im, bpm, expmap,
		    skyname, signame, skymap, sigmap, logfd)
	    default:
		call error (1, "Unknown sky type")
	    }
	}

	call sfree (sp)
end