TPCCLIB
Loading...
Searching...
No Matches
sifisot.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 <string.h>
13#include <math.h>
14#include <unistd.h>
15/*****************************************************************************/
16#include "tpcextensions.h"
17#include "tpccsv.h"
18#include "tpcift.h"
19#include "tpctac.h"
20/*****************************************************************************/
21
22/*****************************************************************************/
23static char *info[] = {
24 "Add isotope to the scan information files (SIF), if they do not contain",
25 "it already. If isotope code is not given by user, then program shows",
26 "the current isotope in SIF.",
27 " ",
28 "Usage: @P [options] filename(s) [isotope code]",
29 " ",
30 "Options:",
31 " --force",
32 " Isotope is set even if it already exists in SIF.",
33 " -stdoptions", // List standard options like --help, -v, etc
34 " ",
35 "Example:",
36 " @P *.sif C-11",
37 " ",
38 "See also: sifcat, taclist, eframe, tacframe, tacweigh",
39 " ",
40 "Keywords: SIF, isotope, physical decay, image, Analyze",
41 0};
42/*****************************************************************************/
43
44/*****************************************************************************/
45/* Turn on the globbing of the command line, since it is disabled by default in
46 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
47 In Unix&Linux wildcard command line processing is enabled by default. */
48/*
49#undef _CRT_glob
50#define _CRT_glob -1
51*/
52int _dowildcard = -1;
53/*****************************************************************************/
54
55/*****************************************************************************/
56/*
57 * Main
58 */
59int main(int argc, char *argv[])
60{
61 int ai, help=0, version=0, verbose=1;
62 char *cptr, siffile[FILENAME_MAX], isotope_name[MAX_ISOTOPE_LEN];
63 int ret, si, ffi=0, fileNr=0, isotope_code=0, forceMode=0;
64
65
66 /*
67 * Get arguments
68 */
69 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
70 siffile[0]=isotope_name[0]=(char)0;
71 /* Options */
72 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
73 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
74 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
75 if(strcasecmp(cptr, "F")==0 || strcasecmp(cptr, "FORCE")==0) {
76 forceMode=1; continue;
77 }
78 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
79 return(1);
80 } else break; // tac name argument may start with '-'
81
82 /* Print help or version? */
83 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
84 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
85 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
86
87 TPCSTATUS status; statusInit(&status);
88 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
89 status.verbose=verbose-1;
90
91 /* Process other arguments, starting from the first non-option */
92 fileNr=ffi=0;
93 for(; ai<argc; ai++) {
94 if(ffi<1) ffi=ai;
95 fileNr++;
96 }
97 /* Check that we got at least something */
98 if(fileNr<1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
99 /* Last argument may or may not be the isotope name */
100 if(fileNr>1 && access(argv[ai-1],0)==-1) {
101 isotope_code=isotopeIdentify(argv[ai-1]);
102 if(isotope_code!=ISOTOPE_UNKNOWN) {
103 strcpy(isotope_name, isotopeName(isotope_code));
104 fileNr--;
105 }
106 }
107
108 /* In verbose mode print arguments and options */
109 if(verbose>1) {
110 printf("fileNr := %d\n", fileNr);
111 if(isotope_code!=ISOTOPE_UNKNOWN)
112 printf("isotope_name := %s\n", isotope_name);
113 printf("forceMode := %d\n", forceMode);
114 fflush(stdout);
115 }
116
117
118 /*
119 * Process each SIF file
120 */
121 char old_isotope_name[MAX_ISOTOPE_LEN+1];
122 int old_isotope_code=0;
123 TAC tac; tacInit(&tac);
124 for(ai=ffi, si=0; ai<argc && si<fileNr; ai++) {
125 strlcpy(siffile, argv[ai], FILENAME_MAX); si++;
126
127 /* Print filename */
128 if(verbose>0) fprintf(stdout, "SIF_filename := %s\n", siffile);
129
130 /* Read SIF file as TAC */
131 ret=tacRead(&tac, siffile, &status);
132 if(ret!=TPCERROR_OK) {
133 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
134 if(verbose>1) printf("ret := %d\n", ret);
135 tacFree(&tac); return(2);
136 }
137 if(tac.format!=TAC_FORMAT_SIF) {
138 fprintf(stderr, "Error: file is not in SIF format.\n");
139 tacFree(&tac); return(3);
140 }
141
142 /* Read current isotope */
143 old_isotope_code=ISOTOPE_UNKNOWN;
144 ret=tacGetHeaderIsotope(&tac.h, old_isotope_name, &status);
145 if(ret==TPCERROR_OK) old_isotope_code=isotopeIdentify(old_isotope_name);
146 strcpy(old_isotope_name, isotopeName(old_isotope_code));
147
148 if(verbose>2) {
149 printf("fileformat := %s\n", tacFormattxt(tac.format));
150 printf("tacNr := %d\n", tac.tacNr);
151 printf("sampleNr := %d\n", tac.sampleNr);
152 printf("old_isotope := %s\n", old_isotope_name);
153 }
154
155 /* If old and new isotope are the same, then do nothing */
156 if(isotope_code==old_isotope_code) {
157 if(verbose>0) fprintf(stderr, "Note: isotope was already correct.\n");
158 tacFree(&tac);
159 continue; /* next SIF */
160 }
161
162 /* If new isotope name was not specified, then just show the isotope */
163 if(isotope_code==ISOTOPE_UNKNOWN) {
164 fprintf(stdout, "isotope := %s\n", old_isotope_name);
165 tacFree(&tac);
166 continue; /* next SIF */
167 }
168
169 /* If isotope was defined in SIF, then do not change it, unless --force */
170 if(old_isotope_code!=ISOTOPE_UNKNOWN && forceMode==0) {
171 fprintf(stderr, "Warning: isotope was set already; not changed.\n");
172 tacFree(&tac);
173 continue; /* next SIF */
174 }
175
176 /* Change the isotope name */
177 ret=tacSetHeaderIsotope(&tac.h, isotope_name);
178 if(ret==TPCERROR_OK) {
179 if(verbose>0)
180 printf("isotope changed from %s to %s\n",
181 old_isotope_name, isotope_name);
182 } else {
183 fprintf(stderr, "Error: cannot set isotope.\n");
184 tacFree(&tac); return(6);
185 }
186
187 /*
188 * Save the modified SIF
189 */
190 if(verbose>1) printf("writing %s\n", siffile);
191 FILE *fp; fp=fopen(siffile, "w");
192 if(fp==NULL) {
193 fprintf(stderr, "Error: cannot open file for writing.\n");
194 tacFree(&tac); return(11);
195 }
196 ret=tacWrite(&tac, fp, TAC_FORMAT_SIF, 1, &status);
197 fclose(fp);
198 if(ret!=TPCERROR_OK) {
199 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
200 if(verbose>1) printf("ret := %d\n", ret);
201 tacFree(&tac); return(12);
202 }
203 if(verbose>0) printf("modified %s saved.\n", siffile);
204
205 tacFree(&tac);
206 } /* next SIF */
207
208 return(0);
209}
210/*****************************************************************************/
211
212/*****************************************************************************/
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 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
char * errorMsg(tpcerror e)
Definition statusmsg.c:68
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
Definition tpctac.h:87
tacformat format
Definition tpctac.h:93
int sampleNr
Definition tpctac.h:89
IFT h
Optional (but often useful) header information.
Definition tpctac.h:141
int tacNr
Definition tpctac.h:91
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
void tacFree(TAC *tac)
Definition tac.c:106
void tacInit(TAC *tac)
Definition tac.c:24
int tacSetHeaderIsotope(IFT *h, const char *s)
Definition tacift.c:341
int tacGetHeaderIsotope(IFT *h, char *s, TPCSTATUS *status)
Definition tacift.c:290
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
Definition tacio.c:413
char * tacFormattxt(tacformat c)
Definition tacio.c:98
int tacWrite(TAC *tac, FILE *fp, tacformat format, int extra, TPCSTATUS *status)
Definition tacio.c:332
Header file for library libtpccsv.
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for library libtpcift.
#define MAX_ISOTOPE_LEN
Max string length for PET isotope.
Definition tpcisotope.h:41
@ ISOTOPE_UNKNOWN
Unknown.
Definition tpcisotope.h:51
Header file for library libtpctac.
@ TAC_FORMAT_SIF
Scan information file.
Definition tpctac.h:43