- Timestamp:
- Feb 10, 2012, 1:55:31 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-20111122/Ohana/src/uniphot/src/ImageSubset.c
r33227 r33241 1 # include " uniphot.h"1 # include "setphot.h" 2 2 3 typedef struct { 4 float Mcal; 5 float dMcal; 6 unsigned int imageID; 7 unsigned int photom_map_id; 8 unsigned int flags; 9 } ImageSubset; 3 ImageSubset *ImageSubsetLoad(char *filename, off_t *nimage) { 10 4 11 ImageSubset *ImageSubsetLoad(char *filename, int *nimage) { 12 13 int i, Ncol; 5 int i, Ncol, Ncol2; 14 6 off_t Nrow; 15 7 Header header; … … 53 45 // this means I need 3 copies in memory at some point. ugh. 54 46 47 char type[16]; 48 55 49 // need to create and assign to flat-field correction 56 50 float *Mcal = gfits_get_bintable_column_data (&theader, &ftable, "MCAL", type, &Nrow, &Ncol); … … 58 52 59 53 float *dMcal = gfits_get_bintable_column_data (&theader, &ftable, "MCAL_ERR", type, &Nrow, &Ncol2); 60 assert (!strcmp(type, "float"), "wrong column type");61 assert (Ncol == Ncol2, "table length mismatch");54 myAssert (!strcmp(type, "float"), "wrong column type"); 55 myAssert (Ncol == Ncol2, "table length mismatch"); 62 56 63 57 unsigned int *imageID = gfits_get_bintable_column_data (&theader, &ftable, "IMAGE_ID", type, &Nrow, &Ncol2); 64 assert (!strcmp(type, "int"), "wrong column type");65 assert (Ncol == Ncol2, "table length mismatch");58 myAssert (!strcmp(type, "int"), "wrong column type"); 59 myAssert (Ncol == Ncol2, "table length mismatch"); 66 60 67 61 unsigned int *map = gfits_get_bintable_column_data (&theader, &ftable, "PHOTOM_MAP", type, &Nrow, &Ncol2); 68 assert (!strcmp(type, "int"), "wrong column type");69 assert (Ncol == Ncol2, "table length mismatch");62 myAssert (!strcmp(type, "int"), "wrong column type"); 63 myAssert (Ncol == Ncol2, "table length mismatch"); 70 64 71 65 unsigned int *flags = gfits_get_bintable_column_data (&theader, &ftable, "FLAGS", type, &Nrow, &Ncol2); 72 assert (!strcmp(type, "int"), "wrong column type");73 assert (Ncol == Ncol2, "table length mismatch");66 myAssert (!strcmp(type, "int"), "wrong column type"); 67 myAssert (Ncol == Ncol2, "table length mismatch"); 74 68 75 69 // XXX free the fits table data here … … 95 89 } 96 90 97 int ImageSubsetSave(char *filename, Image *image, int Nimage) {91 int ImageSubsetSave(char *filename, Image *image, off_t Nimage) { 98 92 99 int i, Ncol; 100 off_t Nrow; 93 int i; 101 94 Header header; 102 95 Header theader; … … 109 102 gfits_create_matrix (&header, &matrix); 110 103 111 gfits_create_table_header (&theader, "BINTABLE", FITS);104 gfits_create_table_header (&theader, "BINTABLE", "IMAGE_SUBSET"); 112 105 113 106 gfits_define_bintable_column (&theader, "E", "MCAL", "zero point offset", "magnitudes", 1.0, 0.0); … … 122 115 gfits_create_table (&theader, &ftable); 123 116 124 // add the vectors to the output array 125 ALLOCATE (Mcal, float, Nimage); 117 float *Mcal, *dMcal; 118 unsigned int *imageID, *map, *flags; 119 120 // create intermediate storage arrays 121 ALLOCATE (Mcal, float, Nimage); 126 122 ALLOCATE (dMcal, float, Nimage); 127 123 ALLOCATE (imageID, unsigned int, Nimage); 128 ALLOCATE (imageID, unsigned int, Nimage); 124 ALLOCATE (map, unsigned int, Nimage); 125 ALLOCATE (flags, unsigned int, Nimage); 129 126 130 for 131 gfits_set_bintable_column_reformat (&theader, &ftable, vec[j][0].name, "double", vec[j][0].elements.Flt, vec[j][0].Nelements); 132 } else { 133 gfits_set_bintable_column_reformat (&theader, &ftable, vec[j][0].name, "int", vec[j][0].elements.Int, vec[j][0].Nelements); 134 } 127 // assign the storage arrays 128 for (i = 0; i < Nimage; i++) { 129 imageID[i] = image[i].imageID; 130 map[i] = image[i].photom_map_id; 131 flags[i] = image[i].flags; 132 Mcal[i] = image[i].Mcal; 133 dMcal[i] = image[i].dMcal; 135 134 } 136 135 137 FILE *f = fopen (filename, "w"); 138 if (!f) { 139 fprintf (stderr, "ERROR: cannot open image subset file for output %s\n", filename); 140 return NULL; 141 } 142 143 /* load in PHU segment (ignore) */ 144 if (!gfits_fread_header (f, &header)) { 145 if (VERBOSE) fprintf (stderr, "can't read image subset header\n"); 146 fclose (f); 147 return NULL; 148 } 149 if (!gfits_fread_matrix (f, &matrix, &header)) { 150 if (VERBOSE) fprintf (stderr, "can't read image subset matrix\n"); 151 gfits_free_header (&header); 152 fclose (f); 153 return NULL; 154 } 155 156 ftable.header = &theader; 157 158 // load data for this header 159 if (!gfits_load_header (f, &theader)) return (NULL); 160 161 // read the fits table bytes 162 if (!gfits_fread_ftable_data (f, &ftable, FALSE)) return (NULL); 163 164 // XXX close the fits file here 165 166 // a bit annoying : we read the entire block of data (180M), then extract the columns, then set the image structure values. 167 // this means I need 3 copies in memory at some point. ugh. 168 169 // need to create and assign to flat-field correction 170 float *Mcal = gfits_get_bintable_column_data (&theader, &ftable, "MCAL", type, &Nrow, &Ncol); 171 myAssert (!strcmp(type, "float"), "wrong column type"); 172 173 float *dMcal = gfits_get_bintable_column_data (&theader, &ftable, "MCAL_ERR", type, &Nrow, &Ncol2); 174 assert (!strcmp(type, "float"), "wrong column type"); 175 assert (Ncol == Ncol2, "table length mismatch"); 176 177 unsigned int *imageID = gfits_get_bintable_column_data (&theader, &ftable, "IMAGE_ID", type, &Nrow, &Ncol2); 178 assert (!strcmp(type, "int"), "wrong column type"); 179 assert (Ncol == Ncol2, "table length mismatch"); 180 181 unsigned int *map = gfits_get_bintable_column_data (&theader, &ftable, "PHOTOM_MAP", type, &Nrow, &Ncol2); 182 assert (!strcmp(type, "int"), "wrong column type"); 183 assert (Ncol == Ncol2, "table length mismatch"); 184 185 unsigned int *flags = gfits_get_bintable_column_data (&theader, &ftable, "FLAGS", type, &Nrow, &Ncol2); 186 assert (!strcmp(type, "int"), "wrong column type"); 187 assert (Ncol == Ncol2, "table length mismatch"); 188 189 // XXX free the fits table data here 190 191 ALLOCATE (image, ImageSubset, Ncol); 192 for (i = 0; i < Nrow; i++) { 193 image[i].imageID = imageID[i]; 194 image[i].photom_map_id = map[i]; 195 image[i].flags = flags[i]; 196 image[i].Mcal = Mcal[i]; 197 image[i].dMcal = dMcal[i]; 198 } 199 fprintf (stderr, "loaded data for %d images\n", Ncol); 136 // add the columns to the output array 137 gfits_set_bintable_column (&theader, &ftable, "MCAL", Mcal, Nimage); 138 gfits_set_bintable_column (&theader, &ftable, "MCAL_ERR", dMcal, Nimage); 139 gfits_set_bintable_column (&theader, &ftable, "IMAGE_ID", imageID, Nimage); 140 gfits_set_bintable_column (&theader, &ftable, "PHOTOM_MAP", map, Nimage); 141 gfits_set_bintable_column (&theader, &ftable, "FLAGS", flags, Nimage); 200 142 201 143 free (Mcal); … … 205 147 free (flags); 206 148 207 *nimage = Ncol; 208 return image; 149 FILE *f = fopen (filename, "w"); 150 if (!f) { 151 fprintf (stderr, "ERROR: cannot open image subset file for output %s\n", filename); 152 return FALSE; 153 } 154 155 gfits_fwrite_header (f, &header); 156 gfits_fwrite_matrix (f, &matrix); 157 gfits_fwrite_Theader (f, &theader); 158 gfits_fwrite_table (f, &ftable); 159 160 gfits_free_header (&header); 161 gfits_free_matrix (&matrix); 162 gfits_free_header (&theader); 163 gfits_free_table (&ftable); 164 165 return TRUE; 209 166 }
Note:
See TracChangeset
for help on using the changeset viewer.
