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

/*********************** fits write header *********************************/
int gfits_write_table (char *filename, FTable *table) {
  
  FILE *f;
  int status;
  
  status = TRUE;
  
  f = fopen (filename, "a+");
  if (f == (FILE *) NULL) return (FALSE);
  
  status = fseek (f, 0, SEEK_END);  /* write table to end of file! */
  status = gfits_fwrite_table (f, table);

  fclose (f);
  return (status);
}	

/*********************** fits write table *********************************/
int gfits_fwrite_table (FILE *f, FTable *table) {
  
  int Nbytes;
  
  Nbytes = fwrite (table[0].buffer, sizeof(char), table[0].size, f);
  
  if (Nbytes != table[0].size) return (FALSE);
  return (TRUE);
}	

/*********************** fits write virtual table *********************************/
int gfits_fwrite_vtable (FILE *f, VTable *table) {
  
  int i, Nx, Ny, Npad, offset, start;
  int Nbytes, *row, Nrow;
  char *pad;

  Nrow = table[0].Nrow;
  row = table[0].row;
  gfits_scan (table[0].header, "NAXIS1", "%d", 1, &Nx);
  gfits_scan (table[0].header, "NAXIS2", "%d", 1, &Ny);

  /* file pointer is at beginning of desired table data */
  start = ftell (f);
  
  for (i = 0; i < Nrow; i++) {
    offset = start + Nx*row[i];
    fseek (f, offset, SEEK_SET);
    Nbytes = fwrite (table[0].buffer[i], sizeof (char), Nx, f);
    if (Nbytes != Nx) { return (FALSE); }
  }
  
  Npad = table[0].size - Nx*Ny;
  ALLOCATE (pad, char, Npad);
  bzero (pad, Npad);

  offset = start + Nx*Ny;
  fseek (f, offset, SEEK_SET);
  Nbytes = fwrite (pad, sizeof (char), Npad, f);
  if (Nbytes != Npad) { return (FALSE); }
  free (pad);

  return (TRUE);
}	


/* this will add data beyond the end of the table in the file if needed,
   filling intervening gap with 0 */

/*********************** fits read ftable data ***********************************/
int gfits_fwrite_ftable_range (FILE *f, FTable *ftable, int start, int Nrows, int Ndisk, int Ntotal) {

  int Nbytes, Nwrite, Nskip, Nx, Npad;
  char *pad;

  if (start < 0) return (FALSE);
  
  /* modify vtable to represent full disk table */
  gfits_modify (ftable[0].header, "NAXIS2", "%d", 1, Ntotal);
  ftable[0].header[0].Naxis[1] = Ntotal;

  Nx = ftable[0].header[0].Naxis[0]; // final output table size on disk 
  ftable[0].size = gfits_data_size (ftable[0].header);

  Nskip = start * Nx;
  Nbytes = Nrows * Nx;

  // cursor must be at start of the table header
  if (!gfits_fwrite_Theader (f, ftable[0].header)) {
    fprintf (stderr, "can't write table header");
    return (FALSE);
  }

  // cursor must be at start of the table (after table header)
  fseek (f, Nskip, SEEK_CUR);
  Nwrite = fwrite (ftable[0].buffer, sizeof (char), Nbytes, f);
  if (Nwrite != Nbytes) {
    return (FALSE);
  }

  if (Ntotal >= Ndisk) {
    Npad = ftable[0].size - Nx*Ntotal;
    ALLOCATE (pad, char, Npad);
    bzero (pad, Npad);
    Nbytes = fwrite (pad, sizeof (char), Npad, f);
    free (pad);

    if (Nbytes != Npad) return (FALSE); 
  }

  return (TRUE);
}	

