#include "LH.h"
#include <stdio.h>
#include <stdlib.h>

static FILE *yout;
static char fn[]="y.out";
static FILE *partime;


void partimeprint(double *beta, int no_linpar, double sigma, double alpha, double logfbet, double ssz)
{ 
  int l;

  for (l=0; l<no_linpar; l++)
    fprintf(partime,"%f ",beta[l]);
  fprintf(partime,"%f %f\n",sigma,alpha);
  fflush(partime);
}

void print_y(fftstruct *y, double sigma, double *beta, int no_linpar, double alpha, double *linpred)
{
  int i, j;

  for (i=0; i<no_linpar; i++)
    fprintf(yout,"%e ",beta[i]);
  fprintf(yout,"%e %e ",sigma,alpha);
  for (i=0; i<Morig; i++)
    for (j=0; j<Norig; j++)
      fprintf(yout,"%f ",sigma*(*y).x[index(i,j)][0]+linpred[index_orig(i,j)]);
  fprintf(yout,"\n");
  fflush(yout);
}

void init_print(int newrun)
{
  yout=fopen(fn,"w");
  if (newrun)
    partime=fopen("partime.out","w");
  else
    partime=fopen("partime.out","a");
}

void close_print()
{
  fclose(yout);
  fclose(partime);
}



void print_results(fftstruct *z, double *meany, double *meanlambda, double *ssy, double *sslambda, double *meanlinpred, int itr, int no_linpar, double sigma, double alpha, double alpha_low, double alpha_up, double beta[], double *residuals){
char fn[40];
FILE *udfil;
int j;

     sprintf(fn,"z.out");
     udfil=fopen(fn,"w");
     for (j=0; j<NM; j++)
       fprintf(udfil,"%e ",(*z).x[j][0]);
     fclose(udfil);

     
     sprintf(fn,"meany.out");
     udfil=fopen(fn,"w");
     for (j=0; j<NMorig; j++)
       fprintf(udfil,"%e ",meany[j]);
     fclose(udfil);



     sprintf(fn,"meanlambda.out");
     udfil=fopen(fn,"w");
     for (j=0; j<NMorig; j++)
       fprintf(udfil,"%e ",meanlambda[j]);
     fclose(udfil);

     sprintf(fn,"ssy.out");
     udfil=fopen(fn,"w");
     for (j=0; j<NMorig; j++)
       fprintf(udfil,"%e ",ssy[j]);
     fclose(udfil);

     sprintf(fn,"sslambda.out");
     udfil=fopen(fn,"w");
     for (j=0; j<NMorig; j++)
       fprintf(udfil,"%e ",sslambda[j]);
     fclose(udfil);     

     sprintf(fn,"meanlinpred.out");
     udfil=fopen(fn,"w");
     for (j=0; j<NMorig; j++)
       fprintf(udfil,"%e ",meanlinpred[j]);
     fclose(udfil);      
     
     sprintf(fn,"residuals.out");
     udfil=fopen(fn,"w");
     for (j=0; j<NMorig; j++)
       fprintf(udfil,"%e ",residuals[j]);
     fclose(udfil); 

     sprintf(fn,"parms.out");
     udfil=fopen(fn,"w");
     fprintf(udfil,"%d %e %e %e %e\n",no_linpar,sigma,alpha,alpha_low,alpha_up);
     fclose(udfil);

     sprintf(fn,"beta.out");
     udfil=fopen(fn,"w");
     for (j=0; j<no_linpar; j++)
       fprintf(udfil,"%e ",beta[j]);
     fclose(udfil);     

     udfil=fopen("startitr.out","w");
     fprintf(udfil,"%d\n",itr);
     fclose(udfil);
}

void printvariables(double sigma2, double logfz, double sum_obsy, double sum_expy, fftstruct * y, fftstruct * gradz, fftstruct * z){

int j;

printf("sum_obsy, sum_expy, logfz, sigma2: %f %f %f %f\n",sum_obsy,sum_expy,logfz,sigma2);
printf("y: ");
for (j=0; j<NM; j++)
   printf("%2.3f ",(*y).x[j][0]);
printf("\n");
printf("g: ");
for (j=0; j<NM; j++)
   printf("%2.3f ",(*gradz).x[j][0]);
printf("\n");
printf("z: ");
for (j=0; j<NM; j++)
   printf("%2.3f ",(*z).x[j][0]);
printf("\n");

}

void printvariables2(double sigma, fftstruct * y, Doublearray linpred)
{
  FILE *outfile;
  int j;

  printf("sigma=%f\n",sigma);

  outfile=fopen("y2.out","w");
  for (j=0; j<NM; j++)
    fprintf(outfile,"%e ",(*y).x[j][0]);
  fclose(outfile);
  
  outfile=fopen("linpred.out","w");
  for (j=0; j<NMorig; j++)
    fprintf(outfile,"%e ",linpred[j]);
  fclose(outfile);

}

/* void printvariables3(fftstruct *y, fftstruct *yprop, fftstruct *z, fftstruct *zprop, fftstruct *gradz, fftstruct *gradzprop, fftstruct *S, fftstruct *Sprop, Doublearray linpred, Doublearray linpredprop)
{
  FILE *outfile;
  int j;

  outfile=fopen("ylinpred.out","w");

  for (j=0;j<NM; j++)
    y[j][0]=1;
  for (j=0;j<NM; j++)
    yprop[j][0]=2;
  for (j=0;j<NM; j++)
    z[j][0]=3;
  for (j=0;j<NM; j++)
    zprop[j][0]=4;
 for (j=0;j<NM; j++)
    gradz[j][0]=5;
  for (j=0;j<NM; j++)
    gradzprop[j][0]=6;
 for (j=0;j<NM; j++)
    S[j][0]=7;
  for (j=0;j<NM; j++)
    Sprop[j][0]=8;
for (j=0;j<NMorig; j++)
    linpred[j]=9;
for (j=0;j<NMorig; j++)
    linpredprop[j]=10;

  for (j=0; j<NM; j++)
    fprintf(outfile,"%e ",y[j][0]);
  fprintf(outfile,"\n");
  for (j=0; j<NM; j++)
    fprintf(outfile,"%e ",yprop[j][0]);
  fprintf(outfile,"\n");
  for (j=0; j<NM; j++)
    fprintf(outfile,"%e ",z[j][0]);
  fprintf(outfile,"\n");
  for (j=0; j<NM; j++)
    fprintf(outfile,"%e ",zprop[j][0]);
  fprintf(outfile,"\n");
  for (j=0; j<NM; j++)
    fprintf(outfile,"%e ",gradz[j][0]);
  fprintf(outfile,"\n");
  for (j=0; j<NM; j++)
    fprintf(outfile,"%e ",gradzprop[j][0]);
  fprintf(outfile,"\n");
  for (j=0; j<NM; j++)
    fprintf(outfile,"%e ",S[j][0]);
  fprintf(outfile,"\n");
  for (j=0; j<NM; j++)
    fprintf(outfile,"%e ",Sprop[j][0]);
  fprintf(outfile,"\n");

 

  for (j=0; j<NMorig; j++)
    fprintf(outfile,"%e ",linpred[j]);
fprintf(outfile,"\n");
  fclose(outfile);

}


*/
