Integer occurances;
int count;
Double vol = null;
Double val = null;
Double pHval = null;
DblMatrix Cbval,Cbcont,Cblof,Cbplot,currentConc;
String conc,titrant,voladded;
double total_vol = 0.0;
String totVol;
int nrows = this.outSet.getRowCount();
while (this.outSet.getRowCount() > 0)
{
this.outSet.removeRow(0);
}
this.outSet.fireTableRowsDeleted(0,nrows-1);
int outrow=0;
// Get an average Contained for each experiment (average over the subsamples). (Done)
// Calculate Cb separately for each titration subsample.
// Pool Cb for all Subsamples and all experiments
// Throw a smooth curve through the negative Cb.
// Subtract the contribution to the total Cb due to titrant(s)
// as they were added.
// Plot this versus pH with circles(data);
//
for (int exp=0;exp<experiments.size();exp++)
{
expname = (String)experiments.get(exp);
/// Calculate average contained...///
containedSOL = new BufferSolution();//This will include water.
subsamples = this.parent.getContainedSubsampleIDs(expname);
Navg = new HashMap();
for (int sub=0;sub<subsamples.size();sub++)
{
//newSol will contain the contains for just this subsample.
//To protect against multiple entries or situations in
//which a complex buffer that consists of a mixture of
//simple buffers already in the contained etc. we create
//the solution first. Then we examime for the entries in
//the final solution. We add a 1 to the integer stored in
//Navg for that buffer. Note that the add method of BufferSolution
//resolves all mixture type buffers to their simpliest elements.
newSol = new BufferSolution();
subsample = (String)subsamples.get(sub);
observations = this.parent.getContainedObservationNumbers(expname,subsample);
for (int obs=0;obs<observations.size();obs++)
{
obsnum = (String)observations.get(obs);
contained = this.parent.getContainedNickname(expname,subsample,obsnum);
conc = this.parent.getContainedConcentration(expname,subsample,obsnum);
try
{
n = new Double(nf.parse(conc).doubleValue());
}
catch (ParseException ex)
{
System.err.println("Problem parsing concentration "+conc+".");
}
newSol.add(contained,n);
}
//System.out.println("We are here");
//Alright now we include instance counts into our running total of instances.
//contents = newSol.getContents();
//Set cnts = contents.keySet();
//Iterator iter = cnts.iterator();
//while (iter.hasNext())
contents = newSol.getComponents();
for (int m=0;m<contents.length;m++)
{
//nickname = (String)iter.next();
nickname = contents[m];
if (Navg.containsKey(nickname))
{
occurances = (Integer)Navg.get(nickname);
count = occurances.intValue();
}
else
{
count = 0;
}
Navg.put(nickname,new Integer(count+1));
}
//addSol differs from mixSol in that mixSol assumes mutual
//dilution from mixing in certain ratios. addSol simply
//adds concentrations.
containedSOL = BufferSolution.addSol(containedSOL,newSol);
}
//Calculate the average concentration of each contained from the sums formulated
//above.
if (contents != null)
{
//Set cnts = contents.keySet();
//Iterator iter = cnts.iterator();
//Calculate the average concentration of each contained.
//while (iter.hasNext())
for (int m=0;m<contents.length;m++)
{
//nickname = (String)iter.next();
nickname = contents[m];
occurances = (Integer)Navg.get(nickname);
count = occurances.intValue();
DblMatrix v = containedSOL.getConcentration(nickname);
//val = (Double)((DblMatrix)contents.get(nickname)).getDoubleAt(0);
containedSOL.setConcentration(nickname,v.divideBy(count));
//contents.put(nickname,val.doubleValue()/count);
}
}
//containedSOL is now a solution consisting of all stated components
//set at their average value, where the average was taken over all
//subsamples in which that component made an appearance. In other
//words the total number of subsamples is not necessarily the normalizing
//value in the average for a particular component.
/////////////////////////////////////
subsamples = this.parent.getTitrationSubsampleIDs(expname);
for (int sub=0;sub<subsamples.size();sub++)
{
subsample = (String)subsamples.get(sub);
//Start with a new buffer solution for this titration.
titrationSol = new BufferSolution();
titrationSol = BufferSolution.addSol(titrationSol,containedSOL);//Put the (avg.) contained stuff in.
observations = this.parent.getTitrationObservationNumbers(expname,subsample);
totVol = (String)this.parent.getInitialVolume(expname,subsample);
try
{
total_vol = nf.parse(totVol).doubleValue();
}
catch (ParseException ex)
{
System.err.println("Problem parsing total volume "+totVol+".");
}
for (int obs=0;obs<observations.size();obs++)
{
obsnum = (String)observations.get(obs);
titrant = this.parent.getTitrant(expname,subsample,obsnum);
conc = this.parent.getTitrantConcentration(expname,subsample,obsnum);
voladded = this.parent.getVolumeAdded(expname,subsample,obsnum);
try
{
n = new Double(nf.parse(conc).doubleValue());
}
catch (ParseException ex)
{
System.err.println("Problem parsing concentration "+conc+".");
}
pH = this.parent.getpH(expname,subsample,obsnum);
try
{
pHval = new Double(nf.parse(pH).doubleValue());
}
catch (ParseException ex)
{
System.err.println("Problem parsing concentration "+conc+".");
}
try
{
vol = new Double(nf.parse(voladded).doubleValue());
}
catch (ParseException ex)
{
System.err.println("Problem parsing volume added "+voladded+".");
}
currentConc = titrationSol.getConcentration(titrant);
if (currentConc == null)
{
currentConc = new DblMatrix(0.0);
}
currentConc = ((currentConc.times(total_vol)).plus(n*vol));
total_vol = total_vol+vol;
currentConc = currentConc.divideBy(total_vol);
titrationSol.setConcentration(titrant,currentConc);
//System.out.println("titrant is"+titrant);
//titrationSol.show("titrationSol");
//Get adjustment coefficients using titrationSol
//AdjCoef adj = new AdjCoef(titrationSol,pHval);
CB = titrationSol.Cb(new DblMatrix(pHval),true);
//System.out.println("OK till here ");
Cbval = (DblMatrix)CB.get("total");
//System.out.println("OK till here youare");
DblMatrix CbNaOH = (DblMatrix)CB.get("com.mockturtlesolutions.NaOH");
if (CbNaOH == null)
{
System.out.println("CbNaOH is null");
}
//CbNaOH.show("CbNaOH");
//Cbval.show("Cbval");
//Because of additivity we can subtract
CB = containedSOL.Cb(new DblMatrix(pHval));
Cbcont = (DblMatrix)CB.get("total");
//Cbcont.show("Cbcont");
//titrationSol.show("titrationSol");
//containedSOL.show("containedSol");