add autoexpose calculator

This commit is contained in:
eddyem 2017-02-07 18:06:04 +03:00
parent 3490c31b98
commit 62e55687ca
3 changed files with 1229 additions and 161 deletions

View File

@ -30,6 +30,21 @@
#include <time.h> // time, gmtime etc
#include <fitsio.h> // save fits
#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
#define SUFFIX_FITS "fits"
@ -175,6 +190,7 @@ int writetiff(imstorage *img){
}
TIFFClose(image);
green(_("Image %s saved\n"), name);
modifytimestamp(name, img);
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_close_file, fp);
if(*filename == '!') ++filename; // remove '!' from filename
modifytimestamp(filename, img);
green(_("Image %s saved\n"), filename);
return 0;
}
@ -340,19 +357,39 @@ uint16_t *get_imdata(imstorage *img){
*/
int save_histo(FILE *f, imstorage *img){
if(!img->imdata) return 1000;
uint8_t histogram[256];
size_t histogram[256];
size_t l, S = img->W*img->H;
uint16_t *ptr = img->imdata;
memset(histogram, 0, 256);
memset(histogram, 0, 256 * sizeof(size_t));
for(l = 0; l < S; ++l, ++ptr){
++histogram[((*ptr)>>8)&0xff];
}
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){
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;
}
@ -372,6 +409,7 @@ int writedump(imstorage *img){
WARN(_("Can't make dump"));
return 3;
}
modifytimestamp(name, img);
name = make_filename(img->imname, "histogram.txt", img->st);
if(!name) return 4;
FILE *h = fopen(name, "w");
@ -380,6 +418,7 @@ int writedump(imstorage *img){
if(f){
i = save_histo(h, img);
fclose(h);
modifytimestamp(name, img);
}
if(i < 0){
WARN(_("Can't save histogram"));
@ -397,8 +436,8 @@ int writedump(imstorage *img){
*/
int store_image(imstorage *img){
int status = 0;
print_stat(img);
if(!img->imdata && !get_imdata(img)) return 1;
print_stat(img);
if(img->imformat & FORMAT_TIFF){ // save tiff file
if(writetiff(img)) status |= 1;
}

File diff suppressed because it is too large Load Diff

20
term.c
View File

@ -118,12 +118,21 @@ trans_status send_cmd_cs(uint8_t cmd){
return wait_checksum();
}
static int download_in_progress = 0; // == 1 when image downloading runs
/**
* Abort image exposition
* Used only on exit, so don't check commands status
*/
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);
read_tty(tmpbuf, 4096);
}
/**
@ -195,12 +204,14 @@ int try_connect(char *device, int speed){
if((spdstart = chkspeed(speed)) < 0) return 0;
spdmax = spdstart + 1;
}
uint8_t tmpbuf[4096];
green(_("Connecting to %s... "), device);
for(curspd = spdstart; curspd < spdmax; ++curspd){
tty_init(device, Bspeeds[curspd]);
DBG("Try speed %d", speeds[curspd]);
int ctr;
for(ctr = 0; ctr < 10; ++ctr){ // 10 tries to send data
read_tty(tmpbuf, 4096); // clear rbuf
if(send_cmd(CMD_COMM_TEST)) continue;
else break;
}
@ -401,7 +412,7 @@ imsubframe *define_subframe(char *parm){
return NULL;
}
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;
}
parm = eptr + 1;
@ -412,7 +423,7 @@ imsubframe *define_subframe(char *parm){
return NULL;
}
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;
}
Y = (uint16_t)L;
@ -514,7 +525,7 @@ int start_exposition(imstorage *im, char *imtype){
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)){
WARNX(_("Error sending command"));
return 7;
@ -604,6 +615,7 @@ uint16_t *get_image(imstorage *img){
FREE(buff);
return NULL;
}
download_in_progress = 1;
#ifdef EBUG
double tstart = dtime();
#endif
@ -666,6 +678,7 @@ uint16_t *get_image(imstorage *img){
printf("\n");
WARNX(_("Error receiving data"));
FREE(buff);
download_in_progress = 0;
return NULL;
}
//DBG("portion %d", ++i);
@ -674,5 +687,6 @@ uint16_t *get_image(imstorage *img){
}while(rest);
printf("\b Done!\n");
DBG("Got full data packet, capture time: %.1f seconds", dtime() - tstart);
download_in_progress = 0;
return buff;
}