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

static double deltax, deltay;
static double xmin, ymin, xmax, ymax;

double geodist(int i, int j){
  return sqrt(pow(min(i,M-i)*deltax,2)+pow(min(j,N-j)*deltay,2));
}

/*
void ys_print(FILE *timeout, doublecomplex *y)
{
  double coordx[5]={ 75.17690 , 83.97651 , 70.21302 , 13.80527, 135.64601};
  double coordy[5]={ 85.141006,  7.278854 ,50.581871, 49.332745, 49.749119 };
  int i;
  
  for (i=0; i<5; i++)
    fprintf(timeout,"%f ",y[index(cellcoordx(xmax,coordx[i]-xmin),cellcoordy(ymax,coordy[i]-ymin))].r);
  fprintf(timeout,"\n");
  
}

void no_points_y(Doublearray obsdata){
  FILE *outfile;
  double coordx[5]={ 75.17690 , 83.97651 , 70.21302 , 13.80527, 135.64601};
  double coordy[5]={ 85.141006,  7.278854 ,50.581871, 49.332745, 49.749119 };
  int i;
  char fn[40];

  sprintf(fn,"no_points_in_ycells.out");
  outfile=fopen(fn,"w");

  for (i=0; i<5; i++)
    fprintf(outfile,"%f ",obsdata[index_orig(cellcoordx(xmax,coordx[i]-xmin),cellcoordy(ymax,coordy[i]-ymin))]);  
  fclose(outfile);

}
*/


void read_points_X_scale(Doublearray obsdata, Doublearray X, int *is_observed, int nolinpar, double *popdensity, double *cellareas)
{ 
  double pointsx,pointsy;
  char fn[200]; 
  /* NB 200 er vigtigt jvf lange filnavne nedenfor */
  FILE *infile; 
  int i, j, k; 
  int noframes; 
  double *llx, *lly, *urx, *ury;


  sprintf(fn,"frames.in");
  infile=fopen(fn,"r");
  if (infile==NULL){
    printf("problems with reading corners\n");
    exit(1);
  }
  fscanf(infile,"%d",&noframes);
  fscanf(infile,"%lf%lf%lf%lf",&xmin,&ymin,&xmax,&ymax);
  
  llx=malloc(sizeof(double)*noframes); lly=malloc(sizeof(double)*noframes);
  urx=malloc(sizeof(double)*noframes); ury=malloc(sizeof(double)*noframes);
  for (i=0; i<noframes; i++)
    fscanf(infile,"%lf%lf%lf%lf",&(llx[i]),&(lly[i]),&(urx[i]),&(ury[i]));
  fclose(infile); 

  deltax=xmax/Morig;
  deltay=ymax/Norig;

  printf("deltax and deltay: %f %f\n",deltax,deltay);


  for (i=0; i<NMorig; i++)
    obsdata[i]=is_observed[i]=cellareas[i]=0;
  for (i=0; i<NMorig; i++)
    popdensity[i]=1;

  /* We now read points into obsdata  */

  sprintf(fn,"points.in");
  infile=fopen(fn,"r");
  if (infile==NULL){
    printf("problems with reading points\n");
    exit(1);
  } 
  fscanf(infile,"%d",&NOPOINTS);

  for (i=0; i<NOPOINTS; i++){
    fscanf(infile,"%lf%lf",&pointsx,&pointsy);
    obsdata[index_orig(cellcoordx(xmax,pointsx-xmin),cellcoordy(ymax,pointsy-ymin))]++;
  }
  fclose(infile);

  /* check results  */
  sprintf(fn,"obsdata.dat");
  infile=fopen(fn,"w");
  for (i=0; i<NMorig; i++)
    fprintf(infile,"%lf ",obsdata[i]);
  fprintf(infile,"\n");
  fclose(infile);  

  /* we now create design matrix x containing covariates.  */   
  /* first column contains ones  */
  sprintf(fn,"X.in");
  infile=fopen(fn,"r");
  if (infile==NULL){
    printf("problems with reading X\n");
    exit(1);
  }  
  for (i=0; i<Morig; i++)
    for (j=0; j<Norig; j++)
      for (k=0; k<nolinpar; k++)
	fscanf(infile,"%lf",&(X[i*Norig*maxlinpar+j*maxlinpar+k]));;
  fclose(infile);
  printf("X[1,]: %e %e %e\n,",X[0],X[1],X[2]);
  /* compute cellareas og is_observed */
  for (i=0; i<Morig; i++)
    for (j=0; j<Norig; j++){
      for (k=0; k<noframes; k++)
	cellareas[index_orig(i,j)]+=overlap(i,j,llx[k],lly[k],urx[k],ury[k]);
      if (cellareas[index_orig(i,j)]>0)
	is_observed[index_orig(i,j)]=1;
    }

  

  sprintf(fn,"cellareas_isobserved.dat");
  infile=fopen(fn,"w");
  for (i=0; i<NMorig; i++)
    fprintf(infile,"%d %f\n",is_observed[i],cellareas[i]);
  fclose(infile);
  /* no_points_y(obsdata); */
}

double overlap(int i, int j, double llx, double lly, double urx, double ury)
{
  double overlap1;
  double overlap2;
  /* printf("i %d j %d deltax %f deltay %f llx: %f, urx: %f lly: %f ury: %f cllx: %f curx %f clly %f, cury %f\n",i,j,deltax,deltay,llx,urx,lly,ury,j*deltax,(j+1)*deltax,i*deltay,(i+1)*deltay); */
  overlap1=MIN(urx,(i+1)*deltax)-MAX(llx,i*deltax);
  overlap2=MIN(ury,(j+1)*deltay)-MAX(lly,j*deltay);
  return MAX(overlap1,0.0)*MAX(overlap2,0.0);
}


int cellcoordx(double xmax, double point){

  if (xmax<=point){
     printf("error: xmax<=point\n");
     exit(1);
  }
  return (int) floor(point/deltax);
}
int cellcoordy(double ymax, double point){

  if (ymax<=point){
     printf("error: ymax<=point\n");
     exit(1);
  }
  return (int) floor(point/deltay);
}


