Changeset 36568
- Timestamp:
- Feb 27, 2014, 1:43:34 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20140206/Ohana/src/libfits/table/F_get_column.c
r35101 r36568 174 174 int gfits_get_bintable_column (Header *header, FTable *table, char *label, void **data) { 175 175 176 off_t Nx, Ny; 177 int i, N, Nfields, Nval, Nbytes, Nstart, Nv, Nb; 178 char tlabel[80], field[80], format[80], type[16], tmpline[16]; 179 char *Pin, *Pout, *array; 180 double Bscale, Bzero; 181 182 if (label == (char *) NULL) return (FALSE); 183 if (label[0] == 0) return (FALSE); 184 185 /* find label in header */ 186 tlabel[0] = 0; 187 gfits_scan (header, "TFIELDS", "%d", 1, &Nfields); 188 for (i = 1; strcasecmp (label, tlabel) && (i < Nfields + 1); i++) { 189 sprintf (field, "TTYPE%d", i); 190 gfits_scan (header, field, "%s", 1, tlabel); 191 } 192 if (strcasecmp (label, tlabel)) return (FALSE); 193 N = i - 1; 194 195 Bscale = 1; 196 Bzero = 0; 197 198 /* interpret format */ 199 sprintf (field, "TSCAL%d", N); 200 gfits_scan (header, field, "%lf", 1, &Bscale); 201 sprintf (field, "TZERO%d", N); 202 gfits_scan (header, field, "%lf", 1, &Bzero); 203 sprintf (field, "TFORM%d", N); 204 gfits_scan (header, field, "%s", 1, format); 205 206 if (!gfits_bintable_format (format, type, &Nval, &Nbytes)) return (FALSE); 207 208 /* check existing table dimensions */ 209 gfits_scan (header, "NAXIS1", OFF_T_FMT, 1, &Nx); 210 gfits_scan (header, "NAXIS2", OFF_T_FMT, 1, &Ny); 211 212 /* scan columns to find insert point */ 213 Nstart = 0; 214 for (i = 1; i < N; i++) { 215 sprintf (field, "TFORM%d", i); 216 gfits_scan (header, field, "%s", 1, format); 217 gfits_bintable_format (format, tmpline, &Nv, &Nb); 218 Nstart += Nv*Nb; 219 } 220 221 /* extract bytes from table into temporary array */ 222 ALLOCATE (array, char, Nbytes*Nval*Ny); 223 Pin = table[0].buffer + Nstart; 224 Pout = array; 225 for (i = 0; i < Ny; i++, Pin += Nx, Pout += Nval*Nbytes) { 226 memcpy (Pout, Pin, Nval*Nbytes); 227 } 228 229 /* convert data in-situ with correct type, byte swap and Bzero/Bscale */ 230 Pin = array; 231 Pout = array; 232 if (!strcmp (type, "char")) { 233 for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) { 234 *(char *)Pout = *(char *)Pin*Bscale + Bzero; 235 } 236 } 237 if (!strcmp (type, "short")) { 238 for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) { 239 # ifdef BYTE_SWAP 240 SWAP_BYTE; 241 # endif 242 *(short *)Pout = *(short *)Pin*Bscale + Bzero; 243 } 244 } 245 if (!strcmp (type, "int")) { 246 for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) { 247 # ifdef BYTE_SWAP 248 SWAP_WORD; 249 # endif 250 *(int *)Pout = *(int *)Pin*Bscale + Bzero; 251 } 252 } 253 if (!strcmp (type, "int64_t")) { 254 // XXX 64 bit int operations with Bzero & Bscale are inaccurate even with doubles 255 if ((Bzero == 0.0) && (Bscale == 1.0)) { 256 for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) { 257 # ifdef BYTE_SWAP 258 SWAP_DBLE; 259 # endif 260 *(int64_t *)Pout = *(int64_t *)Pin; 261 } 262 } else { 263 for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) { 264 # ifdef BYTE_SWAP 265 SWAP_DBLE; 266 # endif 267 *(int64_t *)Pout = *(int64_t *)Pin*Bscale + Bzero; 268 } 269 } 270 } 271 if (!strcmp (type, "float")) { 272 for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) { 273 # ifdef BYTE_SWAP 274 SWAP_WORD; 275 # endif 276 *(float *)Pout = *(float *)Pin*Bscale + Bzero; 277 } 278 } 279 if (!strcmp (type, "double")) { 280 for (i = 0; i < Nval*Ny; i++, Pin+=Nbytes, Pout+=Nbytes) { 281 # ifdef BYTE_SWAP 282 SWAP_DBLE; 283 # endif 284 *(double *)Pout = *(double *)Pin*Bscale + Bzero; 285 } 286 } 176 char type[16]; 177 off_t Nrow; 178 int Ncol; 179 180 char *array = gfits_get_bintable_column_data (header, table, label, type, &Nrow, &Ncol); 181 if (array == NULL) return (FALSE); 287 182 288 183 *data = array; 289 return (TRUE);184 return TRUE; 290 185 } 291 186
Note:
See TracChangeset
for help on using the changeset viewer.
