# include "fits.h"

/*********************** fits get table value ********************************/
  
void fits_get_table_value (table, X, Y, mode, value)
Table *table; 
int X, Y; 
char *mode, *value;
{
  
  char Tform[100], field[100];
  int  start, Nchar, byte;
  char tmp[1000];
  
  /* no error checking, all sorts of problems! */
  
  sprintf (field, "TBCOL%d\0", X);
  fits_scan (&table[0].header, field, "%d", &start, 1);
  start --;
  
  sprintf (field, "TFORM%d\0", X);
  fits_scan (&table[0].header, field, "%s", Tform, 1);
  Nchar = atof (&Tform[1]);
  
  byte = Y*table[0].Naxis[0] + start;
  strncpy (tmp, &table[0].buffer[byte], Nchar);
  tmp[Nchar] = 0;

  if (!strcmp (mode, "%d"))  *(int      *) value = (int)      atof (tmp);
  if (!strcmp (mode, "%u"))  *(unsigned *) value = (unsigned) atof (tmp);
  if (!strcmp (mode, "%ld")) *(long     *) value = (long)     atof (tmp);
  if (!strcmp (mode, "%hd")) *(short    *) value = (short)    atof (tmp);
  if (!strcmp (mode, "%f"))  *(float    *) value = (float)    atof (tmp);
  if (!strcmp (mode, "%lf")) *(double   *) value = (double)   atof (tmp);

  if (!strcmp (mode, "%s")) 
    strcpy (value, tmp);

}

