# include <ohana.h>
# include <gfitsio.h>

/*********************** fits header field ****************************/
char *gfits_header_field (Header *header, char *field, int N) {

  char *buf;
  int i, Nwant, Nfound;
  char keyword[10];

  /* create a blank-padded keyword with exactly 8 characters */
  strcpy (keyword, field);
  for (i = strlen(field); i < 8; i++) keyword[i] = ' ';
  keyword[8] = 0;

  buf = header[0].buffer;

  if (N > 0) {
    /* find the Nth entry */
    Nfound = 0;
    for (i = 0; i < header[0].size; i+= FT_LINE_LENGTH, buf += FT_LINE_LENGTH) {
      if (!strncmp (keyword, buf, 8)) {
	Nfound ++;
	if (Nfound == N) return (buf);
      }
    }
  }

  if (N < 0) {
    /* count the entries */
    Nfound = 0;
    for (i = 0; i < header[0].size; i+= FT_LINE_LENGTH, buf += FT_LINE_LENGTH) {
      if (!strncmp (keyword, buf, 8)) {
	Nfound ++;
      }
    }

    Nwant = Nfound + N + 1;
    buf = header[0].buffer;

    /* find the Nwant entry */
    Nfound = 0;
    for (i = 0; i < header[0].size; i+= FT_LINE_LENGTH, buf += FT_LINE_LENGTH) {
      if (!strncmp (keyword, buf, 8)) {
	Nfound ++;
	if (Nwant == Nfound) return (buf);
      }
    }

  }

  return ((char *) NULL);

/* 

   find the Nth entry of this keyword.
   if N < 0, find the last - N - 1 word (ie, -1 = last)

*/
}

// find the given field among the HIERARCH keywords
// these have the form: HIERARCH KEYWORD = value
// returns a pointer to the start of the field
char *gfits_header_hierarch_field (Header *header, char *field, int N) {

  char *buf, *ptr;
  int i, Nwant, Nfound, Nfield;

  Nfield = strlen (field);
  if (Nfield >= 71) return NULL;

  buf = header[0].buffer;

  /* find the Nth entry */
  if (N > 0) {
    Nfound = 0;
    for (i = 0; i < header[0].size; i+= FT_LINE_LENGTH, buf += FT_LINE_LENGTH) {
      // have we found a HIERARCH entry?
      if (strncmp ("HIERARCH", buf, 8)) continue;
      ptr = buf + 9; // start of following keyword
      if (strncmp (field, ptr, Nfield)) continue;
      Nfound ++;
      if (Nfound == N) return (ptr);
    }
  }

  /* find the Ntotal - Nth entry */
  if (N < 0) {
    /* count the entries */
    Nfound = 0;
    for (i = 0; i < header[0].size; i+= FT_LINE_LENGTH, buf += FT_LINE_LENGTH) {
      if (strncmp ("HIERARCH", buf, 8)) continue;
      ptr = buf + 9; // start of following keyword
      if (strncmp (field, ptr, Nfield)) continue;
      Nfound ++;
    }

    Nwant = Nfound + N + 1;
    buf = header[0].buffer;

    /* find the Nwant entry */
    Nfound = 0;
    for (i = 0; i < header[0].size; i+= FT_LINE_LENGTH, buf += FT_LINE_LENGTH) {
      if (strncmp ("HIERARCH", buf, 8)) continue;
      ptr = buf + 9; // start of following keyword
      if (strncmp (field, ptr, Nfield)) continue;
      Nfound ++;
      if (Nwant == Nfound) return (ptr);
    }
  }

  return ((char *) NULL);

/* 

   find the Nth entry of this keyword.
   if N < 0, find the last - N - 1 word (ie, -1 = last)

*/
}

/*********************** fits header field ****************************/
char *gfits_header_lineno (Header *header, int N) {

  char *buf;

  if (N*80 >= header[0].size) return NULL;

  buf = &header[0].buffer[N*80];

  return (buf);
}

