/*
 * SUMMARY:      InitInterpolationWeights.c - Initialize interpolation
 * USAGE:        Part of DHSVM
 *
 * AUTHOR:       Bart Nijssen
 * ORG:          University of Washington, Department of Civil Engineering
 * E-MAIL:       nijssen@u.washington.edu
 * ORIG-DATE:    Apr-96
 * Last Change: Wed Jan 27 23:03:10 1999 by Bart Nijssen <nijssen@u.washington.edu>
 * DESCRIPTION:  Initialize the interpolation weights
 * DESCRIP-END.
 * FUNCTIONS:    InitInterpolationWeights()
 * COMMENTS:     
 */

#ifndef lint
static char vcid[] = "$Id: InitInterpolationWeights.c,v 1.2 2001/03/31 00:10:26 waichler Exp $";
#endif /* lint */

#include <stdio.h>
#include <stdlib.h>
#include "settings.h"
#include "data.h"
#include "DHSVMerror.h"
#include "functions.h"
#include "constants.h"
#include "readweights.h"

/*****************************************************************************
  InitInterpolationWeights()
*****************************************************************************/

void InitInterpolationWeights(MAPSIZE *Map, OPTIONSTRUCT *Options, 
			      TOPOPIX **TopoMap, unshort ****MetWeights, 
			      METLOCATION *Stats, int NStats, 
                  LISTPTR Input, WEIGHT_INFO ***WeightInfo, WEIGHT_FILES *WeightFiles,
                  int Month)
{
  const char *Routine = "InitInterpolationWeights";
  uchar **BasinMask;
  int x;			/* counter */
  int y;			/* counter */
  int i;          

  
  if (Options->MM5 == TRUE) {
    if (!((*MetWeights) = (unshort ***) calloc(Map->NY, sizeof(unshort **))))
      ReportError("CalcWeights()", 1);
    
    for (y = 0; y < Map->NY; y++) 
      if (!((*MetWeights)[y] = (unshort **) calloc(Map->NX, sizeof(unshort *))))
	ReportError("CalcWeights()", 1);
    
    for (y = 0; y < Map->NY; y++) 
      for (x = 0; x < Map->NX; x++)
	(*MetWeights)[y][x] = NULL;
  }
  else {
    if (!(BasinMask = (uchar **) calloc(Map->NY, sizeof(uchar *))))
      ReportError((char *) Routine, 1);
    for (y = 0; y < Map->NY; y++) {
      if (!(BasinMask[y] = (uchar *) calloc(Map->NX, sizeof(uchar))))
	ReportError((char *) Routine, 1);
    }
    
    for (y = 0; y < Map->NY; y++)
      for (x = 0; x < Map->NX; x++) 
	    BasinMask[y][x] = TopoMap[y][x].Mask;
    
    if( Options->Interpolation == READ ) {
      InitReadWeights(Input, Map->NX, Map->NY, BasinMask, MetWeights, WeightInfo, 
                      WeightFiles, NStats );
      /*ReadWeights(Map->NX, Map->NY, BasinMask, MetWeights, (*WeightInfo), 
        Month, WeightFiles->Path);*/

    }
    else {

      CalcWeights(Stats, NStats, Map->NX, Map->NY, BasinMask, MetWeights, Options);

      printf("we have %d stations \n",NStats);
      printf("\nSummary info on met stations used for current model run \n");
      printf("      Name\t\tY\tX\tIn Mask \tDefined Elev\tActual Elev\n");
      for(i=0; i < NStats; i++) {
        if((Stats[i].Loc.N > Map->NY || Stats[i].Loc.N < 0 || 
	        Stats[i].Loc.E > Map->NX || Stats[i].Loc.E < 0))
          printf("%20s\t%d\t%d\t%5s\t%5.1f\t\t%5s\n",
	      Stats[i].Name,Stats[i].Loc.N,Stats[i].Loc.E,
	      "NA",Stats[i].Elev,
	      "NA");
       else
         printf("%20s\t%d\t%d\t%d\t%5.1f\t\t%5.1f\n",
	     Stats[i].Name,Stats[i].Loc.N,Stats[i].Loc.E,
	     BasinMask[Stats[i].Loc.N][Stats[i].Loc.E],Stats[i].Elev,
	     TopoMap[Stats[i].Loc.N][Stats[i].Loc.E].Dem);
      }
      printf("\n");

    }

    for (y = 0; y < Map->NY; y++)
      free(BasinMask[y]);
    
    free(BasinMask);
  }
}

