aboutsummaryrefslogtreecommitdiff
path: root/pkg/tbtables/tbfwer.x
blob: 2b18cc877e41b38885435fe2a5afa148305dbe6e (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
include <mach.h>		# for SZB_CHAR
include <tbset.h>
include "tbtables.h"
include "tblfits.h"		# defines FITS_TNULL_NOT_SET

# tbfwer -- write empty rows to end of FITS table
#
# Phil Hodge,  6-Jul-1995  Subroutine created
# Phil Hodge,  3-Jun-1996  Remove call to fsirow.
# Phil Hodge, 23-Apr-1997  Add TNULL to header for FITS ASCII table.
# Phil Hodge, 29-Jul-1997  Call fsirow to create new rows.
# Phil Hodge,  7-Jun-1999  Use TB_SUBTYPE instead of TB_HDUTYPE.
# Phil Hodge, 25-Aug-2000  Delete the call to fsirow.
# Phil Hodge, 12-Sep-2000  Use TB_INDEF for a row of undefined values.

procedure tbfwer (tp, nrows, new_nrows)

pointer tp		# i: pointer to table descriptor
int	nrows		# i: number of rows on entry to this routine
int	new_nrows	# i: number of rows after calling this routine
#--
pointer sp
pointer keyword		# for TNULL keyword
pointer cp		# pointer to one column descriptor
int	row, col	# row and column numbers
int	nelem		# number of elements, if column is array type
int	dtype		# data type of column (needed to set TNULL)
int	ival		# undefined value
int	status		# zero is OK
#
pointer comment		# for getting NAXIS1
int	nbytes		# value of NAXIS1 (length of a row, in bytes)
int	nchar		# number of char elements in nbytes
#
pointer tbcnum()
int	tbcigi()
errchk	tbferr

begin
	if (new_nrows <= nrows)
	    return			# nothing to do

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

	status = 0

	if (TB_INDEF_IS_CURRENT(tp)) {

	    # Write the INDEF record to all the new rows.
	    do row = nrows+1, new_nrows {
		call fsptbb (TB_FILE(tp), row, 1, TB_ROWLEN(tp),
			Memc[TB_INDEF(tp)], status)
		if (status != 0)
		    call tbferr (status)
	    }

	} else {

	    # We don't have a valid INDEF record yet, so explictly write
	    # the undefined values for one row, then read that into TB_INDEF.

	    row = nrows + 1
	    do col = 1, TB_NCOLS(tp) {		# loop over columns

		cp = tbcnum (tp, col)
		nelem = tbcigi (cp, TBL_COL_LENDATA)

		call fspclu (TB_FILE(tp), col, row, 1, nelem, status)

		if (status == FITS_TNULL_NOT_SET) {

		    status = 0
		    call ftcmsg()

		    # Create TNULL string, and add to header.

		    call sprintf (Memc[keyword], SZ_FNAME, "TNULL%d")
			call pargi (col)

		    if (TB_SUBTYPE(tp) == TBL_SUBTYPE_ASCII) {

			# TNULL = "*"
			call fspkys (TB_FILE(tp), Memc[keyword],
				"*", "undefined value for column", status)

		    } else if (TB_SUBTYPE(tp) == TBL_SUBTYPE_BINTABLE) {

			dtype = tbcigi (cp, TBL_COL_DATATYPE)
			if (dtype == TY_INT || dtype == TY_SHORT) {
			    if (dtype == TY_INT)
				ival = FITS_INDEFI
			    else if (dtype == TY_SHORT)
				ival = FITS_INDEFS
			    call fspkyj (TB_FILE(tp), Memc[keyword],
				ival, "undefined value for column", status)
			}		# else don't do anything
		    }
		    # try again
		    call fsrdef (TB_FILE(tp), status)
		    call fspclu (TB_FILE(tp), col, row, 1, nelem, status)
		}
		if (status != 0)
		    call tbferr (status)
	    }

	    # Allocate memory for TB_INDEF, and read the record that we just
	    # wrote, reading into TB_INDEF.

	    call salloc (comment, SZ_FNAME, TY_CHAR)
	    call fsrdef (TB_FILE(tp), status)
	    call fsgkyj (TB_FILE(tp), "NAXIS1", nbytes, Memc[comment], status)
	    if (status != 0)
		call tbferr (status)

	    TB_ROWLEN(tp) = nbytes	# note:  this is the number of BYTES

	    # round up
	    nchar = (nbytes + SZB_CHAR-1) / (SZB_CHAR)
	    call realloc (TB_INDEF(tp), nchar, TY_CHAR)
	    call fsgtbb (TB_FILE(tp), row, 1, TB_ROWLEN(tp),
			Memc[TB_INDEF(tp)), status)
	    if (status != 0)
		call tbferr (status)

	    TB_INDEF_IS_CURRENT(tp) = true

	    # Now that we have the INDEF record in TB_INDEF, write it to
	    # all the other new rows.
	    do row = nrows+2, new_nrows {
		call fsptbb (TB_FILE(tp), row, 1, TB_ROWLEN(tp),
			Memc[TB_INDEF(tp)], status)
		if (status != 0)
		    call tbferr (status)
	    }
	}

	call sfree (sp)
end