aboutsummaryrefslogtreecommitdiff
path: root/pkg/xtools/mef/mefwrhdr.x_save
blob: ef1c332bc12ed970ee464c2d93c837f77812ff39 (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
include <error.h>
include <mef.h>

# MEF_WRHDR -- Append the header from an input PHU or extension to output file.

procedure mef_wrhdr (mefi, mefo, in_phdu)

pointer	mefi		#I input mef descriptor
pointer	mefo		#I output mef descriptor
bool	in_phdu		#I true if input header is Primary Header Unit.

pointer hb, sp, ln
int	output_lines, out
int	i, index, naxis, mef_kctype(), strncmp()
bool    endk, new_outf
errchk  open, fcopyo

define	nextb_ 99

begin
	call smark (sp)
	call salloc (ln, LEN_CARDNL, TY_CHAR)

	# At this point the input first header has been read

	hb = MEF_HDRP(mefi)
	if (Memc[hb] == NULL)
	   call error(13,"mef_wrhdr: input header buffer is empty")

	out = MEF_FD(mefo)

	new_outf = false
	if (MEF_ACMODE(mefo) == NEW_IMAGE)
	   new_outf = true

	output_lines = 0
	endk = false

	# If we want to copy the header with no modification
	if (MEF_KEEPXT(mefo) == YES) {
	    for (i=1; i<37; i=i+1) {
	       switch (mef_kctype(Memc[hb], index)) {
	       case END:
		   call mef_pakwr (out, Memc[hb])
		   endk = true
		   output_lines = i
		   break
	       default:
		   call mef_pakwr (out, Memc[hb])
		   hb = hb + LEN_CARDNL
	       }
	    }
	    goto nextb_
	} 

	# Check for 1st card
	if (strncmp (Memc[hb], "SIMPLE  ", 8) == 0) {
	    # Append extension to existing file
	    if (!new_outf) {            
	        call mef_encodec ("XTENSION", "IMAGE", 5, Memc[ln],
		    "Image extension")
	        call mef_pakwr (out, Memc[ln])
	    } else
	        call mef_pakwr (out, Memc[hb])
	} else if (strncmp (Memc[hb], "XTENSION", 8) == 0 ) {
	    if (new_outf) {
	        # Create a PHU
	        # Must create a dummy header if input extension is not image
		if (strncmp (MEF_EXTTYPE(mefi), "IMAGE", 5) != 0) {
		    Memc[ln] = EOS
		    call mef_dummyhdr (out, Memc[ln])
		    new_outf = false
	            call mef_pakwr (out, Memc[hb])
	        } else {
	            call mef_encodeb ("SIMPLE", YES, Memc[ln],
		        "Standard FITS format")
	            call mef_pakwr (out, Memc[ln])
		}
	    } else
	        call mef_pakwr (out, Memc[hb])
	} else {
	    # Is the wrong kind of header
#	    call eprintf ("File %s is not FITS\n")
#		call erract (EA_FATAL)
	    call sprintf (Memc[ln],LEN_CARD, "File %s is not FITS")
	        call pargstr(MEF_FNAME(mefi))
            call error(13, Memc[ln])			
	}
	hb = hb + LEN_CARDNL

	for (i=2; i<37; i=i+1) {
	   switch (mef_kctype(Memc[hb], index)) {
	   case BITPIX:
	      # Get to calculate totpix value
	      call mef_gvali (Memc[hb], MEF_BITPIX(mefi))
	   case NAXIS:
	      naxis = index
	      MEF_NDIM(mefi) = index
	      if (in_phdu && !new_outf && naxis == 0) {
	         call mef_pakwr (out, Memc[hb])
		 call mef_wrpgcount (out)
	         output_lines = output_lines + 2
		 hb = hb + LEN_CARDNL
		 next
	      }
	   case NAXISN:
	      call mef_gvali (Memc[hb], MEF_NAXIS(mefi,index))
	      call mef_pakwr (out, Memc[hb])
	      if (index == naxis) {
	          if (in_phdu && !new_outf ) {
		      # We are writing from a phu to ehu.
		      # 2 new cards PCOUNT and GCOUNT

		      call mef_wrpgcount (out)
		      output_lines = output_lines + 2
	          }			
	          if (!in_phdu && new_outf) {
		      # We are writing from a ehu to a phu
		      call mef_encodeb ("EXTEND", YES, Memc[ln],
			  "There may be extensions")
		      call mef_pakwr (out, Memc[ln])
		      output_lines = output_lines + 1
		  }   
	      }
	      hb = hb + LEN_CARDNL
	      next
	   case EXTEND, FILENAME:
	      if (!new_outf) {
		  # Do not put these cards  when going to an ehu
	          output_lines = output_lines - 1
	          hb = hb + LEN_CARDNL
		  next
	      }
	   case INHERIT:
	      # Eliminate INHERIT keyword from an input IMAGE extension
	      # when creating a new output file. If file already exists
	      # then pass the card along.

	      if (new_outf) {
	          output_lines = output_lines - 1
	          hb = hb + LEN_CARDNL
		  next
	      } 
	   case PCOUNT,GCOUNT,EXTNAME,EXTVER:   
	      # Do not put these cards into PHU
	      if (new_outf) {
	         output_lines = output_lines - 1
	         hb = hb + LEN_CARDNL
		 next
	      } 
	   case END:
	      call mef_pakwr (out, Memc[hb])
	      endk = true
	      output_lines = i + output_lines
	      break
	   default:
	      ;
	   }
	   call mef_pakwr (out, Memc[hb])
	   hb = hb + LEN_CARDNL

	} # end for loop

nextb_
	# See if we need to keep reading header
	#
	if (!endk) 
	   repeat {
	      for (i=1; i<37; i=i+1) {
	        if (strncmp (Memc[hb], "END     ", 8) == 0) {
	           call mef_pakwr (out, Memc[hb])
		   endk = true
		   output_lines = i + output_lines
		   break
	        }
	        call mef_pakwr (out, Memc[hb])
		hb = hb + LEN_CARDNL
	      }
              if (endk) break
	      
	   } #end repeat
	call mef_wrblank (out, output_lines)

	call sfree(sp)
end