Changeset 23268 for trunk/psModules/src/camera/pmFPAfileDefine.c
- Timestamp:
- Mar 11, 2009, 10:54:22 AM (17 years ago)
- File:
-
- 1 edited
-
trunk/psModules/src/camera/pmFPAfileDefine.c (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psModules/src/camera/pmFPAfileDefine.c
r22699 r23268 11 11 #include "pmConfig.h" 12 12 #include "pmConfigMask.h" 13 #include "pmConfigRun.h" 13 14 #include "pmDetrendDB.h" 14 15 … … 216 217 file->imageId = config->imageId; 217 218 } 218 219 // XXX this seems a bit of a hack: use the cameraName to determine the mosaic level...220 # if (0)221 if (cameraName) {222 if (!strcmp(cameraName + strlen(cameraName) - 5, "-CHIP")) {223 file->mosaicLevel = PM_FPA_LEVEL_CHIP;224 }225 if (!strcmp(cameraName + strlen(cameraName) - 5, "-FPA")) {226 file->mosaicLevel = PM_FPA_LEVEL_FPA;227 }228 }229 # endif230 219 231 220 // Use the format we were told to, the format specified in the file rule, or default to the default format … … 383 372 } 384 373 385 psTrace ("psModules.camera", 5, "file: %s, format: %s, fileLevel: %s, extLevel: %s, dataLevel: %s, freeLevel: %s\n", 386 file->name, file->formatName, pmFPALevelToName (file->fileLevel), pmFPALevelToName(extLevel), pmFPALevelToName (file->dataLevel), pmFPALevelToName (file->freeLevel)); 374 psTrace("psModules.camera", 5, 375 "file: %s, format: %s, fileLevel: %s, extLevel: %s, dataLevel: %s, freeLevel: %s\n", 376 file->name, file->formatName, pmFPALevelToName(file->fileLevel), pmFPALevelToName(extLevel), 377 pmFPALevelToName(file->dataLevel), pmFPALevelToName(file->freeLevel)); 387 378 388 379 // add argument-supplied OUTPUT name to this file 389 380 char *outname = psMetadataLookupStr(&status, config->arguments, "OUTPUT"); 390 psMetadataAddStr(file->names, PS_LIST_TAIL, "OUTPUT", PS_META_NO_REPLACE, " ", outname);381 psMetadataAddStr(file->names, PS_LIST_TAIL, "OUTPUT", PS_META_NO_REPLACE, "Output file name", outname); 391 382 392 383 // place the resulting file in the config system 393 psMetadataAddPtr (config->files, PS_LIST_TAIL, name, PS_DATA_UNKNOWN, "", file);384 psMetadataAddPtr(config->files, PS_LIST_TAIL, name, PS_DATA_UNKNOWN, "Output file", file); 394 385 psFree(file); // we free this copy of file, but 'files' still has a copy 395 386 return file; // the returned value is a view into the version on 'files' … … 426 417 } 427 418 428 // search for argname on the config->argument list 429 // construct an FPA based on the files in this list (must represent a single FPA) 430 // built the association between the FPA elements (CHIP/CELL) and the files 431 // define the pmFPAfile filename and bind it to this FPA 432 // save the pmFPAfile on config->files 433 // return the pmFPAfile (a view to the one saved on config->files) 434 pmFPAfile *pmFPAfileDefineFromArgs(bool *success, pmConfig *config, const char *filename, const char *argname) 419 420 /// Define a file from an array of filenames 421 static pmFPAfile *fpaFileDefineFromArray(pmConfig *config, // Configuration 422 pmFPAfile *bind, // File to bind to, or NULL 423 const char *name, // Name of file 424 const psArray *filenames // Array of file names 425 ) 426 { 427 PS_ASSERT_PTR_NON_NULL(config, NULL); 428 PS_ASSERT_STRING_NON_EMPTY(name, NULL); 429 430 pmFPA *fpa = NULL; // FPA for file 431 psMetadata *format = NULL; // Camera format configuration 432 psString formatName = NULL; // Name of camera format 433 psString cameraName = NULL; // Name of camera 434 pmFPALevel fileLevel = PM_FPA_LEVEL_NONE; // Level for files 435 psMetadata *phu = NULL; // Primary header 436 if (bind) { 437 // Use the FPA we're binding to 438 fpa = psMemIncrRefCounter(bind->fpa); 439 fileLevel = bind->fileLevel; 440 } else { 441 // Need to generate an FPA 442 psString realName = pmConfigConvertFilename(filenames->data[0], config, false, false); 443 if (!realName) { 444 psError(PS_ERR_IO, false, "Failed to convert file name %s", (char *)filenames->data[0]); 445 return NULL; 446 } 447 448 // load the header of the first image 449 // EXTWORD (fits->extword) is not relevant to the PHU 450 psFits *fits = psFitsOpen(realName, "r"); // FITS file 451 if (!fits) { 452 psError(PS_ERR_IO, false, "Failed to open file %s", realName); 453 psFree(realName); 454 return NULL; 455 } 456 phu = psFitsReadHeader (NULL, fits); // Primary header 457 if (!phu) { 458 psError(PS_ERR_IO, false, "Failed to read file header %s", realName); 459 psFree(realName); 460 return NULL; 461 } 462 psFitsClose(fits); 463 464 // Determine the current format from the header; determine camera if not specified already. 465 psMetadata *camera = NULL; // Camera configuration 466 format = pmConfigCameraFormatFromHeader(&camera, &cameraName, &formatName, config, phu, true); 467 if (!format) { 468 psError(PS_ERR_IO, false, "Failed to determine camera format for %s", realName); 469 psFree(camera); 470 psFree(formatName); 471 psFree(realName); 472 psFree(phu); 473 psFree(phu); 474 return NULL; 475 } 476 477 fileLevel = pmFPAPHULevel(format); 478 if (fileLevel == PM_FPA_LEVEL_NONE) { 479 psError(PS_ERR_IO, true, "Unable to determine file level for %s", realName); 480 psFree(camera); 481 psFree(formatName); 482 psFree(realName); 483 psFree(phu); 484 return NULL; 485 } 486 487 // build the template fpa, set up the basic view 488 // XXX do we want this to be the baseCamera name or the metaCamera name? 489 fpa = pmFPAConstruct(camera, cameraName); 490 psFree(camera); 491 if (!fpa) { 492 psError(PS_ERR_IO, false, "Failed to construct FPA from %s", realName); 493 psFree(formatName); 494 psFree(realName); 495 psFree(format); 496 psFree(phu); 497 return NULL; 498 } 499 psFree(realName); 500 } 501 502 // load the given filerule (from config->camera) and bind it to the fpa 503 // the returned file is just a view to the entry on config->files 504 pmFPAfile *file = pmFPAfileDefineInput(config, fpa, cameraName, name); // File, to return 505 if (!file) { 506 psError(PS_ERR_IO, false, "File %s not defined", name); 507 psFree(formatName); 508 psFree(format); 509 psFree(fpa); 510 psFree(phu); 511 return NULL; 512 } 513 psFree(cameraName); 514 psFree(fpa); // Drop reference 515 516 file->format = format; 517 file->formatName = formatName; 518 file->fileLevel = fileLevel; 519 520 // We use the filerule and filesrc to identify the files in the file->names data 521 psFree(file->filerule); // this is set in pmFPAfileDefineInput 522 file->filerule = psStringCopy("@FILES"); 523 file->filesrc = psStringCopy("{CHIP.NAME}.{CELL.NAME}"); 524 525 if (file->type == PM_FPA_FILE_MASK) { 526 if (!pmConfigMaskReadHeader(config, phu)) { 527 psError(PS_ERR_IO, false, "Error reading mask bits"); 528 psFree(phu); 529 return NULL; 530 } 531 } 532 533 // Examine the list of input files and validate their cameras 534 // Associate each filename with an element of the FPA 535 // Save the association on file->names 536 for (int i = 0; i < filenames->n; i++) { 537 // Check that the file corresponds to the same camera and format 538 if (!phu) { 539 psString realName = pmConfigConvertFilename(filenames->data[i], config, false, false); 540 if (!realName) { 541 psError(PS_ERR_IO, false, "Failed to convert file name %s", (char*)filenames->data[i]); 542 return NULL; 543 } 544 psFits *fits = psFitsOpen(realName, "r"); // FITS file 545 if (!fits) { 546 psError(PS_ERR_IO, false, "Failed to open file %s", realName); 547 psFree(realName); 548 return NULL; 549 } 550 phu = psFitsReadHeader(NULL, fits); 551 psFitsClose(fits); 552 if (!phu) { 553 psError(PS_ERR_IO, false, "Failed to read file header %s", realName); 554 psFree(realName); 555 return NULL; 556 } 557 psFree(realName); 558 } 559 560 if (bind || i > 0) { 561 if (format) { 562 bool valid = false; 563 if (!pmConfigValidateCameraFormat(&valid, format, phu)) { 564 psError(PS_ERR_UNKNOWN, false, "Error in config scripts\n"); 565 psFree(phu); 566 return NULL; 567 } 568 if (!valid) { 569 psError(PS_ERR_IO, false, "File %s is not from the required camera", 570 (char*)filenames->data[i]); 571 psFree(phu); 572 return NULL; 573 } 574 } else { 575 format = pmConfigCameraFormatFromHeader(NULL, NULL, NULL, config, phu, true); 576 if (!format) { 577 psError(PS_ERR_IO, false, "Failed to determine camera format from %s", 578 (char*)filenames->data[i]); 579 psFree(phu); 580 return NULL; 581 } 582 } 583 } 584 585 // Set the view to the corresponding entry for this phu 586 pmFPAview *view = NULL; // View to PHU 587 if (bind) { 588 view = pmFPAIdentifySourceFromHeader(bind->fpa, phu, format); 589 } else { 590 view = pmFPAAddSourceFromHeader(fpa, phu, format); 591 } 592 psFree(phu); 593 phu = NULL; 594 if (!view) { 595 psError(PS_ERR_IO, false, "Unable to determine source for %s", name); 596 return NULL; 597 } 598 599 // Associate the filename with the FPA element 600 psString location = pmFPAfileNameFromRule(file->filesrc, file, view); 601 psFree(view); 602 psMetadataAddStr(file->names, PS_LIST_TAIL, location, 0, "Location of file", filenames->data[i]); 603 psFree(location); 604 } 605 606 return file; 607 } 608 609 610 pmFPAfile *pmFPAfileDefineFromArgs(bool *success, pmConfig *config, 611 const char *filename, const char *argname) 435 612 { 436 613 PS_ASSERT_PTR_NON_NULL(config, NULL); … … 438 615 PS_ASSERT_STRING_NON_EMPTY(argname, NULL); 439 616 440 bool status; 441 pmFPA *fpa = NULL; 442 psFits *fits = NULL; 443 pmFPAfile *file = NULL; 444 psMetadata *phu = NULL; 445 psMetadata *format = NULL; 446 psMetadata *camera = NULL; 447 psString formatName = NULL; 448 449 // use success to identify valid exit conditions (as opposed to 'argument not supplied') 450 if (success) { 451 *success = false; 452 } 453 454 // we search the argument data for the named fileset (argname) 455 psArray *infiles = psMetadataLookupPtr(&status, config->arguments, argname); 617 // Search the argument data for the named fileset (argname) 618 bool status; // Status of MD lookup 619 psArray *filenames = psMetadataLookupPtr(&status, config->arguments, argname); // Filenames for file 456 620 if (!status) { 457 621 if (success) { … … 460 624 return NULL; 461 625 } 462 if (infiles->n < 1) { 463 psError(PS_ERR_IO, false, "Found n == %ld files in %s in arguments\n", infiles->n, argname); 464 return NULL; 465 } 466 467 // this function is implicitly an INPUT operation: do not create the file 468 psString realName = pmConfigConvertFilename (infiles->data[0], config, false, false); 469 if (!realName) { 470 psError(PS_ERR_IO, false, "Failed to convert file name %s\n", (char *) infiles->data[0]); 471 return NULL; 472 } 473 474 // load the header of the first image 475 // EXTWORD (fits->extword) is not relevant to the PHU 476 fits = psFitsOpen (realName, "r"); 477 if (!fits) { 478 psError(PS_ERR_IO, false, "Failed to open file %s\n", realName); 479 psFree (realName); 480 return NULL; 481 } 482 phu = psFitsReadHeader (NULL, fits); 483 484 if (!phu) { 485 psError(PS_ERR_IO, false, "Failed to read file header %s\n", realName); 486 psFree (realName); 487 return NULL; 488 } 489 psFitsClose(fits); 490 491 // Determine the current format from the header; Determine camera if not specified already. 492 // the returned pointers 'camera' and 'formatName' are allocated here 493 psString cameraName = NULL; 494 format = pmConfigCameraFormatFromHeader(&camera, &cameraName, &formatName, config, phu, true); 495 if (!format) { 496 psError(PS_ERR_IO, false, "Failed to read CCD format from %s\n", realName); 497 psFree(phu); 498 psFree(camera); 499 psFree(formatName); 500 psFree(realName); 501 return NULL; 502 } 503 504 // build the template fpa, set up the basic view 505 // XXX do we want this to be the baseCamera name or the metaCamera name? 506 fpa = pmFPAConstruct(camera, cameraName); 507 if (!fpa) { 508 psError(PS_ERR_IO, false, "Failed to construct FPA from %s", realName); 509 psFree(phu); 510 psFree(camera); 511 psFree(formatName); 512 psFree(realName); 513 psFree(format); 514 return NULL; 515 } 516 psFree (realName); 517 psFree (camera); 518 519 // load the given filerule (from config->camera) and bind it to the fpa 520 // the returned file is just a view to the entry on config->files 521 file = pmFPAfileDefineInput(config, fpa, cameraName, filename); 522 if (!file) { 523 psError(PS_ERR_IO, false, "file %s not defined\n", filename); 524 psFree(phu); 525 psFree(formatName); 526 psFree(format); 527 psFree(fpa); 528 return NULL; 529 } 530 psFree (cameraName); 531 psFree (file->filerule); // this is set in pmFPAfileDefineInput 532 533 file->format = format; 534 file->formatName = formatName; 535 file->filerule = psStringCopy("@FILES"); 536 file->filesrc = psStringCopy("{CHIP.NAME}.{CELL.NAME}"); 537 // we use the above rules to identify these files in the file->names data 538 539 file->fileLevel = pmFPAPHULevel(format); 540 if (file->fileLevel == PM_FPA_LEVEL_NONE) { 541 psError(PS_ERR_IO, true, "Unable to determine file level for %s\n", file->name); 542 psFree(phu); 543 psFree(fpa); 544 return NULL; 545 } 546 547 if (file->type == PM_FPA_FILE_MASK) { 548 if (!pmConfigMaskReadHeader (config, phu)) { 549 psError(PS_ERR_IO, false, "error in mask bits"); 550 return NULL; 551 } 552 } 553 554 // examine the list of input files and validate their cameras 555 // associated each filename with an element of the FPA 556 // save the association on file->names 557 for (int i = 0; i < infiles->n; i++) { 558 if (i > 0) { 559 // this function is implicitly an INPUT operation: do not create the file 560 psString realName = pmConfigConvertFilename (infiles->data[i], config, false, false); 561 if (!realName) { 562 psError(PS_ERR_IO, false, "Failed to convert file name %s", (char *) infiles->data[i]); 563 psFree(phu); 564 psFree(fpa); 565 return NULL; 566 } 567 // EXTWORD (fits->extword) is not relevant to the PHU 568 fits = psFitsOpen (realName, "r"); 569 if (!fits) { 570 psError(PS_ERR_IO, false, "Failed to open file %s\n", realName); 571 psFree(realName); 572 psFree(phu); 573 psFree(fpa); 574 return NULL; 575 } 576 phu = psFitsReadHeader (NULL, fits); 577 if (!phu) { 578 psError(PS_ERR_IO, false, "Failed to read file header %s", realName); 579 psFree(realName); 580 psFitsClose(fits); 581 psFree(phu); 582 psFree(fpa); 583 return NULL; 584 } 585 bool valid = false; 586 if (!pmConfigValidateCameraFormat (&valid, format, phu)) { 587 psError(PS_ERR_UNKNOWN, false, "Error in config scripts\n"); 588 psFree(realName); 589 psFitsClose(fits); 590 psFree(phu); 591 psFree(fpa); 592 return NULL; 593 } 594 if (!valid) { 595 psError(PS_ERR_IO, false, "file %s is not from the required camera", realName); 596 psFree(realName); 597 psFitsClose(fits); 598 psFree(phu); 599 psFree(fpa); 600 return NULL; 601 } 602 psFree(realName); 603 psFitsClose(fits); 604 } 605 606 // set the view to the corresponding entry for this phu 607 pmFPAview *view = pmFPAAddSourceFromHeader (fpa, phu, format); 608 if (!view) { 609 psError(PS_ERR_IO, false, "Unable to determine source for %s", file->name); 610 psFree(phu); 611 psFree(fpa); 612 return NULL; 613 } 614 615 // associate the filename with the FPA element 616 char *name = pmFPAfileNameFromRule(file->filesrc, file, view); 617 618 // save the name association in the pmFPAfile structure 619 psMetadataAddStr(file->names, PS_LIST_TAIL, name, 0, "", infiles->data[i]); 620 621 psFree(view); 622 psFree(name); 623 psFree(phu); 624 } 625 psFree(fpa); 626 if (filenames->n == 0) { 627 psError(PS_ERR_IO, false, "No files in array in %s in arguments", argname); 628 if (success) { 629 *success = false; 630 } 631 return NULL; 632 } 633 634 pmFPAfile *file = fpaFileDefineFromArray(config, NULL, filename, filenames); // File of interest 635 626 636 if (success) { 627 *success = true; 628 } 629 630 return file; 631 } 632 633 // search for argname on the config->argument list 634 // construct an FPA based on the files in this list (must represent a single FPA) 635 // built the association between the FPA elements (CHIP/CELL) and the files 636 // define the pmFPAfile filename and bind it to this FPA 637 // save the pmFPAfile on config->files 638 // return the pmFPAfile (a view to the one saved on config->files) 639 pmFPAfile *pmFPAfileBindFromArgs (bool *success, pmFPAfile *input, pmConfig *config, const char *filename, const char *argname) 637 *success = file ? true : false; 638 } 639 640 return file; 641 } 642 643 pmFPAfile *pmFPAfileBindFromArgs(bool *success, pmFPAfile *input, pmConfig *config, 644 const char *filename, const char *argname) 640 645 { 641 646 PS_ASSERT_PTR_NON_NULL(input, NULL); … … 644 649 PS_ASSERT_STRING_NON_EMPTY(argname, NULL); 645 650 646 bool status; 647 psFits *fits = NULL; 648 pmFPAfile *file = NULL; 649 psMetadata *phu = NULL; 650 651 // use success to identify valid exit conditions (as opposed to 'argument not supplied') 652 if (success) { 653 *success = false; 654 } 655 656 // we search the argument data for the named fileset (argname) 657 psArray *infiles = psMetadataLookupPtr(&status, config->arguments, argname); 651 // Search the argument data for the named fileset (argname) 652 bool status; // Status of MD lookup 653 psArray *filenames = psMetadataLookupPtr(&status, config->arguments, argname); // Filenames for file 658 654 if (!status) { 659 // this is not an error: this just means no matching argument was supplied660 655 if (success) { 661 656 *success = true; … … 663 658 return NULL; 664 659 } 665 if (infiles->n < 1) { 666 psError(PS_ERR_IO, false, "Found n == %ld files in %s in arguments\n", infiles->n, argname); 667 return NULL; 668 } 669 670 // load the given filerule (from config->camera) and bind it to the fpa 671 // the returned file is just a view to the entry on config->files 672 file = pmFPAfileDefineInput (config, input->fpa, NULL, filename); 673 if (!file) { 674 psError(PS_ERR_IO, false, "file %s not defined\n", filename); 675 psFree(phu); 676 return NULL; 677 } 678 679 // set derived values 680 file->fileLevel = input->fileLevel; 681 682 683 // define the rule to identify these files in the file->names data 684 psFree (file->filerule); 685 psFree (file->filesrc); 686 file->filerule = psStringCopy ("@FILES"); 687 file->filesrc = psStringCopy ("{CHIP.NAME}.{CELL.NAME}"); 688 689 // examine the list of input files and validate their cameras 690 // associated each filename with an element of the FPA 691 // save the association on file->names 692 psMetadata *format = NULL; 693 for (int i = 0; i < infiles->n; i++) { 694 // this function is implicitly an INPUT operation: do not create the file 695 psString realName = pmConfigConvertFilename (infiles->data[i], config, false, false); 696 if (!realName) { 697 psError(PS_ERR_IO, false, "Failed to convert file name %s", (char *) infiles->data[i]); 698 return NULL; 699 } 700 // EXTWORD (fits->extword) is not relevant to the PHU 701 fits = psFitsOpen (realName, "r"); 702 if (!fits) { 703 psError(PS_ERR_IO, false, "Failed to open file %s\n", realName); 704 psFree(realName); 705 return NULL; 706 } 707 phu = psFitsReadHeader (NULL, fits); 708 if (!phu) { 709 psError(PS_ERR_IO, false, "Failed to read file header %s", realName); 710 psFree(realName); 711 psFitsClose(fits); 712 return NULL; 713 } 714 715 if (!format) { 716 format = pmConfigCameraFormatFromHeader(NULL, NULL, NULL, config, phu, true); 717 if (!format) { 718 psError(PS_ERR_IO, false, "Failed to read CCD format from %s\n", realName); 719 psFree(phu); 720 psFree(realName); 721 psFitsClose(fits); 722 return NULL; 723 } 724 } else { 725 bool valid = false; 726 if (!pmConfigValidateCameraFormat(&valid, format, phu)) { 727 psError(PS_ERR_UNKNOWN, false, "Error in config scripts\n"); 728 psFree(realName); 729 psFitsClose(fits); 730 return NULL; 731 } 732 if (!valid) { 733 psError(PS_ERR_IO, false, "specified data file %s does not match format of supplied INPUT\n", 734 realName); 735 psFree(realName); 736 psFitsClose(fits); 737 return NULL; 738 } 739 } 740 741 psFree(realName); 742 psFitsClose(fits); 743 744 // set the view to the corresponding entry for this phu 745 pmFPAview *view = pmFPAIdentifySourceFromHeader (input->fpa, phu, format); 746 if (!view) { 747 psError(PS_ERR_IO, false, "Unable to determine source for %s", file->name); 748 psFree(phu); 749 return NULL; 750 } 751 752 // associate the filename with the FPA element 753 char *name = pmFPAfileNameFromRule(file->filesrc, file, view); 754 755 // save the name association in the pmFPAfile structure 756 psMetadataAddStr (file->names, PS_LIST_TAIL, name, 0, "", infiles->data[i]); 757 758 if ((i == 0) && (file->type == PM_FPA_FILE_MASK)) { 759 if (!pmConfigMaskReadHeader (config, phu)) { 760 psError(PS_ERR_IO, false, "error in mask bits"); 761 return NULL; 762 } 763 } 764 765 psFree(view); 766 psFree(name); 767 psFree(phu); 768 } 769 file->format = format; 770 file->formatName = psStringCopy(config->formatName); 660 if (filenames->n == 0) { 661 psError(PS_ERR_IO, false, "No files in array in %s in arguments", argname); 662 if (success) { 663 *success = false; 664 } 665 return NULL; 666 } 667 668 pmFPAfile *file = fpaFileDefineFromArray(config, input, filename, filenames); // File of interest 771 669 772 670 if (success) { 773 *success = true; 774 } 775 return file; 776 } 777 778 // search for argname on the config->argument list 779 // construct an FPA based on the files in this list (each represents the same FPA) 780 // built the association between the FPA elements (CHIP/CELL) and the files 781 // define the pmFPAfile filenames and bind them to the FPAs 782 // save the pmFPAfiles on config->files 783 // return the pmFPAfiles (a view to the one saved on config->files) 784 pmFPAfile *pmFPAfileDefineSingleFromArgs (bool *success, pmConfig *config, const char *filename, 785 const char *argname, int entry) 671 *success = file ? true : false; 672 } 673 674 return file; 675 } 676 677 pmFPAfile *pmFPAfileDefineSingleFromArgs(bool *success, pmConfig *config, const char *filename, 678 const char *argname, int entry) 786 679 { 787 680 PS_ASSERT_PTR_NON_NULL(config, NULL); … … 789 682 PS_ASSERT_STRING_NON_EMPTY(argname, NULL); 790 683 791 bool status; 792 pmFPA *fpa = NULL; 793 psFits *fits = NULL; 794 pmFPAfile *file = NULL; 795 psMetadata *phu = NULL; 796 psMetadata *format = NULL; 797 798 if (success) { 799 *success = false; 800 } 801 802 // we search the argument data for the named fileset (argname) 803 psArray *infiles = psMetadataLookupPtr(&status, config->arguments, argname); 684 // Search the argument data for the named fileset (argname) 685 bool status; // Status from MD lookup 686 psArray *filenames = psMetadataLookupPtr(&status, config->arguments, argname); // Filenames for file 804 687 if (!status) { 805 psTrace("psModules.camera", 5, "Failed to find %s in argument list", argname);806 688 if (success) { 807 689 *success = true; … … 809 691 return NULL; 810 692 } 811 if (infiles->n <= entry) { 812 psError(PS_ERR_IO, false, "only %ld files in %s in argument, entry %d requested\n", 813 infiles->n, argname, entry); 814 return NULL; 815 } 816 817 // examine the list of input files and validate their cameras 818 // associated each filename with an element of the FPA 819 // save the association on file->names 820 // EXTWORD (fits->extword) is not relevant to the PHU 821 fits = psFitsOpen (infiles->data[entry], "r"); 822 phu = psFitsReadHeader (NULL, fits); 823 psFitsClose (fits); 824 825 // on first call to this function, config->camera is not set. 826 // later calls will give an error if the cameras do not match 827 psMetadata *camera = NULL; 828 psString formatName = NULL; 829 psString cameraName = NULL; 830 format = pmConfigCameraFormatFromHeader (&camera, &cameraName, &formatName, config, phu, true); 831 if (!format) { 832 psError(PS_ERR_IO, false, "Failed to read CCD format from %s\n", (char *)infiles->data[0]); 833 psFree(phu); 834 psFree(camera); 835 psFree(formatName); 836 return NULL; 837 } 838 839 // build the template fpa, set up the basic view 840 fpa = pmFPAConstruct (camera, cameraName); 841 if (!fpa) { 842 psError(PS_ERR_IO, false, "Failed to construct FPA from %s", (char *)infiles->data[0]); 843 psFree(phu); 844 psFree(camera); 845 psFree(formatName); 846 psFree(format); 847 return NULL; 848 } 849 psFree(camera); 850 851 // load the given filerule (from config->camera) and bind it to the fpa 852 // the returned file is just a view to the entry on config->files 853 // we need a variable name here... (but in filerule) 854 file = pmFPAfileDefineInput (config, fpa, cameraName, filename); 855 if (!file) { 856 psError(PS_ERR_IO, false, "file %s not defined\n", filename); 857 psFree(phu); 858 psFree(fpa); 859 psFree(format); 860 return NULL; 861 } 862 psFree(cameraName); 863 FPA_TEST_ASSERT (file); 864 865 file->format = format; 866 file->formatName = formatName; 867 file->filerule = psStringCopy ("@FILES"); 868 file->filesrc = psStringCopy ("{CHIP.NAME}.{CELL.NAME}"); 869 // adjust the above rules to identify these files in the file->names data 870 871 file->fileLevel = pmFPAPHULevel(format); 872 if (file->fileLevel == PM_FPA_LEVEL_NONE) { 873 psError(PS_ERR_IO, true, "Unable to determine file level for %s\n", file->name); 874 psFree(phu); 875 psFree(fpa); 876 return NULL; 877 } 878 879 if (file->type == PM_FPA_FILE_MASK) { 880 if (!pmConfigMaskReadHeader (config, phu)) { 881 psError(PS_ERR_IO, false, "error in mask bits"); 882 return NULL; 883 } 884 } 885 886 // set the view to the corresponding entry for this phu 887 pmFPAview *view = pmFPAAddSourceFromHeader (fpa, phu, format); 888 if (!view) { 889 psError(PS_ERR_IO, false, "Unable to determine source for %s", file->name); 890 psFree(phu); 891 psFree(fpa); 892 return NULL; 893 } 894 895 // associate the filename with the FPA element 896 char *name = pmFPAfileNameFromRule (file->filesrc, file, view); 897 898 // save the name association in the pmFPAfile structure 899 psMetadataAddStr (file->names, PS_LIST_TAIL, name, 0, "", infiles->data[entry]); 900 901 psFree(phu); 902 psFree(fpa); 903 psFree(view); 904 psFree(name); 693 if (filenames->n <= entry) { 694 psError(PS_ERR_IO, false, "Insufficient files (%ld) in array in %s in arguments", 695 filenames->n, argname); 696 if (success) { 697 *success = false; 698 } 699 return NULL; 700 } 701 702 psArray *single = psArrayAlloc(1); // Array of single filename of interest 703 single->data[0] = psMemIncrRefCounter(filenames->data[entry]); 704 pmFPAfile *file = fpaFileDefineFromArray(config, NULL, filename, single); // File of interest 705 psFree(single); 905 706 906 707 if (success) { 907 *success = true; 908 } 909 return file; 910 } 708 *success = file ? true : false; 709 } 710 711 return file; 712 } 713 714 pmFPAfile *pmFPAfileDefineFromRun(bool *success, pmConfig *config, const char *filename) 715 { 716 PS_ASSERT_PTR_NON_NULL(config, NULL); 717 PS_ASSERT_STRING_NON_EMPTY(filename, NULL); 718 719 psArray *filenames = pmConfigRunFileGet(config, filename); // Filenames used, or NULL 720 if (!filenames) { 721 if (success) { 722 *success = true; 723 } 724 return NULL; 725 } 726 727 pmFPAfile *file = fpaFileDefineFromArray(config, NULL, filename, filenames); // File of interest 728 psFree(filenames); 729 730 if (success) { 731 *success = file ? true : false; 732 } 733 734 return file; 735 } 736 911 737 912 738 // define the named pmFPAfile from the camera->config 913 739 // only valid for pmFPAfile->mode = READ 914 pmFPAfile *pmFPAfileDefineFromConf (bool *success, const pmConfig *config, const char *filename)740 pmFPAfile *pmFPAfileDefineFromConf(bool *success, const pmConfig *config, const char *filename) 915 741 { 916 742 PS_ASSERT_PTR_NON_NULL(config, false); … … 991 817 pmFPA *fpa = NULL; 992 818 pmFPAfile *file = NULL; 819 820 if (type == PM_DETREND_TYPE_NONE) { 821 return NULL; 822 } 993 823 994 824 if (success) {
Note:
See TracChangeset
for help on using the changeset viewer.
