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

TAC data unit conversions. More...

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

Go to the source code of this file.

Functions

int tacXUnitConvert (TAC *tac, const int u, TPCSTATUS *status)
 
int tacYUnitConvert (TAC *tac, const int u, TPCSTATUS *status)
 
int tacYUnitVolume2Mass (TAC *tac, const double density, TPCSTATUS *status)
 
int tacYUnitMass2Volume (TAC *tac, const double density, TPCSTATUS *status)
 

Detailed Description

TAC data unit conversions.

Definition in file tacunits.c.

Function Documentation

◆ tacXUnitConvert()

int tacXUnitConvert ( TAC * tac,
const int u,
TPCSTATUS * status )

Convert X values (sample times) in TAC struct to the specified unit.

See also
tacYUnitConvert, unitConversionFactor
Returns
Returns TPCERROR_OK (0) when successful.
Parameters
tacPointer to TAC struct
uEnum unit
statusPointer to status data; enter NULL if not needed

Definition at line 23 of file tacunits.c.

30 {
31 int verbose=0; if(status!=NULL) verbose=status->verbose;
32 if(verbose>0) {printf("%s(%s)\n", __func__, unitName(u)); fflush(stdout);}
33 if(tac==NULL || tac->sampleNr<1) {
34 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
35 return TPCERROR_NO_DATA;
36 }
37
38 /* If both units are unknown, then return suitable error code */
39 if(u==UNIT_UNKNOWN && tac->tunit==UNIT_UNKNOWN) {
40 if(verbose>1) {printf(" unknown x units\n"); fflush(stdout);}
41 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_UNKNOWN_UNIT);
43 }
44
45 /* Determine the correction factor */
46 double cf;
47 cf=unitConversionFactor(tac->tunit, u);
48 if(isnan(cf)) {
49 if(verbose>1) {printf(" cannot make x conversion factor\n"); fflush(stdout);}
50 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INCOMPATIBLE_UNIT);
52 }
53
54 /* Multiply x values */
55 if(verbose>1) {printf(" converting x with factor %g\n", cf); fflush(stdout);}
56 for(int i=0; i<tac->sampleNr; i++) {
57 tac->x[i]*=cf; tac->x1[i]*=cf; tac->x2[i]*=cf;
58 }
59 /* Set new unit */
60 tac->tunit=u;
61
62 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
63 return(TPCERROR_OK);
64}
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
double * x
Definition tpctac.h:97
int sampleNr
Definition tpctac.h:89
int tunit
Definition tpctac.h:109
double * x2
Definition tpctac.h:101
double * x1
Definition tpctac.h:99
int verbose
Verbose level, used by statusPrint() etc.
@ UNIT_UNKNOWN
Unknown unit.
@ TPCERROR_UNKNOWN_UNIT
Unknown data unit.
@ TPCERROR_OK
No error.
@ TPCERROR_INCOMPATIBLE_UNIT
Incompatible units.
@ TPCERROR_NO_DATA
File contains no data.
double unitConversionFactor(const int u1, const int u2)
Definition units.c:487
char * unitName(int unit_code)
Definition units.c:143

Referenced by tacInput2sim(), tacReadModelingData(), tacReadModelingInput(), tacReadReference(), tacSetWeights(), tacSetX(), and tacWriteSIF().

◆ tacYUnitConvert()

int tacYUnitConvert ( TAC * tac,
const int u,
TPCSTATUS * status )

Convert Y values (concentrations) in TAC struct to the specified unit.

See also
tacXUnitConvert, unitConversionFactor
Returns
Returns TPCERROR_OK (0) when successful.
Parameters
tacPointer to TAC struct
uEnum unit
statusPointer to status data; enter NULL if not needed

Definition at line 72 of file tacunits.c.

79 {
80 int verbose=0; if(status!=NULL) verbose=status->verbose;
81 if(verbose>0) printf("%s(%s)\n", __func__, unitName(u));
82 if(tac==NULL || tac->sampleNr<1 || tac->tacNr<1) {
83 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
84 return TPCERROR_NO_DATA;
85 }
86
87 /* If both units are unknown, then return suitable error code */
88 if(u==UNIT_UNKNOWN && tac->cunit==UNIT_UNKNOWN) {
89 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_UNKNOWN_UNIT);
91 }
92
93 /* Determine the correction factor */
94 double cf;
95 cf=unitConversionFactor(tac->cunit, u);
96 if(isnan(cf)) {
97 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INCOMPATIBLE_UNIT);
99 }
100
101 /* Multiply y values */
102 for(int j=0; j<tac->tacNr; j++) {
103 for(int i=0; i<tac->sampleNr; i++) {
104 tac->c[j].y[i]*=cf;
105 }
106 }
107 /* Set new unit */
108 tac->cunit=u;
109
110 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
111 return(TPCERROR_OK);
112}
double * y
Definition tpctac.h:75
int cunit
Definition tpctac.h:105
TACC * c
Definition tpctac.h:117
int tacNr
Definition tpctac.h:91

Referenced by tacReadModelingData(), tacReadModelingInput(), tacReadReference(), and tacSetWeights().

◆ tacYUnitMass2Volume()

int tacYUnitMass2Volume ( TAC * tac,
const double density,
TPCSTATUS * status )

Convert Y values (per mass concentrations) in TAC struct to the corresponding per volume concentrations.

See also
tacYUnitVolume2Mass, tacXUnitConvert, tacYUnitConvert, unitConversionFactor
Returns
Returns TPCERROR_OK (0) when successful.
Parameters
tacPointer to TAC struct.
densityDensity (g/mL).
statusPointer to status data; enter NULL if not needed.

Definition at line 169 of file tacunits.c.

176 {
177 int verbose=0; if(status!=NULL) verbose=status->verbose;
178 if(verbose>0) {printf("%s(%g)\n", __func__, density); fflush(stdout);}
179 if(tac==NULL || tac->sampleNr<1 || tac->tacNr<1 || !(density>0.0)) {
180 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
181 return TPCERROR_NO_DATA;
182 }
183
184 /* Check that unit contains mass in divider */
185 if(!unitDividerHasMass(tac->cunit)) {
186 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INCOMPATIBLE_UNIT);
188 }
189
190 /* Try to convert the unit */
192 if(uu==UNIT_UNKNOWN) {
193 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INCOMPATIBLE_UNIT);
195 }
196
197 /* Multiply y values with the density */
198 for(int j=0; j<tac->tacNr; j++) {
199 for(int i=0; i<tac->sampleNr; i++) {
200 tac->c[j].y[i]*=density;
201 }
202 }
203 /* Set new unit */
204 tac->cunit=uu;
205
206 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
207 return(TPCERROR_OK);
208}
int unitDividerMassVolumeConversion(int u)
Definition units.c:775
int unitDividerHasMass(int u)
Definition units.c:694

◆ tacYUnitVolume2Mass()

int tacYUnitVolume2Mass ( TAC * tac,
const double density,
TPCSTATUS * status )

Convert Y values (per volume concentrations) in TAC struct to the corresponding per mass concentrations.

See also
tacYUnitMass2Volume, tacXUnitConvert, tacYUnitConvert, unitConversionFactor
Returns
Returns TPCERROR_OK (0) when successful.
Parameters
tacPointer to TAC struct.
densityDensity (g/mL).
statusPointer to status data; enter NULL if not needed.

Definition at line 121 of file tacunits.c.

128 {
129 int verbose=0; if(status!=NULL) verbose=status->verbose;
130 if(verbose>0) {printf("%s(%g)\n", __func__, density); fflush(stdout);}
131 if(tac==NULL || tac->sampleNr<1 || tac->tacNr<1 || !(density>0.0)) {
132 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
133 return TPCERROR_NO_DATA;
134 }
135
136 /* Check that unit contains volume in divider */
137 if(!unitDividerHasVolume(tac->cunit)) {
138 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INCOMPATIBLE_UNIT);
140 }
141
142 /* Try to convert the unit */
144 if(uu==UNIT_UNKNOWN) {
145 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_INCOMPATIBLE_UNIT);
147 }
148
149 /* Divide y values with the density */
150 for(int j=0; j<tac->tacNr; j++) {
151 for(int i=0; i<tac->sampleNr; i++) {
152 tac->c[j].y[i]/=density;
153 }
154 }
155 /* Set new unit */
156 tac->cunit=uu;
157
158 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
159 return(TPCERROR_OK);
160}
int unitDividerHasVolume(int u)
Definition units.c:678