/*************************************************************************
  Function:     readlai.c
  --------
  Description:
  -----------
 	Read LAI files
***************************************************************************
  CCRS (EMS/Applications Division) 
  Written by:   J. Liu
  Modified for application to China  by:  Gang Mo
  Last update:	Spet. 2008
*****************************************************************************/

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

void readlai(line,lai,jday,rcode)
int long line;
double *lai;
int short jday;
int short *rcode;
{
	FILE *infile;
	int long pix;		
  	int long ptr;
  	int long size;
  	int long nitems;
  	unsigned char filename[255];
  	unsigned char suffix[5];
    unsigned char* laiimg;
	laiimg=(unsigned char *)malloc(npixels*sizeof(unsigned char)); 
    
  	if (jday>=1 && jday<11) strcpy(suffix,"1.bin");
  	if (jday>=11 && jday<21) strcpy(suffix,"2.bin");
  	if (jday>=21 && jday<32) strcpy(suffix,"3.bin");
  	if (jday>=32 && jday<42) strcpy(suffix,"4.bin");
  	if (jday>=42 && jday<52) strcpy(suffix,"5.bin");
  	if (jday>=52 && jday<60) strcpy(suffix,"6.bin");
  	if (jday>=60 && jday<70) strcpy(suffix,"7.bin");
  	if (jday>=70 && jday<80) strcpy(suffix,"8.bin");
  	if (jday>=80 && jday<91) strcpy(suffix,"9.bin");
  	if (jday>=91 && jday<101) strcpy(suffix,"10.bin");
  	if (jday>=101 && jday<111) strcpy(suffix,"11.bin");
  	if (jday>=111 && jday<121) strcpy(suffix,"12.bin");
  	if (jday>=121 && jday<131) strcpy(suffix,"13.bin");
  	if (jday>=131 && jday<141) strcpy(suffix,"14.bin");
  	if (jday>=141 && jday<152) strcpy(suffix,"15.bin");
  	if (jday>=152 && jday<162) strcpy(suffix,"16.bin");
  	if (jday>=162 && jday<172) strcpy(suffix,"17.bin");
  	if (jday>=172 && jday<182) strcpy(suffix,"18.bin");
  	if (jday>=182 && jday<192) strcpy(suffix,"19.bin");
  	if (jday>=192 && jday<202) strcpy(suffix,"20.bin");	
  	if (jday>=202 && jday<213) strcpy(suffix,"21.bin");
  	if (jday>=213 && jday<223) strcpy(suffix,"22.bin");
  	if (jday>=223 && jday<233) strcpy(suffix,"23.bin");
  	if (jday>=233 && jday<244) strcpy(suffix,"24.bin");
  	if (jday>=244 && jday<254) strcpy(suffix,"25.bin");
	if (jday>=254 && jday<264) strcpy(suffix,"26.bin");
  	if (jday>=264 && jday<274) strcpy(suffix,"27.bin");
 	if (jday>=274 && jday<284) strcpy(suffix,"28.bin");    
  	if (jday>=284 && jday<294) strcpy(suffix,"29.bin"); 
 	if (jday>=294 && jday<305) strcpy(suffix,"30.bin");  	
  	if (jday>=305 && jday<315) strcpy(suffix,"31.bin");
  	if (jday>=315 && jday<325) strcpy(suffix,"32.bin");
  	if (jday>=325 && jday<335) strcpy(suffix,"33.bin");
  	if (jday>=335 && jday<345) strcpy(suffix,"34.bin");
  	if (jday>=345 && jday<355) strcpy(suffix,"35.bin");
  	if (jday>=355 && jday<=366) strcpy(suffix,"36.bin");

  	sprintf(filename, "%s%s",leafareaf,suffix);
	/*      Open LAI file */
        if ((infile=fopen(filename, "rb")) == NULL) {
          printf("\n Unable to open file <%s>,  exitting program ...\n\n",
                                                filename);
          *rcode = ERROR;
          return;
        }
  
	ptr=line*npixels;

	/* number of bytes from the beginning of the infile to the specific 	
	location, pix starts from 0, & lin starts from 1 */
  	fseek(infile,ptr,0);
  	fread(&laiimg[0],size=1,nitems=npixels,infile);
  	for (pix=0;pix<npixels;pix++)
	{
	    lai[pix]=laiimg[pix]*0.1;
	    if (lai[pix]==0) lai[pix]=0.01;
    }

	fclose(infile);
    free(laiimg);
}
