00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef HDU_H
00011 #define HDU_H 1
00012 #include <map>
00013
00014 #include <list>
00015
00016 #include <vector>
00017
00018 #include "CCfits.h"
00019
00020 #include "Keyword.h"
00021
00022 #include "NewKeyword.h"
00023
00024 #include "FitsError.h"
00025
00026 #include "FITSUtil.h"
00027
00028 namespace CCfits {
00029 class FITS;
00030 class FITSBase;
00031
00032 }
00033 #ifdef _MSC_VER
00034 #include "MSconfig.h"
00035 #endif
00036 #include "KeywordT.h"
00037
00038
00039 namespace CCfits {
00441 class HDU
00442 {
00443
00444 public:
00445
00446
00447
00448 class InvalidImageDataType : public FitsException
00449 {
00450 public:
00451 InvalidImageDataType (const string& diag, bool silent = true);
00452
00453 protected:
00454 private:
00455 private:
00456 };
00457
00458
00459
00460 class InvalidExtensionType : public FitsException
00461 {
00462 public:
00463 InvalidExtensionType (const string& diag, bool silent = true);
00464
00465 protected:
00466 private:
00467 private:
00468 };
00469
00470
00471
00472 class NoSuchKeyword : public FitsException
00473 {
00474 public:
00475 NoSuchKeyword (const string& diag, bool silent = true);
00476
00477 protected:
00478 private:
00479 private:
00480 };
00481
00482
00483
00484 class NoNullValue : public FitsException
00485 {
00486 public:
00487 NoNullValue (const string& diag, bool silent = true);
00488
00489 protected:
00490 private:
00491 private:
00492 };
00493 HDU(const HDU &right);
00494 bool operator==(const HDU &right) const;
00495
00496 bool operator!=(const HDU &right) const;
00497
00498 virtual HDU * clone (FITSBase* p) const = 0;
00499 fitsfile* fitsPointer () const;
00500
00501
00502
00503
00504
00505 virtual void makeThisCurrent () const;
00506 std::map<String, Keyword*> keyWord ();
00507 Keyword& keyWord (const String& keyName);
00508 const String& getHistory ();
00509 const String& getComments ();
00510 friend std::ostream& operator << (std::ostream& s, const CCfits::HDU& right);
00511
00512
00513
00514 void writeHistory (const String& history = "Generic History String");
00515
00516
00517
00518 void writeComment (const String& comment = "Generic Comment");
00519
00520 void writeDate ();
00521 FITSBase* parent () const;
00522 void readAllKeys ();
00523 long axes () const;
00524 long axis (size_t index) const;
00525 void index (int value);
00526 void deleteKey (const String& doomed);
00527 long bitpix () const;
00528 void bitpix (long value);
00529 int index () const;
00530 const string& history () const;
00531 const string& comment () const;
00532 virtual double zero () const;
00533 virtual void zero (double value);
00534
00535
00536 virtual double scale () const;
00537 virtual void scale (double value);
00538 const std::map<string,Keyword*>& keyWord () const;
00539 const Keyword& keyWord (const string& keyname) const;
00540 void setKeyWord (const string& keyname, Keyword& value);
00541
00542 public:
00543
00544 template <typename T>
00545 Keyword& addKey(const String& name, T val, const String& comment);
00546
00547 Keyword& addKey(const String& name, const char* charString, const String& comment);
00548
00549 template <typename T>
00550 void readKey(const String& keyName, T& val);
00551
00552 template <typename T>
00553 void readKeys(std::vector<String>& keyNames, std::vector<T>& vals);
00554
00555 #ifdef TEMPLATE_AMBIG_DEFECT
00556 inline void readKeyMS(const String& keyName, int & val);
00557 inline void readKeys(std::vector<String>& keyNames, std::vector<String>& vals);
00558
00559 #endif
00560 protected:
00561
00562
00563 HDU (FITSBase* p = 0);
00564 HDU (FITSBase* p, int bitpix, int naxis, const std::vector<long>& axes);
00565 virtual ~HDU();
00566
00567 Keyword& readKeyword (const String &keyname);
00568 void readKeywords (std::list<String>& keynames);
00569 virtual std::ostream & put (std::ostream &s) const = 0;
00570 long& naxis ();
00571 void naxis (const long& value);
00572
00573
00574 bool& anynul ();
00575 void anynul (const bool& value);
00576 FITSBase*& parent ();
00577 std::vector< long >& naxes ();
00578 long& naxes (size_t index);
00579 void naxes (size_t index, const long& value);
00580
00581
00582
00583 private:
00584
00585
00586 void clearKeys ();
00587 virtual void initRead () = 0;
00588 void readHduInfo ();
00589 Keyword* addKeyword (Keyword* newKey);
00590 virtual bool compare (const HDU &right) const;
00591
00592
00593 void copyKeys (const HDU& right);
00594 String getNamedLines (const String& name);
00595
00596
00597 void saveReadKeyword (Keyword* newKey);
00598
00599
00600
00601 private:
00602
00603 long m_naxis;
00604 long m_bitpix;
00605 int m_index;
00606 bool m_anynul;
00607 string m_history;
00608 string m_comment;
00609 double m_zero;
00610 double m_scale;
00611
00612
00613 std::map<string,Keyword*> m_keyWord;
00614 FITSBase* m_parent;
00615 std::vector< long > m_naxes;
00616
00617
00618 friend fitsfile* Keyword::fitsPointer() const;
00619 friend Keyword* KeywordCreator::getKeyword(const String& keyname, HDU* p);
00620 friend Keyword* KeywordCreator::getKeyword(const String& keyname, ValueType keyType, HDU* p);
00621 };
00622 template <typename T>
00623 Keyword& HDU::addKey(const String& name, T value, const String& comment)
00624 {
00625 makeThisCurrent();
00626 NewKeyword<T> keyCreator(this,value);
00627 Keyword& newKey = *(addKeyword(keyCreator.createKeyword(name,comment)));
00628 return newKey;
00629 }
00630
00631 template <typename T>
00632 void HDU::readKey(const String& keyName, T& val)
00633 {
00634 makeThisCurrent();
00635 Keyword& key = readKeyword(keyName);
00636 key.value(val);
00637 }
00638
00639
00640 template <typename T>
00641 void HDU::readKeys(std::vector<String>& keyNames, std::vector<T>& vals)
00642 {
00643 size_t nRead = keyNames.size();
00644
00645 std::list<String> valKeys;
00646 std::list<T> valList;
00647 for (size_t i = 0; i < nRead; i++) valKeys.push_back(keyNames[i]);
00648
00649
00650 readKeywords(valKeys);
00651
00652
00653
00654
00655 T current;
00656 std::list<String>::iterator it = valKeys.begin();
00657 while (it != valKeys.end())
00658 {
00659 try
00660 {
00661 m_keyWord[*it]->value(current);
00662 valList.push_back(current);
00663 ++it;
00664 }
00665 catch ( Keyword::WrongKeywordValueType )
00666 {
00667 it = valKeys.erase(it);
00668 }
00669 }
00670
00671 keyNames.erase(keyNames.begin(),keyNames.end());
00672
00673 if (!valList.empty())
00674 {
00675 if (valList.size() != vals.size()) vals.resize(valList.size());
00676
00677 size_t i=0;
00678 for (typename std::list<T>::const_iterator it1
00679 = valList.begin(); it1 != valList.end(); ++it1,++i)
00680 {
00681 vals[i] = *it1;
00682 }
00683 for (std::list<String>::const_iterator it1= valKeys.begin(); it1 != valKeys.end(); ++it1)
00684 {
00685 keyNames.push_back(*it1);
00686 }
00687 }
00688
00689 }
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701 inline std::map<String, Keyword*> HDU::keyWord ()
00702 {
00703
00704 return m_keyWord;
00705 }
00706
00707 inline Keyword& HDU::keyWord (const String& keyName)
00708 {
00709 std::map<String,Keyword*>::iterator key = m_keyWord.find(keyName);
00710 if (key == m_keyWord.end()) throw HDU::NoSuchKeyword(keyName);
00711 return *((*key).second);
00712 }
00713
00714 inline std::ostream& operator << (std::ostream& s, const CCfits::HDU& right)
00715 {
00716 return right.put(s);
00717 }
00718
00719 inline long HDU::axes () const
00720 {
00721
00722 return m_naxis;
00723 }
00724
00725 inline long HDU::axis (size_t index) const
00726 {
00727
00728 return m_naxes[index];
00729 }
00730
00731 inline void HDU::index (int value)
00732 {
00733
00734 m_index = value;
00735 }
00736
00737 inline void HDU::saveReadKeyword (Keyword* newKey)
00738 {
00739 m_keyWord.insert(std::map<String,Keyword*>::value_type(newKey->name(),newKey->clone()));
00740 }
00741
00742 inline long& HDU::naxis ()
00743 {
00744 return m_naxis;
00745 }
00746
00747 inline void HDU::naxis (const long& value)
00748 {
00749 m_naxis = value;
00750 }
00751
00752 inline long HDU::bitpix () const
00753 {
00754 return m_bitpix;
00755 }
00756
00757 inline void HDU::bitpix (long value)
00758 {
00759 m_bitpix = value;
00760 }
00761
00762 inline int HDU::index () const
00763 {
00764 return m_index;
00765 }
00766
00767 inline bool& HDU::anynul ()
00768 {
00769 return m_anynul;
00770 }
00771
00772 inline void HDU::anynul (const bool& value)
00773 {
00774 m_anynul = value;
00775 }
00776
00777 inline const string& HDU::history () const
00778 {
00779 return m_history;
00780 }
00781
00782 inline const string& HDU::comment () const
00783 {
00784 return m_comment;
00785 }
00786
00787 inline double HDU::zero () const
00788 {
00789 return m_zero;
00790 }
00791
00792 inline void HDU::zero (double value)
00793 {
00794 m_zero = value;
00795 }
00796
00797 inline double HDU::scale () const
00798 {
00799 return m_scale;
00800 }
00801
00802 inline void HDU::scale (double value)
00803 {
00804 m_scale = value;
00805 }
00806
00807 inline const std::map<string,Keyword*>& HDU::keyWord () const
00808 {
00809 return m_keyWord;
00810 }
00811
00812 inline const Keyword& HDU::keyWord (const string& keyname) const
00813 {
00814 std::map<String,Keyword*>::const_iterator key = m_keyWord.find(keyname);
00815 if (key == m_keyWord.end()) throw HDU::NoSuchKeyword(keyname);
00816 return *((*key).second);
00817 }
00818
00819 inline void HDU::setKeyWord (const string& keyname, Keyword& value)
00820 {
00821 m_keyWord[keyname] = &value;
00822 }
00823
00824 inline FITSBase*& HDU::parent ()
00825 {
00826 return m_parent;
00827 }
00828
00829 inline std::vector< long >& HDU::naxes ()
00830 {
00831 return m_naxes;
00832 }
00833
00834 inline long& HDU::naxes (size_t index)
00835 {
00836 return m_naxes[index];
00837 }
00838
00839 inline void HDU::naxes (size_t index, const long& value)
00840 {
00841 m_naxes[index] = value;
00842 }
00843
00844 }
00845 #ifdef SPEC_TEMPLATE_IMP_DEFECT
00846 namespace CCfits {
00847
00848 inline void HDU::readKeyMS(const String& keyName, int & val)
00849 {
00850 makeThisCurrent();
00851 Keyword& key = readKeyword(keyName);
00852 key.value(val);
00853 }
00854
00855 inline void HDU::readKeys(std::vector<String>& keyNames, std::vector<String>& vals)
00856 {
00857 size_t nRead = keyNames.size();
00858
00859 std::list<String> valKeys;
00860 std::list<String> valList;
00861 for (size_t i = 0; i < nRead; i++) valKeys.push_back(keyNames[i]);
00862
00863
00864 readKeywords(valKeys);
00865
00866
00867
00868
00869 String current;
00870 std::list<String>::iterator it = valKeys.begin();
00871 while (it != valKeys.end())
00872 {
00873 try
00874 {
00875 m_keyWord[*it]->value(current);
00876 valList.push_back(current);
00877 ++it;
00878 }
00879 catch ( Keyword::WrongKeywordValueType )
00880 {
00881 it = valKeys.erase(it);
00882 }
00883 }
00884
00885 keyNames.erase(keyNames.begin(),keyNames.end());
00886
00887 if (!valList.empty())
00888 {
00889 if (valList.size() != vals.size()) vals.resize(valList.size());
00890
00891 size_t i=0;
00892 std::list<String>::const_iterator it1 = valList.begin();
00893 for ( ; it1 != valList.end(); ++it1,++i)
00894 {
00895 vals[i] = *it1;
00896 }
00897 for ( it1= valKeys.begin(); it1 != valKeys.end(); ++it1)
00898 {
00899 keyNames.push_back(*it1);
00900 }
00901 }
00902
00903 }
00904 }
00905 #endif
00906
00907
00908 #endif