Ejemplo n.º 1
0
int main()
{
    const Date cDate(10, 16, 2020);
    PrintDate(cDate);

    return 0;
}
Ejemplo n.º 2
0
void DisplayReq(struct fileentry *fe)
{
   printf("%-8.8s %-17.17s %-16.16s %7.7s   %s\n",
      type_names[fe->type],PrintNode(&fe->Node),PrintDate(fe->date),PrintReqNums(fe),fe->file);

   if(cfg_Verbose)
      DisplayReqContents(fe);
}
Ejemplo n.º 3
0
void DisplayPkt(struct fileentry *fe)
{
   char buf[100];

   sprintf(buf,"%s/1",unit(fe->size));

   printf("%-8.8s %-17.17s %-16.16s %7.7s   %s\n",
      type_names[fe->type],PrintNode(&fe->Node),PrintDate(fe->date),buf,fe->file);

   if(cfg_Verbose)
      printf("\n");
}
Ejemplo n.º 4
0
void CWorkspaceView::PrintPageHeader(CDC* pDC)
{
	CString str;
	int yPosition = -20;
	int xPosition = 10;

	str = _T("AIPI Project Information       ") + PrintDate();
	ShowFont2(pDC, xPosition, yPosition, 8, str);
	ShowFont2(pDC, xPosition, yPosition, 8, _T("__________________________________________________________________________________________________________________________________________________"));
	
	
	/*
	//CPoint point1(0,0);
	//pDC->TextOut(point1.x, point1.y, str);
	//CPoint point2(0, 30);
	//pDC->TextOut(point2.x, point2.y, _T("__________________________________________________________________________________________________________________________________________________"));
	//CPoint point3(720, -720);
	//pDC->TextOut(point2.x, point2.y, _T("Aipi Project Settings:"));
	*/
	

}
Ejemplo n.º 5
0
/*****************************************************************************
  Aggregate()
  
  Calculate the average values for the different fluxes and state variables
  over the basin.  Only the runoff and some of the sediment variables (as 
  noted) are calculated as a totals (i.e. runoff is total volume) instead
  of an average.  In the current implementation the local radiation
  elements are not stored for the entire area.  Therefore these components
  are aggregated in AggregateRadiation() inside MassEnergyBalance().

  The aggregated values are set to zero in the function RestAggregate,
  which is executed at the beginning of each time step.
*****************************************************************************/
void MassBalance(DATE *Current, FILES *Out, FILES *SedOut, AGGREGATED *Total,
		 WATERBALANCE *Mass, OPTIONSTRUCT * Options)
{
  float NewWaterStorage;	/* water storage at the end of the time step */
  float Output;			/* total water flux leaving the basin;  */
  float MassError;		/* mass balance error m  */
  float MWMMassError;            /* mass wasting mass balance error m3  */
  float SedInput, SedOutput, SedMassError;  /* sediment mass balance variables 
					       for channel network */

  NewWaterStorage = Total->Soil.IExcess + Total->Road.IExcess + 
    Total->CanopyWater + Total->SoilWater +
    Total->Snow.Swq + Total->Soil.SatFlow;

  Output = Total->ChannelInt + Total->RoadInt + Total->Evap.ETot;

  MassError = (NewWaterStorage - Mass->OldWaterStorage) + Output -
    Total->Precip.Precip - Total->Snow.VaporMassFlux -
    Total->Snow.CanopyVaporMassFlux - Total->CulvertReturnFlow;

  /* update */
  Mass->OldWaterStorage = NewWaterStorage;
  Mass->CumPrecipIn += Total->Precip.Precip;
  Mass->CumIExcess += Total->Soil.IExcess;
  Mass->CumChannelInt += Total->ChannelInt;
  Mass->CumRoadInt += Total->RoadInt;
  Mass->CumET += Total->Evap.ETot;
  Mass->CumSnowVaporFlux += Total->Snow.VaporMassFlux +
    Total->Snow.CanopyVaporMassFlux;
  Mass->CumCulvertReturnFlow += Total->CulvertReturnFlow;
  Mass->CumCulvertToChannel += Total->CulvertToChannel;
  Mass->CumRunoffToChannel += Total->RunoffToChannel;
  
  PrintDate(Current, Out->FilePtr);
  
  fprintf(Out->FilePtr, " %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g\n",
	   Total->Soil.IExcess, Total->CanopyWater, Total->SoilWater, Total->Snow.Swq,
	   Total->Soil.SatFlow, Total->ChannelInt, Total->RoadInt,
	   Total->CulvertReturnFlow, Total->Evap.ETot, Total->Precip.Precip,
	   Total->Snow.VaporMassFlux, Total->Snow.CanopyVaporMassFlux,
	   Mass->OldWaterStorage, Total->CulvertToChannel,
	   Total->RunoffToChannel, MassError);

  if(Options->Sediment){

    /* Calculate sediment mass errors */
    MWMMassError = Total->Fine.MassWasting -Total->Fine.SedimentToChannel - 
      Total->Fine.MassDeposition ;
    
    SedInput = Total->DebrisInflow + 
      (Total->SedimentOverlandInflow - Total->CulvertSedToChannel) + 
      Total->SedimentOverroadInflow ;
    
    SedOutput = Total->SedimentOutflow  - 
      (Total->CulvertSedToChannel + Total->CulvertReturnSedFlow); 
    
    SedMassError = (Total->ChannelSedimentStorage + 
		    Total->ChannelSuspendedSediment - 
		    Mass->LastChannelSedimentStorage) + 
      SedOutput - SedInput;
    
    /* update */
    
    /* Mass Wasting - tota,l m3 */
    Mass->CumMassWasting += Total->Fine.MassWasting;
    Mass->CumSedimentToChannel += Total->Fine.SedimentToChannel;
    Mass->CumMassDeposition += Total->Fine.MassDeposition;
    
    /* Surface Erosion - ave, mm */
    Mass->CumSedimentErosion += Total->Sediment.Erosion;
    
    /* Road Erosion - ave, m */
    Mass->CumRoadErosion += Total->Road.Erosion;
    Mass->CumRoadSedHill += Total->Sediment.RoadSed;
    
    /* Channel Erosion - total, kg */
    Mass->CumDebrisInflow += Total->DebrisInflow;
    
    Mass->CumSedOverlandInflow += Total->SedimentOverlandInflow;
    Mass->CumSedOverroadInflow += Total->SedimentOverroadInflow;
    
    Mass->CumCulvertSedToChannel += Total->CulvertSedToChannel;
    Mass->CumCulvertReturnSedFlow += Total->CulvertReturnSedFlow;
    Mass->CumSedimentOutflow += Total->SedimentOutflow;
    
    Mass->LastChannelSedimentStorage = Total->ChannelSedimentStorage + 
      Total->ChannelSuspendedSediment;   
    
    PrintDate(Current, SedOut->FilePtr);
    
    fprintf(SedOut->FilePtr, " %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g \n", 
	    Total->Fine.MassWasting, Total->Fine.SedimentToChannel, 
	    Total->Fine.MassDeposition, MWMMassError, Total->Sediment.Erosion,
	    Total->Road.Erosion,Total->Sediment.RoadSed, 
	    Total->DebrisInflow, Total->SedimentOverlandInflow, 
	    Total->SedimentOverroadInflow, Total->SedimentOutflow, 
	    Total->CulvertReturnSedFlow, Total->CulvertSedToChannel,
	    Mass->LastChannelSedimentStorage, SedMassError);
  } 
}
Ejemplo n.º 6
0
int main(int argc, char **argv)
{
  float *Hydrograph = NULL;
  float ***MM5Input = NULL;
  float **PrecipLapseMap = NULL;
  float **PrismMap = NULL;
  unsigned char ***ShadowMap = NULL;
  float **SkyViewMap = NULL;
  float ***WindModel = NULL;
  int MaxStreamID, MaxRoadID;
  float SedDiams[NSEDSIZES];     /* Sediment particle diameters (mm) */
  clock_t start, finish1;
  double runtime = 0.0;
  int t = 0;
  float roadarea;
  time_t tloc;
  int flag;
  int i;
  int j;
  int x;						/* row counter */
  int y;						/* column counter */
  int shade_offset;				/* a fast way of handling arraay position given the number of mm5 input options */
  int NStats;					/* Number of meteorological stations */
  uchar ***MetWeights = NULL;	/* 3D array with weights for interpolating meteorological variables between the stations */

  int NGraphics;				/* number of graphics for X11 */
  int *which_graphics;			/* which graphics for X11 */
  char buffer[32];

  AGGREGATED Total = {			/* Total or average value of a  variable over the entire basin */
    {0.0, NULL, NULL, NULL, NULL, 0.0},												/* EVAPPIX */
    {0.0, 0.0, 0.0, 0.0, 0.0, NULL, NULL, 0.0, 0, 0.0},								/* PRECIPPIX */
    {{0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, 0.0, 0.0, 0.0},							/* PIXRAD */
    {0.0, 0.0},																		/* RADCLASSPIX */
    {0.0, 0.0, 0, NULL, NULL, 0.0, 0, 0.0, 0.0, 0.0, 0.0, NULL, 
	NULL, NULL, NULL, NULL, NULL, 0.0},												/* ROADSTRUCT*/
    {0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},		/* SNOWPIX */
    {0, 0.0, NULL, NULL, NULL, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
     0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},			/*SOILPIX */
    { 0.0, 0.0, 0.0, 0.0, 0.0},														/*SEDPIX */
    { 0.0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},							/*FINEPIX */
    0.0, 0.0, 0.0, 0.0, 0.0, 0l, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
  };
  CHANNEL ChannelData = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  NULL, NULL, NULL, NULL};
  DUMPSTRUCT Dump;
  EVAPPIX **EvapMap = NULL;
  INPUTFILES InFiles;
  LAYER Soil;
  LAYER Veg;
  LISTPTR Input = NULL;			/* Linked list with input strings */
  MAPSIZE Map;					/* Size and location of model area */
  MAPSIZE Radar;				/* Size and location of area covered by precipitation radar */
  MAPSIZE MM5Map;				/* Size and location of area covered by MM5 input files */
  METLOCATION *Stat = NULL;
  OPTIONSTRUCT Options;			/* Structure with information which program options to follow */
  PIXMET LocalMet;				/* Meteorological conditions for current pixel */
  FINEPIX ***FineMap	= NULL;
  PRECIPPIX **PrecipMap = NULL;
  RADARPIX **RadarMap	= NULL;
  RADCLASSPIX **RadMap	= NULL;
  PIXRAD **RadiationMap = NULL;
  ROADSTRUCT **Network	= NULL;	/* 2D Array with channel information for each pixel */
  SNOWPIX **SnowMap		= NULL;
  MET_MAP_PIX **MetMap	= NULL;
  SNOWTABLE *SnowAlbedo = NULL;
  SOILPIX **SoilMap		= NULL;
  SEDPIX **SedMap		= NULL;
  SOILTABLE *SType	    = NULL;
  SEDTABLE *SedType		= NULL;
  SOLARGEOMETRY SolarGeo;		/* Geometry of Sun-Earth system (needed for INLINE radiation calculations */
  TIMESTRUCT Time;
  TOPOPIX **TopoMap = NULL;
  UNITHYDR **UnitHydrograph = NULL;
  UNITHYDRINFO HydrographInfo;	/* Information about unit hydrograph */
  VEGPIX **VegMap = NULL;
  VEGTABLE *VType = NULL;
  WATERBALANCE Mass =			/* parameter for mass balance calculations */
    { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,  
      0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };

/*****************************************************************************
  Initialization Procedures 
*****************************************************************************/
  if (argc != 2) {
    fprintf(stderr, "\nUsage: %s inputfile\n\n", argv[0]);
    fprintf(stderr, "DHSVM uses two output streams: \n");
    fprintf(stderr, "Standard Out, for the majority of output \n");
    fprintf(stderr, "Standard Error, for the final mass balance \n");
    fprintf(stderr, "\nTo pipe output correctly to files: \n");
    fprintf(stderr, "(cmd > f1) >& f2 \n");
    fprintf(stderr, "where f1 is stdout_file and f2 is stderror_file\n");
    exit(EXIT_FAILURE);
  }

  sprintf(commandline, "%s %s", argv[0], argv[1]);
  printf("%s \n", commandline);
  fprintf(stderr, "%s \n", commandline);
  strcpy(InFiles.Const, argv[1]);

  printf("\nRunning DHSVM %s\n", version);
  printf("\nSTARTING INITIALIZATION PROCEDURES\n\n");

  /* Start recording time */
  start = clock();

  ReadInitFile(InFiles.Const, &Input);
  InitConstants(Input, &Options, &Map, &SolarGeo, &Time);

  InitFileIO(Options.FileFormat);
  InitTables(Time.NDaySteps, Input, &Options, &SType, &Soil, &VType, &Veg,
	     &SnowAlbedo);

  InitTerrainMaps(Input, &Options, &Map, &Soil, &TopoMap, &SoilMap, &VegMap);

  CheckOut(Options.CanopyRadAtt, Veg, Soil, VType, SType, &Map, TopoMap, 
	   VegMap, SoilMap);

  if (Options.HasNetwork)
    InitChannel(Input, &Map, Time.Dt, &ChannelData, SoilMap, &MaxStreamID, &MaxRoadID, &Options);
  else if (Options.Extent != POINT)
    InitUnitHydrograph(Input, &Map, TopoMap, &UnitHydrograph,
		       &Hydrograph, &HydrographInfo);
 
  InitNetwork(Map.NY, Map.NX, Map.DX, Map.DY, TopoMap, SoilMap, 
	      VegMap, VType, &Network, &ChannelData, Veg, &Options);

  InitMetSources(Input, &Options, &Map, Soil.MaxLayers, &Time,
		 &InFiles, &NStats, &Stat, &Radar, &MM5Map);

  /* the following piece of code is for the UW PRISM project */
  /* for real-time verification of SWE at Snotel sites */
  /* Other users, set OPTION.SNOTEL to FALSE, or use TRUE with caution */

  if (Options.Snotel == TRUE && Options.Outside == FALSE) {
    printf
      ("Warning: All met stations locations are being set to the vegetation class GLACIER\n");
    printf
      ("Warning: This requires that you have such a vegetation class in your vegetation table\n");
    printf("To disable this feature set Snotel OPTION to FALSE\n");
    for (i = 0; i < NStats; i++) {
      printf("veg type for station %d is %d ", i,
	     VegMap[Stat[i].Loc.N][Stat[i].Loc.E].Veg);
      for (j = 0; j < Veg.NTypes; j++) {
	    if (VType[j].Index == GLACIER) {
	      VegMap[Stat[i].Loc.N][Stat[i].Loc.E].Veg = j;
		  break;
		}
      }
      if (j == Veg.NTypes) {	/* glacier class not found */
	    ReportError("MainDHSVM", 62);
	  }
      printf("setting to glacier type (assumed bare class): %d\n", j);
    }
  }

  InitMetMaps(Time.NDaySteps, &Map, &Radar, &Options, InFiles.WindMapPath,
	      InFiles.PrecipLapseFile, &PrecipLapseMap, &PrismMap,
	      &ShadowMap, &SkyViewMap, &EvapMap, &PrecipMap,
	      &RadarMap, &RadMap, SoilMap, &Soil, VegMap, &Veg, TopoMap,
	      &MM5Input, &WindModel);

  InitInterpolationWeights(&Map, &Options, TopoMap, &MetWeights, Stat, NStats);

  InitDump(Input, &Options, &Map, Soil.MaxLayers, Veg.MaxLayers, Time.Dt,
	   TopoMap, &Dump, &NGraphics, &which_graphics);

  if (Options.HasNetwork == TRUE) {
    InitChannelDump(&Options, &ChannelData, Dump.Path);
    ReadChannelState(Dump.InitStatePath, &(Time.Start), ChannelData.streams);
  }

  InitSnowMap(&Map, &SnowMap);
  InitAggregated(Veg.MaxLayers, Soil.MaxLayers, &Total);

  InitModelState(&(Time.Start), &Map, &Options, PrecipMap, SnowMap, SoilMap,
		 Soil, SType, VegMap, Veg, VType, Dump.InitStatePath,
		 SnowAlbedo, TopoMap, Network, &HydrographInfo, Hydrograph);

  InitNewMonth(&Time, &Options, &Map, TopoMap, PrismMap, ShadowMap,
	       RadMap, &InFiles, Veg.NTypes, VType, NStats, Stat, 
	       Dump.InitStatePath);

  InitNewDay(Time.Current.JDay, &SolarGeo);

  if (NGraphics > 0) {
    printf("Initialzing X11 display and graphics \n");
    InitXGraphics(argc, argv, Map.NY, Map.NX, NGraphics, &MetMap);
  }

  shade_offset = FALSE;
  if (Options.Shading == TRUE)
    shade_offset = TRUE;

  /* Done with initialization, delete the list with input strings */
  DeleteList(Input);

  /*****************************************************************************
  Sediment Initialization Procedures 
  *****************************************************************************/
  if(Options.Sediment) {
     time (&tloc);
     srand (tloc);
  /* Randomize Random Generator */
 
  /* Commenting the line above and uncommenting the line below 
     allows for the comparison of scenarios. */
  /*  srand48 (0);  */
    printf("\nSTARTING SEDIMENT INITIALIZATION PROCEDURES\n\n");

    ReadInitFile(Options.SedFile, &Input);

    InitParameters(Input, &Options, &Map, &Network, &ChannelData, TopoMap,
		   &Time, SedDiams);

    InitSedimentTables(Time.NDaySteps, Input, &SedType, &SType, &VType, &Soil, &Veg);

    InitFineMaps(Input, &Options, &Map, &Soil, &TopoMap, &SoilMap, 
		  &FineMap);

    if (Options.HasNetwork){ 
      printf("Initializing channel sediment\n\n");
      InitChannelSedimentDump(&ChannelData, Dump.Path, Options.ChannelRouting); 
      InitChannelSediment(ChannelData.streams, &Total);
      InitChannelSediment(ChannelData.roads, &Total);
    }

    InitSedMap( &Map, &SedMap);

    /* Done with initialization, delete the list with input strings */
    DeleteList(Input);
  }

  /* setup for mass balance calculations */
  Aggregate(&Map, &Options, TopoMap, &Soil, &Veg, VegMap, EvapMap, PrecipMap,
	      RadMap, SnowMap, SoilMap, &Total, VType, Network, SedMap, FineMap,
	      &ChannelData, &roadarea);

  Mass.StartWaterStorage =
    Total.Soil.IExcess + Total.CanopyWater + Total.SoilWater + Total.Snow.Swq +
    Total.Soil.SatFlow;
  Mass.OldWaterStorage = Mass.StartWaterStorage;

  if (Options.Sediment) {
    Mass.StartChannelSedimentStorage = Total.ChannelSedimentStorage;
    Mass.LastChannelSedimentStorage = Mass.StartChannelSedimentStorage;
  }

  /* computes the number of grid cell contributing to one segment */
  if (Options.StreamTemp) 
	Init_segment_ncell(TopoMap, ChannelData.stream_map, Map.NY, Map.NX, ChannelData.streams);

/*****************************************************************************
  Perform Calculations 
*****************************************************************************/
  while (Before(&(Time.Current), &(Time.End)) ||
	 IsEqualTime(&(Time.Current), &(Time.End))) {
    ResetAggregate(&Soil, &Veg, &Total, &Options);

    if (IsNewMonth(&(Time.Current), Time.Dt))
      InitNewMonth(&Time, &Options, &Map, TopoMap, PrismMap, ShadowMap,
		   RadMap, &InFiles, Veg.NTypes, VType, NStats, Stat, 
		   Dump.InitStatePath);

    if (IsNewDay(Time.DayStep)) {
      InitNewDay(Time.Current.JDay, &SolarGeo);
      PrintDate(&(Time.Current), stdout);
      printf("\n");
    }

    /* determine surface erosion and routing scheme */
    SedimentFlag(&Options, &Time); 

    InitNewStep(&InFiles, &Map, &Time, Soil.MaxLayers, &Options, NStats, Stat,
		InFiles.RadarFile, &Radar, RadarMap, &SolarGeo, TopoMap, RadMap,
        SoilMap, MM5Input, WindModel, &MM5Map);

    /* initialize channel/road networks for time step */
    if (Options.HasNetwork) {
      channel_step_initialize_network(ChannelData.streams);
      channel_step_initialize_network(ChannelData.roads);
    }

    for (y = 0; y < Map.NY; y++) {
      for (x = 0; x < Map.NX; x++) {
	    if (INBASIN(TopoMap[y][x].Mask)) {
		  if (Options.Shading)
	        LocalMet =
	        MakeLocalMetData(y, x, &Map, Time.DayStep, &Options, NStats,
			       Stat, MetWeights[y][x], TopoMap[y][x].Dem,
			       &(RadMap[y][x]), &(PrecipMap[y][x]), &Radar,
			       RadarMap, PrismMap, &(SnowMap[y][x]),
			       SnowAlbedo, MM5Input, WindModel, PrecipLapseMap,
			       &MetMap, NGraphics, Time.Current.Month,
			       SkyViewMap[y][x], ShadowMap[Time.DayStep][y][x],
			       SolarGeo.SunMax, SolarGeo.SineSolarAltitude);
		  else
	        LocalMet =
	        MakeLocalMetData(y, x, &Map, Time.DayStep, &Options, NStats,
			       Stat, MetWeights[y][x], TopoMap[y][x].Dem,
			       &(RadMap[y][x]), &(PrecipMap[y][x]), &Radar,
			       RadarMap, PrismMap, &(SnowMap[y][x]),
			       SnowAlbedo, MM5Input, WindModel, PrecipLapseMap,
			       &MetMap, NGraphics, Time.Current.Month, 0.0,
			       0.0, SolarGeo.SunMax,
			       SolarGeo.SineSolarAltitude);
		  
		  for (i = 0; i < Soil.MaxLayers; i++) {
	        if (Options.HeatFlux == TRUE) {
	          if (Options.MM5 == TRUE)
		        SoilMap[y][x].Temp[i] =
				MM5Input[shade_offset + i + N_MM5_MAPS][y][x];
			  else
		        SoilMap[y][x].Temp[i] = Stat[0].Data.Tsoil[i];
			}
	        else
	          SoilMap[y][x].Temp[i] = LocalMet.Tair;
		  }
		  
		  MassEnergyBalance(&Options, y, x, SolarGeo.SineSolarAltitude, Map.DX, Map.DY, 
			    Time.Dt, Options.HeatFlux, Options.CanopyRadAtt, Options.RoadRouting, 
			    Options.Infiltration, Veg.MaxLayers, &LocalMet, &(Network[y][x]), 
			    &(PrecipMap[y][x]), &(VType[VegMap[y][x].Veg-1]), &(VegMap[y][x]),
			    &(SType[SoilMap[y][x].Soil-1]), &(SoilMap[y][x]), &(SnowMap[y][x]), 
				&(EvapMap[y][x]), &(Total.Rad), &ChannelData, SkyViewMap);
		 
		  PrecipMap[y][x].SumPrecip += PrecipMap[y][x].Precip;
		}
	  }
    }

	/* Average all RBM inputs over each segment */
	if (Options.StreamTemp) {
	  channel_grid_avg(ChannelData.streams);
	}

 #ifndef SNOW_ONLY

    /* set sediment inflows to zero - they are incremented elsewhere */
    if ((Options.HasNetwork) && (Options.Sediment)){ 
      InitChannelSedInflow(ChannelData.streams);
      InitChannelSedInflow(ChannelData.roads);
	}
    
    RouteSubSurface(Time.Dt, &Map, TopoMap, VType, VegMap, Network,
		    SType, SoilMap, &ChannelData, &Time, &Options, Dump.Path,
		    SedMap, FineMap, SedType, MaxStreamID, SnowMap);

    if (Options.HasNetwork)
      RouteChannel(&ChannelData, &Time, &Map, TopoMap, SoilMap, &Total, 
		   &Options, Network, SType, PrecipMap, SedMap,
		   LocalMet.Tair, LocalMet.Rh, SedDiams);

    /* Sediment Routing in Channel and output to sediment files */
    if ((Options.HasNetwork) && (Options.Sediment)){
      SPrintDate(&(Time.Current), buffer);
      flag = IsEqualTime(&(Time.Current), &(Time.Start));

      if (Options.ChannelRouting){
	    if (ChannelData.roads != NULL) {
	      RouteChannelSediment(ChannelData.roads, Time, &Dump, &Total, SedDiams);
	      channel_save_sed_outflow_text(buffer, ChannelData.roads,
					ChannelData.sedroadout,
					ChannelData.sedroadflowout, flag);
		  RouteCulvertSediment(&ChannelData, &Map, TopoMap, SedMap, 
			       &Total, SedDiams);
		}
	    RouteChannelSediment(ChannelData.streams, Time, &Dump, &Total, SedDiams);
 	    channel_save_sed_outflow_text(buffer, ChannelData.streams,
				      ChannelData.sedstreamout,
				      ChannelData.sedstreamflowout, flag);
	  }
      else {
	    if (ChannelData.roads != NULL) {
			channel_save_sed_inflow_text(buffer, ChannelData.roads,
			ChannelData.sedroadinflow, SedDiams,flag);
		}
	    channel_save_sed_inflow_text(buffer, ChannelData.streams,
			ChannelData.sedstreaminflow, SedDiams,flag);
      }
      SaveChannelSedInflow(ChannelData.roads, &Total);
      SaveChannelSedInflow(ChannelData.streams, &Total);
    }
    
    if (Options.Extent == BASIN)
      RouteSurface(&Map, &Time, TopoMap, SoilMap, &Options,
		   UnitHydrograph, &HydrographInfo, Hydrograph,
		   &Dump, VegMap, VType, SType, &ChannelData, SedMap,
		   PrecipMap, SedType, LocalMet.Tair, LocalMet.Rh, SedDiams);

#endif

    if (NGraphics > 0)
      draw(&(Time.Current), IsEqualTime(&(Time.Current), &(Time.Start)),
	   Time.DayStep, &Map, NGraphics, which_graphics, VType,
	   SType, SnowMap, SoilMap, SedMap, FineMap, VegMap, TopoMap, PrecipMap,
	   PrismMap, SkyViewMap, ShadowMap, EvapMap, RadMap, MetMap, Network,
	   &Options);
    
    Aggregate(&Map, &Options, TopoMap, &Soil, &Veg, VegMap, EvapMap, PrecipMap,
	      RadMap, SnowMap, SoilMap, &Total, VType, Network, SedMap, FineMap,
	      &ChannelData, &roadarea);
    
    MassBalance(&(Time.Current), &(Dump.Balance), &(Dump.SedBalance), &Total, 
		&Mass, &Options);

    ExecDump(&Map, &(Time.Current), &(Time.Start), &Options, &Dump, TopoMap,
	     EvapMap, RadiationMap, PrecipMap, RadMap, SnowMap, MetMap, VegMap, &Veg, 
		 SoilMap, SedMap, Network, &ChannelData, FineMap, &Soil, &Total, 
		 &HydrographInfo,Hydrograph);
	
    IncreaseTime(&Time);
	t += 1;
  }

  ExecDump(&Map, &(Time.Current), &(Time.Start), &Options, &Dump, TopoMap,
	   EvapMap, RadiationMap, PrecipMap, RadMap, SnowMap, MetMap, VegMap, &Veg, SoilMap,
	   SedMap, Network, &ChannelData, FineMap, &Soil, &Total, &HydrographInfo, Hydrograph);

  FinalMassBalance(&(Dump.FinalBalance), &Total, &Mass, &Options, roadarea);

  /*printf("\nSTARTING CLEANUP\n\n");
  cleanup(&Dump, &ChannelData, &Options);*/
  printf("\nEND OF MODEL RUN\n\n");

  /* record the run time at the end of each time loop */
  finish1 = clock ();
  runtime = (finish1-start)/CLOCKS_PER_SEC;
  printf("***********************************************************************************");
  printf("\nRuntime Summary:\n");
  printf("%6.2f hours elapsed for the simulation period of %d hours (%.1f days) \n", 
	  runtime/3600, t*Time.Dt/3600, (float)t*Time.Dt/3600/24);

  return EXIT_SUCCESS;
}
Ejemplo n.º 7
0
/*****************************************************************************
  ReadMetRecord()
*****************************************************************************/
void ReadMetRecord(OPTIONSTRUCT *Options, DATE *Current, int NSoilLayers,
		   FILES *InFile, unsigned char IsWindModelLocation,
		   MET *MetRecord)
{
  DATE MetDate;			/* Date of meteorological record */
  float Array[MAXMETVARS];	/* Temporary storage of met variables */
  int i;
  int NMetVars;			/* Number of meteorological variables to read */
  NMetVars = 5;
  /* these are - in order: 
     air temp,
     wind,
     humidity
     shortwave (total i.e. direct+diffuse)
     longwave */

  if (Options->HeatFlux == TRUE)
    NMetVars += NSoilLayers;
  /* expect to see temperature for each soil layer */
  if (Options->PrecipType == STATION)
    NMetVars++;
  if (Options->PrecipLapse == VARIABLE)
    NMetVars++;
  if (Options->TempLapse == VARIABLE)
    NMetVars++;
  if (IsWindModelLocation)
    NMetVars++;

  if (!ScanDate(InFile->FilePtr, &MetDate))
    ReportError(InFile->FileName, 23);

  while (!IsEqualTime(&MetDate, Current) && !feof(InFile->FilePtr)) {
    if (ScanFloats(InFile->FilePtr, Array, NMetVars) != NMetVars)
      ReportError(InFile->FileName, 5);
    if (!ScanDate(InFile->FilePtr, &MetDate))
      ReportError(InFile->FileName, 23);
  }

  if (!IsEqualTime(&MetDate, Current)) {
    if (DEBUG) {
      printf("Metfile: ");
      PrintDate(&MetDate, stdout);
      printf("Current: ");
      PrintDate(Current, stdout);
    }
    ReportError(InFile->FileName, 28);
  }

  if (ScanFloats(InFile->FilePtr, Array, NMetVars) != NMetVars)
    ReportError(InFile->FileName, 5);

  MetRecord->Tair = Array[0];
  MetRecord->Wind = Array[1];
  MetRecord->Rh = Array[2];
  if (MetRecord->Rh < 0.0 || MetRecord->Rh > 100.0) {
    printf("warning: RH out of bounds: %s\n", InFile->FileName);
    if (MetRecord->Rh < 0.0)
      MetRecord->Rh = 0.0;
    if (MetRecord->Rh > 100.0)
      MetRecord->Rh = 100.0;
  }
  MetRecord->Sin = Array[3];
  if (MetRecord->Sin > 1380.0) {
    printf("warning: Shortwave out of bounds: %s\n", InFile->FileName);
    MetRecord->Sin = 1380.0;
  }
  if (MetRecord->Sin < 0.0) {
    printf("Warning: Negative Shortwave, setting to zero: %s\n",
	   InFile->FileName);
    MetRecord->Sin = 0.0;
  }
  MetRecord->Lin = Array[4];
  if (MetRecord->Lin < 0.0 || MetRecord->Lin > 1800.0) {
    printf("warning: Longwave out of bounds: %s\n", InFile->FileName);
  }

  i = 0;
  if (Options->HeatFlux == TRUE)
    for (i = 0; i < NSoilLayers; i++)
      MetRecord->Tsoil[i] = Array[5 + i];

  if (Options->PrecipType == STATION) {
    MetRecord->Precip = Array[5 + i];
    if (MetRecord->Precip < 0) {
      printf("Warning: negative precip %s \n", InFile->FileName);
      MetRecord->Precip = 0.0;
    }
    i++;

  }
  else
    MetRecord->Precip = 0.0;

  if (Options->PrecipLapse == VARIABLE) {
    MetRecord->PrecipLapse = Array[5 + i];
    i++;
  }
  else
    MetRecord->PrecipLapse = PRECIPLAPSE;

  if (Options->TempLapse == VARIABLE) {
    MetRecord->TempLapse = Array[5 + i];
    i++;
  }
  else
    MetRecord->TempLapse = TEMPLAPSE;

  if (IsWindModelLocation) {
    MetRecord->WindDirection = (int) Array[5 + i];
    i++;
  }
  else
    MetRecord->WindDirection = NOT_APPLICABLE;

}
Ejemplo n.º 8
0
int main(int argc, char **argv)
{
  float *Hydrograph = NULL;
  float ***MM5Input = NULL;
  float **PrecipLapseMap = NULL;
  float **PrismMap = NULL;
  unsigned char ***ShadowMap = NULL;
  float **SkyViewMap = NULL;
  float ***WindModel = NULL;
  /* Glacier Model Variable */
  double  dt_year;
  double  year_min;
  double  year_max;
  int MaxStreamID, MaxRoadID;
  clock_t start, finish1;
  double runtime = 0.0;
  int t = 0;
  float roadarea;
  int i;
  int j;
  int x;						/* row counter */
  int y;						/* column counter */
  int shade_offset;				/* a fast way of handling arraay position given the number of mm5 input options */
  int NStats;					/* Number of meteorological stations */
  uchar ***MetWeights = NULL;	/* 3D array with weights for interpolating meteorological variables between the stations */

  int NGraphics;				/* number of graphics for X11 */
  int *which_graphics;			/* which graphics for X11 */

  AGGREGATED Total = {			/* Total or average value of a  variable over the entire basin */
    {0.0, NULL, NULL, NULL, NULL, 0.0},												/* EVAPPIX */
    {0.0, 0.0, 0.0, 0.0, 0.0, NULL, NULL, 0.0, 0, 0.0},								/* PRECIPPIX */
    {{0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}, 0.0, {0.0, 0.0}, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
                                                                                    /* PIXRAD */
    {0.0, 0.0, 0, NULL, NULL, 0.0, 0, 0.0, 0.0, 0.0, 0.0, NULL, NULL},				/* ROADSTRUCT*/
    {0, 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},		/* SNOWPIX */
    {0, 0.0, NULL, NULL, NULL, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},			                /* SOILPIX */
    0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0l, 0.0, 0.0
  };
  CHANNEL ChannelData = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  NULL, NULL, NULL, NULL, NULL, NULL};
  DUMPSTRUCT Dump;
  EVAPPIX **EvapMap = NULL;
  INPUTFILES InFiles;
  LAYER Soil;
  LAYER Veg;
  LISTPTR Input = NULL;			/* Linked list with input strings */
  MAPSIZE Map;					/* Size and location of model area */
  MAPSIZE Radar;				/* Size and location of area covered by precipitation radar */
  MAPSIZE MM5Map;				/* Size and location of area covered by MM5 input files */
  GRID Grid;
  METLOCATION *Stat = NULL;
  OPTIONSTRUCT Options;			/* Structure with information which program options to follow */
  PIXMET LocalMet;				/* Meteorological conditions for current pixel */
  PRECIPPIX **PrecipMap = NULL;
  RADARPIX **RadarMap	= NULL;
  PIXRAD **RadiationMap = NULL;
  ROADSTRUCT **Network	= NULL;	/* 2D Array with channel information for each pixel */
  SNOWPIX **SnowMap		= NULL;
  GLPIX **GlacierMap = NULL; /* glacier model*/
  MET_MAP_PIX **MetMap	= NULL;
  SNOWTABLE *SnowAlbedo = NULL;
  SOILPIX **SoilMap		= NULL;
  SOILTABLE *SType	    = NULL;
  SOLARGEOMETRY SolarGeo;		/* Geometry of Sun-Earth system (needed for INLINE radiation calculations */
  TIMESTRUCT Time;
  TOPOPIX **TopoMap = NULL;
  UNITHYDR **UnitHydrograph = NULL;
  UNITHYDRINFO HydrographInfo;	/* Information about unit hydrograph */
  VEGPIX **VegMap = NULL;
  VEGTABLE *VType = NULL;
  WATERBALANCE Mass =			/* parameter for mass balance calculations */
    { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };

/*****************************************************************************
  Initialization Procedures 
*****************************************************************************/
  if (argc != 2) {
    fprintf(stderr, "\nUsage: %s inputfile\n\n", argv[0]);
    fprintf(stderr, "DHSVM uses two output streams: \n");
    fprintf(stderr, "Standard Out, for the majority of output \n");
    fprintf(stderr, "Standard Error, for the final mass balance \n");
    fprintf(stderr, "\nTo pipe output correctly to files: \n");
    fprintf(stderr, "(cmd > f1) >& f2 \n");
    fprintf(stderr, "where f1 is stdout_file and f2 is stderror_file\n");
    exit(EXIT_FAILURE);
  }

  sprintf(commandline, "%s %s", argv[0], argv[1]);
  printf("%s \n", commandline);
  fprintf(stderr, "%s \n", commandline);
  strcpy(InFiles.Const, argv[1]);

  printf("\nRunning DHSVM %s\n", version);
  printf("\nSTARTING INITIALIZATION PROCEDURES\n\n");

  /* Start recording time */
  start = clock();

  ReadInitFile(InFiles.Const, &Input);
  InitConstants(Input, &Options, &Map, &SolarGeo, &Time);

  InitFileIO(Options.FileFormat);
  InitTables(Time.NDaySteps, Input, &Options, &SType, &Soil, &VType, &Veg,
	     &SnowAlbedo);
  InitGlacierMap(&Map, &GlacierMap); /*glacier model*/
  InitTerrainMaps(Input, &Options, &Map, &Soil, &TopoMap, &SoilMap, &VegMap, &GlacierMap);
  
  CheckOut(&Options, Veg, Soil, VType, SType, &Map, TopoMap, VegMap, SoilMap);

  if (Options.HasNetwork)
    InitChannel(Input, &Map, Time.Dt, &ChannelData, SoilMap, &MaxStreamID, &MaxRoadID, &Options);
  else if (Options.Extent != POINT)
    InitUnitHydrograph(Input, &Map, TopoMap, &UnitHydrograph,
		       &Hydrograph, &HydrographInfo);
 
  InitNetwork(Map.NY, Map.NX, Map.DX, Map.DY, TopoMap, SoilMap, 
	      VegMap, VType, &Network, &ChannelData, Veg, &Options);

  InitMetSources(Input, &Options, &Map, TopoMap, Soil.MaxLayers, &Time,
		 &InFiles, &NStats, &Stat, &Radar, &MM5Map, &Grid);

  /* the following piece of code is for the UW PRISM project */
  /* for real-time verification of SWE at Snotel sites */
  /* Other users, set OPTION.SNOTEL to FALSE, or use TRUE with caution */

  if (Options.Snotel == TRUE && Options.Outside == FALSE) {
    printf
      ("Warning: All met stations locations are being set to the vegetation class GLACIER\n");
    printf
      ("Warning: This requires that you have such a vegetation class in your vegetation table\n");
    printf("To disable this feature set Snotel OPTION to FALSE\n");
    for (i = 0; i < NStats; i++) {
      printf("veg type for station %d is %d ", i,
	     VegMap[Stat[i].Loc.N][Stat[i].Loc.E].Veg);
      for (j = 0; j < Veg.NTypes; j++) {
	    if (VType[j].Index == GLACIER) {
	      VegMap[Stat[i].Loc.N][Stat[i].Loc.E].Veg = j;
		  break;
		}
      }
      if (j == Veg.NTypes) {	/* glacier class not found */
	    ReportError("MainDHSVM", 62);
	  }
      printf("setting to glacier type (assumed bare class): %d\n", j);
    }
  }

  InitMetMaps(Time.NDaySteps, &Map, &Radar, &Options, InFiles.WindMapPath,
	      InFiles.PrecipLapseFile, &PrecipLapseMap, &PrismMap,
	      &ShadowMap, &SkyViewMap, &EvapMap, &PrecipMap,
	      &RadarMap, &RadiationMap, SoilMap, &Soil, VegMap, &Veg, TopoMap,
	      &MM5Input, &WindModel);

  InitInterpolationWeights(&Map, &Options, TopoMap, &MetWeights, Stat, NStats);

  InitDump(Input, &Options, &Map, Soil.MaxLayers, Veg.MaxLayers, Time.Dt,
	   TopoMap, &Dump, &NGraphics, &which_graphics);

  if (Options.HasNetwork == TRUE) {
    InitChannelDump(&Options, &ChannelData, Dump.Path);
    ReadChannelState(Dump.InitStatePath, &(Time.Start), ChannelData.streams);
	if (Options.StreamTemp && Options.CanopyShading)
	  InitChannelRVeg(&Time, ChannelData.streams);
  }

  InitSnowMap(&Map, &SnowMap);
  InitAggregated(Veg.MaxLayers, Soil.MaxLayers, &Total);

  InitModelState(&(Time.Start), &Map, &Options, PrecipMap, SnowMap, SoilMap,
		 Soil, SType, VegMap, Veg, VType, Dump.InitStatePath,
		 SnowAlbedo, TopoMap, Network, &HydrographInfo, Hydrograph, &Total, GlacierMap);

#ifdef HAVE_GLACIER
/* Glacier Model is run independently to "spinup" glacier ice state over 1000 years */
  if (Options.Glacier == GLSPINUP){
   dt_year  = 1;
   year_min = 0;
   year_max = 1000;
   printf("Glacier model spin up run for %f years\n", year_max);
   main_spinup(&Map, TopoMap, SnowMap,GlacierMap, dt_year,year_min,year_max, 
   &(Time.Current), &Dump, &Options);
   return EXIT_SUCCESS;
  }
#endif

  InitNewMonth(&Time, &Options, &Map, TopoMap, PrismMap, ShadowMap,
	       &InFiles, Veg.NTypes, VType, NStats, Stat, Dump.InitStatePath);

  InitNewDay(Time.Current.JDay, &SolarGeo);

  if (NGraphics > 0) {
    printf("Initialzing X11 display and graphics \n");
    InitXGraphics(argc, argv, Map.NY, Map.NX, NGraphics, &MetMap);
  }

  shade_offset = FALSE;
  if (Options.Shading == TRUE)
    shade_offset = TRUE;

  /* Done with initialization, delete the list with input strings */
  DeleteList(Input);

  /* setup for mass balance calculations */
  Aggregate(&Map, &Options, TopoMap, &Soil, &Veg, VegMap, EvapMap, PrecipMap,
	      RadiationMap, SnowMap, SoilMap, &Total, VType, Network, &ChannelData, &roadarea);

  Mass.StartWaterStorage =
    Total.Soil.IExcess + Total.CanopyWater + Total.SoilWater + Total.Snow.Swq +
    Total.Soil.SatFlow + Total.Snow.Iwq + Total.Snow.IceRemoved;
  Mass.OldWaterStorage = Mass.StartWaterStorage;

  /* computes the number of grid cell contributing to one segment */
  if (Options.StreamTemp) 
	Init_segment_ncell(TopoMap, ChannelData.stream_map, Map.NY, Map.NX, ChannelData.streams);

/*****************************************************************************
  Perform Calculations 
*****************************************************************************/
  while (Before(&(Time.Current), &(Time.End)) ||
	 IsEqualTime(&(Time.Current), &(Time.End))) {

    /* debug */
    if (Time.Current.Month == 5 && Time.Current.Day == 20 && Time.Current.Hour >= 9 && Time.Current.Hour <= 15)
      printf("stop here for a little\n");
    /* debug ends */

    /* reset aggregated variables */
    ResetAggregate(&Soil, &Veg, &Total, &Options);

    if (IsNewMonth(&(Time.Current), Time.Dt)){
      InitNewMonth(&Time, &Options, &Map, TopoMap, PrismMap, ShadowMap,
		   &InFiles, Veg.NTypes, VType, NStats, Stat, Dump.InitStatePath);
#ifdef HAVE_GLACIER
          /* Run the glacier model at the end of every month */
	  if (Options.Glacier == GLSTATIC || Options.Glacier == GLDYNAMIC){
            dt_year  = 1.0;
            year_min = 1.0;
            year_max = 1.0;
             if(Time.Current.Month == 10 && Time.Current.Day == 1){
            /* Calculate Equilibrium Line Altitude at end of Water Year (assumed Oct1) */
	    /* will need to be changed depending on geography define water year */
	     calc_ela(&Map, TopoMap,SnowMap,GlacierMap,&(Time.Current), &Dump);
	}

            main_gl(&Map, TopoMap, SnowMap,GlacierMap, dt_year,year_min,year_max,
	    &(Time.Current), &Dump,&Options);
            /*Use the next program to output balance information for indivdual glaciers */
            gl_massbalance(&Map, TopoMap, SnowMap,GlacierMap, dt_year,year_min,
	    year_max, &(Time.Current), &Dump);
	}
#endif
    }
    if (IsNewDay(Time.DayStep)) {
      InitNewDay(Time.Current.JDay, &SolarGeo);
      PrintDate(&(Time.Current), stdout);
      printf("\n");
    }

    InitNewStep(&InFiles, &Map, &Time, Soil.MaxLayers, &Options, NStats, Stat,
		InFiles.RadarFile, &Radar, RadarMap, &SolarGeo, TopoMap, 
        SoilMap, MM5Input, WindModel, &MM5Map);

    /* initialize channel/road networks for time step */
    if (Options.HasNetwork) {
      channel_step_initialize_network(ChannelData.streams);
      channel_step_initialize_network(ChannelData.roads);
    }

    for (y = 0; y < Map.NY; y++) {
      for (x = 0; x < Map.NX; x++) {
	    if (INBASIN(TopoMap[y][x].Mask)) {
		  if (Options.Shading)
	        LocalMet =
	        MakeLocalMetData(y, x, &Map, Time.DayStep, &Options, NStats,
			       Stat, MetWeights[y][x], TopoMap[y][x].Dem,
			       &(RadiationMap[y][x]), &(PrecipMap[y][x]), &Radar,
			       RadarMap, PrismMap, &(SnowMap[y][x]),
			       SnowAlbedo, MM5Input, WindModel, PrecipLapseMap,
			       &MetMap, NGraphics, Time.Current.Month,
			       SkyViewMap[y][x], ShadowMap[Time.DayStep][y][x],
			       SolarGeo.SunMax, SolarGeo.SineSolarAltitude);
		  else
	        LocalMet =
	        MakeLocalMetData(y, x, &Map, Time.DayStep, &Options, NStats,
			       Stat, MetWeights[y][x], TopoMap[y][x].Dem,
			       &(RadiationMap[y][x]), &(PrecipMap[y][x]), &Radar,
			       RadarMap, PrismMap, &(SnowMap[y][x]),
			       SnowAlbedo, MM5Input, WindModel, PrecipLapseMap,
			       &MetMap, NGraphics, Time.Current.Month, 0.0,
			       0.0, SolarGeo.SunMax,
			       SolarGeo.SineSolarAltitude);

		  /* get surface tempeature of each soil layer */
		  for (i = 0; i < Soil.MaxLayers; i++) {
	        if (Options.HeatFlux == TRUE) {
	          if (Options.MM5 == TRUE)
		        SoilMap[y][x].Temp[i] =
				MM5Input[shade_offset + i + N_MM5_MAPS][y][x];

              /* read tempeature of each soil layer from met station input */
			  else
		        SoilMap[y][x].Temp[i] = Stat[0].Data.Tsoil[i];
			}
            /* if heat flux option is turned off, soil temperature of all 3 layers 
            is taken equal to air tempeature */
	        else
	          SoilMap[y][x].Temp[i] = LocalMet.Tair;
		  }
		  
		  MassEnergyBalance(&Options, y, x, SolarGeo.SineSolarAltitude, Map.DX, Map.DY, 
			    Time.Dt, Options.HeatFlux, Options.CanopyRadAtt, Options.Infiltration, 
				Veg.MaxLayers, &LocalMet, &(Network[y][x]), &(PrecipMap[y][x]), 
			    &(VType[VegMap[y][x].Veg-1]), &(VegMap[y][x]), &(SType[SoilMap[y][x].Soil-1]),
			    &(SoilMap[y][x]), &(SnowMap[y][x]), &(RadiationMap[y][x]), &(EvapMap[y][x]), 
                &(Total.Rad), &ChannelData, SkyViewMap);
		 
		  PrecipMap[y][x].SumPrecip += PrecipMap[y][x].Precip;
		}
	  }
    }

	/* Average all RBM inputs over each segment */
	if (Options.StreamTemp) {
	  channel_grid_avg(ChannelData.streams);
      if (Options.CanopyShading)
	    CalcCanopyShading(&Time, ChannelData.streams, &SolarGeo);
	}

 #ifndef SNOW_ONLY
    
    RouteSubSurface(Time.Dt, &Map, TopoMap, VType, VegMap, Network,
		    SType, SoilMap, &ChannelData, &Time, &Options, Dump.Path,
		    MaxStreamID, SnowMap);

    if (Options.HasNetwork)
      RouteChannel(&ChannelData, &Time, &Map, TopoMap, SoilMap, &Total, 
		   &Options, Network, SType, PrecipMap, LocalMet.Tair, LocalMet.Rh);

    if (Options.Extent == BASIN)
      RouteSurface(&Map, &Time, TopoMap, SoilMap, &Options,
        UnitHydrograph, &HydrographInfo, Hydrograph,
        &Dump, VegMap, VType, &ChannelData);

#endif

    if (NGraphics > 0)
      draw(&(Time.Current), IsEqualTime(&(Time.Current), &(Time.Start)),
	   Time.DayStep, &Map, NGraphics, which_graphics, VType,
	   SType, SnowMap, SoilMap, VegMap, TopoMap, PrecipMap,
	   PrismMap, SkyViewMap, ShadowMap, EvapMap, RadiationMap, 
	   MetMap, Network, &Options);
    
    Aggregate(&Map, &Options, TopoMap, &Soil, &Veg, VegMap, EvapMap, PrecipMap,
	      RadiationMap, SnowMap, SoilMap, &Total, VType, Network, &ChannelData, &roadarea);
    
    MassBalance(&(Time.Current), &(Time.Start), &(Dump.Balance), &Total, &Mass);

    ExecDump(&Map, &(Time.Current), &(Time.Start), &Options, &Dump, TopoMap,
	     EvapMap, RadiationMap, PrecipMap, SnowMap, MetMap, VegMap, &Veg, 
		 SoilMap, Network, &ChannelData, &Soil, &Total, &HydrographInfo,Hydrograph);
	
    IncreaseTime(&Time);
	t += 1;
  }

  ExecDump(&Map, &(Time.Current), &(Time.Start), &Options, &Dump, TopoMap,
	   EvapMap, RadiationMap, PrecipMap, SnowMap, MetMap, VegMap, &Veg, SoilMap,
	   Network, &ChannelData, &Soil, &Total, &HydrographInfo, Hydrograph);

  FinalMassBalance(&(Dump.FinalBalance), &Total, &Mass);

  printf("\nEND OF MODEL RUN\n\n");

  /* record the run time at the end of each time loop */
  finish1 = clock ();
  runtime = (finish1-start)/CLOCKS_PER_SEC;
  printf("***********************************************************************************");
  printf("\nRuntime Summary:\n");
  printf("%6.2f hours elapsed for the simulation period of %d hours (%.1f days) \n", 
	  runtime/3600, t*Time.Dt/3600, (float)t*Time.Dt/3600/24);

  return EXIT_SUCCESS;
}
Ejemplo n.º 9
0
int main(int argc, char **argv)
{
  int Dt;
  DATE Start;
  DATE End;
  TIMESTRUCT Time;

  if (argc == 1) {
    printf("\nGive a timestep in seconds on the commend-line\n\n");
    exit(EXIT_FAILURE);
  }
  /* test program so no fancy I/O checking */
  Dt = atoi(argv[1]);

  SScanDate("2/3/1999", &Start);
  PrintDate(&Start, stdout);
  printf("\n");
  SScanDate("2/3/1999-0", &Start);
  PrintDate(&Start, stdout);
  printf("\n");
  SScanDate("2/3/1999-0:0", &Start);
  PrintDate(&Start, stdout);
  printf("\n");
  SScanDate("2/3/1999-0:0:0", &Start);
  PrintDate(&Start, stdout);
  printf("\n");
  SScanDate("3/4/2000", &End);
  PrintDate(&End, stdout);
  printf("\n");
  SScanDate("3/4/2000-16", &End);
  PrintDate(&End, stdout);
  printf("\n");
  SScanDate("3/4/2000-16:30", &End);
  PrintDate(&End, stdout);
  printf("\n");
  SScanDate("3/4/2000-16:30:0", &End);
  PrintDate(&End, stdout);
  printf("\n");

  if (!After(&End, &Start))
    printf("Error in After() function\n");
  if (!Before(&Start, &End))
    printf("Error in Before() function\n");

  printf("\nInitializing time structure\nStart: ");
  InitTime(&Time, &Start, &End, NULL, NULL, Dt);
  PrintDate(&(Time.Start), stdout);
  printf("\nEnd: ");
  PrintDate(&(Time.End), stdout);
  printf("\n");
  printf("Timestep: %d seconds\n", Time.Dt);
  printf("Number of timesteps per day: %d\n", Time.NDaySteps);
  printf("Number of timesteps in model run: %d\n", Time.NTotalSteps);

  while (Time.Step < Time.NTotalSteps) {
    if (IsNewMonth(&(Time.Current), Time.Dt))
      printf("Start of new month\n");
    if (IsNewDay(Time.DayStep))
      printf("Start of new day\n");
    PrintDate(&(Time.Current), stdout);
    printf("\n");
    IncreaseTime(&Time);
  }

  printf("\nInitializing time structure\nStart: ");
  SScanDate("2-27-2000-18:30:00", &Start);
  InitTime(&Time, &Start, &End, NULL, NULL, Dt);
  PrintDate(&(Time.Start), stdout);
  printf("\nEnd: ");
  PrintDate(&(Time.End), stdout);
  printf("\n");
  printf("Timestep: %d seconds\n", Time.Dt);
  printf("Number of timesteps per day: %d\n", Time.NDaySteps);
  printf("Number of timesteps in model run: %d\n", Time.NTotalSteps);

  printf("End of tests\n");

  return EXIT_SUCCESS;
}
Ejemplo n.º 10
0
void PrintDateTime(struct rtc_time* real_time) {
  PrintDate(real_time);
  PrintTime(real_time);
}
Ejemplo n.º 11
0
/* -------------------------------------------------------------
   channel_save_outflow_cplmt
   Saves the channel outflow using a text string as the time field
   for John's RBM model
   ------------------------------------------------------------- */
int
channel_save_outflow_text_cplmt
(TIMESTRUCT *Time, char *tstring, Channel * net, CHANNEL * netfile , int flag)
{
  int err = 0;
  int Dt;
  FILE * out, *out2, *out4, *out3, *out5,*out9, *out10, *out11, 
	  *out13, *out14, *out6, *out7;

  Dt = Time->Dt;

  out = netfile->streamoutflow;
  out2 = netfile->streaminflow;
  out4 = netfile->streamISW;
  out5 = netfile->streamILW;
  out6 = netfile->streamBeam;
  out7 = netfile->streamDiffuse;
  out9 = netfile->streamVP;
  out10 = netfile->streamWND;
  out11 = netfile->streamATP;
  out13 = netfile->streamNLW;
  out14 = netfile->streamNSW;
  out3 = netfile->streamSkyView;

  /* print the start date and end date. Note that the true start date is a time step behind the 
  user specified date when the model outputs data */
  if (flag == 1) {
  PrintRBMStartDate(Dt, &(Time->Current), out);
  fprintf(out, " ");
  PrintDate(&(Time->End), out);
  fprintf(out, " %d", Dt/3600);
  fprintf(out, "\n");
  PrintRBMStartDate(Dt, &(Time->Current), out2);
  fprintf(out2, " ");
  PrintDate(&(Time->End), out2);
  fprintf(out2, " %d", Dt/3600);
  fprintf(out2, "\n");
  PrintRBMStartDate(Dt, &(Time->Current), out3);
  fprintf(out3, " ");
  PrintDate(&(Time->End), out3);
  fprintf(out3, " %d", Dt/3600);
  fprintf(out3, "\n");
  PrintRBMStartDate(Dt, &(Time->Current), out4);
  fprintf(out4, " ");
  PrintDate(&(Time->End), out4);
  fprintf(out4, " %d", Dt/3600);
  fprintf(out4, "\n");
  PrintRBMStartDate(Dt, &(Time->Current), out5);
  fprintf(out5, " ");
  PrintDate(&(Time->End), out5);
  fprintf(out5, " %d", Dt/3600);
  fprintf(out5, "\n");
  PrintRBMStartDate(Dt, &(Time->Current), out6);
  fprintf(out6, " ");
  PrintDate(&(Time->End), out6);
  fprintf(out6, " %d", Dt/3600);
  fprintf(out6, "\n");
  PrintRBMStartDate(Dt, &(Time->Current), out7);
  fprintf(out7, " ");
  PrintDate(&(Time->End), out7);
  fprintf(out7, " %d", Dt/3600);
  fprintf(out7, "\n");
  PrintRBMStartDate(Dt, &(Time->Current), out9);
  fprintf(out9, " ");
  PrintDate(&(Time->End), out9);
  fprintf(out9, " %d", Dt/3600);
  fprintf(out9, "\n");
  PrintRBMStartDate(Dt, &(Time->Current), out10);
  fprintf(out10, " ");
  PrintDate(&(Time->End), out10);
  fprintf(out10, " %d", Dt/3600);
  fprintf(out10, "\n");
  PrintRBMStartDate(Dt, &(Time->Current), out11);
  fprintf(out11, " ");
  PrintDate(&(Time->End), out11);
  fprintf(out11, " %d", Dt/3600);
  fprintf(out11, "\n");
  PrintRBMStartDate(Dt, &(Time->Current), out13);
  fprintf(out13, " ");
  PrintDate(&(Time->End), out13);
  fprintf(out13, " %d", Dt/3600);
  fprintf(out13, "\n");
  PrintRBMStartDate(Dt, &(Time->Current), out14);
  fprintf(out14, " ");
  PrintDate(&(Time->End), out14);
  fprintf(out14, " %d", Dt/3600);
  fprintf(out14, "\n");
  }

  if (flag == 1) {
    fprintf(out, "                    ");
    fprintf(out2, "                    ");
	fprintf(out3, "                    ");
    fprintf(out4, "                    ");
    fprintf(out5, "                    ");
	fprintf(out6, "                    ");
    fprintf(out7, "                    ");
    fprintf(out9, "                    ");
    fprintf(out10, "                    ");
    fprintf(out11, "                    ");
    fprintf(out13, "                    ");
    fprintf(out14, "                    ");

    for (; net != NULL; net = net->next) {
	  fprintf(out, "%12d ", net->id);
      fprintf(out2, "%12d ", net->id);
	  fprintf(out3, "%12d ", net->id);
      fprintf(out4, "%8d ", net->id);
      fprintf(out5, "%8d ", net->id);

	  fprintf(out6, "%8d ", net->id);
      fprintf(out7, "%8d ", net->id);

      fprintf(out9, "%9d ", net->id);
      fprintf(out10, "%8d ", net->id);
      fprintf(out11, "%5d ", net->id);

	  fprintf(out13, "%8d ", net->id);
	  fprintf(out14, "%8d ", net->id);
    }
    fprintf(out, "\n");
    fprintf(out2, "\n");
	fprintf(out3, "\n");
    fprintf(out4, "\n");
    fprintf(out5, "\n");
	
	fprintf(out6, "\n");
    fprintf(out7, "\n");

    fprintf(out9, "\n");
    fprintf(out10, "\n");
    fprintf(out11, "\n");

	fprintf(out13, "\n");
	fprintf(out14, "\n");
  }

  Time->Current.JDay = DayOfYear(Time->Current.Year, Time->Current.Month, Time->Current.Day);
  Time->Start.JDay = DayOfYear(Time->Start.Year, Time->Start.Month, Time->Start.Day);

  if ((Time->Current.JDay>=Time->Start.JDay+1) || 
	  (Time->Current.Year>Time->Start.Year)) {
  if (fprintf(out, "%15s ", tstring) == EOF) {
    error_handler(ERRHDL_ERROR,"channel_save_outflow: write error:%s", strerror(errno));
    err++;
  }
  if (fprintf(out2, "%15s ", tstring) == EOF) {
    error_handler(ERRHDL_ERROR,"channel_save_inflow: write error:%s", strerror(errno));
    err++;
  }
  if (fprintf(out3, "%15s ", tstring) == EOF) {
    error_handler(ERRHDL_ERROR,"channel_save_inflow: write error:%s", strerror(errno));
    err++;
  }
  if (fprintf(out4, "%15s ", tstring) == EOF) {
    error_handler(ERRHDL_ERROR,"channel_save_ISW: write error:%s", strerror(errno));
    err++;
  }
  if (fprintf(out5, "%15s ", tstring) == EOF) {
    error_handler(ERRHDL_ERROR,"channel_save_ILW: write error:%s", strerror(errno));
    err++;
  }
  if (fprintf(out9, "%15s ", tstring) == EOF) {
    error_handler(ERRHDL_ERROR,"channel_save_ActualVaporPressure: write error:%s", strerror(errno));
    err++;
  }
  if (fprintf(out10, "%15s ", tstring) == EOF) {
    error_handler(ERRHDL_ERROR,"channel_save_Wind: write error:%s", strerror(errno));
    err++;
  }
  if (fprintf(out11, "%15s ", tstring) == EOF) {
    error_handler(ERRHDL_ERROR,"channel_save_AirTemp: write error:%s", strerror(errno));
    err++;
  }
  if (fprintf(out13, "%15s ", tstring) == EOF) {
    error_handler(ERRHDL_ERROR,"channel_save_NetLW: write error:%s", strerror(errno));
    err++;
  }
  if (fprintf(out14, "%15s ", tstring) == EOF) {
    error_handler(ERRHDL_ERROR,"channel_save_NetSW: write error:%s", strerror(errno));
    err++;
  }
  if (fprintf(out6, "%15s ", tstring) == EOF) {
    error_handler(ERRHDL_ERROR,"channel_save_ILW: write error:%s", strerror(errno));
    err++;
  }
  if (fprintf(out7, "%15s ", tstring) == EOF) {
    error_handler(ERRHDL_ERROR,"channel_save_ILW: write error:%s", strerror(errno));
    err++;
  }

  for (; net != NULL; net = net->next) {
    if (fprintf(out, "%12.4f ", net->outflow/Dt) == EOF) {
	  error_handler(ERRHDL_ERROR, "channel_save_outflow: write error:%s", strerror(errno));
	  err++;
	}
    if (fprintf(out2, "%12.4f ", net->inflow/Dt) == EOF) {
      error_handler(ERRHDL_ERROR, "channel_save_inflow: write error:%s", strerror(errno));
      err++;
	}
    if (fprintf(out4, "%8.2f ", net->ISW ) == EOF) {
      error_handler(ERRHDL_ERROR, "channel_save_ISW: write error:%s", strerror(errno));
      err++;
    }
    if (fprintf(out5, "%8.2f ", net->ILW ) == EOF) {
      error_handler(ERRHDL_ERROR, "channel_save_ILW: write error:%s", strerror(errno));
      err++;
    }
    if (fprintf(out9, "%9.2f ", net->VP ) == EOF) {
      error_handler(ERRHDL_ERROR, "channel_save_ActualVaporPressure: write error:%s", strerror(errno));
      err++;
    }
    if (fprintf(out10, "%8.2f ", net->WND ) == EOF) {
      error_handler(ERRHDL_ERROR, "channel_save_Wind: write error:%s", strerror(errno));
      err++;
    }
    if (fprintf(out11, "%5.2f ", net->ATP ) == EOF) {
      error_handler(ERRHDL_ERROR, "channel_save_AirTemp: write error:%s", strerror(errno));
      err++;
    }
	if (fprintf(out13, "%8.2f ", net->NLW) == EOF) {
      error_handler(ERRHDL_ERROR, "channel_save_NetLW: write error:%s", strerror(errno));
      err++;
    }
	if (fprintf(out14, "%8.2f ", net->NSW) == EOF) {
      error_handler(ERRHDL_ERROR, "channel_save_NetSW: write error:%s", strerror(errno));
      err++;
    }
	if (fprintf(out6, "%8.2f ", net->Beam ) == EOF) {
      error_handler(ERRHDL_ERROR, "channel_save_ISW: write error:%s", strerror(errno));
      err++;
    }
	if (fprintf(out7, "%8.2f ", net->Diffuse) == EOF) {
      error_handler(ERRHDL_ERROR, "channel_save_ILW: write error:%s", strerror(errno));
      err++;
    }
	if (fprintf(out3, "%8.2f ", net->skyview) == EOF) {
      error_handler(ERRHDL_ERROR, "channel_save_ILW: write error:%s", strerror(errno));
      err++;
    }
  }
  fprintf(out, "\n");
  fprintf(out2, "\n");
  fprintf(out3, "\n");
  fprintf(out4, "\n");
  fprintf(out5, "\n");

  fprintf(out6, "\n");
  fprintf(out7, "\n");

  fprintf(out9, "\n");
  fprintf(out10, "\n");
  fprintf(out11, "\n");

  fprintf(out13, "\n");
  fprintf(out14, "\n");
  }

  return (err);
}
Ejemplo n.º 12
0
/*****************************************************************************
  StoreModelState()

  Store the current state of the model.

  The state variables for DHSVM include the following variables:

    - Canopy interception for each vegetation layer

    - Snow pack conditions:
      - presence/absence
      - number of days since last snowfall (used in albedo calculation)
      - snow water equivalent
      - for each layer of the snow pack:
        - liquid water content
        - temperature
      - cold content

    - Soil conditions:
      - for each soil layer:
        - soil moisture (also for the layer below the deepest root zone)
        - temperature
      - surface temperature
      - ground heat storage
*****************************************************************************/
void StoreModelState(char *Path, DATE * Current, MAPSIZE * Map,
		     OPTIONSTRUCT * Options, TOPOPIX ** TopoMap,
		     PRECIPPIX ** PrecipMap, SNOWPIX ** SnowMap,
		     MET_MAP_PIX ** MetMap, RADCLASSPIX ** RadMap,
		     VEGCHEMPIX ** VegChemMap, LAYER * Veg, SOILPIX ** SoilMap,
		     LAYER * Soil, UNITHYDRINFO * HydrographInfo,
		     float *Hydrograph)
{
  const char *Routine = "StoreModelState";
  char Str[NAMESIZE + 1];
  char FileLabel[MAXSTRING + 1];
  char FileName[NAMESIZE + 1];
  FILE *HydroStateFile;
  int i;			/* counter */
  int x;			/* counter */
  int y;			/* counter */
  int NSoil;			/* Number of soil layers for current pixel */
  int NVeg;			/* Number of veg layers for current pixel */
  MAPDUMP DMap;			/* Dump Info */
  void *Array;

  /* print a message to stdout that state is being stored */
  
  printf("Saving Model State at: ");
  PrintDate(Current, stdout);
  printf("\n");

  /* Only save met state during debug, it is not needed for start-up */
  if (MetMap != NULL && DEBUG) {

    sprintf(Str, "%02d.%02d.%04d.%02d.%02d.%02d", Current->Month, Current->Day,
	    Current->Year, Current->Hour, Current->Min, Current->Sec);
    sprintf(FileName, "%sMet.State.%s%s", Path, Str, fileext);
    strcpy(FileLabel, "Basic Meteorology at time step");

    CreateMapFile(FileName, FileLabel, Map);

    if (!(Array = (float *) calloc(Map->NY * Map->NX, sizeof(float))))
      ReportError((char *) Routine, 1);

    for (y = 0; y < Map->NY; y++) {
      for (x = 0; x < Map->NX; x++) {
	if (INBASIN(TopoMap[y][x].Mask)) {

	  ((float *) Array)[y * Map->NX + x] = PrecipMap[y][x].Precip;

	}
	else
	  ((float *) Array)[y * Map->NX + x] = NA;
      }
    }
    DMap.ID = 201;
    DMap.Resolution = MAP_OUTPUT;
    strcpy(DMap.FileName, "");
    GetVarAttr(&DMap);
    Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

    for (y = 0; y < Map->NY; y++) {
      for (x = 0; x < Map->NX; x++) {
	if (INBASIN(TopoMap[y][x].Mask)) {

	  ((float *) Array)[y * Map->NX + x] = MetMap[y][x].accum_precip;

	}
	else
	  ((float *) Array)[y * Map->NX + x] = NA;
      }
    }
    DMap.ID = 701;
    DMap.Resolution = MAP_OUTPUT;
    strcpy(DMap.FileName, "");
    GetVarAttr(&DMap);
    Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

    for (y = 0; y < Map->NY; y++) {
      for (x = 0; x < Map->NX; x++) {
	if (INBASIN(TopoMap[y][x].Mask)) {

	  ((float *) Array)[y * Map->NX + x] = MetMap[y][x].air_temp;

	}
	else
	  ((float *) Array)[y * Map->NX + x] = NA;
      }
    }
    DMap.ID = 702;
    DMap.Resolution = MAP_OUTPUT;
    strcpy(DMap.FileName, "");
    GetVarAttr(&DMap);
    Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

    for (y = 0; y < Map->NY; y++) {
      for (x = 0; x < Map->NX; x++) {
	if (INBASIN(TopoMap[y][x].Mask)) {

	  ((float *) Array)[y * Map->NX + x] = MetMap[y][x].wind_speed;

	}
	else
	  ((float *) Array)[y * Map->NX + x] = NA;
      }
    }
    DMap.ID = 703;
    DMap.Resolution = MAP_OUTPUT;
    strcpy(DMap.FileName, "");
    GetVarAttr(&DMap);
    Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

    for (y = 0; y < Map->NY; y++) {
      for (x = 0; x < Map->NX; x++) {
	if (INBASIN(TopoMap[y][x].Mask)) {

	  ((float *) Array)[y * Map->NX + x] = MetMap[y][x].humidity;

	}
	else
	  ((float *) Array)[y * Map->NX + x] = NA;
      }
    }
    DMap.ID = 704;
    DMap.Resolution = MAP_OUTPUT;
    strcpy(DMap.FileName, "");
    GetVarAttr(&DMap);
    Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

    for (y = 0; y < Map->NY; y++) {
      for (x = 0; x < Map->NX; x++) {
	if (INBASIN(TopoMap[y][x].Mask)) {

	  ((float *) Array)[y * Map->NX + x] = RadMap[y][x].Beam +
	    RadMap[y][x].Diffuse;

	}
	else
	  ((float *) Array)[y * Map->NX + x] = NA;
      }
    }
    DMap.ID = 303;
    DMap.Resolution = MAP_OUTPUT;
    strcpy(DMap.FileName, "");
    GetVarAttr(&DMap);
    Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

    free(Array);
  }
  /* Store the canopy interception */

  sprintf(Str, "%02d.%02d.%04d.%02d.%02d.%02d", Current->Month, Current->Day,
	  Current->Year, Current->Hour, Current->Min, Current->Sec);
  printf("Writing %sInterception.State.%s%s...\n", Path, Str, fileext);
  sprintf(FileName, "%sInterception.State.%s%s", Path, Str, fileext);
  strcpy(FileLabel, "Interception storage for each vegetation layer");

  CreateMapFile(FileName, FileLabel, Map);

  if (!(Array = (float *) calloc(Map->NY * Map->NX, sizeof(float))))
    ReportError((char *) Routine, 1);

  for (i = 0; i < Veg->MaxLayers; i++) {
    for (y = 0; y < Map->NY; y++) {
      for (x = 0; x < Map->NX; x++) {
	if (INBASIN(TopoMap[y][x].Mask)) {
	  NVeg = Veg->NLayers[(VegChemMap[y][x].Veg - 1)];
	  if (i < NVeg)
	    ((float *) Array)[y * Map->NX + x] = PrecipMap[y][x].IntRain[i];
	  else
	    ((float *) Array)[y * Map->NX + x] = NA;
	}
	else
	  ((float *) Array)[y * Map->NX + x] = NA;
      }
    }
    DMap.ID = 202;
    DMap.Layer = i;
    DMap.Resolution = MAP_OUTPUT;
    strcpy(DMap.FileName, "");
    GetVarAttr(&DMap);
    Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);
  }

  for (i = 0; i < Veg->MaxLayers; i++) {
    for (y = 0; y < Map->NY; y++) {
      for (x = 0; x < Map->NX; x++) {
	if (INBASIN(TopoMap[y][x].Mask)) {
	  NVeg = Veg->NLayers[(VegChemMap[y][x].Veg - 1)];
	  if (i < NVeg)
	    ((float *) Array)[y * Map->NX + x] = PrecipMap[y][x].IntSnow[i];
	  else
	    ((float *) Array)[y * Map->NX + x] = NA;
	}
	else
	  ((float *) Array)[y * Map->NX + x] = NA;
      }
    }
    DMap.ID = 203;
    DMap.Layer = i;
    DMap.Resolution = MAP_OUTPUT;
    strcpy(DMap.FileName, "");
    GetVarAttr(&DMap);
    Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);
  }

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask)) {
	((float *) Array)[y * Map->NX + x] = PrecipMap[y][x].TempIntStorage;
      }
      else {
	((float *) Array)[y * Map->NX + x] = NA;
      }
    }
  }
  DMap.ID = 204;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);
  Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

  free(Array);

  /* Store the snow pack conditions */

  printf("Writing %sSnow.State.%s%s...\n", Path, Str, fileext);
  sprintf(FileName, "%sSnow.State.%s%s", Path, Str, fileext);
  strcpy(FileLabel, "Snow pack moisture and temperature state");
  CreateMapFile(FileName, FileLabel, Map);

  if (!(Array = (float *) calloc(Map->NY * Map->NX, sizeof(float))))
    ReportError((char *) Routine, 1);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask))
	((float *) Array)[y * Map->NX + x] = (float) SnowMap[y][x].HasSnow;
      else
	((float *) Array)[y * Map->NX + x] = NA;
    }
  }
  DMap.ID = 401;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);
  Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask))
	((float *) Array)[y * Map->NX + x] = (float) SnowMap[y][x].LastSnow;
      else
	((float *) Array)[y * Map->NX + x] = NA;
    }
  }
  DMap.ID = 403;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);
  Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask))
	((float *) Array)[y * Map->NX + x] = SnowMap[y][x].Swq;
      else
	((float *) Array)[y * Map->NX + x] = NA;
    }
  }
  DMap.ID = 404;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);
  Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask))
	((float *) Array)[y * Map->NX + x] = SnowMap[y][x].PackWater;
      else
	((float *) Array)[y * Map->NX + x] = NA;
    }
  }
  DMap.ID = 406;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);
  Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask))
	((float *) Array)[y * Map->NX + x] = SnowMap[y][x].TPack;
      else
	((float *) Array)[y * Map->NX + x] = NA;
    }
  }
  DMap.ID = 407;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);
  Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask))
	((float *) Array)[y * Map->NX + x] = SnowMap[y][x].SurfWater;
      else
	((float *) Array)[y * Map->NX + x] = NA;
    }
  }
  DMap.ID = 408;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);
  Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask))
	((float *) Array)[y * Map->NX + x] = SnowMap[y][x].TSurf;
      else
	((float *) Array)[y * Map->NX + x] = NA;
    }
  }
  DMap.ID = 409;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);
  Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask))
	((float *) Array)[y * Map->NX + x] = SnowMap[y][x].ColdContent;
      else
	((float *) Array)[y * Map->NX + x] = NA;
    }
  }
  DMap.ID = 410;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);
  Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

  free(Array);

  /* Store the soil conditions */

  printf("Writing %sSoil.State.%s%s...\n", Path, Str, fileext);
  sprintf(FileName, "%sSoil.State.%s%s", Path, Str, fileext);
  strcpy(FileLabel, "Soil moisture and temperature state");
  CreateMapFile(FileName, FileLabel, Map);

  if (!(Array = (float *) calloc(Map->NY * Map->NX, sizeof(float))))
    ReportError((char *) Routine, 1);

  for (i = 0; i < Soil->MaxLayers + 1; i++) {
    for (y = 0; y < Map->NY; y++) {
      for (x = 0; x < Map->NX; x++) {
	if (INBASIN(TopoMap[y][x].Mask)) {
	  NSoil = Soil->NLayers[(SoilMap[y][x].Soil - 1)];
	  if (i <= NSoil)
	    ((float *) Array)[y * Map->NX + x] = SoilMap[y][x].Moist_m_m[i];
	  else
	    ((float *) Array)[y * Map->NX + x] = NA;
	}
	else
	  ((float *) Array)[y * Map->NX + x] = NA;
      }
    }
    DMap.ID = 501;
    DMap.Layer = i;
    DMap.Resolution = MAP_OUTPUT;
    strcpy(DMap.FileName, "");
    GetVarAttr(&DMap);
    Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);
  }

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask))
	((float *) Array)[y * Map->NX + x] = SoilMap[y][x].TSurf;
      else
	((float *) Array)[y * Map->NX + x] = SoilMap[y][x].TSurf;
    }
  }
  DMap.ID = 505;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);
  Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

  for (i = 0; i < Soil->MaxLayers; i++) {
    for (y = 0; y < Map->NY; y++) {
      for (x = 0; x < Map->NX; x++) {
	if (INBASIN(TopoMap[y][x].Mask)) {
	  NSoil = Soil->NLayers[SoilMap[y][x].Soil - 1];
	  if (i < NSoil)
	    ((float *) Array)[y * Map->NX + x] = SoilMap[y][x].Temp[i];
	  else
	    ((float *) Array)[y * Map->NX + x] = NA;
	}
	else
	  ((float *) Array)[y * Map->NX + x] = NA;
      }
    }
    DMap.ID = 511;
    DMap.Layer = i;
    DMap.Resolution = MAP_OUTPUT;
    strcpy(DMap.FileName, "");
    GetVarAttr(&DMap);
    Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);
  }

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask))
	((float *) Array)[y * Map->NX + x] = SoilMap[y][x].Qst;
      else
	((float *) Array)[y * Map->NX + x] = NA;
    }
  }
  DMap.ID = 510;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);
  Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask))
	((float *) Array)[y * Map->NX + x] = SoilMap[y][x].Runoff_m;
      else
	((float *) Array)[y * Map->NX + x] = NA;
    }
  }
  DMap.ID = 512;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);
  Write2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, &DMap, 0);

  free(Array);

  /* If the unit hydrograph is used for flow routing, store the unit 
     hydrograph array */

  if (Options->Extent == BASIN && Options->HasNetwork == FALSE) {
    sprintf(FileName, "%sHydrograph.State.%s", Path, Str);
    OpenFile(&HydroStateFile, FileName, "w", FALSE);
    for (i = 0; i < HydrographInfo->TotalWaveLength; i++)
      fprintf(HydroStateFile, "%f\n", Hydrograph[i]);
    fclose(HydroStateFile);
  }
}