Thanks Chad. I worked along your suggestion.
Note that BZERO =0 does not lead to a 0-65535 range, in my case, but to 0-32767, which tells me that ImgBuff is not coded as unsigned but as signed. Hence, setting BZERO to 32768 successfully solves the issue.
Cheers,
Remi
Attached is a working code.
int main(int argc,char **argv)
{
void parse_args(int, char**);
int width, height;
int i;
char c;
bool bresult;
fitsfile *fptr; /* pointer to the FITS file; defined in fitsio.h */
int fitstatus = 0, ii, jj;
long fpixel = 1, naxis = 2, nelements, exposure;
long naxes = {5496, 3672}; /* image is 5496 pixels wide by 3672 rows */
// long naxes = {1280, 960}; /* image is 1280 pixels wide by 960 rows */
long bzero, bscale, gain, xbin, ybin;
float pixsize;
int time1,time2;
int count=0;
int CamNum=0;
exptime = -1;
/*
* Read command line arguments
*/
parse_args(argc, argv);
int numDevices = ASIGetNumOfConnectedCameras();
if(numDevices <= 0)
{
printf("no camera connected, press any key to exit\n");
getchar();
return -1;
}
else {
ASI_CAMERA_INFO ASICameraInfo;
ASIGetCameraProperty(&ASICameraInfo, 0);
printf("0 %s\n", ASICameraInfo.Name);
if(ASIOpenCamera(CamNum) != ASI_SUCCESS)
{
printf("OpenCamera error,are you root?,press any key to exit\n");
getchar();
return -1;
}
ASIInitCamera(CamNum);
ASI_ERROR_CODE err = ASIEnableDarkSubtract(CamNum, "dark.bmp");
if(err == ASI_SUCCESS)
printf("load dark ok\n");
else
printf("load dark failed %d\n", err);
// printf("%s information\n",ASICameraInfo.Name);
int iMaxWidth, iMaxHeight;
iMaxWidth = ASICameraInfo.MaxWidth;
iMaxHeight = ASICameraInfo.MaxHeight;
// printf("resolution:%d X %d\n", iMaxWidth, iMaxHeight);
/* ASI_CONTROL_CAPS ControlCaps;
int iNumOfCtrl = 0;
ASIGetNumOfControls(CamNum, &iNumOfCtrl);
for( i = 0; i < iNumOfCtrl; i++)
{
ASIGetControlCaps(CamNum, i, &ControlCaps);
printf("%s %s %f\n", ControlCaps.Name, ControlCaps.Description, ControlCaps.DefaultValue);
}*/
// ASI183MM Pro
// Gain Gain 0.000000
// Exposure Exposure Time(us) 0.000000
// Offset offset 0.000000
// BandWidth The total data transfer rate percentage 0.000000
// Flip Flip: 0->None 1->Horiz 2->Vert 3->Both 0.000000
// AutoExpMaxGain Auto exposure maximum gain value 0.000000
// AutoExpMaxExpMS Auto exposure maximum exposure value(unit ms) 0.000000
// AutoExpTargetBrightness Auto exposure target brightness value 0.000000
// HardwareBin Is hardware bin2->No 1->Yes 0.000000
// HighSpeedMode Is high speed mode->No 1->Yes 0.000000
// Temperature Sensor temperature(degrees Celsius) 0.000000
// CoolPowerPerc Cooler power percent 0.000000
// TargetTemp Target temperature(cool camera only) 0.000000
// CoolerOn turn on/off cooler(cool camera only) 0.000000
long ltemp = 0;
ASI_BOOL bAuto = ASI_FALSE;
err = ASISetControlValue(CamNum, ASI_GAIN, 50, ASI_FALSE);
err = ASISetControlValue(CamNum, ASI_TARGET_TEMP, -1, ASI_FALSE);
err = ASISetControlValue(CamNum, ASI_COOLER_ON, 1, ASI_TRUE);
err = ASIGetControlValue(CamNum, ASI_TEMPERATURE, <emp, &bAuto);
printf("sensor temperature:%02f\n", (float)ltemp/10.0);
while (ltemp > -1) {
usleep(30000000);
ASIGetControlValue(CamNum, ASI_TEMPERATURE, <emp, &bAuto);
printf("sensor temperature:%02f\n", (float)ltemp/10.0);
}
// Set image caracteristics: width, height, binning, type (ASI_IMG_RAW8 or ASI_IMG_RAW16)
// default values are CCD size 5496x3672, binning 1, TYPE 16.
width = iMaxWidth;
height = iMaxHeight;
printf("width=%d height=%d\n",iMaxWidth,iMaxHeight);
int bin = 1;
ASI_IMG_TYPE Image_type = ASI_IMG_RAW16;
err = ASISetROIFormat(CamNum, width, height, bin, Image_type);
if (err == ASI_SUCCESS)
printf("Set ROI ok: width %d height %d bin %d Image_type %d\n",
width, height, bin,Image_type);
else
printf("Set ROI failed %d\n", err);
usleep(10000);//10ms
long imgSize = width*height*(1+(Image_type==ASI_IMG_RAW16));
unsigned char* imgBuf = new unsigned char;
//ASISetControlValue(CamNum, ASI_GAIN, 0, ASI_FALSE);
int exp_ms; // input exposure time(ms)
if (exptime<0)
exp_ms = 10000;
else
exp_ms = 1000*exptime;
ASISetControlValue(CamNum, ASI_EXPOSURE, exp_ms*1000, ASI_FALSE);
ASISetControlValue(CamNum, ASI_BANDWIDTHOVERLOAD, 40, ASI_FALSE);
time1 = GetTickCount();
ASI_EXPOSURE_STATUS status;
int iDropped = 0;
//bool bSave = true;
ASIStartExposure(CamNum, ASI_FALSE);
usleep(1000000);//1000ms
status = ASI_EXP_WORKING;
while(status == ASI_EXP_WORKING)
{
ASIGetExpStatus(CamNum, &status);
}
if(status == ASI_EXP_SUCCESS)
{
ASIGetDataAfterExp(CamNum, imgBuf, imgSize);
printf("imgBuf ");
for (i=0;i<20;i+=2) {
printf(" %d ", 256*imgBuf+imgBuf);
}
// printf("\n");
time2 = GetTickCount();
count++;
if(time2-time1 > 1000 )
{
ASIGetDroppedFrames(CamNum, &iDropped);
count = 0;
time1=GetTickCount();
printf("fps:%d dropped frames:%d ImageType:%d\n",count, iDropped, Image_type);
}
// Save a fits image in a folder of the night and time
istimeto();
sprintf(commande,"/bin/mkdir -p ./TBLAstrometry/data/%s", nuit);
system(commande);
sprintf(fitsname,"./TBLAstrometry/data/%s/%s",nuit,image_name);
// fits_create_img(fitsfile *fptr, int bitpix, int naxis, long *naxes, int *fitstatus)
// fits_write_pix(fitsfile *fptr, int datatype, long *fpixel, long nelements,
// void *array, int *fitstatus);
bzero = 32768;
bscale = 1;
gain = 50;
bin = 1;
pixsize = 2.4*bin;
fits_create_file(&fptr, fitsname, &fitstatus);
fits_create_img(fptr, SHORT_IMG, naxis, naxes, &fitstatus); // SHORT_IMG = 16
exposure = exptime*1000;
fits_update_key(fptr, TLONG, "EXPOSURE", &exposure, "Total Exposure Time ", &fitstatus);
fits_update_key(fptr, TLONG, "BZERO", &bzero, "physical = BZERO + BSCALE*array_value", &fitstatus);
fits_update_key(fptr, TLONG, "BSCALE", &bscale, "physical = BZERO + BSCALE*array_value", &fitstatus);
fits_update_key(fptr, TLONG, "GAIN", &gain, "The ratio of output / input", &fitstatus);
fits_update_key(fptr, TLONG, "XBIN", &xbin, "Binning factor in width", &fitstatus);
fits_update_key(fptr, TLONG, "YBIN", &ybin, "Binning factor in height", &fitstatus);
fits_update_key(fptr, TFLOAT, "XPIXSZ", &pixsize, "Pixel Width in um (after binning)", &fitstatus);
fits_update_key(fptr, TFLOAT, "YPIXSZ", &pixsize, "Pixel height in um (after binning)", &fitstatus);
nelements = naxes * naxes; /* number of pixels to write */
fits_write_img(fptr, TUSHORT, fpixel, nelements, &imgBuf, &fitstatus);
fits_close_file(fptr, &fitstatus); /* close the file */
fits_report_error(stderr, fitstatus);
}
ASIStopExposure(CamNum);
ASICloseCamera(CamNum);
if(imgBuf)
delete imgBuf;
printf("main function over\n");
return 1;
}
}