forked from foxsi/SAAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompression.cpp
128 lines (106 loc) · 5.07 KB
/
compression.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "compression.hpp"
#include <CCfits>
#include <cmath>
#include <valarray>
#include <vector>
using namespace CCfits;
int writeFITSImage(unsigned char *data, HeaderData keys, const std::string fileName, int width, int height)
{
try {
std::string timeKey;
if (width == 0 || height == 0)
{
std::cerr << "Image dimension is 0. Not saving." << std::endl;
return -1;
}
// declare auto-pointer to FITS at function scope. Ensures no resources
// leaked if something fails in dynamic allocation.
std::auto_ptr<FITS> pFits(0);
try
{
pFits.reset(new FITS(fileName, BYTE_IMG, 0, 0));
}
catch (FITS::CantCreate)
{
// ... or not, as the case may be.
return -1;
}
catch (FITS::CantOpen)
{
// ... or not, as the case may be.
std::cerr << "Could not open file to save FITS image\n";
return -1;
}
long nelements(1);
std::vector<long int> extAx;
extAx.push_back(width);
extAx.push_back(height);
string newName ("Raw Frame");
pFits->setCompressionType(RICE_1);
ExtHDU* imageExt;
try{
imageExt = pFits->addImage(newName, BYTE_IMG, extAx, 1);
}
catch(FitsError e){
std::cerr << "Error while creating image extension\n";
std::cerr << e.message() << "\n";
return -1;
}
nelements = width*height*sizeof(unsigned char);
std::valarray<unsigned char> array(data, nelements);
long fpixel(1);
//add keys to the primary header
pFits->pHDU().addKey("TELESCOP",std::string("FOXSI"),"Name of source telescope package");
// pFits->pHDU().addKey("SIMPLE",(int)1,"always T for True, if conforming FITS file");
pFits->pHDU().addKey("INSTRUME",std::string("SAAS"),"Name of instrument");
pFits->pHDU().addKey("ORIGIN", std::string("FOXSI/SAAS SBC") , "Location where file was made");
pFits->pHDU().addKey("WAVELNTH", (long)6320, "Wavelength of observation (ang)");
pFits->pHDU().addKey("WAVE_STR", std::string("632 Nm"), "Wavelength of observation string");
pFits->pHDU().addKey("BITPIX", (long) 8, "Bit depth of image");
pFits->pHDU().addKey("BZERO", (long) 128, "Bit depth of image");
pFits->pHDU().addKey("BSCALE", (float) 1.0, "Bit depth of image");
pFits->pHDU().addKey("WAVEUNIT", std::string("angstrom"), "Units of WAVELNTH");
pFits->pHDU().addKey("PIXLUNIT", std::string("DN"), "Pixel units");
pFits->pHDU().addKey("IMG_TYPE", std::string("LIGHT"), "Image type");
pFits->pHDU().addKey("RSUN_REF", (double)6.9600000e+08, "");
pFits->pHDU().addKey("CTLMODE", std::string("LIGHT"), "Image type");
pFits->pHDU().addKey("LVL_NUM", (int) 0 , "Level of data");
pFits->pHDU().addKey("RSUN_OBS", (double) 0, "");
pFits->pHDU().addKey("CTYPE1", std::string("HPLN-TAN"), "A string value labeling each coordinate axis");
pFits->pHDU().addKey("CTYPE2", std::string("HPLN-TAN"), "A string value labeling each coordinate axis");
pFits->pHDU().addKey("CUNIT1", std::string("arcsec"), "Coordinate Units");
pFits->pHDU().addKey("CUNIT2", std::string("arcsec"), "Coordinate Units");
pFits->pHDU().addKey("CRVAL1", (double)0.0, "Coordinate value of the reference pixel");
pFits->pHDU().addKey("CRVAL2", (double)0.0, "Coordinate value of the reference pixel");
pFits->pHDU().addKey("CDELT1", (double)keys.plateScale, "Plate scale");
pFits->pHDU().addKey("CDELT2", (double)keys.plateScale, "Plate scale");
pFits->pHDU().addKey("CRPIX1", (double)0.0, "Reference pixel");
pFits->pHDU().addKey("CRPIX2", (double)0.0, "Reference pixel");
timeKey = asctime(gmtime(&(keys.captureTime).tv_sec));
pFits->pHDU().addKey("EXPTIME", (float)keys.exposure/1e6, "Exposure time in seconds");
pFits->pHDU().addKey("DATE_OBS", timeKey , "Date and time when observation of this image started (UTC)");
pFits->pHDU().addKey("TEMPCCD", (float)keys.cameraTemperature, "Temperature of camera in Celsius");
pFits->pHDU().addKey("FILENAME", fileName , "Name of the data file");
//pFits->pHDU().addKey("TIME", 0 , "Time of observation in seconds within a day");
pFits->pHDU().addKey("CAMERAID", (int)keys.cameraID , "Serial Number of camera");
pFits->pHDU().addKey("EXPOSURE", (int)keys.exposure,"Exposure time in usec");
pFits->pHDU().addKey("GAIN_PRE", (float)keys.preampGain, "Preamp gain of CCD");
pFits->pHDU().addKey("GAIN_ANA", (int)keys.analogGain, "Analog gain of CCD");
pFits->pHDU().addKey("FRAMENUM", (long)keys.frameCount, "Frame number");
pFits->pHDU().addKey("CROSSHAIR_X", (int)keys.cross_x, "Crosshair x coordinate");
pFits->pHDU().addKey("CROSSHAIR_Y", (int)keys.cross_y, "Crosshair y coordinate");
try{
imageExt->write(fpixel, nelements, array);
}
catch(FitsException e){
std::cerr << "Exception while writing image to extension\n";
std::cerr << e.message() << std::endl;
return -1;
}
} catch (FitsError fe) {
std::cerr << "Exception somewhere else in writeFITSImage()\n";
std::cerr << fe.message() << std::endl;
//throw fe;
}
return 0;
}