mirror of
https://github.com/eddyem/SBIG_340.git
synced 2026-02-01 04:45:09 +03:00
add autoexpose calculator
This commit is contained in:
parent
3490c31b98
commit
62e55687ca
@ -30,6 +30,21 @@
|
|||||||
#include <time.h> // time, gmtime etc
|
#include <time.h> // time, gmtime etc
|
||||||
#include <fitsio.h> // save fits
|
#include <fitsio.h> // save fits
|
||||||
#include <libgen.h> // basename
|
#include <libgen.h> // basename
|
||||||
|
#include <sys/stat.h> // utimensat
|
||||||
|
#include <fcntl.h> // AT_...
|
||||||
|
/**
|
||||||
|
* All image-storing functions modify ctime of saved files to be the time of
|
||||||
|
* exposition start!
|
||||||
|
*/
|
||||||
|
|
||||||
|
void modifytimestamp(char *filename, imstorage *img){
|
||||||
|
if(!filename) return;
|
||||||
|
struct timespec times[2];
|
||||||
|
memset(times, 0, 2*sizeof(struct timespec));
|
||||||
|
times[0].tv_nsec = UTIME_OMIT;
|
||||||
|
times[1].tv_sec = img->exposetime; // change mtime
|
||||||
|
if(utimensat(AT_FDCWD, filename, times, 0)) WARN(_("Can't change timestamp for %s"), filename);
|
||||||
|
}
|
||||||
|
|
||||||
// image type suffixes
|
// image type suffixes
|
||||||
#define SUFFIX_FITS "fits"
|
#define SUFFIX_FITS "fits"
|
||||||
@ -175,6 +190,7 @@ int writetiff(imstorage *img){
|
|||||||
}
|
}
|
||||||
TIFFClose(image);
|
TIFFClose(image);
|
||||||
green(_("Image %s saved\n"), name);
|
green(_("Image %s saved\n"), name);
|
||||||
|
modifytimestamp(name, img);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -313,6 +329,7 @@ int writefits(imstorage *img){
|
|||||||
TRYFITS(fits_write_img, fp, TUSHORT, 1, img->W * img->H, img->imdata);
|
TRYFITS(fits_write_img, fp, TUSHORT, 1, img->W * img->H, img->imdata);
|
||||||
TRYFITS(fits_close_file, fp);
|
TRYFITS(fits_close_file, fp);
|
||||||
if(*filename == '!') ++filename; // remove '!' from filename
|
if(*filename == '!') ++filename; // remove '!' from filename
|
||||||
|
modifytimestamp(filename, img);
|
||||||
green(_("Image %s saved\n"), filename);
|
green(_("Image %s saved\n"), filename);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -340,19 +357,39 @@ uint16_t *get_imdata(imstorage *img){
|
|||||||
*/
|
*/
|
||||||
int save_histo(FILE *f, imstorage *img){
|
int save_histo(FILE *f, imstorage *img){
|
||||||
if(!img->imdata) return 1000;
|
if(!img->imdata) return 1000;
|
||||||
uint8_t histogram[256];
|
size_t histogram[256];
|
||||||
size_t l, S = img->W*img->H;
|
size_t l, S = img->W*img->H;
|
||||||
uint16_t *ptr = img->imdata;
|
uint16_t *ptr = img->imdata;
|
||||||
memset(histogram, 0, 256);
|
memset(histogram, 0, 256 * sizeof(size_t));
|
||||||
for(l = 0; l < S; ++l, ++ptr){
|
for(l = 0; l < S; ++l, ++ptr){
|
||||||
++histogram[((*ptr)>>8)&0xff];
|
++histogram[((*ptr)>>8)&0xff];
|
||||||
}
|
}
|
||||||
for(l = 0; l < 256; ++l){
|
for(l = 0; l < 256; ++l){
|
||||||
int status = fprintf(f, "%zd\t%u\n", l, histogram[l]);
|
int status = fprintf(f, "%zd\t%zd\n", l, histogram[l]);
|
||||||
if(status < 0){
|
if(status < 0){
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
size_t low2 = S/50, med = S/2, up2 = (S*49)/50, acc = 0;
|
||||||
|
int lval = -1, mval = -1, tval = -1;
|
||||||
|
for(l = 0; l < 256; ++l){ // get stat parameters
|
||||||
|
acc += histogram[l];
|
||||||
|
if(lval < 0 && acc >= low2) lval = l;
|
||||||
|
else if(mval < 0 && acc >= med) mval = l;
|
||||||
|
else if(tval < 0 && acc >= up2) tval = l;
|
||||||
|
}
|
||||||
|
printf("low 2%% (%zd pixels) = %d, median (%zd pixels) = %d, up 2%% (%zd pixels) = %d\n",
|
||||||
|
low2, lval, med, mval, up2, tval);
|
||||||
|
double mul = 1., mulmax = 255. / tval;
|
||||||
|
if(mval < 120 || mval > 134){
|
||||||
|
if(lval > 32) mul = 96. / mval;
|
||||||
|
else if(mval < 127) mul = 120. / mval;
|
||||||
|
else if(mval < 196) mul = 120. / (mval - lval);
|
||||||
|
else if(mval < 245) mul = 96. / (tval - lval);
|
||||||
|
else mval = 0.03;
|
||||||
|
}
|
||||||
|
if(mul > mulmax) mul = mulmax;
|
||||||
|
green("Recommended exposition time: %.2f seconds\n", img->exptime * mul);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -372,6 +409,7 @@ int writedump(imstorage *img){
|
|||||||
WARN(_("Can't make dump"));
|
WARN(_("Can't make dump"));
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
|
modifytimestamp(name, img);
|
||||||
name = make_filename(img->imname, "histogram.txt", img->st);
|
name = make_filename(img->imname, "histogram.txt", img->st);
|
||||||
if(!name) return 4;
|
if(!name) return 4;
|
||||||
FILE *h = fopen(name, "w");
|
FILE *h = fopen(name, "w");
|
||||||
@ -380,6 +418,7 @@ int writedump(imstorage *img){
|
|||||||
if(f){
|
if(f){
|
||||||
i = save_histo(h, img);
|
i = save_histo(h, img);
|
||||||
fclose(h);
|
fclose(h);
|
||||||
|
modifytimestamp(name, img);
|
||||||
}
|
}
|
||||||
if(i < 0){
|
if(i < 0){
|
||||||
WARN(_("Can't save histogram"));
|
WARN(_("Can't save histogram"));
|
||||||
@ -397,8 +436,8 @@ int writedump(imstorage *img){
|
|||||||
*/
|
*/
|
||||||
int store_image(imstorage *img){
|
int store_image(imstorage *img){
|
||||||
int status = 0;
|
int status = 0;
|
||||||
print_stat(img);
|
|
||||||
if(!img->imdata && !get_imdata(img)) return 1;
|
if(!img->imdata && !get_imdata(img)) return 1;
|
||||||
|
print_stat(img);
|
||||||
if(img->imformat & FORMAT_TIFF){ // save tiff file
|
if(img->imformat & FORMAT_TIFF){ // save tiff file
|
||||||
if(writetiff(img)) status |= 1;
|
if(writetiff(img)) status |= 1;
|
||||||
}
|
}
|
||||||
|
|||||||
1323
sbig340.c.tags
1323
sbig340.c.tags
File diff suppressed because it is too large
Load Diff
20
term.c
20
term.c
@ -118,12 +118,21 @@ trans_status send_cmd_cs(uint8_t cmd){
|
|||||||
return wait_checksum();
|
return wait_checksum();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int download_in_progress = 0; // == 1 when image downloading runs
|
||||||
/**
|
/**
|
||||||
* Abort image exposition
|
* Abort image exposition
|
||||||
* Used only on exit, so don't check commands status
|
* Used only on exit, so don't check commands status
|
||||||
*/
|
*/
|
||||||
void abort_image(){
|
void abort_image(){
|
||||||
|
uint8_t tmpbuf[4096];
|
||||||
|
if(download_in_progress){
|
||||||
|
read_tty(tmpbuf, 4096);
|
||||||
|
send_cmd(IMTRANS_STOP);
|
||||||
|
download_in_progress = 0;
|
||||||
|
}
|
||||||
|
read_tty(tmpbuf, 4096);
|
||||||
send_cmd_cs(CMD_ABORT_IMAGE);
|
send_cmd_cs(CMD_ABORT_IMAGE);
|
||||||
|
read_tty(tmpbuf, 4096);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -195,12 +204,14 @@ int try_connect(char *device, int speed){
|
|||||||
if((spdstart = chkspeed(speed)) < 0) return 0;
|
if((spdstart = chkspeed(speed)) < 0) return 0;
|
||||||
spdmax = spdstart + 1;
|
spdmax = spdstart + 1;
|
||||||
}
|
}
|
||||||
|
uint8_t tmpbuf[4096];
|
||||||
green(_("Connecting to %s... "), device);
|
green(_("Connecting to %s... "), device);
|
||||||
for(curspd = spdstart; curspd < spdmax; ++curspd){
|
for(curspd = spdstart; curspd < spdmax; ++curspd){
|
||||||
tty_init(device, Bspeeds[curspd]);
|
tty_init(device, Bspeeds[curspd]);
|
||||||
DBG("Try speed %d", speeds[curspd]);
|
DBG("Try speed %d", speeds[curspd]);
|
||||||
int ctr;
|
int ctr;
|
||||||
for(ctr = 0; ctr < 10; ++ctr){ // 10 tries to send data
|
for(ctr = 0; ctr < 10; ++ctr){ // 10 tries to send data
|
||||||
|
read_tty(tmpbuf, 4096); // clear rbuf
|
||||||
if(send_cmd(CMD_COMM_TEST)) continue;
|
if(send_cmd(CMD_COMM_TEST)) continue;
|
||||||
else break;
|
else break;
|
||||||
}
|
}
|
||||||
@ -401,7 +412,7 @@ imsubframe *define_subframe(char *parm){
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if(L > IMWIDTH - 1 || L < 1){
|
if(L > IMWIDTH - 1 || L < 1){
|
||||||
WARNX(_("Xstart should be in range 0..%d"), IMWIDTH - 1 );
|
WARNX(_("Xstart should be in range 1..%d"), IMWIDTH - 1 );
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
parm = eptr + 1;
|
parm = eptr + 1;
|
||||||
@ -412,7 +423,7 @@ imsubframe *define_subframe(char *parm){
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if(L > IMHEIGHT - 1 || L < 1){
|
if(L > IMHEIGHT - 1 || L < 1){
|
||||||
WARNX(_("Ystart should be in range 0..%d"), IMHEIGHT - 1 );
|
WARNX(_("Ystart should be in range 1..%d"), IMHEIGHT - 1 );
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
Y = (uint16_t)L;
|
Y = (uint16_t)L;
|
||||||
@ -514,7 +525,7 @@ int start_exposition(imstorage *im, char *imtype){
|
|||||||
return 8;
|
return 8;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
green("Start expose for %gseconds, mode \"%s\", %s image\n", exptime, m, b);
|
green("Start expose for %g seconds, mode \"%s\", %s image\n", exptime, m, b);
|
||||||
if(send_data(cmd, 6)){
|
if(send_data(cmd, 6)){
|
||||||
WARNX(_("Error sending command"));
|
WARNX(_("Error sending command"));
|
||||||
return 7;
|
return 7;
|
||||||
@ -604,6 +615,7 @@ uint16_t *get_image(imstorage *img){
|
|||||||
FREE(buff);
|
FREE(buff);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
download_in_progress = 1;
|
||||||
#ifdef EBUG
|
#ifdef EBUG
|
||||||
double tstart = dtime();
|
double tstart = dtime();
|
||||||
#endif
|
#endif
|
||||||
@ -666,6 +678,7 @@ uint16_t *get_image(imstorage *img){
|
|||||||
printf("\n");
|
printf("\n");
|
||||||
WARNX(_("Error receiving data"));
|
WARNX(_("Error receiving data"));
|
||||||
FREE(buff);
|
FREE(buff);
|
||||||
|
download_in_progress = 0;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
//DBG("portion %d", ++i);
|
//DBG("portion %d", ++i);
|
||||||
@ -674,5 +687,6 @@ uint16_t *get_image(imstorage *img){
|
|||||||
}while(rest);
|
}while(rest);
|
||||||
printf("\b Done!\n");
|
printf("\b Done!\n");
|
||||||
DBG("Got full data packet, capture time: %.1f seconds", dtime() - tstart);
|
DBG("Got full data packet, capture time: %.1f seconds", dtime() - tstart);
|
||||||
|
download_in_progress = 0;
|
||||||
return buff;
|
return buff;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user