TPCCLIB
Loading...
Searching...
No Matches
dcftime.c
Go to the documentation of this file.
1
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <math.h>
13#include <time.h>
14#include <string.h>
15/*****************************************************************************/
16#include "tpcextensions.h"
17#include "tpcisotope.h"
18/*****************************************************************************/
19
20/*****************************************************************************/
21/* Local functions */
22
23/*****************************************************************************/
24
25/*****************************************************************************/
26static char *info[] = {
27 "Calculates the decay correction factor between specified times, optionally",
28 "with dates.",
29 " ",
30 "Usage: @P [options] Isotope time0 time1 [> outputfile]",
31 " ",
32 "Options:",
33 " -title=<Y|n>",
34 " Print title line (y, default), or do not print title (n).",
35 " -stdoptions", // List standard options like --help, -v, etc
36 " ",
37 "Examples:",
38 " @P Zr-89 2018-03-21 15:22:00 2018-03-27 15:46:00",
39 " @P C-11 11:08:13 12:16:52",
40 " ",
41 "See also: halflife, tacdecay, imgdecay, tactime, ecattime",
42 " ",
43 "Keywords: physical decay, decay correction, halflife, isotope",
44 0};
45/*****************************************************************************/
46
47/*****************************************************************************/
48/* Turn on the globbing of the command line, since it is disabled by default in
49 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
50 In Unix&Linux wildcard command line processing is enabled by default. */
51/*
52#undef _CRT_glob
53#define _CRT_glob -1
54*/
55int _dowildcard = -1;
56/*****************************************************************************/
57
58/*****************************************************************************/
62int main(int argc, char **argv)
63{
64 int ai, help=0, version=0, verbose=1;
66 int printTitle=1;
67 char datestr0[128], timestr0[128], datestr1[128], timestr1[128];
68
69
70 /*
71 * Get arguments
72 */
73 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
74 datestr0[0]=timestr0[0]=datestr1[0]=timestr1[0]=(char)0;
75 /* Options */
76 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
77 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
78 char *cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
79 if(strncasecmp(cptr, "TITLE=", 6)==0) {
80 printTitle=tpcYesNo(cptr+6);
81 if(printTitle>=0) continue;
82 }
83 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
84 return(1);
85 } else break;
86
87 TPCSTATUS status; statusInit(&status);
88 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
89 status.verbose=verbose-1;
90
91 /* Print help or version? */
92 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
93 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
94 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
95
96 /* Process other arguments, starting from the first non-option */
97 if(ai<argc) {
98 isotope=isotopeIdentify(argv[ai]);
100 fprintf(stderr, "Error: invalid isotope '%s'\n", argv[ai]); return(1);}
101 ai++;
102 }
103 if(ai<argc) {
104 if(!strDateValid(argv[ai])) strlcpy(datestr0, argv[ai++], 128);
105 else if(!strTimeValid(argv[ai])) strlcpy(timestr0, argv[ai++], 128);
106 else {fprintf(stderr, "Error: invalid date/time '%s'\n", argv[ai]); return(1);}
107 }
108 if(ai<argc) {
109 if(datestr0[0]) {
110 if(!strTimeValid(argv[ai])) strlcpy(timestr0, argv[ai++], 128);
111 else {fprintf(stderr, "Error: invalid time '%s'\n", argv[ai]); return(1);}
112 } else {
113 if(!strTimeValid(argv[ai])) strlcpy(timestr1, argv[ai++], 128);
114 else {fprintf(stderr, "Error: invalid time '%s'\n", argv[ai]); return(1);}
115 }
116 if(timestr1[0] && ai<argc) {fprintf(stderr, "Error: too many arguments: '%s'.\n", argv[ai]); return(1);}
117 }
118 if(ai<argc) {
119 if(!strDateValid(argv[ai])) strlcpy(datestr1, argv[ai++], 128);
120 else if(!strTimeValid(argv[ai])) strlcpy(timestr1, argv[ai++], 128);
121 else {fprintf(stderr, "Error: invalid date/time '%s'\n", argv[ai]); return(1);}
122 }
123 if(ai<argc) {
124 if(timestr1[0]) {fprintf(stderr, "Error: too many arguments: '%s'.\n", argv[ai]); return(1);}
125 if(!strTimeValid(argv[ai])) strlcpy(timestr1, argv[ai++], 128);
126 else {fprintf(stderr, "Error: invalid date/time '%s'\n", argv[ai]); return(1);}
127 }
128 if(ai<argc) {fprintf(stderr, "Error: too many arguments: '%s'.\n", argv[ai]); return(1);}
129
130 /* Check that we got what we need */
131 if(isotope==ISOTOPE_UNKNOWN) {fprintf(stderr, "Error: missing isotope code.\n"); return(1);}
132 if(!timestr1[0]) {fprintf(stderr, "Error: missing date/time.\n"); return(1);}
133
134 /* In verbose mode print arguments and options */
135 if(verbose>1) {
136 for(ai=0; ai<argc; ai++) printf("%s ", argv[ai]);
137 printf("\n");
138 printf("isotope := %s\n", isotopeName(isotope));
139 printf("time0 := %s %s\n", datestr0, timestr0);
140 printf("time1 := %s %s\n", datestr1, timestr1);
141 printf("printTitle := %d\n", printTitle);
142 }
143
144
145 /*
146 * Calculate the difference in given times
147 */
148 char datetimestr0[128], datetimestr1[128];
149 if(datestr0[0]) {
150 strcpy(datetimestr0, datestr0); datetimestr0[10]=(char)0;
151 if(datestr1[0]) {strcpy(datetimestr1, datestr1); datetimestr1[10]=(char)0;}
152 else strcpy(datetimestr1, datestr0);
153 } else {
154 strcpy(datetimestr0, "2018-05-10");
155 strcpy(datetimestr1, datetimestr0);
156 }
157 strcat(datetimestr0, " ");
158 strcat(datetimestr1, " ");
159 strlcat(datetimestr0, timestr0, 128);
160 strlcat(datetimestr1, timestr1, 128);
161 if(verbose>2) {
162 printf("datetime0 := %s\n", datetimestr0);
163 printf("datetime1 := %s\n", datetimestr1);
164 }
165 double tdiff=strDateTimeDifference(datetimestr1, datetimestr0)/60.0;
166 if(verbose>2) {
167 printf("time_difference := %g min\n", tdiff);
168 }
169
170
171 /*
172 * Calculate decay correction factor
173 */
174 double dcf=decayCorrectionFactorFromIsotope(isotope, tdiff, 0.0);
175 if(isnan(dcf)) {
176 fprintf(stderr, "Error: cannot calculate decay correction factor.\n");
177 return(1);
178 }
179
180
181 /*
182 * Print result
183 */
184 if(printTitle) printf("time[min]\tDCF\n");
185 printf("%g\t%g\n", tdiff, dcf);
186
187 return(0);
188}
189/*****************************************************************************/
190
191/*****************************************************************************/
int strDateValid(const char *str)
Definition datetime.c:155
double strDateTimeDifference(const char *dt1, const char *dt0)
Definition datetime.c:519
int strTimeValid(const char *str)
Definition datetime.c:284
double decayCorrectionFactorFromIsotope(int isotope, double starttime, double duration)
Definition decay.c:107
char * isotopeName(int isotope_code)
Definition isotope.c:101
int isotopeIdentify(const char *isotope)
Definition isotope.c:145
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:47
int tpcYesNo(const char *s)
Definition proginfo.c:459
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:169
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:339
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:114
void statusInit(TPCSTATUS *s)
Definition statusmsg.c:104
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
size_t strlcat(char *dst, const char *src, size_t dstsize)
Definition stringext.c:592
int verbose
Verbose level, used by statusPrint() etc.
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for library libtpcisotope.
isotope
Definition tpcisotope.h:50
@ ISOTOPE_UNKNOWN
Unknown.
Definition tpcisotope.h:51