From 1250d0642be96e9d02b84f409d52e684936fb5ef Mon Sep 17 00:00:00 2001 From: eddyem Date: Tue, 19 May 2020 20:23:14 +0300 Subject: [PATCH] Add some usefull functions from lib SOFA --- .gitignore | 10 +- Auxiliary_utils/sofa_functions/Makefile | 56 ++++ Auxiliary_utils/sofa_functions/Readme | 1 + Auxiliary_utils/sofa_functions/sofa.c | 291 ++++++++++++++++++ Auxiliary_utils/sofa_functions/sofa.cflags | 1 + Auxiliary_utils/sofa_functions/sofa.config | 2 + Auxiliary_utils/sofa_functions/sofa.creator | 1 + .../sofa_functions/sofa.creator.user | 165 ++++++++++ Auxiliary_utils/sofa_functions/sofa.cxxflags | 1 + Auxiliary_utils/sofa_functions/sofa.files | 1 + Auxiliary_utils/sofa_functions/sofa.includes | 0 Daemons/10micron_stellarium/libsofa.c | 101 +++++- Daemons/10micron_stellarium/main.c | 13 +- Daemons/10micron_stellarium/telescope.c | 44 +-- Daemons/10micron_stellarium/usefull_macros.c | 13 +- Daemons/domedaemon/main.c | 20 +- Daemons/domedaemon/usefull_macros.c | 16 +- Daemons/teldaemon/main.c | 23 +- Daemons/teldaemon/term.c | 1 - Daemons/teldaemon/usefull_macros.c | 15 +- 20 files changed, 698 insertions(+), 77 deletions(-) create mode 100644 Auxiliary_utils/sofa_functions/Makefile create mode 100644 Auxiliary_utils/sofa_functions/Readme create mode 100644 Auxiliary_utils/sofa_functions/sofa.c create mode 100644 Auxiliary_utils/sofa_functions/sofa.cflags create mode 100644 Auxiliary_utils/sofa_functions/sofa.config create mode 100644 Auxiliary_utils/sofa_functions/sofa.creator create mode 100644 Auxiliary_utils/sofa_functions/sofa.creator.user create mode 100644 Auxiliary_utils/sofa_functions/sofa.cxxflags create mode 100644 Auxiliary_utils/sofa_functions/sofa.files create mode 100644 Auxiliary_utils/sofa_functions/sofa.includes diff --git a/.gitignore b/.gitignore index b76b42b..b897296 100644 --- a/.gitignore +++ b/.gitignore @@ -19,7 +19,15 @@ *.la *.lo -# Shared objects (inc. Windows DLLs) +# Shared objects *.so *.so.* +# qt-creator +*.config +*.cflags +*.cxxflags +*.creator* +*.files +*.includes + diff --git a/Auxiliary_utils/sofa_functions/Makefile b/Auxiliary_utils/sofa_functions/Makefile new file mode 100644 index 0000000..abdb003 --- /dev/null +++ b/Auxiliary_utils/sofa_functions/Makefile @@ -0,0 +1,56 @@ +# run `make DEF=...` to add extra defines +PROGRAM := sofa +LDFLAGS := -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-all +LDFLAGS += -lsofa_c +SRCS := $(wildcard *.c) +DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111 +OBJDIR := mk +CFLAGS += -O2 -Wall -Wextra -Wno-trampolines -std=gnu99 +OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o)) +DEPS := $(OBJS:.o=.d) +CC = gcc +#CXX = g++ +TARGET := RELEASE + +all: $(OBJDIR)/RELEASE +all: $(PROGRAM) +release: all + +debug: CFLAGS += -DEBUG -Werror +debug: TARGET := DEBUG +debug: $(OBJDIR)/DEBUG +debug: $(PROGRAM) + +$(OBJDIR)/DEBUG: + @make clean +$(OBJDIR)/RELEASE: + @make clean + +$(PROGRAM) : $(OBJDIR) $(OBJS) + @echo -e "\t\tTARGET: $(TARGET)" + @> $(OBJDIR)/$(TARGET) + @echo -e "\t\tLD $(PROGRAM)" + $(CC) $(LDFLAGS) $(OBJS) -o $(PROGRAM) + +$(OBJDIR): + @mkdir $(OBJDIR) + +ifneq ($(MAKECMDGOALS),clean) +-include $(DEPS) +endif + +$(OBJDIR)/%.o: %.c + @echo -e "\t\tCC $<" + $(CC) -MD -c $(LDFLAGS) $(CFLAGS) $(DEFINES) -o $@ $< + +clean: + @echo -e "\t\tCLEAN" + @rm -rf $(OBJDIR) 2>/dev/null || true + +xclean: clean + @rm -f $(PROGRAM) + +gentags: + CFLAGS="$(CFLAGS) $(DEFINES)" geany -g $(PROGRAM).c.tags *[hc] 2>/dev/null + +.PHONY: gentags clean xclean diff --git a/Auxiliary_utils/sofa_functions/Readme b/Auxiliary_utils/sofa_functions/Readme new file mode 100644 index 0000000..3a37626 --- /dev/null +++ b/Auxiliary_utils/sofa_functions/Readme @@ -0,0 +1 @@ +Some usefull functions of SOFA library diff --git a/Auxiliary_utils/sofa_functions/sofa.c b/Auxiliary_utils/sofa_functions/sofa.c new file mode 100644 index 0000000..e677995 --- /dev/null +++ b/Auxiliary_utils/sofa_functions/sofa.c @@ -0,0 +1,291 @@ +#include +#include +#include +#include +#include +#include +#include + +typedef struct{ + double utc1; double utc2; // UTC JD, commonly used MJD = utc1+utc2-2400000.5 + double MJD; + double tai1; double tai2; // TAI JD + double tt1; double tt2; // TT JD +} sMJD; + +// polar coordinates & equation of origins (all in radians) +typedef struct{ + double ha; // hour angle + double dec; // declination + double ra; // right ascension + double eo; // equation of origins +} polarCrds; + +// horizontal coordinates (all in radians) +typedef struct{ + double az; // azimuth, 0 @ south, positive clockwise + double zd; // zenith distance +} horizCrds; + +// observational place coordinates and altitude; all coordinates are in radians! +typedef struct{ + double slong; // longitude + double slat; // lattitude + double salt; // altitude, m +} placeData; + +// place weather data +typedef struct{ + double relhum; // rel. humidity, 0..1 + double php; // atm. pressure (hectopascales) + double tc; // temperature, degrC +} placeWeather; + +// DUT/polar almanach data +typedef struct{ + double DUT1; // UT1-UTC, sec + double px; // polar coordinates, arcsec + double py; +} almDut; + +static placeData *pldata = NULL; +// temporal stubs for weather/place/DUT1 data; return 0 if all OK +placeData *getPlace(){ + if(pldata) return pldata; + pldata = malloc(sizeof(placeData)); + /* Site longitude, latitude (radians) and height above the geoid (m). */ + pldata->slong = 0.7232763200; + pldata->slat = 0.7618977414; + pldata->salt = 2070.0; // altitude + return pldata; +} +int getWeath(placeWeather *w){ + if(!w) return 0; + w->relhum = 0.7; + w->tc = 1.; + w->php = 780.; + return 0; +} +int getDUT(almDut *a){ + if(!a) return 0; + a->px = a->py = 0; + a->DUT1 = -0.25080; + return 0; +} + +char *radtodeg(double r){ + static char buf[128]; + int i[4]; char pm; + r = iauAnpm(r); + iauA2af (2, r, &pm, i); + snprintf(buf, 128, "%c%02d %02d %02d.%02d", pm, i[0],i[1],i[2],i[3]); + return buf; +} + +char *radtohrs(double r){ + static char buf[128]; + int i[4]; char pm; + r = iauAnp(r); + iauA2tf(2, r, &pm, i); + snprintf(buf, 128, "%02d:%02d:%02d.%02d", i[0],i[1],i[2],i[3]); + return buf; +} + +/** + * @brief get_MJDt - calculate MJD of date from argument + * @param tval (i) - given date (or NULL for current) + * @param MJD (o) - time (or NULL just to check) + * @return 0 if all OK + */ +int get_MJDt(struct timeval *tval, sMJD *MJD){ + struct tm tms; + double tSeconds; + if(!tval){ + //DBG("MJD for current time"); + struct timeval currentTime; + gettimeofday(¤tTime, NULL); + gmtime_r(¤tTime.tv_sec, &tms); + tSeconds = tms.tm_sec + ((double)currentTime.tv_usec)/1e6; + }else{ + gmtime_r(&tval->tv_sec, &tms); + tSeconds = tms.tm_sec + ((double)tval->tv_usec)/1e6; + } + int y, m, d; + y = 1900 + tms.tm_year; + m = tms.tm_mon + 1; + d = tms.tm_mday; + double utc1, utc2; + /* UTC date. */ + if(iauDtf2d("UTC", y, m, d, tms.tm_hour, tms.tm_min, tSeconds, &utc1, &utc2) < 0) return -1; + if(!MJD) return 0; + MJD->MJD = utc1 - 2400000.5 + utc2; + MJD->utc1 = utc1; + MJD->utc2 = utc2; + //DBG("UTC(m): %g, %.8f\n", utc1 - 2400000.5, utc2); + if(iauUtctai(utc1, utc2, &MJD->tai1, &MJD->tai2)) return -1; + //DBG("TAI"); + if(iauTaitt(MJD->tai1, MJD->tai2, &MJD->tt1, &MJD->tt2)) return -1; + //DBG("TT"); + return 0; +} + +/** + * @brief get_LST - calculate local siderial time + * @param mjd (i) - date/time for LST (utc1 & tt used) + * @param dUT1 - (UT1-UTC) + * @param slong - site longitude (radians) + * @param LST (o) - local sidereal time (radians) + * @return 0 if all OK + */ +double get_LST(sMJD *mjd, double dUT1, double slong, double *LST){ + double ut11, ut12; + if(iauUtcut1(mjd->utc1, mjd->utc2, dUT1, &ut11, &ut12)) return 1; + double ST = iauGst06a(ut11, ut12, mjd->tt1, mjd->tt2); + ST += slong; + if(ST > D2PI) ST -= D2PI; + if(LST) *LST = ST; + return 0; +} + +/** + * @brief hor2eq - convert horizontal coordinates to polar + * @param h (i) - horizontal coordinates + * @param pc (o) - polar coordinates + * @param sidTime - sidereal time + */ +void hor2eq(horizCrds *h, polarCrds *pc, double sidTime){ + if(!h || !pc) return; + placeData *p = getPlace(); + iauAe2hd(h->az, DPI/2. - h->zd, p->slat, &pc->ha, &pc->dec); // A,H -> HA,DEC; phi - site latitude + pc->ra = sidTime - pc->ha; + pc->eo = 0.; +} + +/** + * @brief eq2horH - convert polar coordinates to horizontal + * @param pc (i) - polar coordinates (only HA used) + * @param h (o) - horizontal coordinates + * @param sidTime - sidereal time + */ +void eq2horH(polarCrds *pc, horizCrds *h){ + if(!h || !pc) return; + placeData *p = getPlace(); + double alt; + iauHd2ae(pc->ha, pc->dec, p->slat, &h->az, &alt); + h->zd = DPI/2. - alt; +} + +/** + * @brief eq2hor - convert polar coordinates to horizontal + * @param pc (i) - polar coordinates (only RA used) + * @param h (o) - horizontal coordinates + * @param sidTime - sidereal time + */ +void eq2hor(polarCrds *pc, horizCrds *h, double sidTime){ + if(!h || !pc) return; + double ha = sidTime - pc->ra + pc->eo; + placeData *p = getPlace(); + double alt; + iauHd2ae(ha, pc->dec, p->slat, &h->az, &alt); + h->zd = DPI/2. - alt; +} + +/** + * @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), 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 + */ +int get_ObsPlace(struct timeval *tval, polarCrds *p2000, polarCrds *pnow, horizCrds *hnow){ + 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) + sMJD MJD; + if(get_MJDt(tval, &MJD)) return -1; + if(!p2000) return -1; + placeData *p = getPlace(); + placeWeather w; + almDut d; + if(!p) return -1; + if(getWeath(&w)) return -1; + if(getDUT(&d)) return -1; + /* Effective wavelength (microns) */ + double wl = 0.55; + /* ICRS to observed. */ + double aob, zob, hob, dob, rob, eo; + if(iauAtco13(p2000->ra, p2000->dec, + pr, pd, px, rv, + MJD.utc1, MJD.utc2, + d.DUT1, + p->slong, p->slat, p->salt, + d.px, d.py, + w.php, w.tc, w.relhum, + wl, + &aob, &zob, + &hob, &dob, &rob, &eo)) return -1; + if(pnow){ + pnow->eo = eo; + pnow->ha = hob; + pnow->ra = rob; + pnow->dec = dob; + } + if(hnow){ + hnow->az = aob; + hnow->zd = zob; + } + return 0; +} + +// Y M D MJD x(arcsec) y(arcsec) UT1-UTC(sec) +// 2020 5 15 58984 0.0986 0.4466 -0.25080 + +int main(){ + sMJD mjd; + struct timeval tv; + gettimeofday(&tv, NULL); + if(get_MJDt(&tv, &mjd)) return 1; + double ST; + almDut adut; + if(getDUT(&adut)) return 1; + placeData *place = getPlace(); + if(!place) return 1; + if(get_LST(&mjd, adut.DUT1, place->slong, &ST)) return 1; + printf("ST = %s\n", radtohrs(ST)); + horizCrds htest = {.az = DD2R*91., .zd = DPI/4.}; // Z=45degr, A=1degr from south to west + printf("hzd=%g\n", htest.zd); + polarCrds ptest; + hor2eq(&htest, &ptest, ST); + printf("A=%s, ", radtodeg(htest.az)); + printf("Z=%s; ", radtodeg(htest.zd)); + printf("HOR->EQ: HA=%s, ", radtohrs(ptest.ha)); + printf("RA=%s, ", radtohrs(ptest.ra)); + printf("DEC=%s\n", radtodeg(ptest.dec)); + horizCrds h2; + eq2hor(&ptest, &h2, ST); + printf("Back conversion EQ->HOR: A=%s, ", radtodeg(h2.az)); + printf("Z=%s\n", radtodeg(h2.zd)); + polarCrds pnow; + if(!get_ObsPlace(&tv, &ptest, &pnow, &h2)){ + printf("\nApparent place, RA=%s, ", radtohrs(pnow.ra-pnow.eo)); + printf("HA=%s, ", radtohrs(pnow.ha)); + printf("ST-RA=%s, ", radtohrs(ST-pnow.ra+pnow.eo)); + printf("DEC=%s; ", radtodeg(pnow.dec)); + printf("A=%s, ", radtodeg(h2.az)); + printf("Z=%s\n", radtodeg(h2.zd)); + polarCrds h2p; + hor2eq(&h2, &h2p, ST); + printf("\tHOR->EQ: RA=%s, ", radtohrs(h2p.ra-h2p.eo)); + printf("HA=%s, ", radtohrs(h2p.ha)); + printf("ST-RA=%s, ", radtohrs(ST-h2p.ra+h2p.eo)); + printf("DEC=%s\n", radtodeg(h2p.dec)); + eq2hor(&pnow, &h2, ST); + //eq2horH(&pnow, &h2); + printf("\tEQ->HOR: A=%s, ", radtodeg(h2.az)); + printf("Z=%s\n", radtodeg(h2.zd)); + } + return 0; +} + diff --git a/Auxiliary_utils/sofa_functions/sofa.cflags b/Auxiliary_utils/sofa_functions/sofa.cflags new file mode 100644 index 0000000..68d5165 --- /dev/null +++ b/Auxiliary_utils/sofa_functions/sofa.cflags @@ -0,0 +1 @@ +-std=c17 \ No newline at end of file diff --git a/Auxiliary_utils/sofa_functions/sofa.config b/Auxiliary_utils/sofa_functions/sofa.config new file mode 100644 index 0000000..e0284f4 --- /dev/null +++ b/Auxiliary_utils/sofa_functions/sofa.config @@ -0,0 +1,2 @@ +// Add predefined macros for your project here. For example: +// #define THE_ANSWER 42 diff --git a/Auxiliary_utils/sofa_functions/sofa.creator b/Auxiliary_utils/sofa_functions/sofa.creator new file mode 100644 index 0000000..e94cbbd --- /dev/null +++ b/Auxiliary_utils/sofa_functions/sofa.creator @@ -0,0 +1 @@ +[General] diff --git a/Auxiliary_utils/sofa_functions/sofa.creator.user b/Auxiliary_utils/sofa_functions/sofa.creator.user new file mode 100644 index 0000000..acf68d1 --- /dev/null +++ b/Auxiliary_utils/sofa_functions/sofa.creator.user @@ -0,0 +1,165 @@ + + + + + + EnvironmentId + {7bd84e39-ca37-46d3-be9d-99ebea85bc0d} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + KOI8-R + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + true + 0 + 8 + true + 1 + true + false + true + false + + + + ProjectExplorer.Project.PluginSettings + + + true + + + + ProjectExplorer.Project.Target.0 + + Desktop + Desktop + {65a14f9e-e008-4c1b-89df-4eaa4774b6e3} + 0 + 0 + 0 + + /Big/Data/00__Small_tel/C-sources/sofa + + + + all + + false + + + false + true + Сборка + + GenericProjectManager.GenericMakeStep + + 1 + Сборка + + ProjectExplorer.BuildSteps.Build + + + + + clean + + false + + + false + true + Сборка + + GenericProjectManager.GenericMakeStep + + 1 + Очистка + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + По умолчанию + По умолчанию + GenericProjectManager.GenericBuildConfiguration + + 1 + + + 0 + Развёртывание + + ProjectExplorer.BuildSteps.Deploy + + 1 + Конфигурация развёртывания + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + 2 + + + Особая программа + + ProjectExplorer.CustomExecutableRunConfiguration + + 3768 + false + true + false + false + true + + + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 22 + + + Version + 22 + + diff --git a/Auxiliary_utils/sofa_functions/sofa.cxxflags b/Auxiliary_utils/sofa_functions/sofa.cxxflags new file mode 100644 index 0000000..6435dfc --- /dev/null +++ b/Auxiliary_utils/sofa_functions/sofa.cxxflags @@ -0,0 +1 @@ +-std=c++17 \ No newline at end of file diff --git a/Auxiliary_utils/sofa_functions/sofa.files b/Auxiliary_utils/sofa_functions/sofa.files new file mode 100644 index 0000000..45a227c --- /dev/null +++ b/Auxiliary_utils/sofa_functions/sofa.files @@ -0,0 +1 @@ +sofa.c diff --git a/Auxiliary_utils/sofa_functions/sofa.includes b/Auxiliary_utils/sofa_functions/sofa.includes new file mode 100644 index 0000000..e69de29 diff --git a/Daemons/10micron_stellarium/libsofa.c b/Daemons/10micron_stellarium/libsofa.c index 911acb2..fb68fbe 100644 --- a/Daemons/10micron_stellarium/libsofa.c +++ b/Daemons/10micron_stellarium/libsofa.c @@ -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 diff --git a/Daemons/10micron_stellarium/main.c b/Daemons/10micron_stellarium/main.c index 7537065..1f1f139 100644 --- a/Daemons/10micron_stellarium/main.c +++ b/Daemons/10micron_stellarium/main.c @@ -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; } diff --git a/Daemons/10micron_stellarium/telescope.c b/Daemons/10micron_stellarium/telescope.c index 30f07c2..21ad438 100644 --- a/Daemons/10micron_stellarium/telescope.c +++ b/Daemons/10micron_stellarium/telescope.c @@ -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 diff --git a/Daemons/10micron_stellarium/usefull_macros.c b/Daemons/10micron_stellarium/usefull_macros.c index ac70ffa..9e76923 100644 --- a/Daemons/10micron_stellarium/usefull_macros.c +++ b/Daemons/10micron_stellarium/usefull_macros.c @@ -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); diff --git a/Daemons/domedaemon/main.c b/Daemons/domedaemon/main.c index fbcd8d6..9fc97f4 100644 --- a/Daemons/domedaemon/main.c +++ b/Daemons/domedaemon/main.c @@ -22,17 +22,20 @@ #include #include // wait #include //prctl +#include #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; diff --git a/Daemons/domedaemon/usefull_macros.c b/Daemons/domedaemon/usefull_macros.c index da08cfe..3ba5727 100644 --- a/Daemons/domedaemon/usefull_macros.c +++ b/Daemons/domedaemon/usefull_macros.c @@ -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); diff --git a/Daemons/teldaemon/main.c b/Daemons/teldaemon/main.c index 373f77c..811fa14 100644 --- a/Daemons/teldaemon/main.c +++ b/Daemons/teldaemon/main.c @@ -20,8 +20,9 @@ */ #include "usefull_macros.h" #include -#include // wait +#include // wait #include //prctl +#include // 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; diff --git a/Daemons/teldaemon/term.c b/Daemons/teldaemon/term.c index 3546beb..60b4905 100644 --- a/Daemons/teldaemon/term.c +++ b/Daemons/teldaemon/term.c @@ -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"); } diff --git a/Daemons/teldaemon/usefull_macros.c b/Daemons/teldaemon/usefull_macros.c index da08cfe..4bae7d9 100644 --- a/Daemons/teldaemon/usefull_macros.c +++ b/Daemons/teldaemon/usefull_macros.c @@ -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);