import java.io.*;
import javax.swing.*;
import jxl.*; 
import jxl.write.*;
import jxl.format.*;
import java.io.File;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.swing.filechooser.*;
import java.util.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.text.*;
import java.util.Locale;
 
public class elba_kalibrasyon_excel1
{
   double r2,sigmay,sigmayx;
   String isim[];
   double P[];
   String ciktidosyaismi;
   String ModelNo;
   public static String toString(double left, int w, int d)
   // converts a double to a string with given width and decimals.
   {
	java.text.NumberFormat df=java.text.NumberFormat.getInstance(Locale.US);
	df.setMaximumFractionDigits(d);
    df.setMinimumFractionDigits(d);
    df.setGroupingUsed(false);
    String s = df.format(left);
    while (s.length() < w)
      s = " " + s;
    if (s.length() > w)
    {
        s = "";
        for (int i=0; i<w; i++)
          s = s + "-";
    }
    return s;
    } 

	
public static double[] verigir(String s)
     {
     String s1=JOptionPane.showInputDialog(s);        
     StringTokenizer token=new StringTokenizer(s1);
     int n=token.countTokens()-1;
     int m=n+1;
     double a[]=new double[m];
     int j=0;           
     while(token.hasMoreTokens())
     {
     Double ax=new Double(token.nextToken());
     a[j++]=ax.doubleValue();
     }
     return a; 
     }
//en kucuk kareler metodu
  public static double[] pivotlugauss(double a[][],double b[])
  { //kısmi pivotlu gauss eleme yöntemi
  int n=b.length;	  
  double x[]=new double[n];
  double carpan=0;
  double toplam=0;
  double buyuk;
  double dummy=0;
  //gauss eleme
  int i,j,k,p,ii,jj;
  for(k=0;k<(n-1);k++)
  { // pivotlama
	p=k;
	buyuk=Math.abs(a[k][k]);
	for(ii=k+1;ii<n;ii++)
	{  dummy=Math.abs(a[ii][k]);
	   if(dummy > buyuk) {buyuk=dummy;p=ii;}
	}
	if(p!=k)
	{ for(jj=k;jj<n;jj++)
	  { dummy=a[p][jj];
	    a[p][jj]=a[k][jj];
	    a[k][jj]=dummy;
	  }
	  dummy=b[p];
	  b[p]=b[k];
	  b[k]=dummy;
	}
	//gauss elemeyi çözme  
    for(i=k+1;i<n;i++)
    {  carpan=a[i][k]/a[k][k];
       a[i][k]=0;
       for(j=k+1;j<n;j++)
       {   a[i][j]-=carpan*a[k][j]; }
           b[i]   =b[i]   -carpan*b[k];
	}
  }
  //geriye doğru yerine koyma
  x[n-1]=b[n-1]/a[n-1][n-1];
  for(i=n-2;i>=0;i--)
  {
    toplam=0;
    for(j=i+1;j<n;j++)
    {  toplam+=a[i][j]*x[j];}
    x[i]=(b[i]-toplam)/a[i][i];
  }
  return x;
}
  
public static double[] EKK(double xi[],double yi[],int n)
{
int l=xi.length;
int i,j,k;
int np1=n+1;
double A[][];
A=new double[np1][np1];
double B[];
B=new double[np1];
double X[];
X=new double[np1];
for(i=0;i<n+1;i++)
  {  for(j=0;j<n+1;j++)
     {if(i==0 && j==0) A[i][j]=l;
     else  for(k=0;k<l;k++) A[i][j] += Math.pow(xi[k],(i+j));
     }
     for(k=0;k<l;k++) { if(i==0)  B[i]+= yi[k];
                       else      B[i] += Math.pow(xi[k],i)*yi[k];}
  } 
X=pivotlugauss(A,B);
//X=B/A;
double max=0;
for(i=0;i<n+1;i++)
 if(Math.abs(X[i]) > max) max = Math.abs(X[i]);
for(i=0;i<n+1;i++)
 if((Math.abs(X[i]/max) > 0) && (Math.abs(X[i]/max) < 1.0e-100)) X[i]=0;
return X;
}

public static double fi(double Km,double n,double dT)
{return Km*Math.pow(dT,n);}

public elba_kalibrasyon_excel1(JButton jt[],String RaporNo, double P0[],double Sk,double np, String dosyaismi,String ModelNoi,boolean plot) throws IOException, jxl.write.WriteException
{
  double xx[];
  double yx[];
  double fx[];
  double xxmin=9e10;
  double xxmax=-9e10;
  isim=new String[3];
  //double Sk;
  //double np;
  double fp; 
  double fi;
  double lnKm,Km;
  P=P0;
  // RaporNo;
  String ModelAdi;
  ModelNo=ModelNoi;
  double ntoplam=0;
  Date datec2=null;
  jxl.write.WritableImage image;
  // KALİBRASYON EĞRİLERİ
  // giriş termometresi kalibrasyonu katsayıları
  double cg[]={       -34.7916061297186660000000000,
        3.1056368822447600000000000,
       -0.0513674787542276750000000,
        0.0005847810048050877000000,
       -0.0000030216622211142310000,
        0.0000000053304475754419010 };      
	  
	  
	  
//  	 -4.529357522,
//        1.265368793735647,
//       -0.005846020500955,
//        0.000056552089858,
//       -0.000000200984504};
  // çıkış termometresi kalibrasyon katsayları     

 double cc[]={      -73.8361207609310500000000000,
        6.1609183000598060000000000,
       -0.1453779569774330800000000,
        0.0020042950789536750000000,
       -0.0000135428220244953240000,
        0.0000000359726315556960300};
	 
//  double cc[]={ -2.397760826940111,	 
//   	 -0.557760827,
//        1.015051449705956,
//       -0.000019777875934,
//       -0.000002343786634,
//        0.000000016901041};
  // kütle kalibrasyonu katsayıları
  double cm[]={0.0,
        1.0,
        0.00,
        0.00,
        0.00}; 
//       hava sıcaklığı kalibrasyon katsayıları 
double hc[]={       -5.0097833231405850000000000,
        1.3499379225088375000000000,
       -0.0125652156353954750000000,
        0.0001660256818594842200000};
	
	
	
//	   -29.55339268,
//        4.631360103873972,
//       -0.146436653474795,
//        0.001937890212541};                  	
steam b;
b=new steam();
WritableCellFormat wcf3=new WritableCellFormat();
double Pref=101.3; //kPa
//jxl.format.Colour acolor[];
  //RaporNo=JOptionPane.showInputDialog("RaporNo (Rapor başlığı gir)");
  //String s="üç deney basınç değerini aralarında (kPa) boşluk bırakarak giriniz: ";
  //double [] P0=verigir(s);
  //Sk=Double.parseDouble(JOptionPane.showInputDialog("Isıl güç ışınım faktörü Sk bknz TS EN 442-2/Mart 1998 ÇİZELGE 4"));
  //np=Double.parseDouble(JOptionPane.showInputDialog("Basınç düzeltme np üssü bknz TS EN 442-2/Mart 1998 ÇİZELGE 4"));
  //Locale abd=new Locale("US");
  try
  {
  WritableFont tahoma9font = new WritableFont(WritableFont.TAHOMA, 9, WritableFont.BOLD, true); 
  jxl.write.WritableCellFormat TL=new jxl.write.WritableCellFormat(tahoma9font);
  TL.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.DOUBLE);
  TL.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.DOUBLE);
  TL.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN);
  TL.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.THIN);
  TL.setWrap(true);
  jxl.write.WritableCellFormat TR=new jxl.write.WritableCellFormat(tahoma9font);
  TR.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.DOUBLE);
  TR.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.DOUBLE);
  TR.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN);
  TR.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.THIN);
  TR.setWrap(true);
  jxl.write.WritableCellFormat BL=new jxl.write.WritableCellFormat(tahoma9font);
  BL.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.DOUBLE);
  BL.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.DOUBLE);
  BL.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.THIN);
  BL.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.THIN);
  TR.setWrap(true);
  jxl.write.WritableCellFormat BR=new jxl.write.WritableCellFormat(tahoma9font);
  BR.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.DOUBLE);
  BR.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.DOUBLE);
  BR.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.THIN);
  BR.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.THIN);
  BR.setWrap(true);
  jxl.write.WritableCellFormat L=new jxl.write.WritableCellFormat(tahoma9font);
  L.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.DOUBLE);
  L.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN);
  L.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.THIN);
  L.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.THIN);
  L.setWrap(true);
  jxl.write.WritableCellFormat R=new jxl.write.WritableCellFormat(tahoma9font);
  R.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.DOUBLE);
  R.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN);
  R.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.THIN);
  R.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.THIN);
  R.setWrap(true);
  jxl.write.WritableCellFormat T=new jxl.write.WritableCellFormat(tahoma9font);
  T.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.DOUBLE);
  T.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN);
  T.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.THIN);
  T.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.THIN);
  T.setWrap(true);
  jxl.write.WritableCellFormat B=new jxl.write.WritableCellFormat(tahoma9font);
  B.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.DOUBLE);
  B.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.THIN);
  B.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.THIN);
  B.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.THIN);
  B.setWrap(true);
  jxl.write.WritableCellFormat R_T=new jxl.write.WritableCellFormat(tahoma9font);
  R_T.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.DOUBLE);
  R_T.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN);
  R_T.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.NONE);
  R_T.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.THIN);
  R_T.setWrap(true);
  jxl.write.WritableCellFormat L_R=new jxl.write.WritableCellFormat(tahoma9font);
  L_R.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.DOUBLE);
  L_R.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN);
  L_R.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.NONE);
  L_R.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.THIN);
  L_R.setWrap(true);
  jxl.write.WritableCellFormat R_L=new jxl.write.WritableCellFormat(tahoma9font);
  R_L.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.NONE);
  R_L.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN);
  R_L.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.DOUBLE);
  R_L.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.THIN);
  R_L.setWrap(true);
  jxl.write.WritableCellFormat R_=new jxl.write.WritableCellFormat(tahoma9font);
  R_.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.THIN);
  R_.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN);
  R_.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.NONE);
  R_.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.THIN);
  R_.setWrap(true);
  jxl.write.WritableCellFormat R_L_=new jxl.write.WritableCellFormat(tahoma9font);
  R_L_.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.THIN);
  R_L_.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN);
  R_L_.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.NONE);
  R_L_.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.NONE);
  R_L_.setWrap(true);
  jxl.write.WritableCellFormat R_L_T=new jxl.write.WritableCellFormat(tahoma9font);
  R_L_T.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.DOUBLE);
  R_L_T.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN);
  R_L_T.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.NONE);
  R_L_T.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.NONE);
  R_L_T.setWrap(true);
  jxl.write.WritableCellFormat L_RT=new jxl.write.WritableCellFormat(tahoma9font);
  L_RT.setBorder(jxl.format.Border.TOP,jxl.format.BorderLineStyle.DOUBLE);
  L_RT.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN);
  L_RT.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.DOUBLE);
  L_RT.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.NONE);
  L_RT.setWrap(true);
  jxl.write.WritableCellFormat wcf=new WritableCellFormat(tahoma9font);
  wcf.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
  wcf.setWrap(true);
  
  String s1="DEKORPAN.xls";
  String sr1[]={"",""," "," ",
  "Model Boyutları"," ","Test Ölçüm Bilgileri",
  "Hava Basıncı","Referans Hava sıcaklığı",
  "Giriş suyu sıcaklığı","Çıkış suyu sıcaklığı",
  "Radyatör Sıcaklık Farkı","Giriş suyu entalpisi",
  "Çıkış suyu entalpisi","Entalpi farkı",
  "Radyatör ortalama su sıcaklığı", "Ortalama su ile ortam sıcaklığı farkı",
  "Debi","Ölçülen kapasite","Barometrik basınç ile düzeltilen kapasite"," ","Km"};
  String sr2[]={"   ","    "," "," ",
  "Test Tarihi:"," "," ",
  "Air Pressure","Reference Air Temperature","Water inlet temperature",
  "Water outlet temperature",
  "Temperature Difference","Inlet Water Entalphy",
  "Exit Water Entalphy","Entalphy difference",
  "Mean water temperature", "Excess temperature",
  "Water flow rate","Thermal measured output","Corrected thermal output"," ","n"};
  String sr3[]={"S1","S2","S3",
  " ","S"," "," ",
  "P","t","t1",
  "t2",
  "t1-t2","h1",
  "h2",'\u0394'+"h",
  "tm", '\u0394'+"t",
  "qm",""+'\u03A6'+"me",""+'\u03A6'+""," ","Ölçüm sayısı"," "};
  String dereceC=""+'\u00B0'+"C";
  String sr4[]={"   ","    "," "," ",
  "B"," "," ",
  "kPa",dereceC,dereceC,
  dereceC,dereceC,"J/kg","J/kg","J/kg",dereceC,"K","kg/s","W","W"," "," "," "};
  File f=new File(dosyaismi);
  int snl=dosyaismi.length();  
  StringBuffer sb1=new StringBuffer(dosyaismi);
  sb1.insert(snl-4,"");
  String s2=sb1.toString();
  ModelAdi=s2;
  ciktidosyaismi=RaporNo+".xls";
  File f1=new File(ciktidosyaismi);  
  //System.out.println(s2);
  int nos;

  //excell girdi dosyası	  
  jxl.Workbook workbook = jxl.Workbook.getWorkbook(f); 
  //excell sayfa sayısı
  nos=workbook.getNumberOfSheets();
  double xi[]=new double[nos];
  double yi[]=new double[nos];
  String rn[]=new String[nos];  
  //int sn=Integer.parseInt(JOptionPane.showInputDialog("excel sayfa numarası (0,1,2...)"));
  jxl.write.WritableWorkbook workbook1 = jxl.Workbook.createWorkbook(f1, workbook);  
  jxl.write.WritableSheet wreportsheet = workbook1.createSheet("Report Page", nos+3);  
  jxl.write.Label l1;
  jxl.write.Number number;
 try{
	jxl.write.WritableCellFormat wcf1=new jxl.write.WritableCellFormat();
    wcf1.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.DOUBLE);
	wcf1.setBorder(jxl.format.Border.RIGHT,jxl.format.BorderLineStyle.DOUBLE);
    wcf1.setWrap(true);
    wcf3.setWrap(true);
    wcf.setWrap(true);
	for(int ii=2;ii<4;ii++)
    {
	l1=new jxl.write.Label(0,ii+3,sr1[ii]);
	if((ii+3)==5 || (ii+3)==6 || (ii+3)==9 || (ii+3)==23) l1.setCellFormat(wcf3);
	else l1.setCellFormat(wcf);
	//l1.setCellFormat(wcf);
    wreportsheet.addCell(l1);
    }    
    for(int ii=4;ii<sr1.length;ii++)
    {    
	l1=new jxl.write.Label(0,ii+3,sr1[ii]);
	if(ii==4) l1.setCellFormat(TL);
	else if(ii==6) l1.setCellFormat(R_L);
	else if(ii==20) l1.setCellFormat(R_L_);
	else if(ii==(sr1.length-3)) l1.setCellFormat(BL);
	else if(ii==(sr1.length-1)) l1.setCellFormat(TL);
	else l1.setCellFormat(L);
    wreportsheet.addCell(l1);
    }
    
    for(int ii=4;ii<sr1.length;ii++)
    {
    l1=new jxl.write.Label(1,ii+3,sr2[ii]);
	if(ii==4) l1.setCellFormat(T);
	else if(ii==6) l1.setCellFormat(R_L_);
	else if(ii==20) l1.setCellFormat(R_L_);
	else if(ii==(sr2.length-3)) l1.setCellFormat(B);
	else if(ii==(sr2.length-1)) l1.setCellFormat(T);
	else l1.setCellFormat(wcf);
    wreportsheet.addCell(l1);
    }
    
    for(int ii=4;ii<sr3.length;ii++)
    {
    l1=new jxl.write.Label(2,ii+3,sr3[ii]);
	if(ii==4) l1.setCellFormat(T);
	else if(ii==6) l1.setCellFormat(R_L_);
	else if(ii==20) l1.setCellFormat(R_L_);
	else if(ii==(sr1.length-3)) l1.setCellFormat(B);
	else if(ii==(sr2.length-1)) l1.setCellFormat(T);
	else l1.setCellFormat(wcf);
    wreportsheet.addCell(l1);
    }
    
    for(int ii=4;ii<sr1.length;ii++)
    {
    l1=new jxl.write.Label(3,ii+3,sr4[ii]);
	if(ii==4) l1.setCellFormat(T);
	else if(ii==6) l1.setCellFormat(R_L_);
	else if(ii==20) l1.setCellFormat(R_L_);
	else if(ii==(sr1.length-3)) l1.setCellFormat(B);
	else if(ii==(sr1.length-1)) l1.setCellFormat(T);
	else l1.setCellFormat(wcf);
    wreportsheet.addCell(l1);
    }

	l1=new jxl.write.Label(4,4+3,"Ölçüm Noktaları");
	l1.setCellFormat(R_T);
    wreportsheet.addCell(l1);
    
    l1=new jxl.write.Label(5,4+3,"  ");
	l1.setCellFormat(R_L_T);
    wreportsheet.addCell(l1);
        
    l1=new jxl.write.Label(6,4+3,"  ");
	l1.setCellFormat(L_RT);
    wreportsheet.addCell(l1); 
    }catch(jxl.write.WriteException we) {System.out.println("write exception");}
  int sn;
  Sheet sheet;
  WritableSheet wsheet;
  xx=new double[nos];
  yx=new double[nos];
  fx=new double[nos];
  try{
  //sn excell sayfası ana döngü
  //her excell sayfası için bir kere dön
  for(sn=0;sn<nos;sn++)
  {  
  sheet = workbook.getSheet(sn);
  //input excell sheet name
  rn[sn]=sheet.getName();
  //write excell sheet name
  wsheet = workbook1.createSheet("K_"+rn[sn], sn+3); 
  Cell a1 = sheet.getCell(0,0); 
  String stringa1 = a1.getContents();
  int i=1; 
  int j=0;
  //dolu excel satırlarını say
  try
  {
  while(stringa1!="")
  {	  
  a1 = sheet.getCell(0,i);
  stringa1 = a1.getContents();
  i++; 	  
  }
  }catch(ArrayIndexOutOfBoundsException ai) {}  
  //bu excell sayfası için satır sayısı = n 
  int n=i-2;
  int n1=n;
  double a[][]=new double[8][n];
  //acolor=new jxl.format.Colour[n];
  // test date
  Cell c2 = sheet.getCell(8,1); 
  LabelCell lc = (LabelCell)c2; 
  String ss=lc.getString();
  // StringBuffer sb2=new StringBuffer(ss);
  // sb2.insert(10,"");
  // 9 kolon n satırlık excell sayfası verisini oku 
  for(i=0;i<n;i++)
  {  
  for(j=0;j<8;j++)
  {  
      a1 = sheet.getCell(j,i+1);
      if (a1.getType() == CellType.NUMBER)
      {
      NumberCell nc = (NumberCell) a1;
      a[j][i] = nc.getValue();
      if(j==0)
      {jxl.format.CellFormat format1=a1.getCellFormat();}
      } 
  }
  }
  
double gs[]=new double[n]; // kalibre giriş sıcaklığı
double cs[]=new double[n]; // kalibre çıkış sıcaklığı
double hs[]=new double[n]; //kalibre hava sıcaklığı
double W[]=new double[n];  // enerji çıktısı
double m[]=new double[n];  // kalibre kütlesel debi
for(i=0;i<n;i++)
{
double tg=a[1][i]; //ham giriş sıcaklığı
double tc=a[2][i]; //ham çıkış sıcaklığı
double md=a[6][i]; //ham debi
double th=a[3][i]; //hava sıcaklığı

// okunan değerler üzerinden kalibrasyon eğrilerini uydur
gs[i]=cg[0]+cg[1]*tg+cg[2]*tg*tg+cg[3]*tg*tg*tg+cg[4]*tg*tg*tg*tg+cg[5]*tg*tg*tg*tg*tg;
cs[i]=cc[0]+cc[1]*tc+cc[2]*tc*tc+cc[3]*tc*tc*tc+cc[4]*tc*tc*tc*tc+cc[5]*tc*tc*tc*tc*tc;
hs[i]=hc[0]+hc[1]*th+hc[2]*th*th+hc[3]*th*th*th;
m[i]=(cm[0]+cm[1]*md+cm[2]*md*md+cm[3]*md*md*md+cm[4]*md*md*md*md);
double a2[]=b.property("tp",gs[i],P0[sn]*1e-2);
double a3[]=b.property("tp",cs[i],P0[sn]*1e-2);
double h1=a2[3]; //giriş entalpisi
double h2=a3[3]; //çıkış entalpisi
//System.out.println(gs[i]+" "+h1+" "+cs[i]+" "+h2);
W[i]=m[i]*(h1-h2)*1000; //kalibre çıkış gücü
}
double bb[][]=new double[10][n];
for(i=0;i<n;i++)
{
bb[0][i]=a[1][i];
bb[1][i]=gs[i];
bb[2][i]=a[2][i];
bb[3][i]=cs[i];
bb[4][i]=a[3][i];
bb[5][i]=hs[i];
bb[6][i]=a[6][i];
bb[7][i]=m[i];
bb[8][i]=a[7][i];
bb[9][i]=W[i];
}
// çıktı excell sayfası başlıkları 
String s3[]={"D.G.sıcaklığı","K.G.sıcaklığı","D.Ç.Sıcaklığı","K.Ç.Sıcaklığı","Ortam sıcaklığı","K. Ortam sıcaklığı","D. Debi","K. Debi","D. Watt","K. Watt"};	
jxl.write.Label l;
for(i=0;i<10;i++)
{l=new jxl.write.Label(i,0+3,s3[i]);
 wsheet.addCell(l);
}
  //jxl.write.Number number;  
  double ortalama[]=new double[10]; //kolon için ortalama değerler
  double stsapma[]=new double[10];  //kolon için standart sapma
  double toplam[]=new double[10];   //kolon için toplam
  int n2[]=new int[10];  
  double w1,w2;
  double ort=0;
  double sts=0;
  for(i=0;i<n;i++)
  {ort+=bb[9][i];}
   ort=ort/n;
  for(i=0;i<n;i++)
  {sts+=(bb[9][i]-ort)*(bb[9][i]-ort);}
   sts=Math.sqrt(sts/(n-1)); 
 
  w1=ort-2.0*sts;
  w2=ort+2.0*sts;	  
  for(j=0;j<10;j++)
  {
  n2[j]=0;
  toplam[j]=0;     
  for(i=0;i<n;i++)
  {  	  
	  //WritableCellFormat wcf=new WritableCellFormat();
	  //System.out.println("i="+i+"***"+acolor[i].getValue());
	  //wcf.setBackground(acolor[i]);
	  //jxl.format.Border bdr=new jxl.format.Border("all"); 
	  //wcf.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
	  //if(acolor[i].getValue()==11) 

	  if(bb[9][i]>=w1 && bb[9][i]<=w2) 
	  {  //acolor[i].setValue(11);
	     //wcf.setBackground(acolor[i]);	     
		 toplam[j]+=bb[j][i];
	     n2[j]++;   
	     number = new jxl.write.Number(j,i+4,bb[j][i]);
	     number.setCellFormat(wcf);
         wsheet.addCell(number); 
      }    
  }
  
  }
  ntoplam+=n2[0];
  
  for(j=0;j<10;j++)
  {  ortalama[j]=toplam[j]/n2[j];}
  
  for(j=0;j<10;j++)
  { for(i=0;i<n;i++)
    {   if(bb[9][i]>=w1 && bb[9][i]<=w2) 
	    { stsapma[j]+=(bb[j][i]-ortalama[j])*(bb[j][i]-ortalama[j]);}  
    }
    stsapma[j]=Math.sqrt(stsapma[j]/(n2[j]-1));
  }  
  for(j=0;j<10;j++)
  {  // ortalama
     number = new jxl.write.Number(j,n+5,ortalama[j]);
     //WritableCellFormat wcf=new WritableCellFormat();
	 //wcf.setBackground(jxl.format.Colour.LIGHT_BLUE); 
     //wcf.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
	 number.setCellFormat(wcf);
     wsheet.addCell(number); 
     // standart sapma
     number = new jxl.write.Number(j,n+6,stsapma[j]);
     number.setCellFormat(wcf);
     wsheet.addCell(number);
     //alt sınır
     
     number = new jxl.write.Number(j,n+7,(ortalama[j]-2.0*stsapma[j]));
     number.setCellFormat(wcf);
     wsheet.addCell(number);
     // üst sınır
     number = new jxl.write.Number(j,n+8,(ortalama[j]+2.0*stsapma[j]));
     number.setCellFormat(wcf);
     wsheet.addCell(number);
  }  
  //WritableCellFormat wcf=new WritableCellFormat();
  //wcf.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
	//Test date
	int len = ss.length(); 
    StringBuffer dest = new StringBuffer(len); 
    for (i = 0; i <10; i++) { 
    dest.append(ss.charAt(i)); } 
    ss=dest.toString(); 
	jxl.write.Label ll1=new jxl.write.Label(1,8,ss);
	ll1.setCellFormat(wcf);
    wreportsheet.addCell(ll1);  
//  Test ölçüm satırı
    l1=new jxl.write.Label((4+sn),6+3," ");
	if(sn==(nos-1)) l1.setCellFormat(L_R);
	else l1.setCellFormat(R_L_);
    wreportsheet.addCell(l1);  
    
    //Hava Basıncı
    l1=new jxl.write.Label((4+sn),7+3,toString(P0[sn],5,1));
	if(sn==(nos-1)) l1.setCellFormat(R);
	else l1.setCellFormat(wcf);
    wreportsheet.addCell(l1);  
    //Referans hava sıaklığı
    l1=new jxl.write.Label((4+sn),8+3,toString(ortalama[5],4,1));
	if(sn==(nos-1)) l1.setCellFormat(R);
	else l1.setCellFormat(wcf);
	wreportsheet.addCell(l1); 
	//Giriş suyu sıcaklığı
    l1=new jxl.write.Label((4+sn),9+3,toString(ortalama[1],4,1));
	if(sn==(nos-1)) l1.setCellFormat(R);
	else l1.setCellFormat(wcf);
	wreportsheet.addCell(l1); 
	//Çıkış suyu sıcaklığı
    l1=new jxl.write.Label((4+sn),10+3,toString(ortalama[3],4,1));
	if(sn==(nos-1)) l1.setCellFormat(R);
	else l1.setCellFormat(wcf);
	wreportsheet.addCell(l1); 
    //Radyatör sıcaklık farkı
    l1=new jxl.write.Label((4+sn),11+3,toString((ortalama[1]-ortalama[3]),4,1));
	if(sn==(nos-1)) l1.setCellFormat(R);
	else l1.setCellFormat(wcf);
	wreportsheet.addCell(l1); 
    //Giriş suyu entalpisi
    double a4[]=b.property("tp",ortalama[1],P0[sn]*1e-2);
    double h4=a4[3]*1e3;    
    l1=new jxl.write.Label((4+sn),12+3,toString(h4,6,0));
	if(sn==(nos-1)) l1.setCellFormat(R);
	else l1.setCellFormat(wcf);
	wreportsheet.addCell(l1); 
    //Çıkış suyu entalpisi
    double a5[]=b.property("tp",ortalama[3],P0[sn]*1e-2);
    double h5=a5[3]*1e3;    
    l1=new jxl.write.Label((4+sn),13+3,toString(h5,6,0));
	if(sn==(nos-1)) l1.setCellFormat(R);
	else l1.setCellFormat(wcf);
	wreportsheet.addCell(l1); 
    //entalpi farkı
    double h6=h4-h5;    
    l1=new jxl.write.Label((4+sn),14+3,toString(h6,6,0));
	if(sn==(nos-1)) l1.setCellFormat(R);
	else l1.setCellFormat(wcf);
	wreportsheet.addCell(l1); 
    //Radyatör ortalama su sıcaklığı
    double to=(ortalama[1]+ortalama[3])/2.0;
    l1=new jxl.write.Label((4+sn),15+3,toString(to,4,1));
	if(sn==(nos-1)) l1.setCellFormat(R);
	else l1.setCellFormat(wcf);
	wreportsheet.addCell(l1); 
    //ortalama su ile ortam sıcaklığı farkı
    xx[sn]=to-ortalama[5];
    if(xx[sn]<xxmin) xxmin=xx[sn];
    if(xx[sn]>xxmax) xxmax=xx[sn];
    //System.out.println("xi["+sn+"] = "+xi[sn]+"to="+to+"ortalama[5]="+ortalama[5]);
    l1=new jxl.write.Label((4+sn),16+3,toString(xx[sn],4,1));
    xi[sn]=Math.log(xx[sn]);
	if(sn==(nos-1)) l1.setCellFormat(R);
	else l1.setCellFormat(wcf);
	wreportsheet.addCell(l1); 
    //Debi
    number=new jxl.write.Number((4+sn),17+3,ortalama[7]);
	if(sn==(nos-1)) number.setCellFormat(R);
	else number.setCellFormat(wcf);
	wreportsheet.addCell(number); 
    // ölçülen kapasite
    l1=new jxl.write.Label((4+sn),18+3,toString(ortalama[9],5,0));
    if(sn==(nos-1)) l1.setCellFormat(R);
	else l1.setCellFormat(wcf);
	wreportsheet.addCell(l1); 
    // barometrik basınç ile düzeltilmiş kapasite
    fp=Math.pow((P0[sn]/Pref),np);
    fi=ortalama[9]*(Sk+(1.0-Sk)*fp);
    yx[sn]=fi;
    yi[sn]=Math.log(fi);
    l1=new jxl.write.Label((4+sn),19+3,toString(fi,4,0));
    if(sn==(nos-1)) l1.setCellFormat(BR);
	else l1.setCellFormat(B);
    wreportsheet.addCell(l1); 
    isim[sn]=rn[sn];
    jt[sn]=new JButton();
    Text.print(jt[sn],Text.T(bb),s3,RaporNo+" sıcaklık bölgesi : "+rn[sn]);	
}
double d[]=EKK(xi,yi,1);
Km=Math.exp(d[0]);
double n=d[1];
double fistn[]=new double[3];
fistn[0]=fi(Km,n,60.0);
fistn[1]=fi(Km,n,50.0);
fistn[2]=fi(Km,n,30.0);
double ym=0;
for(int ii=0;ii<nos;ii++)
{fx[ii]=fi(Km,n,xx[ii]);
ym+=yx[ii];
}
ym=ym/nos;
sigmay=0;
sigmayx=0;
for(int ii=0;ii<nos;ii++)
{sigmay+=(yx[ii]-ym)*(yx[ii]-ym)/(nos-1);
 sigmayx+=(yx[ii]-fx[ii])*(yx[ii]-fx[ii])/(nos-2);
}
double R2=(sigmay-sigmayx)/sigmay;
double x2[]=new double[100];
double y2[]=new double[100];
xxmax=60.0;
for(int ii=0;ii<100;ii++)
{x2[ii]=xxmin+(xxmax-xxmin)/100.0*ii;y2[ii]=fi(Km,n,x2[ii]);}
if(plot)
{
Plot1 pp=new Plot1(xx,yx);
pp.setPlabel(RaporNo+" "+ModelNo+" yüzey-ortam sıcaklık farkı ısı çıktı fonksiyonu");
pp.setXlabel("ortalama yüzey sıcaklığı-ortam sıcaklığı farkı "+'\u0394'+"T "+dereceC); 
pp.setYlabel("Isı çıktısı W"); 
pp.addData(xx,fx);
pp.setPlotType(0,22);
pp.setPlotType(1,24);
String ss[]={"basınç için düzeltilmiş orijinal ortalama veri",
            '\u0394'+"T için ortalama sıcaklık değerine eğri uydurma",
            '\u0394'+"T için 30-50-60 C eğri uydurma",
            "f("+'\u0394'+"T)="+Km+"*"+'\u0394'+"T^"+n};            
String s=" R"+'\u00B2'+" = "+R2;
double x1[]={60,50,30};
pp.addData(x1,fistn);
pp.setPlotType(2,28);
pp.addData(x2,y2);
double xp1=pp.xmin+0.4*(pp.xmax-pp.xmin);
double yp1=pp.ymin+0.2*(pp.ymax-pp.ymin);
double xp2=pp.xmin+0.1*(pp.xmax-pp.xmin);
double yp2=pp.ymin+0.8*(pp.ymax-pp.ymin);
pp.addStringwithPlotType(ss,xp1,yp1);
pp.addString(s,xp2,yp2,0);
pp.plot();
}
File ff=new File("ELBAlogo.png");
image=new jxl.write.WritableImage(4,0,2,5,ff);
  //WritableCellFormat wcf=new WritableCellFormat();
  
  //wcf.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
  WritableCellFormat wcf1=new WritableCellFormat();
  wcf1.setBorder(jxl.format.Border.LEFT,jxl.format.BorderLineStyle.DOUBLE);
  wreportsheet.addImage(image);
  wreportsheet.setColumnView(1,20);
  wreportsheet.setColumnView(2,20);
    l1=new jxl.write.Label(4,5+3,rn[0]);
    l1.setCellFormat(wcf);
    wreportsheet.addCell(l1);
    l1=new jxl.write.Label(5,5+3,rn[1]);
	l1.setCellFormat(wcf);
	wreportsheet.addCell(l1);
	
    l1=new jxl.write.Label(6,5+3,rn[2]);
	l1.setCellFormat(R);
	wreportsheet.addCell(l1);
	
	//Km
	l1=new jxl.write.Label(0,22+3,toString(Km,8,5));
    l1.setCellFormat(BL);
	wreportsheet.addCell(l1); 
	//n
	l1=new jxl.write.Label(1,22+3,toString(n,8,5));
    l1.setCellFormat(B);
	wreportsheet.addCell(l1); 
	//ölçüm sayısı
    number=new jxl.write.Number(2,22+3,ntoplam);
    number.setCellFormat(B);
	wreportsheet.addCell(number); 
	//dt60 dt50 dt30
	l1=new jxl.write.Label(3,22+3," ");
	l1.setCellFormat(B);
	wreportsheet.addCell(l1);
	l1=new jxl.write.Label(4,22+3,'\u0394'+"t 60");
	l1.setCellFormat(wcf);
	wreportsheet.addCell(l1);
	l1=new jxl.write.Label(5,22+3,'\u0394'+"t 50");
	l1.setCellFormat(wcf);
	wreportsheet.addCell(l1);
	l1=new jxl.write.Label(6,22+3,'\u0394'+"t 30");
	l1.setCellFormat(R);
	wreportsheet.addCell(l1);
	//R2
	String s=" R"+'\u00B2'+" = ";
	l1=new jxl.write.Label(0,22+4,s);
	l1.setCellFormat(R);
	wreportsheet.addCell(l1);
    String ss1=""+toString(R2,10,7);
	l1=new jxl.write.Label(1,22+4,ss1);
	l1.setCellFormat(R);
	wreportsheet.addCell(l1);
	//=================
	jxl.write.WritableFont times18font = new jxl.write.WritableFont(WritableFont.TIMES, 18); 
    jxl.write.WritableCellFormat times18format = new jxl.write.WritableCellFormat(times18font); 
    //WritableCellFormat wcf1=new WritableCellFormat(times18format);
    
    l1=new jxl.write.Label(4,21+3,"       "+'\u03A6'+"L(W)");
	l1.setCellFormat(R_T);
	wreportsheet.addCell(l1);
	
	l1=new jxl.write.Label(5,21+3," ");
	l1.setCellFormat(R_L_T);
	wreportsheet.addCell(l1);
	
	l1=new jxl.write.Label(6,21+3," ");
	l1.setCellFormat(L_RT);
	wreportsheet.addCell(l1);
	
	l1=new jxl.write.Label(4,23+3,toString(fistn[0],5,0));
    l1.setCellFormat(BL);
	wreportsheet.addCell(l1); 

	l1=new jxl.write.Label(5,23+3,toString(fistn[1],5,0));
    l1.setCellFormat(B);
	wreportsheet.addCell(l1); 

	l1=new jxl.write.Label(6,23+3,toString(fistn[2],5,0));
    l1.setCellFormat(BR);
	wreportsheet.addCell(l1);

    l1=new jxl.write.Label(0,8,ModelNo);
    l1.setCellFormat(L);
	wreportsheet.addCell(l1);

	//Date now = Calendar.getInstance().getTime(); 
    //jxl.write.DateFormat customDateFormat = new jxl.write.DateFormat ("dd.mm.yyyy"); 
    //WritableCellFormat dateFormat = new WritableCellFormat (customDateFormat); 
    //dateFormat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
    //DateTime dateCell = new DateTime(1, 4, now, dateFormat); 
    //dateFormat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
    //dateFormat.setAlignment(jxl.format.Alignment.LEFT);
    //dateCell.setCellFormat(dateFormat);
    
    //wreportsheet.addCell(dateCell);
    // write 
    // dateCell = new DateTime(2, 8,datec2, dateFormat); 
    // dateFormat.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
    // dateCell.setCellFormat(dateFormat);
    // wreportsheet.addCell(dateCell);
     
workbook1.write();
workbook1.close(); 
}catch(jxl.write.WriteException we) {System.out.println("write exception");}
}catch(jxl.read.biff.BiffException be) {System.err.println("BiffException");}  
}
}