summaryrefslogtreecommitdiffstats
path: root/funtools/fitsy/cardfind.c
blob: 999d143fd5a431926d53f00038fdef7910ef334f (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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Fitsy FITS card find routines.

   DESCRIPTION

	Routines to find FITS cards in a FITS headers data structure.

   EXAMPLES
+

		/-* Declair some fitsy types.
		 *-/
		#FITSHead	fits;
		#FITSCard	foo;
		#FITSCard	goo;
		#FITSBuff	key;

	/-* Look up a card using sequential search.
	 *-/
	#ft_cardkey(key, "FOO");
	foo = #ft_cardfind(fits, key, 0);

	/-* This is the same thing.  But the card is added to the header
	   if it isn't found.  This will also invalidate the index if
	   there is one.
	 *-/
	#ft_cardkey(key, "FOO");
	foo = #ft_cardfind(fits, key, 1);

	/-* Now index the header so that searches are faster.  #ft_cardfind
	   will automatically use the index if its valid.
	 *-/
	#ft_index(fits);
	#ft_cardkey(key, "GOO", 0);
	goo = #ft_cardfind(fits, key, 0);
+
 */

#include "fitsy.h"

int ft_compare(a, b)
	char	**a;
	char	**b;

{
		int	ax, bx;

	if ( !strncmp(*a, *b, 5) 
	  && ((*a)[5] != ' ') && ((*b)[5] != ' ')
	  && (ax = atoi(&(*a)[5])) 
	  && (bx = atoi(&(*b)[5])) ) {
		if ( ax <  bx ) return -1;
		if ( ax == bx ) return  0;
		if ( ax >  bx ) return  1;
	}

	return strncmp(*a, *b, 8);
}

/* Find a FITS card in the header.

   #cardfind will use the index if is has been created otherwise
   it searches sequentially through the header to find the card.
 */
FITSCard ft_cardfind(fits, key, add)
	FITSHead	fits;		/* The FITS header to look in.	*/
	FITSCard	key;		/* The card keyword to lookup.	*/
	int		add;		/* If add is true the card will
					   be added to the header if it is
					   not found.
					 */
{
		FITSCard	 card;
		int		 match;

	if ( fits == NULL ) return NULL;
	if ( key  == NULL ) return NULL;

	if ( fits->index ) card = ft_cardfindidx(fits, key, &match);
	else 		   card = ft_cardfindseq(fits, key, &match);

	if ( !match && add )
		return ft_cardins(fits, key, card);
	else    return match ? card : NULL;
}

/* Find a FITS card in the header using an index.

   If the header is not indexed an index is created for it.
 */
FITSCard ft_cardfindidx(fits, key, match)
	FITSHead	 fits;
	FITSCard	 key;
	int		*match;		/* Returns true if the card found is
					   an exact match for the keyword requested.
					 */
{
                FITSCard    	*base;
                int     	 length;

                int     lo, hi, cut;
                int     i; 
 
	if ( fits == NULL ) return NULL;
	if ( key  == NULL ) return NULL;

	base   = fits->index;
	length = ft_ncards(fits);

	lo  = -1;
	hi  = length;
	cut = length / 2;

	if ( !fits->index ) ft_headindex(fits);

	*match = 0;
        while ( hi - lo > 1 ) {
                if ( !(i = ft_compare((char **)&key, (char **)&base[cut])) ) {
		    *match = 1;
                    return base[cut];
		}
 
                if ( i < 0 ) {
                        hi = cut;
                        cut = (lo + hi) / 2;
                } else { 
                        lo = cut;
                        cut = (lo + hi) / 2;
                } 
        }

	if ( !strncmp(key->c, base[cut]->c, 5) )
             return base[cut];

	return NULL;
}
 
/* Find a card in the FITS header using sequential search.

   If the requested card is a FITS indexed keyword and an exact match
   is not found, the last card of that type is returned and
   match is set to 0.
 */
FITSCard ft_cardfindseq(fits, key, match)
	FITSHead	 fits;
	FITSCard	 key;
	int		*match;
{
	    FITSCard  card;
	    FITSCard  xnear = NULL;

    if ( fits == NULL ) return NULL;
    if ( key  == NULL ) return NULL;

    *match = 0;
    for ( card = fits->cards; card != &fits->cards[fits->ncard]; card++ ) {
	    if ( !strncmp(key->c, card->c, 8) ) {
		*match = 1;
		return card;
	    }
	    if (  !strncmp(key->c, card->c, 5) 
	      && (isdigit((unsigned int) (*card).c[5]) || (*card).c[5] == ' ' )
	      && (isdigit((unsigned int) (*card).c[6]) || (*card).c[6] == ' ' )
	      && (isdigit((unsigned int) (*card).c[7]) || (*card).c[7] == ' ' ) ) {
		xnear = card;
    	    }
    }

    return xnear;
}

FITSCard ft_cardfindblok(cards, key, match, nhist)
	FITSCard	 cards;
	FITSCard	 key;
	int		*match;
	int		*nhist;
{
	    FITSCard  card;
	    FITSCard  xnear = NULL;

    if ( cards== NULL ) return NULL;
    if ( key  == NULL ) return NULL;

    *nhist = 0;
    *match = 0;
    for ( card = cards; card != &cards[FT_CARDS]; card++ ) {
	    if ( !strncmp(key->c, card->c, 8) ) {
		*match = 1;
		return card;
	    }
	    if (  !strncmp(key->c, card->c, 5) 
	      && (isdigit((unsigned int) (*card).c[5]) || (*card).c[5] == ' ' )
	      && (isdigit((unsigned int) (*card).c[6]) || (*card).c[6] == ' ' )
	      && (isdigit((unsigned int) (*card).c[7]) || (*card).c[7] == ' ' ) ) {
		xnear = card;
    	    }

	    if ( !strncmp(key->c, "HISTORY", 7) ) (*nhist)++;
    }

    return xnear;
}