/*
* SUMMARY:      functions.h - header file for number of DHSVM functions 
* USAGE:        Part of DHSVM
*
* AUTHOR:       Bart Nijssen
* ORG:          University of Washington, Department of Civil Engineering
* E-MAIL:       nijssen@u.washington.edu
* ORIG-DATE:    Apr-1996
* Last Change: Wed Dec  4 16:52:16 2002 by Scott Waichler <waichler@tuff.pnl.gov>
* DESCRIPTION:  header file for large number of DHSVM functions 
* DESCRIP-END.
* FUNCTIONS:    
* COMMENTS:     
*/

/*  $Id: functions.h,v 1.5 2001/05/15 17:30:28 waichler Exp $  */

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#include "data.h"
#include "calcsolar.h"
#include "DHSVMChannel.h"
#include "readweights.h"
#include "groundwater.h"
#include "frozensoil.h"

void Aggregate(int SpatialExtent, MAPSIZE *Map, OPTIONSTRUCT *Options, TOPOPIX **TopoMap, 
               LAYER *Soil, LAYER *Veg, VEGPIX **VegMap, EVAPPIX **Evap, 
               PRECIPPIX **Precip, RADCLASSPIX **RadMap, SNOWPIX **Snow, 
               SOILPIX **SoilMap, AGGREGATED *Total, VEGTABLE *VType, 
               ROADSTRUCT **Network, GWPIX **Groundwater, FRZPIX **FrozenSoil);

double CalcDistance(COORD *LocA, COORD *LocB);

float CalcSnowAlbedo(float TSurf, unsigned short Last, SNOWTABLE *SnowAlbedo);

void CalcWeights(METLOCATION *Station, int NStats, int NX, int NY, 
                 uchar **BasinMask, unshort ****WeightArray, OPTIONSTRUCT *Options);

void DefaultModelState(DATE *Start, MAPSIZE *Map, OPTIONSTRUCT *Options,
                       PRECIPPIX **PrecipMap, SNOWPIX **SnowMap, 
                       SOILPIX **SoilMap, LAYER Soil, SOILTABLE *SType, 
                       VEGPIX **VegMap, LAYER Veg, VEGTABLE *VType, char *Path, 
                       SNOWTABLE *SnowAlbedo, TOPOPIX **TopoMap, 
                       ROADSTRUCT **Network, UNITHYDRINFO *HydrographInfo,
                       float *Hydrograph, STATESTRUCT State);

void draw(DATE *Day, int first, int NX, int NY, int NGraphics, int *which_graphics, 
          VEGTABLE *VType, SOILTABLE *SType,
          SNOWPIX **SnowMap, SOILPIX **SoilMap, VEGPIX **VegMap, TOPOPIX **TopoMap,
          PRECIPPIX **PrecipMap, float **PrismMap, EVAPPIX **EvapMap, RADCLASSPIX **RadMap,
          MET_MAP_PIX **MetMap);

void DumpList( FILES *Out, int NSoil, int NVeg); 


void DumpMap(MAPSIZE *Map, DATE *Current, MAPDUMP *DMap, TOPOPIX **TopoMap,
             EVAPPIX **EvapMap, PRECIPPIX **PrecipMap, RADCLASSPIX **RadMap, 
             SNOWPIX **SnowMap, SOILPIX **SoilMap, LAYER *Soil, 
             VEGPIX **VegMap, LAYER *Veg, GWPIX **Groundwater, FRZPIX **FrozenSoil,
             MET_MAP_PIX **MetMap);

void DumpPix(DATE *Current, FILES *OutFile, EVAPPIX *Evap, PRECIPPIX *Precip, 
             RADCLASSPIX *Rad, SNOWPIX *Snow, SOILPIX *Soil, int NSoil, 
             int NVeg);

void ExecDump(MAPSIZE *Map, DATE *Current, OPTIONSTRUCT *Options, 
              DUMPSTRUCT *Dump, TOPOPIX **TopoMap, EVAPPIX **EvapMap, 
              PRECIPPIX **PrecipMap, RADCLASSPIX **RadMap, SNOWPIX **SnowMap, 
              MET_MAP_PIX **MetMap,
              VEGPIX **VegMap, LAYER *Veg, SOILPIX **SoilMap, LAYER *Soil, 
              AGGREGATED *Total, UNITHYDRINFO *HydrographInfo,
              Channel *ChannelData, float *Hydrograph, int TimeLoopIsOver,
              GWPIX **Groundwater, FRZPIX **FrozenSoil);

void FinalMassBalance(FILES *Out, AGGREGATED *Total, WATERBALANCE *Mass);

void GenerateScales(MAPSIZE *Map, int NumberType, void **XScale, void **YScale);

void GetMetData(OPTIONSTRUCT *Options, TIMESTRUCT *Time, int NSoilLayers, 
                int NStats, float SunMax, METLOCATION *Stat, MAPSIZE *Radar, 
                RADARPIX **RadarMap, char *RadarFileName);

uchar InArea(MAPSIZE *Map, COORD *Loc);

uchar InBasin(uchar MaskValue);

void InitAggregated(int MaxVegLayers, int MaxSoilLayers, AGGREGATED *Total, MAPSIZE Map,
                    TOPOPIX **TopoMap);

void InitConstants(LISTPTR Input, OPTIONSTRUCT *Options, MAPSIZE *Map, 
                   SOLARGEOMETRY *SolarGeo, TIMESTRUCT *Time);

void InitDump(LISTPTR Input, OPTIONSTRUCT *Options, MAPSIZE *Map, 
              int MaxSoilLayers, int MaxVegLayers, int Dt, 
              TOPOPIX **TopoMap, DUMPSTRUCT *Dump, int *NGraphics, int **which_graphics,
              STATESTRUCT *State);

void InitEvapMap(MAPSIZE *Map, EVAPPIX ***EvapMap, SOILPIX **SoilMap, 
                 LAYER *Soil, VEGPIX **VegMap, LAYER *Veg, TOPOPIX **TopoMap);

void InitImageDump(LISTPTR Input, int Dt, MAPSIZE *Map, int MaxSoilLayers, 
                   int MaxVegLayers, char *Path, int NMaps, int NImages, 
                   MAPDUMP **DMap); 

void InitInFiles(INPUTFILES *InFiles);

void InitInterpolationWeights(MAPSIZE *Map, OPTIONSTRUCT *Options, 
                              TOPOPIX **TopoMap, unshort ****MetWeights, 
                              METLOCATION *Stats, int NStats, 
                              LISTPTR Input, WEIGHT_INFO ***WeightInfo, WEIGHT_FILES *WeightFiles,
                              int Month);

void InitIPW(LISTPTR Input, TIMESTRUCT *Time, INPUTFILES *InFiles, 
             RADTABLE *RadClass);

void InitMapDump(LISTPTR Input, MAPSIZE *Map, int MaxSoilLayers, 
                 int MaxVegLayers, char *Path, int TotalMapImages, int NMaps,
                 MAPDUMP **DMap); 

void InitMetMaps(int NDaySteps, MAPSIZE *Map, MAPSIZE *Radar, 
                 OPTIONSTRUCT *Options, char *WindPath, char *PrecipLapsePath,
                 float ***PrecipLapseMap, float ***PrismMap, EVAPPIX ***EvapMap, 
                 PRECIPPIX ***PrecipMap, RADARPIX ***RadarMap, 
                 RADCLASSPIX ***RadMap, SOILPIX **SoilMap, LAYER *Soil, 
                 VEGPIX **VegMap, LAYER *Veg, TOPOPIX **TopoMap, 
                 float ****MM5Input, float ****WindModel);

void InitMetSources(LISTPTR Input, OPTIONSTRUCT *Options, MAPSIZE *Map, 
                    int NSoilLayers, TIMESTRUCT *Time, INPUTFILES *InFiles, 
                    int *NStats, METLOCATION **Stat, RADTABLE *RadClass, 
                    MAPSIZE *Radar);

void InitMM5(LISTPTR Input, int NSoilLayers, TIMESTRUCT *Time, 
             INPUTFILES *InFiles);

void InitMM5Maps(int NSoilLayers, int NY, int NX, float ****MM5Input,
                 RADCLASSPIX ***RadMap);

void InitModelState(DATE *Start, MAPSIZE *Map, OPTIONSTRUCT *Options,
                    PRECIPPIX **PrecipMap, SNOWPIX **SnowMap, 
                    SOILPIX **SoilMap, LAYER Soil, SOILTABLE *SType, 
                    VEGPIX **VegMap, LAYER Veg, VEGTABLE *VType, char *Path, 
                    SNOWTABLE *SnowAlbedo, TOPOPIX **TopoMap, 
                    ROADSTRUCT **Network, UNITHYDRINFO *HydrographInfo,
                    float *Hydrograph);

void InitNetwork(int HasNetwork, int NY, int NX, float DX, TOPOPIX **TopoMap,
                 SOILPIX **SoilMap, VEGPIX **VegMap, VEGTABLE *VType,
                 ROADSTRUCT ***Network, CHANNEL *ChannelData);

void InitNewDay(int DayOfYear, SOLARGEOMETRY *SolarGeo, int Nvegs, VEGTABLE *VType, DATE Current);

void InitNewMonth(TIMESTRUCT *Time, OPTIONSTRUCT *Options, MAPSIZE *Map, TOPOPIX **TopoMap, 
                  float **PrismMap, int RadType, RADCLASSPIX** RadMap, RADTABLE *RadClass, 
                  INPUTFILES *InFiles, int NVegs, VEGTABLE *VType,
                  int NStats, METLOCATION *Stat, char *Path,
                  unshort ****MetWeights, WEIGHT_INFO **WeightInfo, WEIGHT_FILES WeightFiles);

void InitNewStep(INPUTFILES *InFiles, MAPSIZE *Map, TIMESTRUCT *Time, 
                 int NSoilLayers, OPTIONSTRUCT *Options, int NStats, 
                 METLOCATION *Stat, char *RadarFileName, MAPSIZE *Radar, 
                 RADTABLE *RadClass, RADARPIX **RadarMap, 
                 SOLARGEOMETRY *SolarGeo, TOPOPIX **TopoMap, 
                 RADCLASSPIX **RadMap, SOILPIX **SoilMap,
                 float ***MM5Input, float ***WindModel);

void InitPixDump(LISTPTR Input, MAPSIZE *Map, uchar **BasinMask, char *Path, 
                 int NPix, PIXDUMP **Pix);

void InitPrecipLapse(LISTPTR Input, INPUTFILES *InFiles);

void InitPrecipLapseMap(char *PrecipLapseFile, int NY, int NX, 
                        float ***PrecipLapseMap);

void InitPrismMap(int NY, int NX, float ***PrismMap);

void InitPrecipMap(MAPSIZE *Map, PRECIPPIX ***PrecipMap, VEGPIX **VegMap, 
                   LAYER *Veg, TOPOPIX **TopoMap);

void InitRadar(LISTPTR Input, MAPSIZE *Map, TIMESTRUCT *Time, 
               INPUTFILES *InFiles, MAPSIZE *Radar);

void InitRadarMap(MAPSIZE *Radar, RADARPIX ***RadarMap);

void InitRadMap(unsigned char RadType, int NDaySteps, MAPSIZE *Map, 
                TOPOPIX **TopoMap, RADCLASSPIX ***RadMap);

void InitSatVaporTable(void);

void InitSnowMap(MAPSIZE *Map, SNOWPIX ***SnowMap);

void InitSoilMap(LISTPTR Input, MAPSIZE *Map, LAYER *Soil, 
                 TOPOPIX **TopoMap, SOILPIX ***SoilMap);

int InitSoilTable(SOILTABLE **SType, LISTPTR Input, LAYER *Soil, 
                  int infiltrationRate, int hydraulicRouting);

void InitSnowTable(SNOWTABLE **SnowAlbedo, int Dt);

void InitStateDump(LISTPTR Input, int NStates, DATE **DState);

void InitGraphicsDump(LISTPTR Input, int NGraphics, int ***which_graphics);

void InitStations(LISTPTR Input, MAPSIZE *Map, int NDaySteps, 
                  unsigned char RadType, unsigned char IncludeOut, 
                  int *NStats, METLOCATION **Stat, int PrecipLapse, int TempLapse, 
                  unsigned char Prism);  /* SRW includes Prism flag 08/15/02  */
                  /*
                  void InitStations(LISTPTR Input, MAPSIZE *Map, int NDaySteps, 
                  unsigned char RadType, unsigned char IncludeOut,
int *NStats, METLOCATION **Stat);*/

void InitTables(int StepsPerDay, LISTPTR Input, SOILTABLE **SType, LAYER *Soil, 
                VEGTABLE **VType, LAYER *Veg, SNOWTABLE **SnowAlbedo, 
                int infiltrationRate, int hydraulicRouting);

void InitTerrainMaps(LISTPTR Input, OPTIONSTRUCT *Options, MAPSIZE *Map, 
                     LAYER *Soil, TOPOPIX ***TopoMap, SOILPIX ***SoilMap, 
                     VEGPIX ***VegMap);

void InitTopoMap(LISTPTR Input, OPTIONSTRUCT *Options, MAPSIZE *Map, 
                 TOPOPIX ***TopoMap);

void InitUnitHydrograph(LISTPTR Input, MAPSIZE *Map, TOPOPIX **TopoMap,
                        UNITHYDR ***UnitHydrograph, float **Hydrograph, 
                        UNITHYDRINFO *HydrographInfo);

void InitVegMap(LISTPTR Input, MAPSIZE *Map, VEGPIX ***VegMap);

int InitVegTable(VEGTABLE **VType, LISTPTR Input, LAYER *Veg);

void InitWindModel(LISTPTR Input, INPUTFILES *InFiles, int NStats,
                   METLOCATION *Stat);

void InitWindModelMaps(char *WindPath, int NY, int NX, float ****WindModel);

uchar IsStationLocation(COORD *Loc, int NStats, METLOCATION *Station, 
                        int *WhichStation);

void InitXGraphics(int argc, char **argv,
                   int ny, int nx, int nd,
                   MET_MAP_PIX ***MetMap);

float LapseT(float Temp, float FromElev, float ToElev, int NLapse,
             float LapseRate1, float LapseRate2, float BreakElev, int OptionsTempLapse);

float LapsePrecip(float Precip, float FromElev, float ToElev, 
                  int NLapse, float PrecipLapse1, float PrecipLapse2, float BreakElev,
                  int OptionsPrecipLapse);

                  /*float LapsePrecip(float Precip, float FromElev, float ToElev, float PrecipLapse);
                  
float LapseT(float Temp, float FromElev, float ToElev, float LapseRate);*/

PIXMET MakeLocalMetData(int y, int x, MAPSIZE *Map, int DayStep, 
                        OPTIONSTRUCT *Options, int NStats, 
                        METLOCATION *Stat, unshort *MetWeights, 
                        float LocalElev, RADCLASSPIX *RadMap, 
                        PRECIPPIX *PrecipMap, MAPSIZE *Radar, 
                        RADARPIX **RadarMap, float **PrismMap, SNOWPIX *LocalSnow,
                        SNOWTABLE *SnowAlbedo, float ***MM5Input,
                        float ***WindModel, float **PrecipLapseMap,
                        MET_MAP_PIX ***MetMap, int NGraphics,
                        int Month);  

void MakeState(MAPSIZE *Map, DATE *Current, OPTIONSTRUCT *Options, 
              DUMPSTRUCT *Dump, TOPOPIX **TopoMap, EVAPPIX **EvapMap, 
              PRECIPPIX **PrecipMap, RADCLASSPIX **RadMap, SNOWPIX **SnowMap, 
              MET_MAP_PIX **MetMap,
              VEGPIX **VegMap, LAYER *Veg, SOILPIX **SoilMap, LAYER *Soil, 
              AGGREGATED *Total, UNITHYDRINFO *HydrographInfo,
              Channel *ChannelData, float *Hydrograph, int TimeLoopIsOver,
              GWPIX **Groundwater, FRZPIX **FrozenSoil);


void MassBalance(DATE *Current, FILES *Out, AGGREGATED *Total, 
                 WATERBALANCE *Mass);


void MassEnergyBalance(int y, int x, float DX, int Dt, int HeatFluxOption,
                       int SpatialExtent, int MaxVegLayers, PIXMET *LocalMet, 
                       ROADSTRUCT *LocalNetwork, PRECIPPIX *LocalPrecip, 
                       VEGTABLE *VType, VEGPIX *LocalVeg, SOILTABLE *SType, 
                       SOILPIX *LocalSoil, SNOWPIX *LocalSnow, 
                       EVAPPIX *LocalEvap, PIXRAD *TotalRad, int infiltrationRate,
                       DATE *Current, FRZPIX **FrozenSoil, int FrozenSoilOption);

void ReadChannelState(char *Path, DATE *Current, Channel *Head);

void ReadMetRecord(OPTIONSTRUCT *Options, DATE *Current, int NSoilLayers, 
                   FILES *InFile, unsigned char IsWindModelLocation, 
                   MET *MetRecord, int NPrecipLapse, int NTempLapse); 


                   /*void ReadMetRecord(OPTIONSTRUCT *Options, DATE *Current, int NSoilLayers, 
                   FILES *InFile, unsigned char IsWindModelLocation, 
MET *MetRecord);*/

void ReadRadarMap(DATE *Current, DATE *StartRadar, int Dt, MAPSIZE *Radar, 
                  RADARPIX **RadarMap, char *HDFFileName);

void ReadPRISMMap(DATE *Current, DATE *StartRadar, int Dt, MAPSIZE *Radar, 
                  RADARPIX **RadarMap, char *HDFFileName);

void ReadRadMap(MAPSIZE *Map, int EndStep, int NDaySteps, int NStats, 
                char *FileName, TOPOPIX **TopoMap, RADCLASSPIX **RadMap, 
                METLOCATION *Stat);

void ReadRadTable(FILES *InFile, int NDaySteps, RADTABLE *RadClass);

int ReadStatInfo(FILES *InFile, uchar PrecipType, uchar RadType, 
                 int NDaySteps, METLOCATION **Stat, MAPSIZE *Map); 

void ResetAggregate(LAYER *Soil, LAYER *Veg, AGGREGATED *Total);

void ResetValues(MAPSIZE *Map, SOILPIX **SoilMap);

void RouteSubSurface(int Dt, MAPSIZE *Map, TOPOPIX **TopoMap, 
                     VEGTABLE *VType, VEGPIX **VegMap, 
                     ROADSTRUCT **Network, SOILTABLE *SType, 
                     SOILPIX **SoilMap, CHANNEL *ChannelData, AGGREGATED *Total);

void RouteSurface(MAPSIZE *Map, TIMESTRUCT *Time, TOPOPIX **TopoMap, 
                  SOILPIX **SoilMap, int HasNetwork, 
                  UNITHYDR **UnitHydrograph, 
                  UNITHYDRINFO *HydrographInfo, float *Hydrograph, 
                  FILES *StreamFile, int HydraulicRouting, SOILTABLE *SType, CHANNEL *ChannelData);

int ScanInts(FILE *FilePtr, int *X, int N);

int ScanDoubles(FILE *FilePtr, double *X, int N);

int ScanFloats(FILE *FilePtr, float *X, int N);

uchar ScanUChars(FILE *FilePtr, uchar *X, int N);

void SkipHeader(FILES *InFile, int NLines);

void SkipLines(FILES *InFile, int NLines);

float SoilResistanceEvap(int Dt, float Temp, float Slope, float Gamma, float Lv, 
                         float AirDens, float Vpd, float NetRad, float RaSoil, 
                         float MaxEvap, float FieldCap, float Rmin, float Cs, float RootDepth, 
                         float *MoistContent, float Adjust, int Overstory);


void StoreChannelState(char *Path, DATE *Current, Channel *Head);

void StoreModelState(char *Path, DATE *Current, MAPSIZE *Map, 
                     OPTIONSTRUCT *Options, TOPOPIX **TopoMap, 
                     PRECIPPIX **PrecipMap, SNOWPIX **SnowMap, 
                     MET_MAP_PIX **MetMap, RADCLASSPIX **RadMap,
                     VEGPIX **VegMap, LAYER *Veg, SOILPIX **SoilMap, 
                     LAYER *Soil, UNITHYDRINFO *HydrographInfo,
                     float *Hydrograph);

void ZeroChannelState(Channel *Head);

#endif


