Add some usefull functions from lib SOFA

This commit is contained in:
eddyem
2020-05-19 20:23:14 +03:00
parent 1a7a4db46e
commit 1250d0642b
20 changed files with 698 additions and 77 deletions

View File

@@ -45,7 +45,7 @@ void radtodeg(double r){
#define REP(a,b,c)
#endif
// temporal stubs for weather/place data; return 0 if all OK
// temporal stubs for weather/place/DUT1 data; return 0 if all OK
int getPlace(placeData *p){
if(!p) return 0;
/* Site longitude, latitude (radians) and height above the geoid (m). */
@@ -63,7 +63,7 @@ int getWeath(placeWeather *w){
if(!w) return 0;
w->relhum = 0.7;
w->tc = 1.;
w->php = 78.; // temporary, to fix bug of firmware
w->php = 780.;
return 0;
}
int getDUT(almDut *a){
@@ -75,7 +75,7 @@ int getDUT(almDut *a){
/**
* @brief get_MJDt - calculate MJD of date from argument
* @param tval (i) - given date (or NULL for current)
* @param MJD (o) - time
* @param MJD (o) - time (or NULL just to check)
* @return 0 if all OK
*/
int get_MJDt(struct timeval *tval, sMJD *MJD){
@@ -113,7 +113,7 @@ int get_MJDt(struct timeval *tval, sMJD *MJD){
/**
* @brief get_ObsPlace - calculate observed place (without PM etc) for given date @550nm
* @param tval (i) - time
* @param p2000 (i) - polar coordinates for J2000 (only ra/dec used)
* @param p2000 (i) - polar coordinates for J2000 (only ra/dec used), ICRS (catalog)
* @param pnow (o) - polar coordinates for given epoch (or NULL)
* @param hnow (o) - horizontal coordinates for given epoch (or NULL)
* @return 0 if all OK
@@ -171,3 +171,96 @@ int get_ObsPlace(struct timeval *tval, polarCrds *p2000, polarCrds *pnow, horizC
#endif
return 0;
}
// azimuth: north=zero, east=90deg
// parallactic angle: iauHd2pa ( ha, dec, phi );
// refraction coefficients: iauRefco
// iauAe2hd ( az, el, phi, &ha, &dec ); A,H -> HA,DEC; phi - site latitude
// iauHd2ae ( ha, dec, phi, &az, &el ); HA,DEC -> A,H
// iauAtoc13 - obs->ICRS(catalog)
// iauAtoi13 - obs->CIRS
// iauAtio13 - CIRS->observed
#if 0
/**
* convert geocentric coordinates (nowadays, CIRS) to mean (JD2000, ICRS)
* appRA, appDecl in seconds
* r, d in seconds
*/
void JnowtoJ2000(double appRA, double appDecl, double *r, double *dc){
double ra=0., dec=0., utc1, utc2, tai1, tai2, tt1, tt2, fd, eo, ri;
int y, m, d, H, M;
DBG("appRa: %g'', appDecl'': %g", appRA, appDecl);
appRA *= DS2R;
appDecl *= DAS2R;
#define SOFA(f, ...) do{if(f(__VA_ARGS__)){WARNX("Error in " #f); goto rtn;}}while(0)
// 1. convert system JDate to UTC
SOFA(iauJd2cal, JDate, 0., &y, &m, &d, &fd);
fd *= 24.;
H = (int)fd;
fd = (fd - H)*60.;
M = (int)fd;
fd = (fd - M)*60.;
SOFA(iauDtf2d, "UTC", y, m, d, H, M, fd, &utc1, &utc2);
SOFA(iauUtctai, utc1, utc2, &tai1, &tai2);
SOFA(iauTaitt, tai1, tai2, &tt1, &tt2);
iauAtic13(appRA, appDecl, tt1, tt2, &ri, &dec, &eo);
ra = iauAnp(ri + eo);
ra *= DR2S;
dec *= DR2AS;
DBG("SOFA: r=%g'', d=%g''", ra, dec);
#undef SOFA
rtn:
if(r) *r = ra;
if(dc) *dc = dec;
}
/**
* @brief J2000toJnow - convert ra/dec between epochs
* @param in - J2000 (degrees)
* @param out - Jnow (degrees)
* @return
*/
int J2000toJnow(const polar *in, polar *out){
if(!out) return 1;
double utc1, utc2;
time_t tsec;
struct tm *ts;
tsec = time(0); // number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC)
ts = gmtime(&tsec);
int result = 0;
result = iauDtf2d ( "UTC", ts->tm_year+1900, ts->tm_mon+1, ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec, &utc1, &utc2 );
if (result != 0) {
fprintf(stderr, "iauDtf2d call failed\n");
return 1;
}
// Make TT julian date for Atci13 call
double tai1, tai2;
double tt1, tt2;
result = iauUtctai(utc1, utc2, &tai1, &tai2);
if(result){
fprintf(stderr, "iauUtctai call failed\n");
return 1;
}
result = iauTaitt(tai1, tai2, &tt1, &tt2);
if(result){
fprintf(stderr, "iauTaitt call failed\n");
return 1;
}
double pr = 0.0; // RA proper motion (radians/year; Note 2)
double pd = 0.0; // Dec proper motion (radians/year)
double px = 0.0; // parallax (arcsec)
double rv = 0.0; // radial velocity (km/s, positive if receding)
double rc = DD2R * in->ra, dc = DD2R * in->dec; // convert into radians
double ri, di, eo;
iauAtci13(rc, dc, pr, pd, px, rv, tt1, tt2, &ri, &di, &eo);
out->ra = iauAnp(ri - eo) * DR2D;
out->dec = di * DR2D;
return 0;
}
#endif

View File

@@ -53,10 +53,8 @@ volatile int global_quit = 0;
// quit by signal
void signals(int sig){
signal(sig, SIG_IGN);
unlink(GP->crdsfile); // remove header file
if(childpid){ // parent process
restore_console();
restore_tty();
restore_tty(); // restore all parameters
unlink(GP->pidfile); // and remove pidfile
}
DBG("Get signal %d, quit.\n", sig);
@@ -190,7 +188,12 @@ int setCoords(double ra, double dec){
return pointfunction(ra, dec);
}
// return 1 if all OK
/**
* @brief proc_data - process data received from Stellarium
* @param data - raw data
* @param len - its length
* @return 1 if all OK
*/
int proc_data(uint8_t *data, ssize_t len){
FNAME();
if(len != sizeof(indata)){
@@ -315,7 +318,7 @@ static void *hdrthread(_U_ void *buf){
// write FITS-header at most once per second
while(!global_quit){
wrhdr();
usleep(1000); // give a chanse to write/read for others
usleep(1000); // give a chance to write/read for others
}
return NULL;
}

View File

@@ -141,7 +141,11 @@ static int makecorr(){
ret = 1;
}
DBG("curtime: %s", write_cmd(":GUDT#", ibuff));
placeWeather w;
sprintf(buf, ":SREF0#"); // turn off 2-coord guiding & refraction
write_cmd(buf, ibuff);
sprintf(buf, ":Sdat0#"); // turn off dual-axis tracking
write_cmd(buf, ibuff);
/*placeWeather w;
if(getWeath(&w)) putlog("Can't determine weather data");
else{ // set refraction model data
snprintf(buf, 64, ":SRPRS%.1f#", w.php);
@@ -152,7 +156,7 @@ static int makecorr(){
ans = write_cmd(buf, ibuff);
if(!ans || *ans != '1') putlog("Can't set temperature data of refraction model");
else putlog("Correct temperature to %g", w.tc);
}
}*/
return ret;
}
@@ -388,7 +392,7 @@ static void getplace(){
static const char *statuses[12] = {
[0] = "'Tracking'",
[1] = "'Stoped or homing'",
[1] = "'Stopped or homing'",
[2] = "'Slewing to park'",
[3] = "'Unparking'",
[4] = "'Slewing to home'",
@@ -446,6 +450,7 @@ void wrhdr(){
}
failcounter = 0;
tlast = time(NULL);
// check it here, not in the beginning of function - to check connection with mount first
if(!hdname){
DBG("hdname not given!");
return;
@@ -467,19 +472,19 @@ void wrhdr(){
mountstatus = atoi(ans);
//DBG("Status: %d", mountstatus);
}
#define WRHDR(k, v, c) do{if(printhdr(hdrfd, k, v, c)){close(hdrfd); return;}}while(0)
int l = strlen(hdname) + 7;
char *aname = MALLOC(char, l);
snprintf(aname, l, "%sXXXXXX", hdname);
int fd = mkstemp(aname);
if(fd < 0){
WARN("Can't write header file: mkstemp()");
FREE(aname);
FREE(jd); FREE(lst); FREE(date); FREE(pS);
return;
}
fchmod(fd, 0644);
char val[22];
if(unlink(hdname) && errno != ENOENT){ // can't unlink existng file
WARN("unlink(%s)", hdname);
FREE(jd); FREE(lst); FREE(date); FREE(pS);
return;
}
int hdrfd = open(hdname, O_WRONLY | O_TRUNC | O_CREAT, 0644);
if(hdrfd < 0){
WARN("Can't open %s", hdname);
FREE(jd); FREE(lst); FREE(date); FREE(pS);
return;
}
#define WRHDR(k, v, c) do{if(printhdr(fd, k, v, c)){goto returning;}}while(0)
WRHDR("TIMESYS", "'UTC'", "Time system");
WRHDR("ORIGIN", "'SAO RAS'", "Organization responsible for the data");
WRHDR("TELESCOP", "'Astrosib-500'", "Telescope name");
@@ -508,10 +513,13 @@ void wrhdr(){
if(latitude) WRHDR("LATITUDE", latitude, "Geo latitude of site (south negative)");
if(lst) WRHDR("LSTEND", lst, "Local sidereal time of observations end");
if(date) WRHDR("DATE-END", date, "Date (UTC) of observations end");
FREE(jd); FREE(lst); FREE(date); FREE(pS);
// WRHDR("", , "");
// WRHDR("", , "");
#undef WRHDR
close(hdrfd);
returning:
FREE(jd); FREE(lst); FREE(date); FREE(pS);
close(fd);
rename(aname, hdname);
FREE(aname);
}
// terminal thread: allows to work with terminal through socket

View File

@@ -393,9 +393,9 @@ int str2double(double *num, const char *str){
return TRUE;
}
FILE *Flog = NULL; // log file descriptor
char *logname = NULL;
time_t log_open_time = 0;
static FILE *Flog = NULL; // log file descriptor
static char *logname = NULL;
// static time_t log_open_time = 0;
/**
* Try to open log file
* if failed show warning message
@@ -411,8 +411,7 @@ void openlogfile(char *name){
WARN(_("Can't open log file"));
return;
}
DBG("here");
log_open_time = time(NULL);
// log_open_time = time(NULL);
logname = name;
}
@@ -422,7 +421,7 @@ void openlogfile(char *name){
int putlog(const char *fmt, ...){
if(!Flog) return 0;
time_t t_now = time(NULL);
if(t_now - log_open_time > 86400){ // rotate log
/*if(t_now - log_open_time > 86400){ // rotate log
fprintf(Flog, "\n\t\t%sRotate log\n", ctime(&t_now));
fclose(Flog);
char newname[PATH_MAX];
@@ -430,7 +429,7 @@ int putlog(const char *fmt, ...){
if(rename(logname, newname)) WARN("rename()");
openlogfile(logname);
if(!Flog) return 0;
}
}*/
int i = fprintf(Flog, "\n\t\t%s", ctime(&t_now));
va_list ar;
va_start(ar, fmt);

View File

@@ -22,17 +22,20 @@
#include <signal.h>
#include <sys/wait.h> // wait
#include <sys/prctl.h> //prctl
#include <time.h>
#include "cmdlnopts.h"
#include "socket.h"
// dome @ /dev/ttyS2
glob_pars *GP;
static pid_t childpid = 0;
void signals(int signo){
restore_console();
restore_tty();
putlog("exit with status %d", signo);
if(childpid){ // parent process
restore_tty();
putlog("exit with status %d", signo);
}
exit(signo);
}
@@ -56,13 +59,16 @@ int main(int argc, char **argv){
if(daemon(1, 0)){
ERR("daemon()");
}
time_t lastd = 0;
while(1){ // guard for dead processes
pid_t childpid = fork();
childpid = fork();
if(childpid){
putlog("create child with PID %d\n", childpid);
DBG("Created child with PID %d\n", childpid);
wait(NULL);
putlog("child %d died\n", childpid);
time_t t = time(NULL);
if(t - lastd > 600) // at least 10 minutes of work
putlog("child %d died\n", childpid);
lastd = t;
WARNX("Child %d died\n", childpid);
sleep(1);
}else{
@@ -74,9 +80,9 @@ int main(int argc, char **argv){
if(GP->device) try_connect(GP->device);
if(ping()){
putlog("Can't write command PING");
ERRX(_("Can't write command PING"));
}
putlog("Child %d connected to %s", getpid(), GP->device);
daemonize(GP->port);
signals(0); // newer reached
return 0;

View File

@@ -390,9 +390,9 @@ int str2double(double *num, const char *str){
return TRUE;
}
FILE *Flog = NULL; // log file descriptor
char *logname = NULL;
time_t log_open_time = 0;
static FILE *Flog = NULL; // log file descriptor
static char *logname = NULL;
/**
* Try to open log file
* if failed show warning message
@@ -407,7 +407,6 @@ void openlogfile(char *name){
WARN(_("Can't open log file"));
return;
}
log_open_time = time(NULL);
logname = name;
}
@@ -417,15 +416,6 @@ void openlogfile(char *name){
int putlog(const char *fmt, ...){
if(!Flog) return 0;
time_t t_now = time(NULL);
if(t_now - log_open_time > 86400){ // rotate log
fprintf(Flog, "\n\t\t%sRotate log\n", ctime(&t_now));
fclose(Flog);
char newname[PATH_MAX];
snprintf(newname, PATH_MAX, "%s.old", logname);
if(rename(logname, newname)) WARN("rename()");
openlogfile(logname);
if(!Flog) return 0;
}
int i = fprintf(Flog, "\n\t\t%s", ctime(&t_now));
va_list ar;
va_start(ar, fmt);

View File

@@ -20,8 +20,9 @@
*/
#include "usefull_macros.h"
#include <signal.h>
#include <sys/wait.h> // wait
#include <sys/wait.h> // wait
#include <sys/prctl.h> //prctl
#include <time.h> // time
#include "cmdlnopts.h"
#include "socket.h"
@@ -29,10 +30,13 @@
glob_pars *GP;
pid_t childpid = 0;
void signals(int signo){
restore_console();
restore_tty();
putlog("exit with status %d", signo);
if(childpid){ // parent process
restore_tty();
putlog("exit with status %d", signo);
}
exit(signo);
}
@@ -56,13 +60,16 @@ int main(int argc, char **argv){
if(daemon(1, 0)){
ERR("daemon()");
}
time_t lastd = 0;
while(1){ // guard for dead processes
pid_t childpid = fork();
childpid = fork();
if(childpid){
putlog("create child with PID %d\n", childpid);
DBG("Created child with PID %d\n", childpid);
wait(NULL);
putlog("child %d died\n", childpid);
time_t t = time(NULL);
if(t - lastd > 600) // at least 10 minutes of work
putlog("child %d died\n", childpid);
lastd = t;
WARNX("Child %d died\n", childpid);
sleep(1);
}else{
@@ -74,9 +81,9 @@ int main(int argc, char **argv){
if(GP->device) try_connect(GP->device);
if(ping()){
putlog("Can't write command PING");
ERRX(_("Can't write command PING"));
}
putlog("Child %d connected to %s", getpid(), GP->device);
daemonize(GP->port);
signals(0); // newer reached
return 0;

View File

@@ -75,7 +75,6 @@ void try_connect(char *device){
fflush(stdout);
tty_init(device);
read_tty(tmpbuf, 4096); // clear rbuf
putlog("Connected to %s", device);
DBG("connected");
}

View File

@@ -390,9 +390,8 @@ int str2double(double *num, const char *str){
return TRUE;
}
FILE *Flog = NULL; // log file descriptor
char *logname = NULL;
time_t log_open_time = 0;
static FILE *Flog = NULL; // log file descriptor
static char *logname = NULL;
/**
* Try to open log file
* if failed show warning message
@@ -407,7 +406,6 @@ void openlogfile(char *name){
WARN(_("Can't open log file"));
return;
}
log_open_time = time(NULL);
logname = name;
}
@@ -417,15 +415,6 @@ void openlogfile(char *name){
int putlog(const char *fmt, ...){
if(!Flog) return 0;
time_t t_now = time(NULL);
if(t_now - log_open_time > 86400){ // rotate log
fprintf(Flog, "\n\t\t%sRotate log\n", ctime(&t_now));
fclose(Flog);
char newname[PATH_MAX];
snprintf(newname, PATH_MAX, "%s.old", logname);
if(rename(logname, newname)) WARN("rename()");
openlogfile(logname);
if(!Flog) return 0;
}
int i = fprintf(Flog, "\n\t\t%s", ctime(&t_now));
va_list ar;
va_start(ar, fmt);