Changeset 1394 for trunk/psLib/src/astronomy/psMetadata.c
- Timestamp:
- Aug 5, 2004, 10:55:34 AM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/astronomy/psMetadata.c (modified) (45 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/astronomy/psMetadata.c
r1393 r1394 11 11 * @author Ross Harman, MHPCC 12 12 * 13 * @version $Revision: 1.1 0$ $Name: not supported by cvs2svn $14 * @date $Date: 2004-08-05 19:38:51$13 * @version $Revision: 1.11 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2004-08-05 20:55:16 $ 15 15 * 16 16 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 39 39 /******************************************************************************/ 40 40 41 /** Maximum length of string */ 42 #define MAX_STRING_LENGTH 1024 43 44 /** Maximum length of FITS line */ 45 #define FITS_LINE_SIZE 80 41 #define FITS_ERROR(STRING,PS_ERROR) \ 42 fits_get_errstatus(status, fitsErr); \ 43 psError( __func__, STRING, PS_ERROR, fitsErr); \ 44 status = 0; \ 45 if(fits_close_file(fd, &status) != 0) { \ 46 fits_get_errstatus(status, fitsErr); \ 47 psError( __func__, "FITS error while closing file: ", fitsErr); \ 48 } \ 49 return output; 46 50 47 51 /******************************************************************************/ … … 72 76 type = metadataItem->type; 73 77 74 if ( metadataItem == NULL) {78 if(metadataItem == NULL) { 75 79 return ; 76 80 } … … 80 84 psFree( metadataItem->items ); 81 85 82 if (type == PS_META_STR ||86 if(type == PS_META_STR || 83 87 type == PS_META_IMG || 84 88 type == PS_META_JPEG || 85 89 type == PS_META_PNG || 86 90 type == PS_META_ASTROM || 87 type == PS_META_UNKNOWN ) {91 type == PS_META_UNKNOWN) { 88 92 psFree( metadataItem->data.V ); 89 93 } … … 92 96 static void metadataFree( psMetadata *metadata ) 93 97 { 94 if ( metadata == NULL) {98 if(metadata == NULL) { 95 99 return ; 96 100 } … … 124 128 psMetadataItem * metadataItem = NULL; 125 129 126 if ( name == NULL) {130 if(name == NULL) { 127 131 psError( __func__, "Null value for string not allowed" ); 128 132 return NULL; … … 131 135 // Allocate metadata item 132 136 metadataItem = ( psMetadataItem * ) psAlloc( sizeof( psMetadataItem ) ); 133 if ( metadataItem == NULL) {137 if(metadataItem == NULL) { 134 138 psAbort( __func__, "Failed to allocate memory" ); 135 139 } … … 140 144 // Allocate and set metadata item comment 141 145 metadataItem->comment = ( char * ) psAlloc( sizeof( char ) * MAX_STRING_LENGTH ); 142 if ( comment == NULL) {146 if(comment == NULL) { 143 147 // Per SDRS, null isn't allowed, must use "" instead 144 148 strncpy( metadataItem->comment, "", MAX_STRING_LENGTH ); … … 154 158 155 159 // Set metadata item value 156 switch ( type) {160 switch(type) { 157 161 case PS_META_BOOL: 158 162 metadataItem->data.B = ( bool ) va_arg( argPtr, int ); … … 197 201 // Allocate metadata 198 202 metadata = ( psMetadata * ) psAlloc( sizeof( psMetadata ) ); 199 if ( metadata == NULL) {203 if(metadata == NULL) { 200 204 psAbort( __func__, "Failed to allocate metadata" ); 201 205 } … … 206 210 // Allocate metadata's internal containers 207 211 list = ( psList * ) psListAlloc( NULL ); 208 if ( list == NULL) {212 if(list == NULL) { 209 213 psAbort( __func__, "Failed to allocate list" ); 210 214 } 211 215 212 216 table = ( psHash * ) psHashAlloc( 10 ); 213 if ( table == NULL) {217 if(table == NULL) { 214 218 psAbort( __func__, "Failed to allocate table" ); 215 219 } … … 229 233 psMetadataType type = PS_META_ITEM_SET; 230 234 231 if ( md == NULL) {235 if(md == NULL) { 232 236 psError( __func__, "Null metadata collection not allowed" ); 233 237 return false; 234 238 } 235 239 236 if ( metadataItem == NULL) {240 if(metadataItem == NULL) { 237 241 psError( __func__, "Null metadata item not allowed" ); 238 242 return false; … … 242 246 243 247 mdTable = md->table; 244 if ( mdTable == NULL) {248 if(mdTable == NULL) { 245 249 psError( __func__, "Null metadata table not allowed" ); 246 250 return false; … … 248 252 249 253 mdList = md->list; 250 if ( mdList == NULL) {254 if(mdList == NULL) { 251 255 psError( __func__, "Null metadata list not allowed" ); 252 256 return false; … … 254 258 255 259 key = metadataItem->name; 256 if ( key == NULL) {260 if(key == NULL) { 257 261 psError( __func__, "Null key item not allowed" ); 258 262 return false; … … 261 265 // Check if key is already in table 262 266 value = ( psMetadataItem* ) psHashLookup( mdTable, key ); 263 if ( value != NULL && type != PS_META_ITEM_SET) {267 if(value != NULL && type != PS_META_ITEM_SET) { 264 268 265 269 // The key was found and the new metadata item is a leaf node (its type isn't PS_META_ITEM_SET), so 266 270 // add the new metadata item to hash as a child of the existing metadata item folder node. 267 if ( !psListAdd( value->items, metadataItem, where )) {271 if(!psListAdd( value->items, metadataItem, where )) { 268 272 psError( __func__, "Couldn't add metadata item to items list. Name: %s", 269 273 metadataItem->name ); 270 274 return false; 271 275 } 272 } else if ( value != NULL) {276 } else if(value != NULL) { 273 277 274 278 // The key was found and the new metadata item is a folder node. Don't add new metadata item, since … … 280 284 281 285 // Duplicate key not found. Add new metadata item to metadata collection's hash 282 if ( !psHashAdd( mdTable, key, metadataItem )) {286 if(!psHashAdd( mdTable, key, metadataItem )) { 283 287 psError( __func__, "Couldn't add metadata item to metadata collection table. Name: %s", 284 288 metadataItem->name ); … … 288 292 289 293 // Add all items to metadata collection's list, even if they have the same metadata item names 290 if ( !psListAdd( md->list, metadataItem, where )) {294 if(!psListAdd( md->list, metadataItem, where )) { 291 295 psError( __func__, "Couldn't add metadata item to metadata collection list. Name: %s", 292 296 metadataItem->name ); … … 307 311 va_end( argPtr ); 308 312 309 if ( !psMetadataAddItem( md, where, metadataItem )) {313 if(!psMetadataAddItem( md, where, metadataItem )) { 310 314 psError( __func__, "Couldn't add metadata item to metadata collection list. Name: %s", 311 315 metadataItem->name ); … … 329 333 330 334 mdList = md->list; 331 if ( mdList == NULL) {335 if(mdList == NULL) { 332 336 psError( __func__, "Null metadata list not allowed" ); 333 337 return false; … … 335 339 336 340 mdTable = md->table; 337 if ( mdTable == NULL) {341 if(mdTable == NULL) { 338 342 psError( __func__, "Null metadata table not allowed" ); 339 343 return false; … … 341 345 342 346 // Select removal by key or index 343 if ( key != NULL) {347 if(key != NULL) { 344 348 345 349 // Remove by key name 346 350 entry = ( psMetadataItem* ) psHashLookup( mdTable, key ); 347 if ( entry == NULL) {351 if(entry == NULL) { 348 352 psError( __func__, "Couldn't find metadata item remove. Name: %s", key ); 349 353 return false; … … 351 355 352 356 numChildren = entry->items->size; 353 if ( entry->type == PS_META_ITEM_SET && numChildren > 0) {357 if(entry->type == PS_META_ITEM_SET && numChildren > 0) { 354 358 355 359 // Table entry has children. Entry and children must be removed from metadata collection's list 356 360 psListSetIterator( mdList, PS_LIST_HEAD ); 357 361 entryChild = psListGetCurrent( mdList ); 358 while ( entryChild != NULL) {359 if ( !psListRemove( entry->items, entryChild, PS_LIST_UNKNOWN )) {362 while(entryChild != NULL) { 363 if(!psListRemove( entry->items, entryChild, PS_LIST_UNKNOWN )) { 360 364 psError( __func__, "Couldn't remove metadata item from list. Name: %s", key ); 361 365 return false; … … 366 370 367 371 // Remove entry from metadata collection's list 368 if ( !psListRemove( mdList, entry, PS_LIST_UNKNOWN )) {372 if(!psListRemove( mdList, entry, PS_LIST_UNKNOWN )) { 369 373 psError( __func__, "Couldn't remove metadata item from list. Name: %s", key ); 370 374 return false; … … 372 376 373 377 // Remove entry from metadata collection's table 374 if ( !psHashRemove( mdTable, key )) {378 if(!psHashRemove( mdTable, key )) { 375 379 psError( __func__, "Couldn't remove metadata item from table. Name: %s", key ); 376 380 return false; … … 380 384 // Remove by index 381 385 entry = psListGet( mdList, where ); 382 if ( entry == NULL) {386 if(entry == NULL) { 383 387 psError( __func__, "Couldn't find metadata item from list. Index: %d", where ); 384 388 return false; … … 386 390 387 391 key = entry->name; 388 if ( key == NULL) {392 if(key == NULL) { 389 393 psError( __func__, "Null key name not allowed. Index: %d", where ); 390 394 return false; … … 404 408 405 409 mdTable = md->table; 406 if ( mdTable == NULL) {410 if(mdTable == NULL) { 407 411 psError( __func__, "Null metadata table not allowed" ); 408 412 return NULL; 409 413 } 410 414 411 if ( key == NULL) {415 if(key == NULL) { 412 416 psError( __func__, "Null key name not allowed" ); 413 417 return NULL; … … 415 419 416 420 entry = ( psMetadataItem* ) psHashLookup( mdTable, key ); 417 if ( entry == NULL) {421 if(entry == NULL) { 418 422 psError( __func__, "Could not find metadata item with given key. Key: %s", key ); 419 423 return NULL; … … 429 433 430 434 mdList = md->list; 431 if ( mdList == NULL) {435 if(mdList == NULL) { 432 436 psError( __func__, "Null metadata list not allowed" ); 433 437 return NULL; … … 435 439 436 440 entry = ( psMetadataItem* ) psListGet( mdList, where ); 437 if ( entry == NULL) {441 if(entry == NULL) { 438 442 psError( __func__, "Couldn't find metadata item with given index. Index: %d", where ); 439 443 return NULL; … … 448 452 449 453 mdList = md->list; 450 if ( mdList == NULL) {454 if(mdList == NULL) { 451 455 psError( __func__, "Null metadata list not allowed" ); 452 456 return false; … … 464 468 465 469 mdList = md->list; 466 if ( mdList == NULL) {467 psError( __func__, "Null metadata list not allowed" ); 468 return NULL; 469 } 470 471 mdList = md->list; 472 if ( mdList == NULL) {470 if(mdList == NULL) { 471 psError( __func__, "Null metadata list not allowed" ); 472 return NULL; 473 } 474 475 mdList = md->list; 476 if(mdList == NULL) { 473 477 psError( __func__, "Null metadata list not allowed" ); 474 478 return NULL; … … 477 481 psListSetIterator( mdList, which ); 478 482 entry = psListGetCurrent( mdList ); 479 while ( entry != NULL) {480 if ( !strncmp( match, entry->name, strlen( match ) )) {483 while(entry != NULL) { 484 if(!strncmp( match, entry->name, strlen( match ) )) { 481 485 482 486 // Match found … … 487 491 488 492 // Match not found 489 if ( entry == NULL) {493 if(entry == NULL) { 490 494 psError( __func__, "Couldn't find metadata item with given match. Match: %s", match ); 491 495 } … … 500 504 501 505 mdList = md->list; 502 if ( mdList == NULL) {503 psError( __func__, "Null metadata list not allowed" ); 504 return NULL; 505 } 506 507 mdList = md->list; 508 if ( mdList == NULL) {506 if(mdList == NULL) { 507 psError( __func__, "Null metadata list not allowed" ); 508 return NULL; 509 } 510 511 mdList = md->list; 512 if(mdList == NULL) { 509 513 psError( __func__, "Null metadata list not allowed" ); 510 514 return NULL; … … 513 517 psListSetIterator( mdList, which ); 514 518 entry = psListGetCurrent( mdList ); 515 while ( entry != NULL) {516 if ( !strncmp( match, entry->name, strlen( match ) )) {519 while(entry != NULL) { 520 if(!strncmp( match, entry->name, strlen( match ) )) { 517 521 518 522 // Match found … … 523 527 524 528 // Match not found 525 if ( entry == NULL) {529 if(entry == NULL) { 526 530 psError( __func__, "Couldn't find metadata item with given match. Match: %s", match ); 527 531 } … … 534 538 psMetadataType type; 535 539 536 if ( fd == NULL) {540 if(fd == NULL) { 537 541 psError( __func__, "Null file descriptor not allowed" ); 538 542 return ; 539 543 } 540 544 541 if ( format == NULL) {545 if(format == NULL) { 542 546 psError( __func__, "Null format not allowed" ); 543 547 return ; 544 548 } 545 549 546 if ( metadataItem == NULL) {550 if(metadataItem == NULL) { 547 551 psError( __func__, "Null metadata not allowed" ); 548 552 return ; … … 551 555 type = metadataItem->type; 552 556 553 switch ( type) {557 switch(type) { 554 558 case PS_META_BOOL: 555 559 fprintf( fd, format, metadataItem->data.B ); … … 578 582 } 579 583 580 psMetadata *psMetadataFReadHeader( psMetadata *output, c onst char *extname, int extnum, fitsfile *fd )584 psMetadata *psMetadataFReadHeader( psMetadata *output, char *extName, int extNum, fitsfile *fd ) 581 585 { 582 586 bool tempBool; … … 587 591 char keyComment[ FITS_LINE_SIZE ]; 588 592 char fitsErr[ MAX_STRING_LENGTH ]; 589 char* tempExtname = NULL;590 593 int i; 591 594 int hduType = 0; … … 595 598 psMetadataType metadataItemType; 596 599 597 if ( fd == NULL) {600 if(fd == NULL) { 598 601 psError( __func__, "Null FITS file descriptor not allowed" ); 599 602 return NULL; 600 603 } 601 604 602 if ( extname == NULL && extnum == 0) {603 psError( __func__, "Null ext name AND extnum = 0 not allowed" );604 return NULL; 605 } else if ( extname && extnum) {606 psError( __func__, "Both ext name AND extnum arguments should not havevalues." );605 if(extName == NULL && extNum == 0) { 606 psError( __func__, "Null extName and extNum = 0 not allowed" ); 607 return NULL; 608 } else if(extName && extNum) { 609 psError( __func__, "Both extName and extNum arguments should not have non zero values." ); 607 610 return NULL; 608 611 } 609 612 610 613 // Allocate metadata if user didn't 611 if ( output == NULL) {614 if(output == NULL) { 612 615 output = psMetadataAlloc(); 613 616 } 614 617 615 618 // Move to user designated HDU number or HDU name in FITS file. HDU numbers starts at one. 616 if ( extname == NULL ) { 617 if ( fits_movabs_hdu( fd, extnum, &hduType, &status ) != 0 ) { 619 if(extName != NULL) { 620 if(fits_movnam_hdu( fd, ANY_HDU, extName, 0, &status ) != 0) { 621 FITS_ERROR("FITS error while locating header %s: %s", extName); 622 } 623 } else { 624 if(fits_movabs_hdu( fd, extNum, &hduType, &status ) != 0) { 625 FITS_ERROR("FITS error while locating header %d: %s", extNum); 626 } 627 } 628 629 // Get number of key names 630 if(fits_get_hdrpos( fd, &numKeys, &keyNum, &status ) != 0) { 631 FITS_ERROR("FITS error while reading key %d: %s", keyNum); 632 } 633 634 // Get each key name. Keywords start at one. 635 for(i = 1; i <= numKeys; i++) { 636 if(fits_read_keyn( fd, i, keyName, keyValue, keyComment, &status ) != 0) { 637 FITS_ERROR("FITS error while reading key %d: %s", keyNum); 638 } 639 if(fits_get_keytype( keyValue, &keyType, &status ) != 0) { 618 640 fits_get_errstatus( status, fitsErr ); 619 psError( __func__, "FITS error while locating header: %s", fitsErr ); 620 status = 0; 621 return output; 622 } 623 } else if ( extnum > 0 ) { 624 tempExtname = ( char* ) extname; 625 if ( fits_movnam_hdu( fd, ANY_HDU, tempExtname, 0, &status ) != 0 ) { 626 fits_get_errstatus( status, fitsErr ); 627 psError( __func__, "FITS error while locating header: %s", fitsErr ); 628 status = 0; 629 return output; 630 } 631 } 632 633 // Get number of key names 634 if ( fits_get_hdrpos( fd, &numKeys, &keyNum, &status ) != 0 ) { 635 fits_get_errstatus( status, fitsErr ); 636 psError( __func__, "FITS error while reading keys: %s", fitsErr ); 637 status = 0; 638 return output; 639 } 640 641 // Get each key name. Keywords start at one. 642 for ( i = 1; i <= numKeys; i++ ) { 643 if ( fits_read_keyn( fd, i, keyName, keyValue, keyComment, &status ) != 0 ) { 644 fits_get_errstatus( status, fitsErr ); 645 psError( __func__, "FITS error while reading key number %d: %s", i, fitsErr ); 646 status = 0; 647 return output; 648 } 649 if ( fits_get_keytype( keyValue, &keyType, &status ) != 0 ) { 650 fits_get_errstatus( status, fitsErr ); 651 if ( status != VALUE_UNDEFINED ) { 652 psError( __func__, "FITS error while determining key type: %s", fitsErr ); 653 status = 0; 654 return output; 641 if(status != VALUE_UNDEFINED) { 642 FITS_ERROR("FITS error while determining key %d type: %s", keyNum); 655 643 } else { 656 // Some keywords are still valid even though they don't have a type or value, like COMMENTS644 // Some keywords are still valid even though they don't have a type, like COMMENTS and HISTORY 657 645 keyType = 'C'; 658 646 status = 0; … … 660 648 } 661 649 662 switch ( keyType) {650 switch(keyType) { 663 651 case 'I': 664 652 metadataItemType = PS_META_S32; 665 success = psMetadataAdd( output, PS_LIST_ HEAD, keyName, metadataItemType, keyComment, atoi( keyValue ) );653 success = psMetadataAdd( output, PS_LIST_TAIL, keyName, metadataItemType, keyComment, atoi( keyValue ) ); 666 654 break; 667 655 case 'F': 668 656 metadataItemType = PS_META_F64; 669 success = psMetadataAdd( output, PS_LIST_ HEAD, keyName, metadataItemType, keyComment, atof( keyValue ) );657 success = psMetadataAdd( output, PS_LIST_TAIL, keyName, metadataItemType, keyComment, atof( keyValue ) ); 670 658 break; 671 659 case 'C': 672 660 metadataItemType = PS_META_STR; 673 success = psMetadataAdd( output, PS_LIST_ HEAD, keyName, metadataItemType, keyComment, keyValue );661 success = psMetadataAdd( output, PS_LIST_TAIL, keyName, metadataItemType, keyComment, keyValue ); 674 662 break; 675 663 case 'L': 676 664 metadataItemType = PS_META_BOOL; 677 665 tempBool = ( keyValue[ 0 ] == 'T' ) ? 1 : 0; 678 success = psMetadataAdd( output, PS_LIST_ HEAD, keyName, metadataItemType, keyComment, tempBool );666 success = psMetadataAdd( output, PS_LIST_TAIL, keyName, metadataItemType, keyComment, tempBool ); 679 667 break; 680 668 case 'U': … … 685 673 } 686 674 687 if ( !success) {675 if(!success) { 688 676 psError( __func__, "Failed to add metadata item. Name: %s", keyName ); 677 return output; 689 678 } 690 679 }
Note:
See TracChangeset
for help on using the changeset viewer.
