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

Combinations of data in PAR structures. More...

#include "tpcclibConfig.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 parSelectByAnother (PAR *d1, PAR *d2, int *pn, int *pr, TPCSTATUS *status)
int parCombineTACs (PAR *d1, PAR *d2, TPCSTATUS *status)

Detailed Description

Combinations of data in PAR structures.

Definition in file parcomb.c.

Function Documentation

◆ parCombineTACs()

int parCombineTACs ( PAR * d1,
PAR * d2,
TPCSTATUS * status )

Copy parameters from source PAR structure to target structure for TACs that are available in both structures.

Returns
enum tpcerror (TPCERROR_OK when successful).
See also
parSelectByAnother, parSelectTACs, parEnsureNames
Parameters
d1Pointer to target PAR structure.
d2Pointer to source PAR structure; not modified.
statusPointer to status data; enter NULL if not needed.

Definition at line 99 of file parcomb.c.

106 {
107 int verbose=0; if(status!=NULL) verbose=status->verbose;
108 if(verbose>0) printf("%s()\n", __func__);
109
110 /* Check what there is to copy */
111 int tacNr;
112 int ret=parSelectByAnother(d1, d2, NULL, &tacNr, status);
113 if(ret!=TPCERROR_OK) return(ret);
114 if(tacNr<1) {
115 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
116 return TPCERROR_NO_DATA;
117 }
118 int parNr=d2->parNr;
119 if(verbose>1) printf("%d TACs and %d parameters to copy.\n", tacNr, parNr);
120
121 /* Allocate memory for the new parameters */
122 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
123 ret=parAllocateMore(d1, parNr, 0);
124 if(ret!=TPCERROR_OK) return(ret);
125
126 /* Copy new parameter names */
127 for(int i=0; i<d2->parNr; i++) {
128 strcpy(d1->n[i+d1->parNr].name, d2->n[i].name);
129 d1->n[i+d1->parNr].unit=d2->n[i].unit;
130 d1->n[i+d1->parNr].lim1=d2->n[i].lim1;
131 d1->n[i+d1->parNr].lim2=d2->n[i].lim2;
132 d1->n[i+d1->parNr].tol=d2->n[i].tol;
133 }
134
135 /* Copy contents for common TACs */
136 for(int i=0; i<d1->tacNr; i++) {
137 /* find matching TAC name */
138 if(strnlen(d1->r[i].name, 1)<1) continue;
139 int j;
140 for(j=0; j<d2->tacNr; j++) {
141 if(strnlen(d2->r[j].name, 1)<1) continue;
142 if(strcasecmp(d1->r[i].name, d2->r[j].name)==0) break;
143 /* Allow mixed '-' and '_' */
144 char *buf1, *buf2;
145 buf1=strdup(d1->r[i].name); strReplaceChar(buf1, '-', '_');
146 buf2=strdup(d2->r[j].name); strReplaceChar(buf2, '-', '_');
147 int ret=strcasecmp(buf1, buf2);
148 free(buf1); free(buf2);
149 if(ret==0) break;
150 }
151 if(j==d2->tacNr) continue; // matching name not found, leave values as NaN
152
153 /* Copy new parameter values */
154 for(int k=0; k<d2->parNr; k++) {
155 d1->r[i].p[k+d1->parNr]=d2->r[j].p[k];
156 d1->r[i].sd[k+d1->parNr]=d2->r[j].sd[k];
157 d1->r[i].cl1[k+d1->parNr]=d2->r[j].cl1[k];
158 d1->r[i].cl2[k+d1->parNr]=d2->r[j].cl2[k];
159 }
160 }
161
162 d1->parNr+=d2->parNr;
163
164 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
165 return TPCERROR_OK;
166}
int parAllocateMore(PAR *par, int parNr, int tacNr)
Definition par.c:183
int parSelectByAnother(PAR *d1, PAR *d2, int *pn, int *pr, TPCSTATUS *status)
Definition parcomb.c:22
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
void strReplaceChar(char *s, char c1, char c2)
Definition stringext.c:134
char * strdup(const char *s)
Definition stringext.c:185
size_t strnlen(const char *s, size_t n)
Definition stringext.c:566
int parNr
Definition tpcpar.h:108
int tacNr
Definition tpcpar.h:104
PARR * r
Definition tpcpar.h:114
PARN * n
Definition tpcpar.h:112
int unit
Definition tpcpar.h:86
double lim2
Definition tpcpar.h:90
double tol
Definition tpcpar.h:92
char name[MAX_PARNAME_LEN+1]
Definition tpcpar.h:82
double lim1
Definition tpcpar.h:88
char name[MAX_TACNAME_LEN+1]
Definition tpcpar.h:50
double * cl2
Definition tpcpar.h:70
double * p
Definition tpcpar.h:64
double * sd
Definition tpcpar.h:66
double * cl1
Definition tpcpar.h:68
int verbose
Verbose level, used by statusPrint() etc.
@ TPCERROR_OK
No error.
@ TPCERROR_NO_DATA
File contains no data.

◆ parSelectByAnother()

int parSelectByAnother ( PAR * d1,
PAR * d2,
int * pn,
int * pr,
TPCSTATUS * status )

Select parameters and regions in a PAR structure that are available in another PAR structure.

Returns
enum tpcerror (TPCERROR_OK when successful).
See also
parSelectTACs, parEnsureNames, parCompareParameterNames, parCompareTacNames, parSelectedParameters, parSelectedTACs
Parameters
d1Pointer to target PAR structure, in which the PARN and PARR switches are set.
d2Pointer to PAR structure to which the first is compared to; not modified.
pnPointer to store the number of available parameters; enter NULL, if not needed.
prPointer to store the number of available regions; enter NULL, if not needed.
statusPointer to status data; enter NULL if not needed.

Definition at line 22 of file parcomb.c.

33 {
34 int verbose=0; if(status!=NULL) verbose=status->verbose;
35 if(verbose>0) printf("%s()\n", __func__);
36 if(pn!=NULL) *pn=0;
37 if(pr!=NULL) *pr=0;
38 /* Check that required data exists */
39 if(d1==NULL || d1->parNr<1 || d1->tacNr<1) {
40 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
41 return TPCERROR_NO_DATA;
42 }
43 /* Unselect all */
44 for(int i=0; i<d1->parNr; i++) d1->n[i].sw=0;
45 for(int i=0; i<d1->tacNr; i++) d1->r[i].sw=0;
46 /* If no data to compare to, then that is it */
47 if(d2==NULL || d2->parNr<1 || d2->tacNr<1) {
48 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
49 return TPCERROR_OK;
50 }
51
52 /* Search for parameter names */
53 for(int i=0; i<d1->parNr; i++) {
54 if(strnlen(d1->n[i].name, 1)<1) continue;
55 for(int j=0; j<d2->parNr; j++) {
56 if(strnlen(d2->n[j].name, 1)<1) continue;
57 if(strcasecmp(d1->n[i].name, d2->n[j].name)==0) {d1->n[i].sw=1; continue;}
58 /* Allow mixed '-' and '_' */
59 char *buf1, *buf2;
60 buf1=strdup(d1->n[i].name); strReplaceChar(buf1, '-', '_');
61 buf2=strdup(d2->n[j].name); strReplaceChar(buf2, '-', '_');
62 int ret=strcasecmp(buf1, buf2);
63 free(buf1); free(buf2);
64 if(ret==0) {d1->n[i].sw=1; continue;}
65 }
66 }
67
68 /* Search for TAC names */
69 for(int i=0; i<d1->tacNr; i++) {
70 if(strnlen(d1->r[i].name, 1)<1) continue;
71 for(int j=0; j<d2->tacNr; j++) {
72 if(strnlen(d2->r[j].name, 1)<1) continue;
73 if(strcasecmp(d1->r[i].name, d2->r[j].name)==0) {d1->r[i].sw=1; continue;}
74 /* Allow mixed '-' and '_' */
75 char *buf1, *buf2;
76 buf1=strdup(d1->r[i].name); strReplaceChar(buf1, '-', '_');
77 buf2=strdup(d2->r[j].name); strReplaceChar(buf2, '-', '_');
78 int ret=strcasecmp(buf1, buf2);
79 free(buf1); free(buf2);
80 if(ret==0) {d1->r[i].sw=1; continue;}
81 }
82 }
83
84 /* Count the selected numbers, if needed */
85 if(pn!=NULL) *pn=parSelectedParameters(d1);
86 if(pr!=NULL) *pr=parSelectedTACs(d1);
87
88 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
89 return TPCERROR_OK;
90}
int parSelectedTACs(PAR *d)
Definition parselect.c:100
int parSelectedParameters(PAR *d)
Definition parselect.c:199
char sw
Definition tpcpar.h:94
char sw
Definition tpcpar.h:74

Referenced by parCombineTACs().