darks, min storing interval

This commit is contained in:
eddyem 2017-03-22 10:27:53 +03:00
parent b85b89c468
commit c0c7b357ac
5 changed files with 40 additions and 24 deletions

View File

@ -40,6 +40,7 @@ glob_pars const Gdefault = {
.port = "55555", // port to listen boltwood data .port = "55555", // port to listen boltwood data
.server = NULL, // server name .server = NULL, // server name
.filename = NULL, // input file name .filename = NULL, // input file name
.min_storage_time = 60.,// minimal storage period
}; };
/* /*
@ -52,6 +53,7 @@ myoption cmdlnopts[] = {
{"port", NEED_ARG, NULL, 'p', arg_string, APTR(&G.port), _("port to connect (default: 55555)")}, {"port", NEED_ARG, NULL, 'p', arg_string, APTR(&G.port), _("port to connect (default: 55555)")},
{"address", NEED_ARG, NULL, 'a', arg_string, APTR(&G.server), _("server name")}, {"address", NEED_ARG, NULL, 'a', arg_string, APTR(&G.server), _("server name")},
{"filename",NEED_ARG, NULL, 'i', arg_string, APTR(&G.filename), _("input FITS file name")}, {"filename",NEED_ARG, NULL, 'i', arg_string, APTR(&G.filename), _("input FITS file name")},
{"mindt", NEED_ARG, NULL, 't', arg_double, APTR(&G.min_storage_time), _("minimal time interval (in seconds) for image storage (default: 60s)")},
end_option end_option
}; };

View File

@ -34,6 +34,7 @@ typedef struct{
char *cwd; // change working directory to given char *cwd; // change working directory to given
char *server; // server name char *server; // server name
char *filename; // input file name char *filename; // input file name
double min_storage_time;// minimal storage period
} glob_pars; } glob_pars;

View File

@ -154,7 +154,7 @@ do{ int status = 0; \
* file stored in current directory under YYYY/MM/DD/hh:mm:ss.fits.gz * file stored in current directory under YYYY/MM/DD/hh:mm:ss.fits.gz
* @return file descriptor for new notifications * @return file descriptor for new notifications
*/ */
int store_fits(char *name, datarecord *data){ void store_fits(char *name, datarecord *data){
DBG("Try to store %s", name); DBG("Try to store %s", name);
double t0 = dtime(); double t0 = dtime();
long modtm = 0; // modification time - to change later long modtm = 0; // modification time - to change later
@ -168,9 +168,11 @@ int store_fits(char *name, datarecord *data){
TRYFITS(fits_get_hdrspace, fptr, &nkeys, NULL); TRYFITS(fits_get_hdrspace, fptr, &nkeys, NULL);
DBG("%d keys", nkeys); DBG("%d keys", nkeys);
green("open & get hdr: %gs\n", dtime() - t0); green("open & get hdr: %gs\n", dtime() - t0);
char dateobs[FLEN_KEYWORD], timeobs[FLEN_KEYWORD]; char dateobs[FLEN_KEYWORD], timeobs[FLEN_KEYWORD], imtype[FLEN_KEYWORD];
TRYFITS(fits_read_key, fptr, TSTRING, "DATE-OBS", dateobs, NULL); TRYFITS(fits_read_key, fptr, TSTRING, "DATE-OBS", dateobs, NULL);
DBG("DATE-OBS: %s", dateobs); DBG("DATE-OBS: %s", dateobs);
TRYFITS(fits_read_key, fptr, TSTRING, "IMAGETYP", imtype, NULL);
DBG("IMAGETYP: %s", dateobs);
TRYFITS(fits_read_key, fptr, TSTRING, "START", timeobs, NULL); TRYFITS(fits_read_key, fptr, TSTRING, "START", timeobs, NULL);
DBG("START: %s", timeobs); DBG("START: %s", timeobs);
TRYFITS(fits_read_key, fptr, TLONG, "UNIXTIME", &modtm, NULL); TRYFITS(fits_read_key, fptr, TLONG, "UNIXTIME", &modtm, NULL);
@ -180,7 +182,10 @@ int store_fits(char *name, datarecord *data){
green("+ got headers: %gs\n", dtime() - t0); green("+ got headers: %gs\n", dtime() - t0);
gotodir(dateobs); gotodir(dateobs);
char newname[PATH_MAX]; char newname[PATH_MAX];
snprintf(newname, PATH_MAX, "%s.fits.gz", timeobs); if(strncasecmp(imtype, "dark", 4) == 0)
snprintf(newname, PATH_MAX, "%s.dark.fits.gz", timeobs);
else
snprintf(newname, PATH_MAX, "%s.fits.gz", timeobs);
// remove if exists // remove if exists
unlink(newname); unlink(newname);
TRYFITS(fits_create_file, &ofptr, newname); TRYFITS(fits_create_file, &ofptr, newname);
@ -199,15 +204,17 @@ int store_fits(char *name, datarecord *data){
else else
fits_report_error(stderr, status); fits_report_error(stderr, status);
} }
DBG("Boltwood data"); if(data){
// now the file copied -> add header from Boltwood's sensor DBG("Boltwood data");
while(data->varname){ // now the file copied -> add header from Boltwood's sensor
if(data->type == INTEGR){ while(data->varname){
WRITEIREC(data->keyname, data->data.i, data->comment); if(data->type == INTEGR){
}else{ WRITEIREC(data->keyname, data->data.i, data->comment);
WRITEDREC(data->keyname, data->data.d, data->comment); }else{
WRITEDREC(data->keyname, data->data.d, data->comment);
}
++data;
} }
++data;
} }
long npix = naxes[0]*naxes[1]; long npix = naxes[0]*naxes[1];
imdat = MALLOC(uint16_t, npix); imdat = MALLOC(uint16_t, npix);
@ -225,9 +232,7 @@ ret:
modifytimestamp(newname, (time_t)modtm); modifytimestamp(newname, (time_t)modtm);
} }
// as cfitsio removes old file instead of trunkate it, we need to refresh inotify every time! // as cfitsio removes old file instead of trunkate it, we need to refresh inotify every time!
int fd = watch_fits(name);
if(chdir(oldwd)){ // return back to BD root directory if(chdir(oldwd)){ // return back to BD root directory
ERR("Can't chdir"); ERR("Can't chdir");
}; };
return fd;
} }

View File

@ -27,6 +27,6 @@
#include "cmdlnopts.h" #include "cmdlnopts.h"
int test_fits(char *name); int test_fits(char *name);
int store_fits(char *name, datarecord *data); void store_fits(char *name, datarecord *data);
#endif // __IMFUNCTIONS_H__ #endif // __IMFUNCTIONS_H__

View File

@ -38,6 +38,8 @@
// Max amount of connections // Max amount of connections
#define BACKLOG (30) #define BACKLOG (30)
static double minstoragetime;
// keyname, comment, type, data // keyname, comment, type, data
static datarecord boltwood_data[] = { static datarecord boltwood_data[] = {
{"humidstatTempCode" , "HSCODE" , "hum. and ambient temp. sensor code (0 - OK)" , INTEGR, {0}}, {"humidstatTempCode" , "HSCODE" , "hum. and ambient temp. sensor code (0 - OK)" , INTEGR, {0}},
@ -179,20 +181,19 @@ static int waittoread(int sock){
* @return inotify file descriptor * @return inotify file descriptor
*/ */
int watch_fits(char *name){ int watch_fits(char *name){
static int oldfd = -1, oldwd = -1; static int fd = -1, wd = -1;
if(oldfd > 0 && oldwd > 0){ if(fd > 0 && wd > 0){
inotify_rm_watch(oldfd, oldwd); inotify_rm_watch(fd, wd);
oldfd = -1, oldwd = -1; wd = -1;
} }
if(!test_fits(name)) ERRX(_("File %s is not FITS file with 2D image!"), name); if(!test_fits(name)) ERRX(_("File %s is not FITS file with 2D image!"), name);
int fd; if(fd < 0)
fd = inotify_init1(IN_NONBLOCK); fd = inotify_init1(IN_NONBLOCK);
if(fd == -1) ERR("inotify_init1()"); if(fd == -1) ERR("inotify_init1()");
oldfd = fd;
FILE* f = fopen(name, "r"); FILE* f = fopen(name, "r");
if(!f) ERR("fopen()"); // WTF??? if(!f) ERR("fopen()"); // WTF???
fclose(f); fclose(f);
if((oldwd = inotify_add_watch(fd, name, IN_CLOSE_WRITE)) < 0) if((wd = inotify_add_watch(fd, name, IN_CLOSE_WRITE)) < 0)
ERR("inotify_add_watch()"); ERR("inotify_add_watch()");
DBG("file %s added to inotify", name); DBG("file %s added to inotify", name);
return fd; return fd;
@ -211,7 +212,7 @@ static void test_path(char *path){
/** /**
* Client daemon itself * Client daemon itself
* @param G - global parameters * @param FITSpath - path to file watched
* @param infd - inotify file descriptor * @param infd - inotify file descriptor
* @param sock - socket's file descriptor * @param sock - socket's file descriptor
*/ */
@ -225,6 +226,7 @@ static void client_(char *FITSpath, int infd, int sock){
fds.fd = infd; fds.fd = infd;
fds.events = POLLIN; fds.events = POLLIN;
char buf[256]; char buf[256];
double lastTstorage = -1.; // last time of file saving
void rlc(size_t newsz){ void rlc(size_t newsz){
if(newsz >= Bufsiz){ if(newsz >= Bufsiz){
Bufsiz += 1024; Bufsiz += 1024;
@ -255,7 +257,12 @@ static void client_(char *FITSpath, int infd, int sock){
}else{ }else{
DBG("file changed"); DBG("file changed");
usleep(100000); // wait a little for file changes usleep(100000); // wait a little for file changes
fds.fd = store_fits(FITSpath, last_good_msrment); fds.fd = watch_fits(FITSpath);
if(dtime() - lastTstorage > minstoragetime){
DBG("lastT: %.2g, now: %.2g", lastTstorage, dtime());
lastTstorage = dtime();
store_fits(FITSpath, last_good_msrment);
}
} }
} }
} }
@ -293,6 +300,7 @@ void daemonize(glob_pars *G){
int fd = watch_fits(G->filename); int fd = watch_fits(G->filename);
// CD to archive directory if user wants // CD to archive directory if user wants
test_path(G->cwd); test_path(G->cwd);
minstoragetime = G->min_storage_time;
// run fork before socket opening to prevent daemon's death if there's no network // run fork before socket opening to prevent daemon's death if there's no network
#ifndef EBUG #ifndef EBUG
green("Daemonize\n"); green("Daemonize\n");