aboutsummaryrefslogtreecommitdiff
path: root/noao/digiphot/daophot/daolib/dpreadpsf.x
blob: 77aa0b74e606923b4f114e792610ec88e75e15c4 (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
include <error.h>
include	<imhdr.h>
include "../lib/daophotdef.h"

# DP_READPSF -- Read in the PSF from the specified image.

procedure dp_readpsf (dao, im)

pointer	dao			# pointer to the DAOPHOT Structure
pointer	im			# image descriptor

int	i, ival, npsfstars
pointer	sp, str, v, psffit, psflut, buf
real	scale, rval
bool	imgetb(), streq()
int	imgeti(), imgnlr(), btoi()
real	imgetr()
errchk	imgetr(), imgeti()

begin
	# Allocate working memory.
	call smark (sp)
	call salloc (str, SZ_FNAME, TY_CHAR)
	call salloc (v, IM_MAXDIM, TY_LONG)

	# Set up needed daophot pointers.
	psffit = DP_PSFFIT(dao)

	# Read in the function parameters.
	call imgstr (im, "FUNCTION", Memc[str], SZ_FNAME)
	if (streq (Memc[str], "gauss")) {
	    DP_PSFUNCTION(psffit) = FCTN_GAUSS
	    call strcpy ("gauss", DP_FUNCTION(dao), SZ_FNAME)
	} else if (streq (Memc[str], "moffat25")) {
	    DP_PSFUNCTION(psffit) = FCTN_MOFFAT25
	    call strcpy ("moffat25", DP_FUNCTION(dao), SZ_FNAME)
	} else if (streq (Memc[str], "penny1")) {
	    DP_PSFUNCTION(psffit) = FCTN_PENNY1
	    call strcpy ("penny1", DP_FUNCTION(dao), SZ_FNAME)
	} else if (streq (Memc[str], "moffat15")) {
	    DP_PSFUNCTION(psffit) = FCTN_MOFFAT15
	    call strcpy ("moffat15", DP_FUNCTION(dao), SZ_FNAME)
	} else if (streq (Memc[str], "penny2")) {
	    DP_PSFUNCTION(psffit) = FCTN_PENNY2
	    call strcpy ("penny2", DP_FUNCTION(dao), SZ_FNAME)
	} else if (streq (Memc[str], "lorentz")) {
	    DP_PSFUNCTION(psffit) = FCTN_LORENTZ
	    call strcpy ("lorentz", DP_FUNCTION(dao), SZ_FNAME)
	} else
	    call error (0, "Unknown PSF function in PSF image\n")

	# Read in the position and brightness parameters.
	DP_PSFX (psffit) = imgetr (im, "PSFX")
	DP_PSFY (psffit) = imgetr (im, "PSFY")
	DP_PSFHEIGHT(psffit) = imgetr (im, "PSFHEIGHT")
	DP_PSFMAG (psffit) = imgetr (im, "PSFMAG")

	DP_PSFNPARS(psffit) = imgeti (im, "NPARS")
	do i = 1, DP_PSFNPARS(psffit) {
	    call sprintf (Memc[str], SZ_FNAME, "PAR%d")
		call pargi (i)
	    Memr[DP_PSFPARS(psffit)+i-1] = imgetr (im, Memc[str])
	}

	# Get the psfradius with which the psf was made. Make sure the
	# psf radius requested by the user is less than or equal to the
	# stored psf radius. 

	iferr {
	    scale = imgetr (im, "SCALE")
	} then {
	    DP_PSFRAD(dao) = min (DP_RPSFRAD(dao) / DP_SCALE(dao),
	        imgetr (im, "PSFRAD"))
	    DP_SPSFRAD(dao) = DP_SCALE(dao) * DP_PSFRAD(dao)
	} else {
	    DP_PSFRAD(dao) = min (DP_RPSFRAD(dao) / DP_SCALE(dao),
	        imgetr (im, "PSFRAD") / scale)
	    DP_SPSFRAD(dao) = DP_SCALE(dao) * DP_PSFRAD(dao)
	}

	# Get the lookup table(s) parameters.
	DP_VARORDER(dao) = imgeti (im, "VARORDER")
	switch (DP_VARORDER(dao)) {
	case -1:
	    DP_NVLTABLE(psffit) = 0
	case 0:
	    DP_NVLTABLE(psffit) = 1
	case 1:
	    DP_NVLTABLE(psffit) = 3
	case 2:
	    DP_NVLTABLE(psffit) = 6
	}
	DP_FEXPAND(dao) = btoi (imgetb (im, "FEXPAND"))
	if (DP_FEXPAND(dao) == NO)
	    DP_NFEXTABLE(psffit) = 0
	else
	    DP_NFEXTABLE(psffit) = 5

	# Read in the lookup table(s).
	if ((DP_NVLTABLE(psffit) + DP_NFEXTABLE(psffit)) <= 0) {
	    DP_PSFSIZE(psffit) = 0
	} else {
	    DP_PSFSIZE(psffit) = IM_LEN(im,1)
	    call realloc (DP_PSFLUT(psffit), DP_PSFSIZE(psffit) *
	        DP_PSFSIZE(psffit) * IM_LEN(im,3), TY_REAL)
	    psflut = DP_PSFLUT (psffit)
	    call amovkl (long(1), Meml[v], IM_MAXDIM)
	    while (imgnlr (im, buf, Meml[v]) != EOF) {
	        call amovr (Memr[buf], Memr[psflut], DP_PSFSIZE(psffit))
	        psflut = psflut + DP_PSFSIZE(psffit)
	    }
	}

	# Check that the complete header can be read.
	iferr {
	    npsfstars = imgeti (im, "NPSFSTAR")
	    call sprintf (Memc[str], SZ_FNAME, "ID%d")
		call pargi (npsfstars)
	    ival = imgeti (im, Memc[str])
	    call sprintf (Memc[str], SZ_FNAME, "X%d")
		call pargi (npsfstars)
	    rval = imgetr (im, Memc[str])
	    call sprintf (Memc[str], SZ_FNAME, "Y%d")
		call pargi (npsfstars)
	    rval = imgetr (im, Memc[str])
	    call sprintf (Memc[str], SZ_FNAME, "MAG%d")
		call pargi (npsfstars)
	    rval = imgetr (im, Memc[str])
	} then {
	    call eprintf ("PSF image header is too long to be read in.\n")
	    call eprintf (
	        "Reset min_lenusearea environment variable and try again.")
	    call erract (EA_ERROR)
	}


	call sfree (sp)
end