IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 34529


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

add IO functions for splines

Location:
branches/eam_branches/ipp-20120905/Ohana/src/opihi
Files:
6 edited

Legend:

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

    r34526 r34529  
    44int spline_create (int argc, char **argv);
    55int spline_apply (int argc, char **argv);
     6int spline_load (int argc, char **argv);
     7int spline_save (int argc, char **argv);
    68int spline_delete (int argc, char **argv);
    79int spline_getspline (int argc, char **argv);
     
    1113  {1, "create",     spline_create,     "create a spline"},
    1214  {1, "apply",      spline_apply,      "apply a spline"},
     15  {1, "load",       spline_load,       "write a spline to a FITS file"},
     16  {1, "save",       spline_save,       "read a spline from a FITS file"},
    1317  {1, "delete",     spline_delete,     "delete a spline"},
    1418};
  • branches/eam_branches/ipp-20120905/Ohana/src/opihi/cmd.data/spline_commands.c

    r34526 r34529  
    6969}
    7070
     71int spline_save (int argc, char **argv) {
     72
     73  int N;
     74
     75  int APPEND = FALSE;
     76  if ((N = get_argument (argc, argv, "-append"))) {
     77    APPEND = TRUE;
     78    remove_argument (N, &argc, argv);
     79  }
     80
     81  if (argc != 3) {
     82    gprint (GP_ERR, "USAGE: spline save (name) (filename) [-append]\n");
     83    return FALSE;
     84  }
     85
     86  if (!SaveSpline(argv[2], argv[1], APPEND)) {
     87    gprint (GP_ERR, "failed to save spline %s\n", argv[1]);
     88    return (FALSE);
     89  }
     90  return TRUE;
     91}
     92
     93int spline_load (int argc, char **argv) {
     94
     95  if (argc != 3) {
     96    gprint (GP_ERR, "USAGE: spline load (name) (filename)\n");
     97    return FALSE;
     98  }
     99
     100  if (!LoadSpline(argv[2], argv[1])) {
     101    gprint (GP_ERR, "failed to load spline %s\n", argv[1]);
     102    return (FALSE);
     103  }
     104  return TRUE;
     105}
     106
    71107int spline_delete (int argc, char **argv) {
    72108
  • branches/eam_branches/ipp-20120905/Ohana/src/opihi/cmd.data/write_vectors.c

    r33963 r34529  
    9999  }
    100100
    101   /* open file for outuput */
     101  /* open file for output */
    102102  if (append) {
    103103    f = fopen (argv[1], "a");
  • branches/eam_branches/ipp-20120905/Ohana/src/opihi/include/data.h

    r34525 r34529  
    190190int DeleteSpline (Spline *spline);
    191191void ListSplines ();
     192int SaveSpline (char *filename, char *name, int append);
     193int LoadSpline (char *filename, char *name);
    192194
    193195/* hermitian functions */
  • 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}
  • branches/eam_branches/ipp-20120905/Ohana/src/opihi/lib.shell/VectorIO.c

    r33963 r34529  
    22 
    33// write a set of vectors to a FITS file (vectors names become fits column names)
    4 int WriteVectorTableFITS (char *filename, char *extname, Vector **vec, int Nvec, int append, char *format) {
    5  
     4int WriteVectorTable (Header *theader, FTable *ftable, char *extname, Vector **vec, int Nvec, char *format) {
     5 
     6  int j;
     7
    68  char *tformat = NULL;
    7   Header header;
    8   Matrix matrix;
    9   Header theader;
    10   FTable ftable;
    11 
    12   int j;
    13   FILE *f = NULL;
    14 
    15   /* open file for outuput */
    16   if (append) {
    17     f = fopen (filename, "a");
    18   } else {
    19     f = fopen (filename, "w");
    20   }
    21   if (f == (FILE *) NULL) {
    22     gprint (GP_ERR, "can't open file for write : %s\n", filename);
    23     return (FALSE);
    24   }
    25 
    26   if (!append) {
    27     gfits_init_header (&header);
    28     header.extend = TRUE;
    29     gfits_create_header (&header);
    30     gfits_create_matrix (&header, &matrix);
    31   }
    32 
    33   gfits_create_table_header (&theader, "BINTABLE", extname);
     9
     10  gfits_create_table_header (theader, "BINTABLE", extname);
    3411
    3512  ALLOCATE (tformat, char, 2*Nvec);
     
    7148  // this somehow
    7249  for (j = 0; j < Nvec; j++) {
    73     gfits_define_bintable_column (&theader, &tformat[2*j], vec[j][0].name, NULL, NULL, 1.0, 0.0);
     50    gfits_define_bintable_column (theader, &tformat[2*j], vec[j][0].name, NULL, NULL, 1.0, 0.0);
    7451  }
    7552  free (tformat);
    7653
    7754  // generate the output array that carries the data
    78   gfits_create_table (&theader, &ftable);
     55  gfits_create_table (theader, ftable);
    7956
    8057  // add the vectors to the output array
    8158  for (j = 0; j < Nvec; j++) {
    8259    if (vec[j][0].type == OPIHI_FLT) {
    83       gfits_set_bintable_column_reformat (&theader, &ftable, vec[j][0].name, "double", vec[j][0].elements.Flt, vec[j][0].Nelements);
     60      gfits_set_bintable_column_reformat (theader, ftable, vec[j][0].name, "double", vec[j][0].elements.Flt, vec[j][0].Nelements);
    8461    } else {
    85       gfits_set_bintable_column_reformat (&theader, &ftable, vec[j][0].name, "int", vec[j][0].elements.Int, vec[j][0].Nelements);
    86     }
    87   }
     62      gfits_set_bintable_column_reformat (theader, ftable, vec[j][0].name, "int", vec[j][0].elements.Int, vec[j][0].Nelements);
     63    }
     64  }
     65  return (TRUE);
     66
     67 escape:
     68  if (tformat) free (tformat);
     69  return (FALSE);
     70}
     71 
     72// write a set of vectors to a FITS file (vectors names become fits column names)
     73int WriteVectorTableFITS (char *filename, char *extname, Vector **vec, int Nvec, int append, char *format) {
     74 
     75  Header header;
     76  Matrix matrix;
     77  Header theader;
     78  FTable ftable;
     79
     80  FILE *f = NULL;
     81
     82  /* open file for outuput */
     83  if (append) {
     84    f = fopen (filename, "a");
     85  } else {
     86    f = fopen (filename, "w");
     87  }
     88  if (f == (FILE *) NULL) {
     89    gprint (GP_ERR, "can't open file for write : %s\n", filename);
     90    return (FALSE);
     91  }
     92
     93  if (!WriteVectorTable (&theader, &ftable, extname, vec, Nvec, format)) goto escape;
    8894
    8995  if (!append) {
     96    gfits_init_header (&header);
     97    header.extend = TRUE;
     98    gfits_create_header (&header);
     99    gfits_create_matrix (&header, &matrix);
    90100    gfits_fwrite_header  (f, &header);
    91101    gfits_fwrite_matrix  (f, &matrix);
     
    111121  gfits_free_table (&ftable);
    112122
    113   if (tformat) free (tformat);
    114123  fclose (f);
    115124  fflush (f);
Note: See TracChangeset for help on using the changeset viewer.