TPCCLIB
Loading...
Searching...
No Matches
parselect.c
Go to the documentation of this file.
1
4/*****************************************************************************/
5#include "tpcclibConfig.h"
6/*****************************************************************************/
7#include "tpcift.h"
8/*****************************************************************************/
9#include <stdio.h>
10#include <stdlib.h>
11#include <math.h>
12#include <time.h>
13#include <string.h>
14/*****************************************************************************/
15#include "tpcpar.h"
16/*****************************************************************************/
17
18/*****************************************************************************/
26 PAR *d,
31 const char *region_name,
33 int reset,
35 TPCSTATUS *status
36) {
37 int verbose=0; if(status!=NULL) verbose=status->verbose;
38 if(verbose>0) printf("%s()\n", __func__);
39 /* Check that required data exists */
40 if(d==NULL || d->tacNr<1) {
41 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
42 return(0);
43 }
44
45 int ri, len, match_nr=0;
46
47 /* Check if NULL was given as tac name; if, then select all */
48 if(region_name==NULL) {
49 for(ri=0; ri<d->tacNr; ri++) d->r[ri].sw=1;
50 return(d->tacNr);
51 }
52
53 /* Reset all selections if required */
54 if(reset!=0) for(ri=0; ri<d->tacNr; ri++) d->r[ri].sw=0;
55
56 /* Remove any quotation marks from the name */
57 len=strlen(region_name);
58 char local_name[len+1];
59 if(len>1) len=strncpyClean(local_name, region_name, len+1);
60 else strcpy(local_name, region_name);
61
62 /* If tac name is empty, then select all tacs with missing names */
63 if(len<1 || strcmp(local_name, ".")==0) {
64 for(ri=0; ri<d->tacNr; ri++) {
65 if(strlen(d->r[ri].name)==0) {d->r[ri].sw=1; match_nr++; continue;}
66 /* maybe here should also be checked for names consisting of only space characters */
67 }
68 return(match_nr);
69 }
70
71 /* Check each TAC name */
72 for(ri=0; ri<d->tacNr; ri++) {
73 /* does the name match? */
74 if(roinameMatch(d->r[ri].name, local_name, status)!=0) {d->r[ri].sw=1; match_nr++; continue;}
75 }
76 /* If at least one match was found, then we are ready */
77 if(match_nr>0) return(match_nr);
78
79 /* Since there was no match with names, we'll try with numbers */
80 INTLIST li; intlistInit(&li);
81 if(intlistExpandFromString(local_name, ", ", &li, 1)<1) {intlistFree(&li); return(0);}
82 intlistSort(&li);
83 for(int i=0; i<li.nr; i++) {
84 if(li.i[i]<1) continue;
85 if(li.i[i]>d->tacNr) break;
86 d->r[li.i[i]-1].sw=1; match_nr++;
87 }
88 intlistFree(&li);
89 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
90 return(match_nr);
91}
92/*****************************************************************************/
93
94/*****************************************************************************/
102 PAR *d
103) {
104 int ri, n;
105
106 /* Check the input */
107 if(d==NULL || d->tacNr<1) return(0);
108 /* Check each VOI */
109 for(ri=n=0; ri<d->tacNr; ri++) if(d->r[ri].sw!=0) n++;
110 return(n);
111}
112/*****************************************************************************/
113
114/*****************************************************************************/
122 PAR *d,
127 const char *par_name,
129 int reset,
131 TPCSTATUS *status
132) {
133 int verbose=0; if(status!=NULL) verbose=status->verbose;
134 if(verbose>0) printf("%s()\n", __func__);
135 /* Check that required data exists */
136 if(d==NULL || d->parNr<1) {
137 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
138 return(0);
139 }
140
141 int pi, len, match_nr=0;
142
143 /* Check if NULL was given as parameter name; if, then select all */
144 if(par_name==NULL) {
145 for(pi=0; pi<d->parNr; pi++) d->n[pi].sw=1;
146 return(d->parNr);
147 }
148
149 /* Reset all selections if required */
150 if(reset!=0) for(pi=0; pi<d->parNr; pi++) d->n[pi].sw=0;
151
152 /* Remove any quotation marks from the name */
153 len=strlen(par_name);
154 char local_name[len+1];
155 if(len>1) len=strncpyClean(local_name, par_name, len+1);
156 else strcpy(local_name, par_name);
157
158 /* If parameter name is empty, then select all tacs with missing names */
159 if(len<1 || strcmp(local_name, ".")==0) {
160 for(pi=0; pi<d->parNr; pi++) {
161 if(strlen(d->n[pi].name)==0) {d->n[pi].sw=1; match_nr++; continue;}
162 /* maybe here should also be checked for names consisting of only space characters */
163 }
164 return(match_nr);
165 }
166
167 /* Check each parameter name */
168 for(pi=0; pi<d->parNr; pi++) {
169 /* does the name match? */
170 if(strcasecmp(d->n[pi].name, local_name)==0) {
171 d->n[pi].sw=1; match_nr++; continue;
172 }
173 }
174 /* If at least one match was found, then we are ready */
175 if(match_nr>0) return(match_nr);
176
177 /* Since there was no match with names, we'll try with numbers */
178 INTLIST li; intlistInit(&li);
179 if(intlistExpandFromString(local_name, ", ", &li, 1)<1) {intlistFree(&li); return(0);}
180 intlistSort(&li);
181 for(int i=0; i<li.nr; i++) {
182 if(li.i[i]<1) continue;
183 if(li.i[i]>d->parNr) break;
184 d->n[li.i[i]-1].sw=1; match_nr++;
185 }
186 intlistFree(&li);
187
188 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
189 return(match_nr);
190}
191/*****************************************************************************/
192
193/*****************************************************************************/
201 PAR *d
202) {
203 int pi, n;
204
205 /* Check the input */
206 if(d==NULL || d->parNr<1) return(0);
207 /* Check each VOI */
208 for(pi=n=0; pi<d->parNr; pi++) if(d->n[pi].sw!=0) n++;
209 return(n);
210}
211/*****************************************************************************/
212
213/*****************************************************************************/
221 PAR *d,
224 const char *par_name
225) {
226 /* Check that required data exists */
227 if(d==NULL || d->parNr<1 || par_name==NULL) return(-1);
228 /* Find the name */
229 for(int i=0; i<d->parNr; i++) {
230 if(strcasecmp(par_name, d->n[i].name)==0) return(i);
231 }
232 return(-1);
233}
234/*****************************************************************************/
235
236/*****************************************************************************/
244 PAR *d,
247 const char *par_name,
249 const int ti
250) {
251 double v=nan("");
252 /* Check that required data exists */
253 if(d==NULL || d->parNr<1 || ti<0 || ti>=d->tacNr || par_name==NULL) return(v);
254 /* Find the name */
255 for(int pi=0; pi<d->parNr; pi++)
256 if(strcasecmp(par_name, d->n[pi].name)==0) v=d->r[ti].p[pi];
257 return(v);
258}
259/*****************************************************************************/
260
261/*****************************************************************************/
269 PAR *d,
271 const char *par_name
272) {
273 int u=UNIT_UNKNOWN;
274 /* Check that required data exists */
275 if(d==NULL || d->parNr<1 || par_name==NULL) return(u);
276 /* Find the name */
277 for(int pi=0; pi<d->parNr; pi++) if(strcasecmp(par_name, d->n[pi].name)==0) u=d->n[pi].unit;
278 return(u);
279}
280/*****************************************************************************/
281
282/*****************************************************************************/
289 PAR *d,
291 const int i,
293 double *pmin,
295 double *pmax
296) {
297 if(pmin!=NULL) *pmin=nan("");
298 if(pmax!=NULL) *pmax=nan("");
299 if(d==NULL || i<0 || i>=d->parNr || d->tacNr<1) return;
300
301 double mi, ma; mi=ma=d->r[0].p[i];
302 for(int j=1; j<d->tacNr; j++) {
303 if(d->r[j].p[i]<mi) mi=d->r[j].p[i];
304 else if(d->r[j].p[i]>ma) ma=d->r[j].p[i];
305 }
306 if(pmin!=NULL) *pmin=mi;
307 if(pmax!=NULL) *pmax=ma;
308 return;
309}
310/*****************************************************************************/
311
312/*****************************************************************************/
void intlistInit(INTLIST *l)
Definition intutil.c:80
int intlistExpandFromString(const char *s1, const char *s2, INTLIST *l, const int ifnew)
Definition intutil.c:190
void intlistFree(INTLIST *l)
Definition intutil.c:92
void intlistSort(INTLIST *l)
Definition intutil.c:137
int parSelectTACs(PAR *d, const char *region_name, int reset, TPCSTATUS *status)
Definition parselect.c:24
int parSelectParameters(PAR *d, const char *par_name, int reset, TPCSTATUS *status)
Definition parselect.c:120
double parGetParameter(PAR *d, const char *par_name, const int ti)
Definition parselect.c:242
int parGetParameterUnit(PAR *d, const char *par_name)
Definition parselect.c:267
void parValueRange(PAR *d, const int i, double *pmin, double *pmax)
Definition parselect.c:287
int parFindParameter(PAR *d, const char *par_name)
Definition parselect.c:219
int parSelectedTACs(PAR *d)
Definition parselect.c:100
int parSelectedParameters(PAR *d)
Definition parselect.c:199
int roinameMatch(const char *roiname, const char *test_str, TPCSTATUS *status)
Definition roiname.c:183
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
int strncpyClean(char *s1, const char *s2, int maxlen)
Definition stringext.c:321
Definition tpcpar.h:101
int parNr
Definition tpcpar.h:109
int tacNr
Definition tpcpar.h:105
PARR * r
Definition tpcpar.h:115
PARN * n
Definition tpcpar.h:113
int unit
Definition tpcpar.h:87
char sw
Definition tpcpar.h:95
char name[MAX_PARNAME_LEN+1]
Definition tpcpar.h:83
char name[MAX_TACNAME_LEN+1]
Definition tpcpar.h:50
char sw
Definition tpcpar.h:75
double * p
Definition tpcpar.h:65
int verbose
Verbose level, used by statusPrint() etc.
@ UNIT_UNKNOWN
Unknown unit.
@ TPCERROR_OK
No error.
@ TPCERROR_NO_DATA
File contains no data.
Header file for library libtpcift.
Header file for libtpcpar.