Changeset 6618 for branches/rel10_ifa/psModules/src/astrom/pmFPAConstruct.c
- Timestamp:
- Mar 16, 2006, 3:47:44 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/rel10_ifa/psModules/src/astrom/pmFPAConstruct.c
r6589 r6618 1 1 #include <stdio.h> 2 #include <strings.h> 2 #include <assert.h> 3 #include <string.h> 3 4 #include "pslib.h" 5 #include "psMetadataItemParse.h" 4 6 5 7 #include "pmFPA.h" 6 8 #include "pmConcepts.h" 7 9 #include "pmFPAConstruct.h" 10 #include "pmFPAview.h" 8 11 9 12 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// … … 31 34 } 32 35 33 // Find a chip by name 34 static pmChip *findChip(pmFPA *fpa, // FPA in which to find the chip35 const char *name // Name of the chip36 )36 // Find a chip by name; return the index 37 static int findChip(pmFPA *fpa, // FPA in which to find the chip 38 const char *name // Name of the chip 39 ) 37 40 { 38 41 psArray *chips = fpa->chips; // Array of chips … … 41 44 psString testName = psMetadataLookupStr(NULL, chip->concepts, "CHIP.NAME"); // Name of this chip 42 45 if (strcmp(name, testName) == 0) { 43 return chip;46 return i; 44 47 } 45 48 } 46 49 47 50 psError(PS_ERR_IO, true, "Unable to find chip %s\n", name); 48 return NULL;49 } 50 51 // Find a cell by name 52 static pmCell *findCell(pmChip *chip, // Chip in which to find the cell53 const char *name // Name of the cell54 )51 return -1; 52 } 53 54 // Find a cell by name; return the index 55 static int findCell(pmChip *chip, // Chip in which to find the cell 56 const char *name // Name of the cell 57 ) 55 58 { 56 59 psArray *cells = chip->cells; // Array of cells … … 59 62 psString testName = psMetadataLookupStr(NULL, cell->concepts, "CELL.NAME"); // Name of this cell 60 63 if (strcmp(name, testName) == 0) { 61 return cell;64 return i; 62 65 } 63 66 } 64 67 65 68 psError(PS_ERR_IO, true, "Unable to find cell %s\n", name); 66 return NULL;69 return -1; 67 70 } 68 71 … … 100 103 } 101 104 102 // Get the chip name from a primary header lookup 103 static psString chipNameFromHeader(const psMetadata *format, // FORMAT within the camera format description 104 const psMetadata *header // Primary header 105 ) 105 // Get the name of a PHU chip or cell from the header 106 static psString phuNameFromHeader(const char *name, // The name to lookup: "CELL.NAME" or "CHIP.NAME" 107 const psMetadata *format, // FORMAT within the camera format description 108 const psMetadata *header // Primary header 109 ) 106 110 { 107 111 bool mdok = true; // Result of MD lookup 108 psString chipNameHeader = psMetadataLookupStr(&mdok, format, "CHIP.NAME");109 if (!mdok || strlen( chipNameHeader) == 0) {110 psError(PS_ERR_IO, false, "Unable to find CHIP.NAME in the FORMAT.\n");112 psString keyword = psMetadataLookupStr(&mdok, format, name); 113 if (!mdok || strlen(keyword) == 0) { 114 psError(PS_ERR_IO, false, "Unable to find %s in the FORMAT.\n", name); 111 115 return false; 112 116 } 113 psString chipName = psMetadataLookupStr(&mdok, header, chipNameHeader);114 if (! chipName || strlen(chipName) == 0) {115 psError(PS_ERR_IO, false, "Unable to find %s in primary header to identify chip name.\n", chipName);117 psString result = psMetadataLookupStr(&mdok, header, keyword); 118 if (!mdok || strlen(result) == 0) { 119 psError(PS_ERR_IO, false, "Unable to find %s in primary header to identify %s.\n", keyword, name); 116 120 return NULL; 117 121 } 118 122 119 return chipName;123 return result; 120 124 } 121 125 … … 159 163 160 164 // Add an input file to the FPA 161 bool pmFPAAddSource(pmFPA *fpa, // FPA to which to add 162 pmChip *chip, // Chip to which to add, or NULL 163 pmCell *cell, // Cell to which to add, or NULL 164 psMetadata *phu, // Primary header of file 165 psMetadata *format // Format of file 166 ) 167 { 168 // Just in case someone's playing silly billy... 169 if (cell && !chip) { 170 chip = cell->parent; 171 } 172 if (chip && !fpa) { 173 fpa = chip->parent; 174 } 165 pmFPAview *pmFPAAddSource(pmFPA *fpa, // The FPA 166 psMetadata *phu, // Primary header of file 167 psMetadata *format // Format of file 168 ) 169 { 170 assert(fpa); 171 assert(phu); 172 assert(format); 173 174 pmChip *chip = NULL; // The chip, if any one in particular 175 pmCell *cell = NULL; // The cell, if any one in particular 176 psString phuChipName = NULL; // Name of the chip from the PHU 175 177 176 178 bool mdok = true; // Status from metadata lookups … … 178 180 if (!mdok || !formatSpec) { 179 181 psError(PS_ERR_IO, false, "Unable to find FORMAT in the camera format configuration.\n"); 180 return false; 181 } 182 return NULL; 183 } 184 185 // Check the name of the FPA 186 const char *nameKeyword = psMetadataLookupStr(&mdok, formatSpec, "FPA.NAME"); // Keyword that gives name 187 if (!mdok || strlen(nameKeyword) == 0) { 188 psError(PS_ERR_IO, false, "Unable to find FPA.NAME in the format specification.\n"); 189 return NULL; 190 } 191 psMetadataItem *fpaNameItem = psMetadataLookup(phu, nameKeyword); // Name of the FPA 192 const char *newFPAname = psMetadataItemParseString(fpaNameItem); // FPA.NAME for new source 193 fpaNameItem = psMetadataLookup(fpa->concepts, "FPA.NAME"); // Current name of FPA 194 if (fpaNameItem && fpaNameItem->type == PS_DATA_STRING) { 195 const char *currentFPAname = fpaNameItem->data.V; // Name of the current FPA 196 if (currentFPAname && strlen(currentFPAname) > 0 && strcmp(currentFPAname, newFPAname) != 0) { 197 psLogMsg(__func__, PS_LOG_WARN, "FPA.NAME for new source (%s) doesn't match FPA.NAME for current " 198 "fpa (%s).\n", newFPAname, currentFPAname); 199 } 200 } 201 psMetadataAddStr(fpa->concepts, PS_LIST_HEAD, "FPA.NAME", PS_META_REPLACE, "Name of FPA", newFPAname); 182 202 183 203 // Where does the PHU go? … … 185 205 if (!mdok || strlen(phuType) == 0) { 186 206 psError(PS_ERR_IO, false, "Unable to find PHU in the format specification.\n"); 187 return false;207 return NULL; 188 208 } 189 209 pmHDU *phdu = pmHDUAlloc("PHU"); // The primary header data unit 190 210 phdu->header = psMemIncrRefCounter(phu); 191 211 phdu->format = psMemIncrRefCounter(format); 192 if (fpa && !fpa->hdu && strcasecmp(phuType, "FPA") == 0) { 212 213 // Generate the view 214 pmFPAview *view = pmFPAviewAlloc(0); // The FPA view corresponding to the PHU, to be returned 215 view->chip = -1; 216 view->cell = -1; 217 view->readout = -1; 218 view->fpa = psMemIncrRefCounter(fpa); 219 if (strcasecmp(phuType, "FPA") == 0) { 220 if (fpa->hdu) { 221 // Something's already here 222 psError(PS_ERR_IO, true, "Unable to add source since FPA already has a PHU.\n"); 223 psFree(view); 224 return NULL; 225 } 193 226 fpa->hdu = phdu; 194 227 pmConceptsReadFPA(fpa, PM_CONCEPT_SOURCE_HEADER, NULL); 195 } else if (chip && !chip->hdu && strcasecmp(phuType, "CHIP") == 0) { 196 chip->hdu = phdu; 197 pmConceptsReadChip(chip, PM_CONCEPT_SOURCE_HEADER, NULL); 198 } else if (cell && !cell->hdu && strcasecmp(phuType, "CELL") == 0) { 199 // cell->hdu = phdu; 200 psError(PS_ERR_IO, true, "The case of PHU == CELL has not been written yet!\n"); 201 return false; 228 } else { 229 // Get the chip 230 phuChipName = phuNameFromHeader("CHIP.NAME", formatSpec, phu); // Name of the chip 231 int chipNum = findChip(fpa, phuChipName); // Chip number 232 if (chipNum == -1) { 233 psError(PS_ERR_IO, true, "Unable to find chip %s in FPA.\n", phuChipName); 234 psFree(view); 235 return NULL; 236 } 237 chip = fpa->chips->data[chipNum]; 238 view->chip = chipNum; 239 240 if (strcasecmp(phuType, "CHIP") == 0) { 241 if (chip->hdu) { 242 // Something's already here 243 psError(PS_ERR_IO, true, "Unable to add source since chip already has a PHU.\n"); 244 psFree (view); 245 return NULL; 246 } 247 chip->hdu = phdu; 248 pmConceptsReadChip(chip, PM_CONCEPT_SOURCE_HEADER, true, NULL); 249 } else if (strcasecmp(phuType, "CELL") == 0) { 250 psError(PS_ERR_IO, true, "Not yet sure how to handle PHU=CELL.\n"); 251 psFree(view); 252 return NULL; 253 #if 0 254 // Added this for possible future plans which might allow PHU=CELL 255 phuCellName = phuNameFromHeader("CELL.NAME", formatSpec, phu); // Name of the cell 256 int cellNum = findCell(chip, phuCellName); // Cell number 257 if (cellNum == -1) { 258 psError(PS_ERR_IO, true, "Unable to find cell %s in chip %s.\n", phuCellName, phuChipName); 259 psFree(view); 260 return NULL; 261 } 262 cell = chip->cells->data[cellNum]; 263 view->cell = cellNum; 264 265 if (cell->hdu) { 266 // Something's already here 267 psError(PS_ERR_IO, true, "Unable to add source since cell already has a PHU.\n"); 268 psFree(view); 269 return NULL; 270 } 271 cell->hdu = phdu; 272 pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_HEADER, true, NULL); 273 #endif 274 275 } else { 276 psError(PS_ERR_IO, true, "The format of the PHU (%s) is not FPA, CHIP or CELL.\n", phuType); 277 psFree(view); 278 return NULL; 279 } 202 280 } 203 281 … … 206 284 if (!mdok || strlen(extType) == 0) { 207 285 psError(PS_ERR_IO, false, "Unable to find EXTENSIONS in the format specification.\n"); 208 return false; 286 psFree(view); 287 return NULL; 209 288 } 210 289 psMetadata *contents = psMetadataLookupMD(&mdok, format, "CONTENTS"); // The contents of the FITS file 211 290 if (!mdok || !contents) { 212 291 psError(PS_ERR_IO, false, "Unable to find CONTENTS in the camera format configuration.\n"); 213 return false; 214 } 215 292 psFree(view); 293 return NULL; 294 } 295 296 297 // Now, different actions, according to what's in the file: 298 299 // No extensions --- it's all in the PHU 216 300 if (strcasecmp(extType, "NONE") == 0) { 217 // No extensions --- it's all in the PHU 218 psString chipType = chipNameFromHeader(formatSpec, phu); // Type of chip 219 printf("chipType: %s<---\n", chipType); 220 #if 1 // This is here for the courtesy of MegaCam, which has "CCD13 " 221 222 char *space = NULL; // Position of a space 223 if ((space = strchr(chipType, ' '))) { 224 psString temp = psStringNCopy(chipType, strlen(chipType) - strlen(space)); 225 // Free memory??? 226 chipType = temp; 227 } 228 #endif 229 230 psString content = psMetadataLookupStr(&mdok, contents, chipType); // The content line 301 if (strcasecmp(phuType, "FPA") == 0) { 302 psError(PS_ERR_IO, true, "Not sure how to handle the case PHU=FPA, EXTENSIONS=NONE.\n"); 303 psFree(view); 304 return NULL; 305 } 306 if (strcasecmp(phuType, "CELL") == 0) { 307 psError(PS_ERR_IO, true, "Not sure how to handle the case PHU=CELL, EXTENSIONS=NONE.\n"); 308 psFree(view); 309 return NULL; 310 } 311 312 psString content = psMetadataLookupStr(&mdok, contents, phuChipName); // The content line 231 313 if (!mdok || strlen(content) == 0) { 232 psError(PS_ERR_IO, true, "Cannot find chip %s in the list of CONTENTS.\n"); 233 psFree(chipType); 234 psFree(content); 235 return false; 314 psError(PS_ERR_IO, true, "Cannot find chip %s in the list of CONTENTS.\n", phuChipName); 315 psFree(view); 316 return NULL; 236 317 } 237 318 … … 241 322 parsePairs(&names, &values, content); 242 323 for (int i = 0; i < names->n; i++) { 243 psString chipName = names->data[i]; // The name of the chip 244 psString cellName = values->data[i]; // The name of the cell 245 pmChip *chip = findChip(fpa, chipName); // The chip we're looking for 246 if (!chip) { 247 psLogMsg(__func__, PS_LOG_WARN, "Unable to find chip %s in fpa --- ignored.\n", chipName); 324 psString chipName = names->data[i]; // The name of the chip specified 325 if (phuChipName && strcmp(phuChipName, chipName) != 0) { 326 psLogMsg(__func__, PS_LOG_WARN, "Chip name in CONTENT (%s) does not match chip name from PHU " 327 "(%s) --- there may be problems when reading!\n", chipName, phuChipName); 328 // Find the other chip 329 int chipNum = findChip(fpa, chipName); // The chip we're looking for 330 if (chipNum == -1) { 331 psLogMsg(__func__, PS_LOG_WARN, "Unable to find chip %s in fpa --- ignored.\n", chipName); 332 continue; 333 } 334 chip = fpa->chips->data[chipNum]; 335 } 336 psString cellName = values->data[i]; // The name of the cell specified 337 int cellNum = findCell(chip, cellName); // The cell we're looking for 338 if (cellNum == -1) { 339 psLogMsg(__func__, PS_LOG_WARN, "Unable to find cell %s in chip %s --- ignored.\n", 340 cellName, chipName); 248 341 continue; 249 342 } 250 pmCell *cell = findCell(chip, cellName); // The cell we're looking for 251 if (!cell) { 252 psLogMsg(__func__, PS_LOG_WARN, "Unable to find cell %s in chip %s --- ignored.\n", cellName, 253 chipName); 254 continue; 255 } 343 cell = chip->cells->data[cellNum]; 344 256 345 psMetadata *cellData = getCellData(format, cellName); // Data for this cell 257 346 258 // Have already plugged in the PHU 347 // Have already plugged in the PHU, which is all we have 259 348 260 349 // Put in the cell data … … 265 354 } 266 355 cell->config = psMemIncrRefCounter(cellData); 267 pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_CAMERA | PM_CONCEPT_SOURCE_DEFAULTS, NULL); 268 } 269 return true; 270 } 271 272 273 // Go through the contents 356 pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_CAMERA | PM_CONCEPT_SOURCE_DEFAULTS, false, NULL); 357 } 358 359 return view; 360 } 361 362 363 // Otherwise, go through the contents 274 364 psMetadataIterator *contentsIter = psMetadataIteratorAlloc(contents, PS_LIST_HEAD, NULL); 275 365 psMetadataItem *contentsItem = NULL; // Item from contents … … 295 385 psString chipName = names->data[i]; // The name of the chip 296 386 psString cellName = values->data[i]; // The name of the cell 297 pmChip *chip= findChip(fpa, chipName); // The chip we're looking for298 if ( !chip) {387 int chipNum = findChip(fpa, chipName); // The chip we're looking for 388 if (chipNum == -1) { 299 389 psLogMsg(__func__, PS_LOG_WARN, "Unable to find chip %s in FPA --- ignored.\n", chipName); 300 390 continue; 301 391 } 302 pmCell *cell = findCell(chip, cellName); // The cell we're looking for 303 if (!cell) { 392 chip = fpa->chips->data[chipNum]; 393 int cellNum = findCell(chip, cellName); // The cell we're looking for 394 if (cellNum == -1) { 304 395 psLogMsg(__func__, PS_LOG_WARN, "Unable to find cell %s in chip %s --- ignored\n", 305 396 cellName, chipName); 306 397 continue; 307 398 } 399 pmCell *cell = chip->cells->data[cellNum]; 308 400 psMetadata *cellData = getCellData(format, cellName); // Data for this cell 309 401 … … 322 414 } 323 415 cell->config = psMemIncrRefCounter(cellData); 324 pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_CAMERA | PM_CONCEPT_SOURCE_DEFAULTS, NULL);416 pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_CAMERA | PM_CONCEPT_SOURCE_DEFAULTS, false, NULL); 325 417 } 326 418 … … 331 423 if (! fpa->hdu->phu) { 332 424 // Need to look up the name of the chip 333 chipName = chipNameFromHeader(formatSpec, phu);334 chip= findChip(fpa, chipName);335 if ( !chip) {425 chipName = phuNameFromHeader("CHIP.NAME", formatSpec, phu); 426 int chipNum = findChip(fpa, chipName); 427 if (chipNum == -1) { 336 428 psLogMsg(__func__, PS_LOG_WARN, "Unable to find chip %s in FPA --- ignored.\n", chipName); 337 429 continue; 338 430 } 431 chip = fpa->chips->data[chipNum]; 339 432 } 340 433 for (int i = 0; i < names->n; i++) { … … 345 438 chipName = names->data[i]; 346 439 cellName = cellType; 347 chip = findChip(fpa, chipName);348 if ( ! chip) {440 int chipNum = findChip(fpa, chipName); // The chip we're looking for 441 if (chipNum == -1) { 349 442 psLogMsg(__func__, PS_LOG_WARN, "Unable to find chip %s in FPA --- ignored.\n", 350 443 chipName); 351 444 continue; 352 445 } 446 chip = fpa->chips->data[chipNum]; 353 447 } else { 354 448 // We've got cellName:cellType and the chipName comes from before 355 449 cellName = names->data[i]; 356 450 } 357 pmCell *cell= findCell(chip, cellName); // The cell we're looking for358 if ( !cell) {451 int cellNum = findCell(chip, cellName); // The cell we're looking for 452 if (cellNum == -1) { 359 453 psLogMsg(__func__, PS_LOG_WARN, "Unable to find cell %s in chip %s --- ignored.\n", 360 454 cellName, chipName); 361 455 continue; 362 456 } 457 pmCell *cell = chip->cells->data[cellNum]; 363 458 psMetadata *cellData = getCellData(format, cellType); // Data for this cell 364 459 … … 378 473 } 379 474 cell->config = psMemIncrRefCounter(cellData); 380 pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_CAMERA | PM_CONCEPT_SOURCE_DEFAULTS, NULL);475 pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_CAMERA | PM_CONCEPT_SOURCE_DEFAULTS, false, NULL); 381 476 } 382 477 … … 390 485 pmConceptsReadFPA(fpa, PM_CONCEPT_SOURCE_DEFAULTS, NULL); 391 486 392 return true;487 return view; 393 488 } 394 489 … … 663 758 pmReadout *readout = readouts->data[k]; // The readout 664 759 psImage *image = readout->image; // The image 665 psTrace(__func__, 8, "Image: [%d:%d,%d:%d] (%dx%d)\n", image->col0, image->col0 + 666 image->numCols, image->row0, image->row0 + image->numRows, image->numCols, 667 image->numRows); 760 if (image) { 761 psTrace(__func__, 8, "Image: [%d:%d,%d:%d] (%dx%d)\n", image->col0, image->col0 + 762 image->numCols, image->row0, image->row0 + image->numRows, image->numCols, 763 image->numRows); 764 } 668 765 } // Iterating over cell 669 766 } // Iterating over chip
Note:
See TracChangeset
for help on using the changeset viewer.
