summaryrefslogtreecommitdiffstats
path: root/funtools/fitsy/headfile.c
blob: b80788daae8037dff3c0ef231a3fb7cf9a2c2d74 (plain)
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
/* Fitsy FITS read and write header routines.
 */

#include <xos.h>
#include "fitsy.h"

FITSHead ft_headread0(file, card1, n)
	File		 file;
	char		*card1;	
	size_t		 n;
{
	    int	 nbloks;

	    FITSHead	fits;
	    FITSCard	cards;
	    FITSCard	end;
	    FITSBuff	key;
	    int		match;
	    int		nhist;

    if ( file == NULL )	return NULL;

    Malloc(cards, FT_BLOCK+1);
    ft_cardclr(cards, FT_CARDS);
    /* make sure there is a null at the end (EGM) */
    ((char *)cards)[FT_BLOCK] = '\0';
    fits = ft_headinit(cards, -FT_CARDS*FT_CARDLEN);

    fits->seek  = ftTell(file);

    if ( card1 )
	memmove(fits->cards, card1, n);

    if ( ftRead(file, (void *)&fits->cards[0].c[n],
	      1, FT_BLOCK - n) != FT_BLOCK - n ) {
      (void)Free(fits->cards);
      (void)Free(fits);
      return NULL;
    };
    
    ft_cardkey(&key, "END", 0);

    for ( nbloks = 1; 1; nbloks++ ) {
	if ( (end = ft_cardfindblok(&fits->cards[(nbloks-1) * FT_CARDS]
				, &key, &match, &nhist)) ) break;

	if ( (nbloks <= 25) || (nbloks > 25 && nhist < FT_CARDS) ) {
	    fits->ncard += FT_CARDS;
	    (void)ReAlloc(fits->cards, (nbloks+1) * FT_BLOCK + 1);
	    /* make sure there is a null at the end (EGM) */
	    ((char *)fits->cards)[(nbloks+1) * FT_BLOCK] = '\0';
	    ft_cardclr(&fits->cards[nbloks * FT_CARDS], FT_CARDS);
	} else
	    nbloks--;

	if ( ftRead(file, (void *)&fits->cards[nbloks * FT_CARDS], 
		  1, FT_BLOCK) != FT_BLOCK ) {
	  (void)Free(fits->cards);
	  (void)Free(fits);
	  return NULL;
	}
    }

    fits->data  = ftTell(file);

    fits->ncard = end - fits->cards + 1;
    fits->acard = nbloks * FT_CARDS;

    ft_syncdata(fits);

    return fits;
}

/* Read a FITS header from a file pointer.

   Returns
	The #FITSHead read from #file.
 */
FITSHead ft_headread(file)
	File	file;	/* File pointer to read or write a FITS header on. */
{
	return ft_headread0(file, NULL, 0);
}

/* Write a FITS header to a file pointer.
 */
int ft_headwrite(file, fits)
	File		file;
	FITSHead	fits;	/* Header to write.		*/
{
    if ( file == NULL )		return 0;
    if ( fits == NULL )		return 0;

    if ( fits->sync ) ft_synchead(fits);

    ftWrite(file, (void *)fits->cards, 
	  FT_BLOCK, ((fits->ncard+FT_CARDS-1)/FT_CARDS));
    ftFlush(file);

    return 1;
}

void *ft_fileparse(ifile, callback, cdata)
	File	ifile;
	int	(*callback)();
	void	*cdata;
{
		FITSHead	 fits;
		void		*data = NULL;

    if ( ifile == NULL )	return NULL;

    while ( (fits = ft_headread(ifile)) ) {
	if ( (*callback)(ifile, fits, &data, cdata) )
	    return data;
    }

    return data;
}

/* Seek back to the header part of a FITS HDU.  The file handle must be the same
   one from which the FITS header was read and must be seekable.
 */
off_t ft_headseek(file, fits)
	File		file;
	FITSHead	fits;
{
    if ( file == NULL )		return -1;
    if ( fits == NULL )		return -1;

    return ftSeek(file, fits->seek, 0) != -1;
}

int ft_sync(fits, x)
	FITSHead	fits;
	int		x;
{
		int sync;

    if ( fits == NULL )		return -1;

    sync = fits->sync;
    fits->sync = x;

    return sync;
	
}