Commit 95919a75 authored by Vesa Oikonen's avatar Vesa Oikonen
Browse files

added parCombineTACs()

parent ff50a3db
......@@ -289,6 +289,11 @@ int main(
statusPrint(stderr, &status); statusFree(&status);
return(i);
}
i++; if((ret=test_parCombineTACs(&status))!=0) {
fprintf(stderr, "failed (%d).\n", ret);
statusPrint(stderr, &status); statusFree(&status);
return(i);
}
/* parcomp */
i++; if((ret=test_parCompareParameterNames(&status))!=0) {
fprintf(stderr, "failed (%d).\n", ret);
......
......@@ -82,12 +82,84 @@ int parSelectByAnother(
}
/* Count the selected numbers, if needed */
if(pn!=NULL) *pn=parSelectedParameters(d1);
if(pr!=NULL) *pr=parSelectedTACs(d1);
// if(pn!=NULL) for(int i=0; i<d1->parNr; i++) if(d1->n[i].sw) *pn=*pn+1;
// if(pr!=NULL) for(int i=0; i<d1->tacNr; i++) if(d1->r[i].sw) *pr=*pr+1;
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
return TPCERROR_OK;
}
/*****************************************************************************/
/*****************************************************************************/
/** Copy parameters from source PAR structure to target structure for TACs that are available
in both structures.
@return enum tpcerror (TPCERROR_OK when successful).
@sa parSelectByAnother, parSelectTACs, parEnsureNames
*/
int parCombineTACs(
/** Pointer to target PAR structure. */
PAR *d1,
/** Pointer to source PAR structure; not modified. */
PAR *d2,
/** Pointer to status data; enter NULL if not needed. */
TPCSTATUS *status
) {
int verbose=0; if(status!=NULL) verbose=status->verbose;
if(verbose>0) printf("%s()\n", __func__);
/* Check what there is to copy */
int tacNr;
int ret=parSelectByAnother(d1, d2, NULL, &tacNr, status);
if(ret!=TPCERROR_OK) return(ret);
if(tacNr<1) {
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
return TPCERROR_NO_DATA;
}
int parNr=d2->parNr;
if(verbose>1) printf("%d TACs and %d parameters to copy.\n", tacNr, parNr);
/* Allocate memory for the new parameters */
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
ret=parAllocateMore(d1, parNr, 0);
if(ret!=TPCERROR_OK) return(ret);
/* Copy new parameter names */
for(int i=0; i<d2->parNr; i++) {
strcpy(d1->n[i+d1->parNr].name, d2->n[i].name);
d1->n[i+d1->parNr].unit=d2->n[i].unit;
d1->n[i+d1->parNr].lim1=d2->n[i].lim1;
d1->n[i+d1->parNr].lim2=d2->n[i].lim2;
d1->n[i+d1->parNr].tol=d2->n[i].tol;
}
/* Copy contents for common TACs */
for(int i=0; i<d1->tacNr; i++) {
/* find matching TAC name */
if(d1->r[i].name==NULL || strnlen(d1->r[i].name, 1)<1) continue;
int j;
for(j=0; j<d2->tacNr; j++) {
if(d2->r[i].name==NULL || strnlen(d2->r[j].name, 1)<1) continue;
if(strcasecmp(d1->r[i].name, d2->r[j].name)==0) break;
/* Allow mixed '-' and '_' */
char *buf1, *buf2;
buf1=strdup(d1->r[i].name); strReplaceChar(buf1, '-', '_');
buf2=strdup(d2->r[j].name); strReplaceChar(buf2, '-', '_');
int ret=strcasecmp(buf1, buf2);
free(buf1); free(buf2);
if(ret==0) break;
}
if(j==d2->tacNr) continue; // matching name not found, leave values as NaN
/* Copy new parameter values */
for(int k=0; k<d2->parNr; k++) {
d1->r[i].p[k+d1->parNr]=d2->r[j].p[k];
d1->r[i].sd[k+d1->parNr]=d2->r[j].sd[k];
d1->r[i].cl1[k+d1->parNr]=d2->r[j].cl1[k];
d1->r[i].cl2[k+d1->parNr]=d2->r[j].cl2[k];
}
}
d1->parNr+=d2->parNr;
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
return TPCERROR_OK;
......
......@@ -101,3 +101,60 @@ int test_parSelectByAnother(
/*****************************************************************************/
/*****************************************************************************/
int test_parCombineTACs(
TPCSTATUS *status
) {
int verbose=0; if(status!=NULL) verbose=status->verbose;
statusSet(status, __func__, __FILE__, __LINE__, 0);
if(verbose>0) {
printf("\n=====================================\n");
printf("\n%s\n", __func__);
printf("\n=====================================\n");
}
if(verbose>1) printf("\n should not crash with NULL input\n");
if(parCombineTACs(NULL, NULL, NULL)==TPCERROR_OK) return(1);
PAR par; parInit(&par);
if(verbose>1) printf("\n should not crash with empty PAR struct\n");
if(parCombineTACs(NULL, &par, NULL)==TPCERROR_OK) return(2);
if(parCombineTACs(&par, NULL, NULL)==TPCERROR_OK) return(3);
int ret;
if(verbose>1) printf("\n create test data\n");
ret=create_par(&par); if(ret!=0) {parFree(&par); return(10);}
if(verbose>1) parWrite(&par, stdout, PAR_FORMAT_CSV_UK, 0, NULL);
if(verbose>1) printf("\n test against NULL\n");
if(parCombineTACs(&par, NULL, NULL)==TPCERROR_OK) {parFree(&par); return(101);}
if(parCombineTACs(NULL, &par, NULL)==TPCERROR_OK) {parFree(&par); return(102);}
if(verbose>1) printf("\n create test data\n");
PAR par2; parInit(&par2);
ret=create_par(&par2); if(ret!=0) {parFree(&par); parFree(&par2); return(20);}
if(verbose>1) printf("\n one region less\n");
par2.tacNr--;
for(int i=0; i<par2.parNr; i++) sprintf(par2.n[i].name, "p%d", 1+i);
if(verbose>1) parWrite(&par2, stdout, PAR_FORMAT_CSV_UK, 0, NULL);
if(parCombineTACs(&par, &par2, NULL)!=TPCERROR_OK) {parFree(&par); parFree(&par2); return(201);}
if(verbose>1) parWrite(&par, stdout, PAR_FORMAT_CSV_UK, 0, NULL);
parDeletePar(&par, 0);
parDeletePar(&par, 0);
parDeletePar(&par, 0);
parDeletePar(&par, 0);
parDeletePar(&par, 0);
if(parCompareParameterUnits(&par, &par2, -1, NULL)) {parFree(&par); parFree(&par2); return(202);}
if(parCompareTacNames(&par, &par2, 0, 1, NULL)) {parFree(&par); parFree(&par2); return(203);}
for(int i=0; i<par2.tacNr; i++)
if(parCompareParameters(&par, &par2, -1, i, 1, 1, 1, 1.0E-06, 1.0E-07, NULL))
{parFree(&par); parFree(&par2); return(i+211);}
parFree(&par); parFree(&par2);
statusSet(status, __func__, __FILE__, __LINE__, 0);
return(0);
}
/*****************************************************************************/
/*****************************************************************************/
......@@ -88,6 +88,7 @@ extern int test_parReadFIT(TPCSTATUS *status);
/*****************************************************************************/
/* parcomb */
extern int test_parSelectByAnother(TPCSTATUS *status);
extern int test_parCombineTACs(TPCSTATUS *status);
/*****************************************************************************/
/*****************************************************************************/
......
......@@ -219,6 +219,7 @@ extern int parWriteXML(PAR *par, FILE *fp, TPCSTATUS *status);
/*****************************************************************************/
/* parcomb */
int parSelectByAnother(PAR *d1, PAR *d2, int *pn, int *pr, TPCSTATUS *status);
int parCombineTACs(PAR *d1, PAR *d2, TPCSTATUS *status);
/*****************************************************************************/
/*****************************************************************************/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment