void main(int argc, char **argv)     /* commandline arguments */
{
  FILE *fpprcp, *fptmax, *fptmin, *fpflist, *fpout;   /* filepointers used */
  char prcp[BUFSIZ+1];  char tmaxc[BUFSIZ+1];  char tminc[BUFSIZ+1]; /* strings holding filenames */
  char elev_mask[BUFSIZ+1];   /* string holding filename */
  char out_dir[BUFSIZ+1];     /* string holding out directory */
  char old_out_dir[BUFSIZ+1]; /* string holding the original output directory */
  char s1[BUFSIZ+1];              /* dummy string used to read strings */
  int cells, steps, tempsteps;
  int i,j,k,l, binflag;                    /* counters used in loops */
  float *prec,*tmax,*tmin,***BIGA;
  short int *precip,*tempmax,*tempmin,***BIG;
  float BIG_temp;

  if (argc<7) {           /* Must be exactly 6 arguments behind the program name */
  printf("Not correct number of commandline arguments \n");
  printf("vicinput \"prcp\" \"tmax\" \"tmin\" \"elev_mask.txt\" \"out_dir\" \n"); 
  exit(EXIT_FAILURE); }
  strcpy(prcp,argv[1]);        printf("%s \n",prcp);
  strcpy(tmaxc,argv[2]);        printf("%s \n",tmaxc);
  strcpy(tminc,argv[3]);        printf("%s \n",tminc);
  strcpy(elev_mask,argv[4]);   printf("%s \n",elev_mask);
  strcpy(out_dir,argv[5]); strcpy(old_out_dir,out_dir);printf("%s \n",old_out_dir);
  binflag=atoi(argv[6]);        printf("%d \n",binflag);

    /*--------------------------------------------------*/
    /* ALLOCATE MEMORY TO ARRAYS BASED ON BINARY STATUS */
    /*--------------------------------------------------*/
  if(binflag!=1) {
    if(!(prec = (float*) calloc(VALID_CELLS,sizeof(float)))) {
      printf("Memory Allocation error: PREC\n"); exit(8); }
    if(!(tmax = (float*) calloc(VALID_CELLS,sizeof(float)))) {
      printf("Memory Allocation error: TMAX\n"); exit(8); }
    if(!(tmin = (float*) calloc(VALID_CELLS,sizeof(float)))) {
      printf("Memory Allocation error: TMIN\n"); exit(8); }
    if(!(BIGA = (float***) calloc(3,sizeof(float**)))) {
      printf("Memory Allocation Error: BIGA\n"); exit(8); }
    for(j=0; j<3;j++) {
      if(!(BIGA[j] = (float**) calloc(stp50,sizeof(float*)))) {
	printf("Memory Allocation Error: BIGA\n");exit(8); }
      for(i=0; i<stp50;i++) {
	if(!(BIGA[j][i] = (float*) calloc(VALID_CELLS,sizeof(float)))) {
	  printf("Memory Allocation Error: BIGA\n"); exit(8); } } }
  }
  else {
    if(!(precip = (short int*) calloc(VALID_CELLS,sizeof(short int)))) {
      printf("Memory Allocation error: PREC\n"); exit(8); }
    if(!(tempmax = (short int*) calloc(VALID_CELLS,sizeof(short int)))) {
      printf("Memory Allocation error: TMAX\n"); exit(8); }
    if(!(tempmin = (short int*) calloc(VALID_CELLS,sizeof(short int)))) {
      printf("Memory Allocation error: TMIN\n"); exit(8); }
    if(!(BIG = (short int***) calloc(3,sizeof(short int**)))) {
      printf("Memory Allocation Error: BIGA\n"); exit(8); }
    for(j=0; j<3;j++) {
      if(!(BIG[j] = (short int**) calloc(stp50,sizeof(short int*)))) {
	printf("Memory Allocation Error: BIGA\n");exit(8); }
      for(i=0; i<stp50;i++) {
	if(!(BIG[j][i] = (short int*) calloc(VALID_CELLS,sizeof(short int)))) {
	  printf("Memory Allocation Error: BIGA\n"); exit(8); } } }
  }
  /*------------------------------------------------------------*/

  if (valid_cells(elev_mask)>VALID_CELLS) {   /* Check if array is big enough */
    printf("Number of valid cells = %d \n",valid_cells(elev_mask));
    printf("Change VALID_CELLS to %d in source\n",(valid_cells(elev_mask)+1));
    printf("Then recompile\n"); }
  else printf("Number of valid cells = %d \n",valid_cells(elev_mask));

  make_flist(elev_mask);    /* Generates a filelist that will be used later  */
  make_outfiles(old_out_dir,binflag); /* Open files in write mode, closes them again */
  cells = valid_cells(elev_mask); /* Calculates the number of valid cells in basin */
  printf("cells  = %d \n",cells);
  steps = nr_timesteps(prcp,cells,binflag);  
  printf("timesteps = %d \n",steps);

  if((fpflist = fopen(flist,"r"))==NULL) {             /* latlong99.txt  */
    printf("Cannot open file %s \n",flist);exit(0);} 
  if (binflag!=1) {
    if((fpprcp = fopen(prcp,"r"))==NULL){       /* prcp.grd after rescale */
      printf("Cannot open file %s \n",prcp);exit(0);}
    if((fptmax = fopen(tmaxc,"r"))==NULL){             /* tmax.grd      */
      printf("Cannot open file %s \n",tmaxc);exit(0);}
    if((fptmin = fopen(tminc,"r"))==NULL){              /* tmin.grd    */
      printf("Cannot open file %s \n",tminc);exit(0);} 
  }
  else { /* OPEN FILES FOR BINARY READ */
  if((fpprcp = fopen(prcp,"rb"))==NULL) {       /* prcp.grd after rescale */
    printf("Cannot open file %s \n",prcp);exit(0);}
  if((fptmax = fopen(tmaxc,"rb"))==NULL) {           /* tmax.grd      */
    printf("Cannot open file %s \n",tmaxc);exit(0);} 
  if((fptmin = fopen(tminc,"rb"))==NULL) {           /* tmin.grd    */
    printf("Cannot open file %s \n",tminc);exit(0);} 
  }

/***** RUNNING the remainder of steps/50     ****************************/  
  tempsteps = steps%50; /* running timesteps then write to files */
  printf("timestep %d \n",tempsteps);
  for (j=0;j<tempsteps;j++)
    {
      if(binflag!=1) {
	one_tstp_ascii(fpprcp,fptmax,fptmin,prec,tmax,tmin,cells);/* 1 timestep */ 
	for (i=0;i<cells;i++) {
	  BIGA[0][j][i]=prec[i];
	  BIGA[1][j][i]=tmax[i];
	  BIGA[2][j][i]=tmin[i]; }
      }
      else {
	one_tstp(fpprcp,fptmax,fptmin,precip, tempmax, tempmin,cells);/* 1 timestep */ 
	      for (i=0;i<cells;i++) {           /* putting the numbers into BIG array */
		BIG[0][j][i]=precip[i];
		BIG[1][j][i]=tempmax[i];
		BIG[2][j][i]=tempmin[i]; 
	      }
     }
    }

    for(k=0;k<cells;k++)    /* How many files to run */
      {
      fscanf(fpflist,"%s",s1);    
      strcpy(out_dir, old_out_dir);
      strcat(out_dir, s1);

      if(binflag!=1) {if((fpout = fopen(out_dir,"a"))==NULL){
	printf("Cannot open file %s \n",out_dir);exit(0);}}
      else  {if((fpout = fopen(out_dir,"ab"))==NULL){
	printf("Cannot open file %s \n",out_dir);exit(0);} }

      for (i=0;i<tempsteps;i++) {  /* BIG [unit][timestep][gridcell] */
	if(binflag!=1) BIG_temp = BIGA[0][i][k];
	else BIG_temp = (float) BIG[0][i][k];
	if(BIG_temp<0.00) {
	  fprintf(stderr,"neg. prcp reset to 0 \n");
	  if(binflag!=1) BIGA[0][i][k] = 0;
	  else BIG[0][i][k] = 0; }
	if(binflag!=1)
	  fprintf(fpout,"%4.2f  %4.2f  %4.2f\n",
		  BIGA[0][i][k],BIGA[1][i][k],BIGA[2][i][k]);
	else { /* WRITE OUT IN BINARY FORMAT */
	  fwrite(&BIG[0][i][k],sizeof(short int),1,fpout);
	  fwrite(&BIG[1][i][k],sizeof(short int),1,fpout);
	  fwrite(&BIG[2][i][k],sizeof(short int),1,fpout);
	}
      }
      fclose(fpout);
      }
  rewind(fpflist);
/****** END running the remainder of steps/50  ********************/

/***** RUNNING steps/50     ****************************/  
  if (steps>50) 
    {
    tempsteps = steps/stp50;
    for (l=0;l<tempsteps;l++) {
      printf(" timestep %d \n",(steps%50 + (1+l)*50));
      for (j=0;j<stp50;j++)
        {
      if(binflag!=1) {
	one_tstp_ascii(fpprcp,fptmax,fptmin,prec, tmax, tmin,cells);
	for (i=0;i<cells;i++) {    /* putting the numbers into BIG array */
	  BIGA[0][j][i]=prec[i];
	  BIGA[1][j][i]=tmax[i];
	  BIGA[2][j][i]=tmin[i]; }
      } 
      else {
	one_tstp(fpprcp,fptmax,fptmin,precip, tempmax, tempmin,cells);
      for (i=0;i<cells;i++) {    /* putting the numbers into BIG array */
	BIG[0][j][i]=precip[i];
	BIG[1][j][i]=tempmax[i];
	BIG[2][j][i]=tempmin[i]; }
      }
    }

      for(k=0;k<cells;k++)    /* How many files to run */
        {
      fscanf(fpflist,"%s",s1);    
      strcpy(out_dir, old_out_dir);
      strcat(out_dir, s1);

      if(binflag!=1) fpout = fopen(out_dir,"a");
      else fpout = fopen(out_dir,"ab");

      for (i=0;i<stp50;i++) {  /* BIG [unit][timestep][gridcell] */
	/*reconvert precip to actual values for check */
	if(binflag!=1) BIG_temp = BIGA[0][i][k];
	else BIG_temp = (float) (BIG[0][i][k]);
	if(BIG_temp<0.00) {
	  //fprintf(stderr,"neg. prcp reset to 0 \n");
	  if(binflag!=1) BIGA[0][i][k] = 0;
	  else BIG[0][i][k] = 0; }
	if(binflag!=1)
	  fprintf(fpout,"%4.2f  %4.2f  %4.2f\n",
		  BIGA[0][i][k],BIGA[1][i][k],BIGA[2][i][k]);
	else { /* WRITE OUT IN BINARY FORMAT */
	  fwrite(&BIG[0][i][k],sizeof(short int),1,fpout);
	  fwrite(&BIG[1][i][k],sizeof(short int),1,fpout);
	  fwrite(&BIG[2][i][k],sizeof(short int),1,fpout);
	}
      }

      fclose(fpout);
      }
     rewind(fpflist);
     }
    }
/****** END running steps/50 **********************************************/

  fclose(fpprcp);  fclose(fptmax);  fclose(fptmin);  fclose(fpflist);
}
示例#2
0
void main(int argc, char *argv[])
{

FILE *fu, *fv, *fposlat, *fposlon, *fm, *fpout,*fpflist,*fpmean;
char ufile[100], vfile[100],str1[200],out_dir[200],mask[200];
char old_out_dir[200], s1[200], maskfile[200],in_dir[200],old_in_dir[200];
char uftemp[100], vftemp[100];
int MASKROWS, MASKCOLS;
float MASKRES, void_nr,high,low, xll, yll, maskulong, maskulat;
int i,j,k,l,t;
long int count;
int lon, lat, min_lat, max_lat, min_lon, max_lon, skipdays;
float **uwnd, **vwnd,sum_wind, **maskx;
int ROWS,COLUMNS,SIZEM;
int twind, ii,cell_no;
int year, start_year, start_mo, end_year, end_mo, days_per_year,active_cells;
double ***wspeed,**cell_wind;
float *latitude, *longitude, uinterp, vinterp, dum;
float reflat, reflon,avg_wind;
double vertdist, reldist, hordist;
float newuleft, newvleft, newuright, newvright;
short int vic_wind, cellavg;

  if(argc!=12)
    {
      printf("\nUsage: %s <start_year> <start_mo> <end_year> <end_mo> <min_lat> <max_lat> <min_lon> <max_lon> <mask> <in_dir> <out_dir>\n",argv[0]);
      printf("See code for more details\n");
      exit(8);
    }

  printf("Assigning command line inputs to variables...\n\n");

   start_year = atoi(argv[1]); printf("start_year %d \n",start_year);
   start_mo = atoi(argv[2]);   printf("start_mo %d \n",start_mo);
   end_year = atoi(argv[3]);   printf("end_year %d \n",end_year);
   end_mo = atoi(argv[4]);     printf("end_mo %d \n",end_mo);
   min_lat = atoi(argv[5]);    printf("gauss_t62 coords:\nmin_lat %d \n",min_lat);
   max_lat = atoi(argv[6]);    printf("max_lat %d \n",max_lat);
   min_lon = atoi(argv[7]);    printf("min_lon %d \n",min_lon);
   max_lon = atoi(argv[8]);    printf("max_lon %d \n",max_lon);
   strcpy(mask,argv[9]);       printf("mask %s \n",mask);
   strcpy(in_dir,argv[10]); strcpy(old_in_dir,in_dir);
   strcpy(out_dir,argv[11]); strcpy(old_out_dir,out_dir); 
                printf("out_dir %s \n\n",old_out_dir);

  printf("Creating output file list %s\n",flist);
  strcpy(maskfile,mask);
  make_flist(mask);     /* Generates a filelist that will be used later  */
  printf("Making Output Files\n");
  make_outfiles(old_out_dir);/* Open all files in write mode and closes them again */
  ROWS=max_lat - min_lat +1; /* rows in reanalysis grid */
  COLUMNS=max_lon - min_lon + 1; /* columns in reanalysis grid */
  if((fpflist = fopen(flist,"r"))==NULL) {  
    printf("Cannot open file %s \n",flist);exit(0);} 

  /*------------------------------------------------------*/
  /* FIRST READ IN HEADER FOR MASK FILE (ARC/INFO STYLE)  */
  /*------------------------------------------------------*/
  if((fm = fopen(maskfile,"r")) == NULL) {
    printf("Cannot open/read mask\n"); exit(0); }
    fscanf(fm,"%*s %s",str1); MASKCOLS = atoi(str1);
    fscanf(fm,"%*s %s",str1); MASKROWS = atoi(str1);
    fscanf(fm,"%*s %s",str1); xll = atof(str1);
    fscanf(fm,"%*s %s",str1); yll = atof(str1);
    fscanf(fm,"%*s %s",str1); MASKRES = atof(str1);
    fscanf(fm,"%*s %s",str1); void_nr = atof(str1);
    high=void_nr+0.001;
    low=void_nr-0.001;

    /* convert to deg. east and make cell centered */
    maskulong = (xll+360.0)+MASKRES/2.0;
    /* upper left corner latitude cell center */
    maskulat = yll+(MASKROWS*MASKRES)-(MASKRES/2.0);
    /* initialize days_per_year for memory allocation */
    days_per_year=366;
    active_cells=0;
    SIZEM = MASKCOLS*MASKROWS;

    /*----------------------------*/
    /* ALLOCATE MEMORY TO ARRAYS  */
    /*----------------------------*/

  if(!(longitude = (float*) calloc(COLUMNS+1,sizeof(float)))) {
    printf("Cannot allocate memory to first record: BASIN\n");
    exit(8); }
  
  if(!(latitude = (float*) calloc(ROWS,sizeof(float)))) {
    printf("Cannot allocate memory to first record: BASIN\n");
    exit(8); }
  
  if(!(uwnd = (float**) calloc(ROWS,sizeof(float*)))) {
    printf("Cannot allocate memory to first record: BASIN\n");
    exit(8); }
  for(i=0; i<ROWS;i++) {
  if(!(uwnd[i]= (float*) calloc(COLUMNS,sizeof(float)))) {
    printf("Cannot allocate memory to first record: BASIN\n");
    exit(8); } }

  if(!(maskx = (float**) calloc(MASKROWS,sizeof(float*)))) {
    printf("Cannot allocate memory to first record: BASIN\n");
    exit(8); }
  for(i=0; i<MASKROWS;i++) {
  if(!(maskx[i] = (float*) calloc(MASKCOLS,sizeof(float)))) {
    printf("Cannot allocate memory to first record: BASIN\n");
    exit(8); } }

 if(!(vwnd = (float**) calloc(ROWS,sizeof(float*)))) {
    printf("Cannot allocate memory to first record: BASIN\n");
    exit(8); }
  for(i=0; i<ROWS;i++) {
  if(!(vwnd[i] = (float*) calloc(COLUMNS,sizeof(float)))) {
    printf("Cannot allocate memory to first record: BASIN\n");
    exit(8); } }
  
  if(!(wspeed = (double***) calloc(days_per_year,sizeof(double**)))) {
    printf("Cannot allocate memory to first record: WSPEED\n");
    exit(8); }
  for(j=0; j<days_per_year;j++) {
    if(!(wspeed[j] = (double**) calloc(MASKROWS,sizeof(double*)))) {
      printf("Cannot allocate memory to first record: BASIN\n");
      exit(8); }
    for(i=0; i<MASKROWS;i++) {
      if(!(wspeed[j][i] = (double*) calloc(MASKCOLS,sizeof(double)))) {
	printf("Cannot allocate memory to first record: BASIN\n");
	exit(8); } } }

  if(!(cell_wind = (double**) calloc(SIZEM,sizeof(double*)))) {
    printf("Cannot allocate memory to first record: CELL_WIND\n");
    exit(8); }
  for(j=0; j<SIZEM;j++) {
    if(!(cell_wind[j] = (double*) calloc(days_per_year,sizeof(double)))) {
      printf("Cannot allocate memory to first record: CELL_WIND\n");
      exit(8); } } 


  /*--------------------------------------------*/
  /* READ IN REMAINDER OF MASK FILE             */
  /*--------------------------------------------*/
    printf("reading mask file\n\n");
  for(i=0; i<MASKROWS;i++) {
    for(j=0; j<MASKCOLS; j++) {
      fscanf(fm,"%f",&maskx[i][j]);
      if(maskx[i][j]>high || maskx[i][j]<low ) active_cells++;
    }
  }
  fclose(fm);
  printf("%d active cells\n",active_cells);

  /*------------------------------------------------------*/
  /* BEGIN LOOP FOR EACH YEAR                             */
  /*------------------------------------------------------*/

  for(year=start_year;year<=end_year;year++) {
    printf("Processing year %d\n",year);
    count = 0; sum_wind = 0.0;

  /* NAMES OF REANALYSIS U-WIND AND V-WIND DATA FILES FOR CURRENT YEAR */

  sprintf(uftemp,"uwnd.%d.asc",year);
  sprintf(vftemp,"vwnd.%d.asc",year);
  strcat(in_dir,uftemp);
  strcpy(ufile,in_dir);
  strcpy(in_dir,old_in_dir);
  strcat(in_dir,vftemp);
  strcpy(vfile,in_dir);
  strcpy(in_dir,old_in_dir);
  printf("files %s and %s\n",ufile, vfile);
  days_per_year= num_days(year,end_year,end_mo);
  printf("days_per_year= %d\n",days_per_year);
  /* if (LEAPYR(year)) */
  /*  days_per_year=366; */
  /* else */
  /*  days_per_year=365; */

  /*--------------------------------------------*/
  /* OPEN REANALYSIS WIND FILES                 */
  /*--------------------------------------------*/

  if((fu = fopen(ufile,"r")) == NULL) {
    printf("Cannot open/read %s\n",ufile);
    exit(8); }

  if((fv = fopen(vfile,"r")) == NULL) {
    printf("Cannot open/read %s\n",vfile);
    exit(8); }

  if((fposlat = fopen(ncar_lat, "r")) == NULL) {
    printf("Cannot open/read %s\n",ncar_lat);
    exit(8); }
  if((fposlon = fopen(ncar_lon, "r")) == NULL) {
    printf("Cannot open/read %s\n",ncar_lon);
    exit(8); }
 
/*  printf("Files successfully opened...\n\n"); */

  /*----------------------------------------------------*/
  /* READ LAT/LON FILES -- POSITION IN REANALYSIS GRID  */
  /*----------------------------------------------------*/
  /* move to starting position for min lat and long (using reanalysis numbers 
             for coordinates in each file */
  for(i=1; i<min_lat;i++) fscanf(fposlat,"%f",&dum);
  for(i=1; i<min_lon;i++) fscanf(fposlon,"%f",&dum);

  for(i=0; i<ROWS;i++) 
    fscanf(fposlat,"%f",&latitude[i]);

  for(i=0; i<COLUMNS+1;i++) 
    fscanf(fposlon,"%f",&longitude[i]);
  
  fclose(fposlat);
  fclose(fposlon);


  /*-----------------------------------------------------------------*/
  /* READ IN REANALYSIS WIND GRID -- RUN FOR ALL DAYS IN ONE YEAR    */
  /*-----------------------------------------------------------------*/
  for(t=0; t<days_per_year;t++) {
      for(i=0; i<ROWS;i++) {
	for(j=0; j<COLUMNS;j++) {
	  fscanf(fu, "%d", &twind);
	  uwnd[i][j]= (float)twind*SCALE + OFFSET;
	  fscanf(fv, "%d", &twind);
	  vwnd[i][j]= (float)twind*SCALE + OFFSET;
	}
      }

      for(j=0; j<MASKROWS; j++) {
	reflat = maskulat - (float)j*MASKRES;
	for(i=0; i<MASKCOLS; i++) {
	  reflon = maskulong + (float)i*MASKRES;
	  lat=lon=9999;

      if(maskx[j][i]>high || maskx[j][i]<low) { /* if active cell,interpolate */

	  for(k=0; k<ROWS;k++) {
	    if(reflat<=latitude[k] && reflat>latitude[k+1]){
	      lat = k;
	    }
	  }
	  for(l=0; l<COLUMNS+1; l++) {
	    if(reflon>=longitude[l] && reflon<longitude[l+1]){
	      lon=l;
	    }
	  }
	  if(lat==9999 || lon==9999) {
	    printf("reflat/lon not in range\n");
	    exit(0);
	  }
	  
	  /*-----------------------------------*/
	  /* VERTICAL INTERPOLATION            */
	  /*-----------------------------------*/
	  vertdist=get_dist(latitude[lat], longitude[lon], latitude[lat+1],
			    longitude[lon]);
	  reldist=get_dist(latitude[lat],longitude[lon], reflat, longitude[lon]);

	  newuleft=((uwnd[lat][lon]*(1-(reldist/vertdist)))+(reldist/vertdist)
	    *uwnd[lat+1][lon]);
	  newvleft=((vwnd[lat][lon]*(1-(reldist/vertdist)))+(reldist/vertdist)
	    *vwnd[lat+1][lon]);

	  if(lon==COLUMNS) {
	     newuright=((uwnd[lat][0]*(1-(reldist/vertdist)))+
		     (reldist/vertdist)*uwnd[lat+1][0]);
	     newvright=((vwnd[lat][0]*(1-(reldist/vertdist)))+
		     (reldist/vertdist)*vwnd[lat+1][0]);
	  }
	  else {
	    newuright=((uwnd[lat][lon+1]*(1-(reldist/vertdist)))+
		       (reldist/vertdist)*uwnd[lat+1][lon+1]);
	    newvright=((vwnd[lat][lon+1]*(1-(reldist/vertdist)))+
		       (reldist/vertdist)*vwnd[lat+1][lon+1]);
	  }
	  /*-----------------------------------*/
	  /* HORIZONTAL INTERPOLATION          */
	  /*-----------------------------------*/

	  hordist=get_dist(reflat,longitude[lon],reflat,longitude[lon+1]);
	  reldist=get_dist(reflat,longitude[lon],reflat,reflon);

	  uinterp=newuleft*(1-(reldist/hordist)) + newuright*(reldist/hordist);
	  vinterp=newvleft*(1-(reldist/hordist)) + newvright*(reldist/hordist);
	  wspeed[t][j][i] = sqrt((double) (uinterp*uinterp + vinterp*vinterp));
      } /* end of if statement for interpolation only for active cell */
	} /* end loop for MASKCOLS */ 
      } /* end loop for MASKROWS */
  }    /* end loop for days per year */

  /*--------------------------------------------------*/
  /* OUTPUT TIMESERIES FOR EACH CELL IN MASK          */
  /*--------------------------------------------------*/
  printf("Writing output\n");
  skipdays=num_days( year, start_year, start_mo-1);
  if(year!=start_year) skipdays=0;
  printf("skipdays = %d\n",skipdays);
  cell_no=0;
  for(j=0; j<MASKROWS; j++) {
    for(i=0; i<MASKCOLS; i++) {
      if( maskx[j][i]>high || maskx[j][i]<low ) { /* if active cell, write */
	    fscanf(fpflist,"%s",s1);    
	    strcpy(out_dir,old_out_dir);
	    strcat(out_dir,s1);          /* create file name for grid cell */
	    if((fpout = fopen(out_dir,"ab"))==NULL) {
              printf("error opening output file %s\n",out_dir);exit(0);}
	    /* convert wind to short int values -- multiplying by 100 */
	    for(t=skipdays;t<days_per_year;t++) {
	      sum_wind += wspeed[t][j][i]; count+=1; /* calc sum for gross average */
	      vic_wind= (short int) (wspeed[t][j][i]*100);
	      fwrite(&vic_wind,sizeof(short int),1,fpout);
	    }
	    ii=skipdays;
	    for(t=skipdays;t<days_per_year;t++) {  /*avg daily wind for each cell */
      	      if( !(LEAPYR(year)) || t !=59){ /* skip feb 29 data for daily avg */
		cell_wind[cell_no][ii]+=wspeed[t][j][i]; /* for each active cell, sum for each day */
		ii++;}
	    }
	    cell_no++;
      fclose(fpout);
      }
    }
  }
  avg_wind = sum_wind/(float)count; /* gross average wind speed for year */
  printf("Average Daily Wind Speed Year %d = %.2f m/s\n\n",year,avg_wind);
  rewind(fpflist);

 fclose(fu); /* close reanalysis data files for the current year */
 fclose(fv);
  }  /* end year loop */
 /* write file with 365 daily averages for each cell */
  if((fpmean = fopen("cell_avg.out","wb")) == NULL) {
    printf("Cannot open cell_avg.out\n"); exit(0); }
  for(i=0;i<active_cells;i++){
    for(j=0;j<365;j++){
              cellavg = (short int) (cell_wind[i][j]/(end_year-start_year+1)*100);
	      fwrite(&cellavg,sizeof(short int),1,fpmean);
    }
  }
  fclose(fpmean);

} /* end main program */