TPCCLIB
Loading...
Searching...
No Matches
parselect.c File Reference

Selecting parameter(s) and/or TAC(s) in PAR struct. More...

#include "tpcclibConfig.h"
#include "tpcift.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "tpcpar.h"

Go to the source code of this file.

Functions

int parSelectTACs (PAR *d, const char *region_name, int reset, TPCSTATUS *status)
int parSelectedTACs (PAR *d)
int parSelectParameters (PAR *d, const char *par_name, int reset, TPCSTATUS *status)
int parSelectedParameters (PAR *d)
int parFindParameter (PAR *d, const char *par_name)
double parGetParameter (PAR *d, const char *par_name, const int ti)
int parGetParameterUnit (PAR *d, const char *par_name)
void parValueRange (PAR *d, const int i, double *pmin, double *pmax)

Detailed Description

Selecting parameter(s) and/or TAC(s) in PAR struct.

Definition in file parselect.c.

Function Documentation

◆ parFindParameter()

int parFindParameter ( PAR * d,
const char * par_name )

Searches PAR structure for the given parameter name (case insensitive).

Returns
the index [0..parNr-1] of the parameter, or <0 if not found.
Author
Vesa Oikonen
See also
parGetParameter, parSelectParameters
Parameters
dPointer to PAR structure.
par_nameName of the parameter. Comparison is case-insensitive, but otherwise strict match is required.

Definition at line 219 of file parselect.c.

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}
int parNr
Definition tpcpar.h:108
PARN * n
Definition tpcpar.h:112
char name[MAX_PARNAME_LEN+1]
Definition tpcpar.h:82

◆ parGetParameter()

double parGetParameter ( PAR * d,
const char * par_name,
const int ti )

Searches PAR structure for the given parameter name (case insensitive).

Returns
the value of the parameter for specified TAC, or NaN if not found.
Author
Vesa Oikonen
See also
parGetParameterUnit, parFindParameter, parSelectParameters
Parameters
dPointer to PAR structure.
par_nameName of the parameter. Comparison is case-insensitive, but otherwise strict match is required.
tiTAC index [0..tacNr-1].

Definition at line 242 of file parselect.c.

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}
int tacNr
Definition tpcpar.h:104
PARR * r
Definition tpcpar.h:114
double * p
Definition tpcpar.h:64

◆ parGetParameterUnit()

int parGetParameterUnit ( PAR * d,
const char * par_name )

Searches PAR structure for the given parameter name (case insensitive).

Returns
the enum of the unit of the parameter, or UNIT_UNKNOWN if not found.
Author
Vesa Oikonen
See also
parFindParameter, parGetParameter
Parameters
dPointer to PAR structure.
par_nameName of the parameter. Comparison is case-insensitive, but otherwise strict match is required.

Definition at line 267 of file parselect.c.

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}
int unit
Definition tpcpar.h:86
@ UNIT_UNKNOWN
Unknown unit.

◆ parSelectedParameters()

int parSelectedParameters ( PAR * d)

Get the number of selected parameters (sw!=0) in PAR structure.

Returns
the number of selected parameters.
Author
Vesa Oikonen
See also
parSelectParameters, parFindParameter, parSelectByAnother
Parameters
dPointer to PAR data

Definition at line 199 of file parselect.c.

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}
char sw
Definition tpcpar.h:94

Referenced by parSelectByAnother().

◆ parSelectedTACs()

int parSelectedTACs ( PAR * d)

Get the number of selected TACs (sw!=0) in PAR structure.

Returns
the number of selected TACs.
Author
Vesa Oikonen
See also
parSelectTACs, parSortByName, parSelectByAnother
Parameters
dPointer to PAR data.

Definition at line 100 of file parselect.c.

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}
char sw
Definition tpcpar.h:74

Referenced by parSelectByAnother().

◆ parSelectParameters()

int parSelectParameters ( PAR * d,
const char * par_name,
int reset,
TPCSTATUS * status )

Select the parameter(s) in PAR struct that have matching name or number.

Returns
the number of newly selected parameters, or <=0 in case of an error.
Author
Vesa Oikonen
See also
parSelectedParameters, parFindParameter, parSelectTACs, parSelectByAnother, parEnsureNames
Parameters
dPointer to PAR structure.
par_nameName of parameter, or parameter numbers (as string, for example '1-3' or '1,2,3'); string can be given with quotation marks. Enter NULL to select all parameters, or empty string to select parameters with empty name (which really should not exist).
reset1=Non-matching parameters are deselected, 0=Old selections are preserved.
statusPointer to status data; enter NULL if not needed.

Definition at line 120 of file parselect.c.

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}
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
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
int verbose
Verbose level, used by statusPrint() etc.
@ TPCERROR_OK
No error.
@ TPCERROR_NO_DATA
File contains no data.

◆ parSelectTACs()

int parSelectTACs ( PAR * d,
const char * region_name,
int reset,
TPCSTATUS * status )

Select the TAC(s) in PAR structure that have matching ID name or number.

Returns
the number of newly selected VOIs, or <=0 in case of an error.
Author
Vesa Oikonen
See also
parSelectedTACs, parSelectParameters, parSortByName, parSelectByAnother, parEnsureNames
Parameters
dPointer to PAR structure.
region_nameName of TAC, or TAC numbers (as string, for example '1-3' or '1,2,3'); string can be given with quotation marks. Enter NULL to select all TACs, or empty string to select TACs with empty name fields (which really should not exist).
reset1=Non-matching VOIs are deselected, 0=Old selections are preserved.
statusPointer to status data; enter NULL if not needed.

Definition at line 24 of file parselect.c.

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}
int roinameMatch(const char *roiname, const char *test_str, TPCSTATUS *status)
Definition roiname.c:183
char name[MAX_TACNAME_LEN+1]
Definition tpcpar.h:50

◆ parValueRange()

void parValueRange ( PAR * d,
const int i,
double * pmin,
double * pmax )

Finds the min and max parameter value in PAR structure.

Author
Vesa Oikonen
See also
parFindParameter, parGetParameter, parSelectParameters
Parameters
dPointer to PAR structure.
iParameter index [0..parNr-1].
pminPointer to min parameter value; NULL if not needed.
pmaxPointer to max parameter value; NULL if not needed.

Definition at line 287 of file parselect.c.

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}