IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 8, 2012, 2:30:52 PM (14 years ago)
Author:
eugene
Message:

add IO functions for splines

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20120905/Ohana/src/opihi/lib.data/SplineOps.c

    r34526 r34529  
    2727void InitSpline (Spline *spline, char *name, int Nknots) {
    2828
     29  if (!spline[0].name) {
    2930    spline[0].name = strcreate (name);
    30 
    31     spline[0].Nknots = Nknots;
     31  }
     32  spline[0].Nknots = Nknots;
     33 
     34  if (spline[0].xk) {
     35    REALLOCATE (spline[0].xk, opihi_flt, spline[0].Nknots);
     36    REALLOCATE (spline[0].yk, opihi_flt, spline[0].Nknots);
     37    REALLOCATE (spline[0].y2, opihi_flt, spline[0].Nknots);
     38  } else {
    3239    ALLOCATE (spline[0].xk, opihi_flt, spline[0].Nknots);
    3340    ALLOCATE (spline[0].yk, opihi_flt, spline[0].Nknots);
    3441    ALLOCATE (spline[0].y2, opihi_flt, spline[0].Nknots);
    35     memset (spline[0].xk, 0, spline[0].Nknots * sizeof(opihi_flt));
    36     memset (spline[0].yk, 0, spline[0].Nknots * sizeof(opihi_flt));
    37     memset (spline[0].y2, 0, spline[0].Nknots * sizeof(opihi_flt));
     42  }
     43  memset (spline[0].xk, 0, spline[0].Nknots * sizeof(opihi_flt));
     44  memset (spline[0].yk, 0, spline[0].Nknots * sizeof(opihi_flt));
     45  memset (spline[0].y2, 0, spline[0].Nknots * sizeof(opihi_flt));
    3846}
    3947
     
    7684
    7785  spline = FindSpline (name);
    78   if (spline != NULL) return (spline);
     86  if (spline != NULL) {
     87    InitSpline (spline, name, Nknots);
     88    return (spline);
     89  }
    7990
    8091  N = Nsplines;
     
    8293  CHECK_REALLOCATE (splines, Spline *, NSPLINES, Nsplines, 16);
    8394  ALLOCATE (spline, Spline, 1);
     95  spline->name = NULL;
     96  spline->xk = NULL;
     97  spline->yk = NULL;
     98  spline->y2 = NULL;
    8499  InitSpline (spline, name, Nknots);
    85100  splines[N] = spline;
     
    126141  return;
    127142}
     143
     144// write the spline data to a FITS file.
     145int SaveSpline (char *filename, char *name, int append) {
     146 
     147  Header header;
     148  Matrix matrix;
     149  Header theader;
     150  FTable ftable;
     151
     152  Spline *myspline = FindSpline (name);
     153  if (!myspline) {
     154    gprint (GP_ERR, "can't find spline for write : %s\n", name);
     155    return (FALSE);
     156  }
     157
     158  FILE *f = NULL;
     159
     160  /* open file for output */
     161  if (append) {
     162    f = fopen (filename, "a");
     163  } else {
     164    f = fopen (filename, "w");
     165  }
     166  if (f == (FILE *) NULL) {
     167    gprint (GP_ERR, "can't open file for write : %s\n", filename);
     168    return (FALSE);
     169  }
     170
     171  gfits_create_table_header (&theader, "BINTABLE", name);
     172
     173  gfits_define_bintable_column (&theader, "D", "X_KNOT", NULL, NULL, 1.0, 0.0);
     174  gfits_define_bintable_column (&theader, "D", "Y_KNOT", NULL, NULL, 1.0, 0.0);
     175  gfits_define_bintable_column (&theader, "D", "DY2_DX", NULL, NULL, 1.0, 0.0);
     176
     177  // generate the output array that carries the data
     178  gfits_create_table (&theader, &ftable);
     179
     180  gfits_set_bintable_column_reformat (&theader, &ftable, "X_KNOT", "double", myspline->xk, myspline->Nknots);
     181  gfits_set_bintable_column_reformat (&theader, &ftable, "Y_KNOT", "double", myspline->yk, myspline->Nknots);
     182  gfits_set_bintable_column_reformat (&theader, &ftable, "DY2_DX", "double", myspline->y2, myspline->Nknots);
     183
     184  if (!append) {
     185    gfits_init_header (&header);
     186    header.extend = TRUE;
     187
     188    gfits_create_header (&header);
     189    gfits_create_matrix (&header, &matrix);
     190
     191    gfits_fwrite_header  (f, &header);
     192    gfits_fwrite_matrix  (f, &matrix);
     193
     194    gfits_free_header (&header);
     195    gfits_free_matrix (&matrix);
     196  }
     197  gfits_fwrite_Theader (f, &theader);
     198  gfits_fwrite_table  (f, &ftable);
     199
     200  gfits_free_header (&theader);
     201  gfits_free_table (&ftable);
     202
     203  fclose (f);
     204  fflush (f);
     205  return (TRUE);
     206}
     207
     208// read the spline data from a FITS file.
     209// these functions (LoadSpline, SaveSpline) should probably take spline pointers not names
     210int LoadSpline (char *filename, char *name) {
     211 
     212  int i, Ncol;
     213  off_t Nrow;
     214  char type[16];
     215
     216  Header theader;
     217  FTable ftable;
     218
     219  FILE *f = NULL;
     220
     221  /* open file for input */
     222  f = fopen (filename, "r");
     223  if (f == (FILE *) NULL) {
     224    gprint (GP_ERR, "can't open file for read : %s\n", filename);
     225    return FALSE;
     226  }
     227
     228  ftable.header = &theader;
     229
     230  // read the full table data into a buffer
     231  if (!gfits_fread_ftable (f, &ftable, name)) {
     232    fclose (f);
     233    gprint (GP_ERR, "can't read FITS file : %s\n", filename);
     234    return FALSE;
     235  }
     236
     237  // XXX: need to handle case of spline data not existing...
     238
     239  // need to create and assign to flat-field correction
     240  double *xk = gfits_get_bintable_column_data (&theader, &ftable, "X_KNOT", type, &Nrow, &Ncol);
     241  assert (!strcmp(type, "double"));
     242 
     243  // need to create and assign to flat-field correction
     244  double *yk = gfits_get_bintable_column_data (&theader, &ftable, "Y_KNOT", type, &Nrow, &Ncol);
     245  assert (!strcmp(type, "double"));
     246
     247  // need to create and assign to flat-field correction
     248  double *y2 = gfits_get_bintable_column_data (&theader, &ftable, "DY2_DX", type, &Nrow, &Ncol);
     249  assert (!strcmp(type, "double"));
     250
     251  Spline *myspline = CreateSpline (name, Nrow);
     252  for (i = 0; i < Nrow; i++) {
     253    myspline->xk[i] = xk[i];
     254    myspline->yk[i] = yk[i];
     255    myspline->y2[i] = y2[i];
     256  }
     257  free (xk);
     258  free (yk);
     259  free (y2);
     260
     261  fclose (f);
     262  return (TRUE);
     263}
Note: See TracChangeset for help on using the changeset viewer.