| 1 | /* | 
|---|
| 2 | * | 
|---|
| 3 | * FITS.h | 
|---|
| 4 | * | 
|---|
| 5 | * Global fits header | 
|---|
| 6 | * | 
|---|
| 7 | * Author: lyard | 
|---|
| 8 | * | 
|---|
| 9 | */ | 
|---|
| 10 |  | 
|---|
| 11 | #ifndef MARS_FITS | 
|---|
| 12 | #define MARS_FITS | 
|---|
| 13 |  | 
|---|
| 14 | #include <stdint.h> | 
|---|
| 15 | #include <string.h> | 
|---|
| 16 |  | 
|---|
| 17 | #include <vector> | 
|---|
| 18 | #include <string> | 
|---|
| 19 |  | 
|---|
| 20 | #ifndef __CINT__ | 
|---|
| 21 | #include <unordered_set> | 
|---|
| 22 | #endif | 
|---|
| 23 |  | 
|---|
| 24 | namespace FITS | 
|---|
| 25 | { | 
|---|
| 26 | static inline bool IsReservedKeyWord(const std::string &key) | 
|---|
| 27 | { | 
|---|
| 28 | #ifndef __CINT__ | 
|---|
| 29 | static const std::unordered_set<std::string> keys = | 
|---|
| 30 | { | 
|---|
| 31 | "DATASUM",  "END",      "EXTNAME",  "PCOUNT",   "NAXIS", | 
|---|
| 32 | "NAXIS1",   "NAXIS2",   "RAWSUM",   "SIMPLE",   "TFIELDS", | 
|---|
| 33 | "THEAP",    "XTENSION", "ZHEAPPTR", "ZNAXIS1",  "ZNAXIS2", | 
|---|
| 34 | "ZPCOUNT",  "ZRATIO",   "ZSHRINK",  "ZTABLE",   "ZTILELEN", | 
|---|
| 35 | }; | 
|---|
| 36 |  | 
|---|
| 37 | static const std::unordered_set<std::string> short_keys = | 
|---|
| 38 | { | 
|---|
| 39 | "TFORM", "TUNIT", "TTYPE", "ZCTYP", "ZFORM", | 
|---|
| 40 | }; | 
|---|
| 41 |  | 
|---|
| 42 | if (keys.find(key)!=keys.end()) | 
|---|
| 43 | return true; | 
|---|
| 44 |  | 
|---|
| 45 | const std::string five = key.substr(0, 5); | 
|---|
| 46 | return short_keys.find(five)!=short_keys.end(); | 
|---|
| 47 | #endif | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | static inline std::string CommentFromType(char type) | 
|---|
| 51 | { | 
|---|
| 52 | std::string comment; | 
|---|
| 53 |  | 
|---|
| 54 | switch (type) | 
|---|
| 55 | { | 
|---|
| 56 | case 'L': comment = "[1-byte BOOL]";  break; | 
|---|
| 57 | case 'A': comment = "[1-byte CHAR]";  break; | 
|---|
| 58 | case 'B': comment = "[1-byte BOOL]";  break; | 
|---|
| 59 | case 'I': comment = "[2-byte INT]";   break; | 
|---|
| 60 | case 'J': comment = "[4-byte INT]";   break; | 
|---|
| 61 | case 'K': comment = "[8-byte INT]";   break; | 
|---|
| 62 | case 'E': comment = "[4-byte FLOAT]"; break; | 
|---|
| 63 | case 'D': comment = "[8-byte FLOAT]"; break; | 
|---|
| 64 | case 'Q': comment = "[var. Length]"; break; | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | return comment; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | static inline uint32_t SizeFromType(char type) | 
|---|
| 71 | { | 
|---|
| 72 | size_t size = 0; | 
|---|
| 73 |  | 
|---|
| 74 | switch (type) | 
|---|
| 75 | { | 
|---|
| 76 | case 'L': | 
|---|
| 77 | case 'A': | 
|---|
| 78 | case 'B': size =  1; break; | 
|---|
| 79 | case 'I': size =  2; break; | 
|---|
| 80 | case 'J': | 
|---|
| 81 | case 'E': size =  4; break; | 
|---|
| 82 | case 'K': | 
|---|
| 83 | case 'D': size =  8; break; | 
|---|
| 84 | case 'Q': size = 16; break; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | return size; | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | //Identifier of the compression schemes processes | 
|---|
| 91 | enum CompressionProcess_t | 
|---|
| 92 | { | 
|---|
| 93 | kFactRaw       = 0x0, | 
|---|
| 94 | kFactSmoothing = 0x1, | 
|---|
| 95 | kFactHuffman16 = 0x2 | 
|---|
| 96 | }; | 
|---|
| 97 |  | 
|---|
| 98 | //ordering of the columns / rows | 
|---|
| 99 | enum RowOrdering_t | 
|---|
| 100 | { | 
|---|
| 101 | kOrderByCol = 'C', | 
|---|
| 102 | kOrderByRow = 'R' | 
|---|
| 103 | }; | 
|---|
| 104 |  | 
|---|
| 105 | #ifdef __CINT__ | 
|---|
| 106 | // CINT doesn't like the packed attribute... | 
|---|
| 107 | // Therefore we give another hint of the size of the structure | 
|---|
| 108 | struct TileHeader  { char dummy[16]; }; | 
|---|
| 109 | struct BlockHeader { char dummy[10]; }; | 
|---|
| 110 | #else | 
|---|
| 111 | //Structure helper for tiles headers | 
|---|
| 112 | struct TileHeader | 
|---|
| 113 | { | 
|---|
| 114 | char     id[4]; | 
|---|
| 115 | uint32_t numRows; | 
|---|
| 116 | uint64_t size; | 
|---|
| 117 |  | 
|---|
| 118 | TileHeader() {} | 
|---|
| 119 |  | 
|---|
| 120 | TileHeader(uint32_t nRows, | 
|---|
| 121 | uint64_t s) : id{'T', 'I', 'L', 'E'}, | 
|---|
| 122 | numRows(nRows), | 
|---|
| 123 | size(s) | 
|---|
| 124 | { }; | 
|---|
| 125 | } __attribute__((__packed__)); | 
|---|
| 126 |  | 
|---|
| 127 | //Structure helper for blocks headers and compresion schemes | 
|---|
| 128 | struct BlockHeader | 
|---|
| 129 | { | 
|---|
| 130 | uint64_t      size; | 
|---|
| 131 | char          ordering; | 
|---|
| 132 | unsigned char numProcs; | 
|---|
| 133 | uint16_t      processings[]; | 
|---|
| 134 |  | 
|---|
| 135 | BlockHeader(uint64_t      s=0, | 
|---|
| 136 | char          o=kOrderByRow, | 
|---|
| 137 | unsigned char n=1) : size(s), | 
|---|
| 138 | ordering(o), | 
|---|
| 139 | numProcs(n) | 
|---|
| 140 | { | 
|---|
| 141 | } | 
|---|
| 142 | } __attribute__((__packed__)); | 
|---|
| 143 | #endif | 
|---|
| 144 |  | 
|---|
| 145 | //Helper structure to simplify the initialization and handling of compressed blocks headers | 
|---|
| 146 | struct Compression | 
|---|
| 147 | { | 
|---|
| 148 | std::vector<uint16_t> sequence; | 
|---|
| 149 | BlockHeader header; | 
|---|
| 150 |  | 
|---|
| 151 | Compression(const std::vector<uint16_t> &seq, const RowOrdering_t &order=kOrderByCol) | 
|---|
| 152 | : sequence(seq), header(0, order, seq.size()) | 
|---|
| 153 | { | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | Compression(const CompressionProcess_t &compression=kFactRaw, const RowOrdering_t &order=kOrderByCol) | 
|---|
| 157 | : sequence(1), header(0, order, 1) | 
|---|
| 158 | { | 
|---|
| 159 | sequence[0] = compression; | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | #ifdef __MARS__ // needed for CINT | 
|---|
| 163 | Compression(const int &compression) | 
|---|
| 164 | : sequence(1), header(0, kOrderByCol, 1) | 
|---|
| 165 | { | 
|---|
| 166 | sequence[0] = compression; | 
|---|
| 167 | } | 
|---|
| 168 | #endif | 
|---|
| 169 |  | 
|---|
| 170 | RowOrdering_t getOrdering() const { return RowOrdering_t(header.ordering); } | 
|---|
| 171 | uint32_t getSizeOnDisk() const { return sizeof(BlockHeader) + sizeof(uint16_t)*header.numProcs; } | 
|---|
| 172 | CompressionProcess_t getProc(uint32_t i) const { return CompressionProcess_t(sequence[i]); } | 
|---|
| 173 | uint16_t getNumProcs() const { return header.numProcs; } | 
|---|
| 174 |  | 
|---|
| 175 | void SetBlockSize(uint64_t size) { header.size = size; } | 
|---|
| 176 | void Memcpy(char *dest) const | 
|---|
| 177 | { | 
|---|
| 178 | memcpy(dest, &header, sizeof(BlockHeader)); | 
|---|
| 179 | memcpy(dest+sizeof(BlockHeader), sequence.data(), header.numProcs*sizeof(uint16_t)); | 
|---|
| 180 | } | 
|---|
| 181 | }; | 
|---|
| 182 | }; | 
|---|
| 183 |  | 
|---|
| 184 | #endif //_FITS_H_ | 
|---|
| 185 |  | 
|---|