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