/*************************************************************************
  Program:     readclim.c
  --------
  Description:
  Read meteorological data

  Written for application to China  by:  Gang Mo
  Last update:	Spet. 2008
*****************************************************************************/

#include <stdio.h>
#include <string.h>
#include "beps.h"

void readclim(lin,jday,sdat0,rcode)

int lin;		/* Line number */
int jday;		/* Julian Day */
struct climatedata *sdat0;	/* climate data */
int short *rcode;
{

    FILE *prad,*ptmx,*ptmi,*phum,*pprc; 
	char radfn[99],tmxfn[99],tmifn[99],humfn[99],prcfn[99];
	int bytes2=2;	/* Number of bytes for a int short data type */
    long int ptr,nitems;
	int short *rv,*mx,*mi,*hv,*pv;
	int pixel;

    rv=(int short *)malloc(npixels*sizeof(int short)); 
    mx=(int short *)malloc(npixels*sizeof(int short)); 
    mi=(int short *)malloc(npixels*sizeof(int short)); 
    hv=(int short *)malloc(npixels*sizeof(int short)); 
    pv=(int short *)malloc(npixels*sizeof(int short)); 

  	sprintf(radfn,"%s%d.bin",radf,jday);
  	sprintf(tmxfn,"%s%d.bin",tmxf,jday);
  	sprintf(tmifn,"%s%d.bin",tmif,jday);
  	sprintf(humfn,"%s%d.bin",humf,jday);
  	sprintf(prcfn,"%s%d.bin",prcf,jday);

/*	Open meteo. data file */
	if ((prad=fopen(radfn,"rb")) == NULL) {
          printf("\n Unable to open file <%s>,  exitting program ...\n\n",
							radfn);
          *rcode = ERROR;
	  return;
        }
	if ((ptmx=fopen(tmxfn,"rb")) == NULL) {
          printf("\n Unable to open file <%s>,  exitting program ...\n\n",
							tmxfn);
          *rcode = ERROR;
	  return;
        }
	if ((ptmi=fopen(tmifn,"rb")) == NULL) {
          printf("\n Unable to open file <%s>,  exitting program ...\n\n",
							tmifn);
          *rcode = ERROR;
	  return;
        }
	if ((phum=fopen(humfn,"rb")) == NULL) {
          printf("\n Unable to open file <%s>,  exitting program ...\n\n",
							humfn);
          *rcode = ERROR;
	  return;
        }
	if ((pprc=fopen(prcfn,"rb")) == NULL) {
          printf("\n Unable to open file <%s>,  exitting program ...\n\n",
							prcfn);
          *rcode = ERROR;
	  return;
        }

    ptr=sizeof(int short)*(lin*npixels);

  	fseek(prad,ptr,0);
  	fread(&rv[0],sizeof(int short),nitems=npixels,prad);
  	fseek(ptmx,ptr,0);
  	fread(&mx[0],sizeof(int short),nitems=npixels,ptmx);
  	fseek(ptmi,ptr,0);
  	fread(&mi[0],sizeof(int short),nitems=npixels,ptmi);
  	fseek(phum,ptr,0);
  	fread(&hv[0],sizeof(int short),nitems=npixels,phum);
  	fseek(pprc,ptr,0);
  	fread(&pv[0],sizeof(int short),nitems=npixels,pprc);


/*	  Assign defualt values to pixels */
      for (pixel=0;  pixel<npixels; pixel++) {
	    sdat0[pixel].srad = 10.0*rv[pixel];
	    sdat0[pixel].tmax = 0.1*mx[pixel];
	    sdat0[pixel].tmin = 0.1*mi[pixel];
	    sdat0[pixel].precip = 0.1*pv[pixel];
		sdat0[pixel].dewp= 0.1*hv[pixel];
	  }

	  fclose(prad);
	  fclose(ptmx);
	  fclose(ptmi);
	  fclose(pprc);
	  fclose(phum);
	  
	  free(rv);
	  free(mx);
	  free(mi);
	  free(hv);
	  free(pv);

	  return;
}

