- Timestamp:
- Oct 8, 2012, 2:30:52 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20120905/Ohana/src/opihi/lib.data/SplineOps.c
r34526 r34529 27 27 void InitSpline (Spline *spline, char *name, int Nknots) { 28 28 29 if (!spline[0].name) { 29 30 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 { 32 39 ALLOCATE (spline[0].xk, opihi_flt, spline[0].Nknots); 33 40 ALLOCATE (spline[0].yk, opihi_flt, spline[0].Nknots); 34 41 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)); 38 46 } 39 47 … … 76 84 77 85 spline = FindSpline (name); 78 if (spline != NULL) return (spline); 86 if (spline != NULL) { 87 InitSpline (spline, name, Nknots); 88 return (spline); 89 } 79 90 80 91 N = Nsplines; … … 82 93 CHECK_REALLOCATE (splines, Spline *, NSPLINES, Nsplines, 16); 83 94 ALLOCATE (spline, Spline, 1); 95 spline->name = NULL; 96 spline->xk = NULL; 97 spline->yk = NULL; 98 spline->y2 = NULL; 84 99 InitSpline (spline, name, Nknots); 85 100 splines[N] = spline; … … 126 141 return; 127 142 } 143 144 // write the spline data to a FITS file. 145 int 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 210 int 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.
