aboutsummaryrefslogtreecommitdiff
path: root/sys/mwcs/mwnewcopy.x
blob: cdc7907b4c6a11c1c7fdd92703d7b16ad9816660 (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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	"mwcs.h"

# MW_NEWCOPY -- Copy a MWCS.  The copy is done by constructing a new MWCS
# from the objects stored in the first one, freeing any dead storage in the
# process.

pointer procedure mw_newcopy (o_mw)

pointer	o_mw			#I pointer to old MWCS descriptor

int	ndim, nelem, i, j
pointer	mw, wp, o_wp, at, o_at

bool	streq()
int	mw_copys(), mw_copyd()
errchk	calloc, mw_copys, mw_copyd

begin
	# Make a copy of the main descriptor.
	call malloc (mw, LEN_MWCS, TY_STRUCT)
	call amovi (Memi[o_mw], Memi[mw], LEN_MWCS)

	# We have to allocate our own string and data buffers.
	MI_SBUF(mw) = NULL
	MI_SBUFLEN(mw) = 0
	MI_SBUFUSED(mw) = 0
	MI_DBUF(mw) = NULL
	MI_DBUFLEN(mw) = 0
	MI_DBUFUSED(mw) = 0

	# Copy the Lterm data.
	ndim = MI_NDIM(mw)
	nelem = ndim * ndim
	MI_LTV(mw) = mw_copyd (mw, o_mw, MI_LTV(o_mw), ndim)
	MI_LTM(mw) = mw_copyd (mw, o_mw, MI_LTM(o_mw), nelem)

	# We don't inherit open CTRAN descriptors.
	call aclri (MI_CTRAN(mw,1), MAX_CTRAN)

	# Copy the WCS.
	do i = 1, MI_NWCS(o_mw) {
	    wp = MI_WCSP(mw,i)
	    o_wp = MI_WCSP(o_mw,i)
	    ndim = WCS_NDIM(wp)
	    nelem = ndim * ndim

	    # Copy the WCS data.
	    WCS_R(wp) = mw_copyd (mw, o_mw, WCS_R(o_wp), ndim)
	    WCS_W(wp) = mw_copyd (mw, o_mw, WCS_W(o_wp), ndim)
	    WCS_CD(wp) = mw_copyd (mw, o_mw, WCS_CD(o_wp), nelem)

	    # Each axis can have its own sampled WCS.
	    do j = 1, ndim {
		WCS_PV(wp,j) =
		    mw_copyd (mw, o_mw, WCS_PV(o_wp,j), WCS_NPTS(o_wp,j))
		WCS_WV(wp,j) =
		    mw_copyd (mw, o_mw, WCS_WV(o_wp,j), WCS_NPTS(o_wp,j))
	    }

	    # Copy the WCS attributes.
	    do j = 1, WCS_NWATTR(o_wp) {
		at = WCS_WATTR(wp,j)
		o_at = WCS_WATTR(o_wp,j)
		AT_NAME(at) = mw_copys (mw, o_mw, AT_NAME(o_at))
		AT_VALUE(at) = mw_copys (mw, o_mw, AT_VALUE(o_at))
		if (streq (S(mw,AT_NAME(at)), "system"))
		    WCS_SYSTEM(wp) = AT_VALUE(at)
	    }

	    # Preserve the default WCS.
	    if (MI_WCS(o_mw) == o_wp)
		MI_WCS(mw) = wp
	}

	return (mw)
end


# MW_COPYD -- Copy a block of type double data from one MWCS to another.
# If the buffer offset in the old system is NULL, there was no data, and
# a null offset is output.

int procedure mw_copyd (mw, o_mw, o_off, nelem)

pointer	mw			#I pointer to output MWCS
pointer	o_mw			#I pointer to input (old) MWCS
int	o_off			#I buffer offset in old MWCS
int	nelem			#I number of type double data elements

int	off
int	mw_allocd()
errchk	mw_allocd

begin
	if (o_off == NULL)
	    off = NULL
	else {
	    off = mw_allocd (mw, nelem)
	    call amovd (D(o_mw,o_off), D(mw,off), nelem)
	}

	return (off)
end


# MW_COPYS -- Copy an EOS delimited string from one MWCS to another.
# If the buffer offset in the old system is NULL, there is no data, and
# a null offset is output.

int procedure mw_copys (mw, o_mw, o_off)

pointer	mw			#I pointer to output MWCS
pointer	o_mw			#I pointer to input (old) MWCS
int	o_off			#I buffer offset in old MWCS

int	off
int	mw_refstr()
errchk	mw_refstr

begin
	if (o_off == NULL)
	    off = NULL
	else
	    off = mw_refstr (mw, S(o_mw,o_off))

	return (off)
end