aboutsummaryrefslogtreecommitdiff
path: root/noao/digiphot/daophot/psf/dpwritepsf.x
blob: 239e4fafcd76cdd3040ffba0c3a0fdd99a654628 (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
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
include <time.h>
include <imhdr.h>
include	"../lib/daophotdef.h"
include	"../lib/apseldef.h"
include	"../lib/psfdef.h"

# DP_WRITEPSF -- Write out the PSF into an IRAF image. 

procedure dp_writepsf (dao, im, psfim)

pointer	dao			# pointer to the daophot structure
pointer	im			# the input image descriptor
pointer	psfim			# pointer to the output psf image

begin
	# Check that the psfimage is open.
	if (psfim == NULL)
	    return

	# Write out the id and fitting parameters.
	call dp_widpars (dao, psfim)

	# Write out the psf function definition parameters.
	call dp_wfuncpars (dao, psfim) 

	# Write out the list of PSF stars.
	call dp_wstars (dao, im, psfim)

	# Write out the lookup table.
	call dp_wlt (dao, psfim)
end


# DP_WIDPARS -- Add the id and fitting parameters to the PSF image header

procedure dp_widpars (dao, psfim)

pointer	dao			# pointer to the daophot structure
pointer	psfim			# the psf image descriptor

pointer	sp, outstr, date, time
bool	itob()
int	envfind()

begin
	# Allocate working space.
	call smark (sp)
	call salloc (outstr, SZ_LINE, TY_CHAR)
	call salloc (date, SZ_DATE, TY_CHAR)
	call salloc (time, SZ_DATE, TY_CHAR)

	# Record IRAF version, user, host, date, time, package and task.
	if (envfind ("version", Memc[outstr], SZ_LINE) <= 0)
	    call strcpy ("IRAF", Memc[outstr], SZ_LINE)
	call dp_rmwhite (Memc[outstr], Memc[outstr], SZ_LINE)
	call imastr (psfim, "IRAF", Memc[outstr])
	call gethost (Memc[outstr], SZ_LINE)
	call imastr (psfim, "HOST", Memc[outstr])
	if (envfind ("userid", Memc[outstr], SZ_LINE) <= 0)
	    Memc[outstr] = EOS
	call imastr (psfim, "USER", Memc[outstr])
	call dp_date (Memc[date], Memc[time], SZ_DATE)
	call imastr (psfim, "DATE", Memc[date])
	call imastr (psfim, "TIME", Memc[time])

	# Write out the package, task, and input/output file names.
	call imastr (psfim, "PACKAGE", "daophot")
	call imastr (psfim, "TASK", "psf")
	call dp_imroot (DP_INIMAGE(dao), Memc[outstr], SZ_LINE)
	call imastr (psfim, "IMAGE", Memc[outstr])
	call dp_froot (DP_INPHOTFILE(dao), Memc[outstr], SZ_LINE)
	call imastr (psfim, "PHOTFILE", Memc[outstr])
	call dp_froot (DP_COORDS(dao), Memc[outstr], SZ_LINE)
	call imastr (psfim, "PSTFILE", Memc[outstr])
	call dp_imroot (DP_PSFIMAGE(dao), Memc[outstr], SZ_LINE)
	call imastr (psfim, "PSFIMAGE", Memc[outstr])
	call dp_froot (DP_OUTREJFILE(dao), Memc[outstr], SZ_LINE)
	call imastr (psfim, "OPSTFILE", Memc[outstr])
	call dp_froot (DP_OUTPHOTFILE(dao), Memc[outstr], SZ_LINE)
	call imastr (psfim, "GRPSFILE", Memc[outstr])

	# Add information about fitting parameters.
	call imaddr (psfim, "SCALE", DP_SCALE(dao))
	call imaddr (psfim, "PSFRAD", DP_SPSFRAD (dao))
	call imaddr (psfim, "FITRAD", DP_SFITRAD(dao))
	call imaddr (psfim, "DATAMIN", DP_MINGDATA(dao))
	call imaddr (psfim, "DATAMAX", DP_MAXGDATA(dao))
	call imaddi (psfim, "NCLEAN", DP_NCLEAN(dao))
	call imaddb (psfim, "USESAT", itob (DP_SATURATED(dao)))

	# Define the image title.
	call sprintf (IM_TITLE(psfim), SZ_IMTITLE, "PSF for image: %s")
	    call pargstr (DP_INIMAGE(dao))

	call sfree (sp)
end


# DP_WFUNCPARS -- Write out the the parameters of the PSF function
# to the PSF image.

procedure dp_wfuncpars (dao, psfim)

pointer	dao			# pointer to the daophot structure
pointer	psfim			# image descriptor

int	i
pointer	sp, str, psffit
bool	itob()

begin
	psffit = DP_PSFFIT(dao)

	call imastr (psfim, "FUNCTION", DP_FUNCTION(dao))
	call imaddr (psfim, "PSFX", DP_PSFX(psffit))
	call imaddr (psfim, "PSFY", DP_PSFY(psffit))
	call imaddr (psfim, "PSFHEIGHT", DP_PSFHEIGHT(psffit))
	call imaddr (psfim, "PSFMAG", DP_PSFMAG (psffit))

	call smark (sp)
	call salloc (str, SZ_FNAME, TY_CHAR)

	switch (DP_PSFUNCTION(psffit)) {
	case FCTN_MOFFAT25:
	    call imaddi (psfim, "NPARS", DP_PSFNPARS(psffit)+1)
	    do i = 1, DP_PSFNPARS(psffit) {
	        call sprintf (Memc[str], SZ_FNAME, "PAR%d")
		    call pargi (i)
	        call imaddr (psfim, Memc[str], Memr[DP_PSFPARS(psffit)+i-1])
	    }
	    call sprintf (Memc[str], SZ_FNAME, "PAR%d")
		call pargi (DP_PSFNPARS(psffit)+1)
	    call imaddr (psfim, Memc[str], 2.5)
	case FCTN_MOFFAT15:
	    call imaddi (psfim, "NPARS", DP_PSFNPARS(psffit)+1)
	    do i = 1, DP_PSFNPARS(psffit) {
	        call sprintf (Memc[str], SZ_FNAME, "PAR%d")
		    call pargi (i)
	        call imaddr (psfim, Memc[str], Memr[DP_PSFPARS(psffit)+i-1])
	    }
	    call sprintf (Memc[str], SZ_FNAME, "PAR%d")
		call pargi (DP_PSFNPARS(psffit)+1)
	    call imaddr (psfim, Memc[str], 1.5)
	default:
	    call imaddi (psfim, "NPARS", DP_PSFNPARS(psffit))
	    do i = 1, DP_PSFNPARS(psffit) {
	        call sprintf (Memc[str], SZ_FNAME, "PAR%d")
		    call pargi (i)
	        call imaddr (psfim, Memc[str], Memr[DP_PSFPARS(psffit)+i-1])
	    }
	}

	call imaddi (psfim, "VARORDER", DP_VARORDER(dao))
	call imaddb (psfim, "FEXPAND", itob (DP_FEXPAND(dao)))

	call sfree (sp)
end


# DP_WSTARS -- Write out the PSF star list to the PSF image.

procedure dp_wstars (dao, im, psfim)

pointer	dao			# pointer to the daophot descriptor
pointer	im			# the input image descriptor
pointer	psfim			# the psfimage descriptor

real	tx, ty
pointer	apsel, psf, sp, str
int	i

begin
	apsel = DP_APSEL(dao)
	psf = DP_PSF(dao)

	call smark (sp)
	call salloc (str, SZ_FNAME, TY_CHAR)

	# Write out the number of PSF stars.
	call sprintf (Memc[str], SZ_FNAME, "NPSFSTAR")
	call imaddi (psfim, Memc[str], DP_PNUM(psf))

	# Write out the ids of all the PSF stars.
	do i = 1, DP_PNUM(psf) {

	    call dp_wout (dao, im, Memr[DP_APXCEN(apsel)+i-1],
		Memr[DP_APYCEN(apsel)+i-1], tx, ty, 1)
	    call sprintf (Memc[str], SZ_FNAME, "ID%d")
	        call pargi (i)
	    call imaddi (psfim, Memc[str], Memi[DP_APID(apsel)+i-1])

	    call sprintf (Memc[str], SZ_FNAME, "X%d")
	        call pargi (i)
	    call imaddr (psfim, Memc[str], tx)

	    call sprintf (Memc[str], SZ_FNAME, "Y%d")
	        call pargi (i)
	    call imaddr (psfim, Memc[str], ty)

	    call sprintf (Memc[str], SZ_FNAME, "MAG%d")
	        call pargi (i)
	    call imaddr (psfim, Memc[str], Memr[DP_APMAG(apsel)+i-1]) 
	}

	call sfree (sp)
end


# DP_WLT -- Write out the PSF lookup table to the output PSF image.

procedure dp_wlt (dao, psfim)

pointer	dao			# pointer to DAO Structure
pointer	psfim			# image descriptor

int	nexp
pointer	psffit

begin
	psffit = DP_PSFFIT(dao)
	nexp = DP_NVLTABLE(psffit) + DP_NFEXTABLE(psffit)

	IM_PIXTYPE(psfim) = TY_REAL
	if (nexp == 0) {
	    IM_NDIM(psfim) = 0
	} else if (nexp == 1) {
	    IM_NDIM(psfim) = 2
	    IM_LEN(psfim,1) = DP_PSFSIZE(psffit)
	    IM_LEN(psfim,2) = DP_PSFSIZE(psffit)
	} else {
	    IM_NDIM(psfim) = 3
	    IM_LEN(psfim,1) = DP_PSFSIZE(psffit)
	    IM_LEN(psfim,2) = DP_PSFSIZE(psffit)
	    IM_LEN(psfim,3) = nexp
	}

	if (nexp > 0)
	    call dp_wltim (psfim, Memr[DP_PSFLUT(psffit)], DP_PSFSIZE(psffit),
	        DP_PSFSIZE(psffit), nexp) 
end


# DP_WLTIM -- Write the lookup tables into the image pixels.

procedure dp_wltim (psfim, psflut, nxlut, nylut, nexp)

pointer	psfim			# image descriptor
real	psflut[nexp,nxlut,ARB]	# the psf lookup table
int	nxlut, nylut,nexp	# the dimensions of the psf look-up table

int	i, j, k
pointer	sp, v, buf
int	impnlr()

begin
	call smark (sp)
	call salloc (v, IM_MAXDIM, TY_LONG)

	call amovkl (long(1), Meml[v], IM_MAXDIM)
	do k = 1, nexp {
	    do j = 1, nylut {
		if (impnlr (psfim, buf, Meml[v]) == EOF)
		    ;
		do i = 1, nxlut
		    Memr[buf+i-1] = psflut[k,i,j]
	    }
	}

	call sfree (sp)
end