Ejemplo n.º 1
0
/*****************************************************************************
  ReadRadarMap()
*****************************************************************************/
void ReadRadarMap(DATE * Current, DATE * StartRadar, int Dt, MAPSIZE * Radar,
		  RADARPIX ** RadarMap, char *FileName)
{
  const char *Routine = "ReadRadarMap";
  int i;			/* counter */
  int x;			/* counter */
  int y;			/* counter */
  int RadarStep;		/* Location of current timestep in radarfile */
  int NumberType;		/* number type */
  void *Array;

  if (DEBUG)
    printf("Reading precipitation radar data from file: %s\n", FileName);

  NumberType = NC_FLOAT;

  if (!(Array = (float *) calloc(Radar->NY * Radar->NX,
				 SizeOfNumberType(NumberType))))
    ReportError((char *) Routine, 1);

  RadarStep = NumberOfSteps(StartRadar, Current, Dt);

  /* Read the precipitation */
  Read2DMatrix(FileName, Array, NumberType, Radar->NY, Radar->NX, RadarStep);

  for (y = 0, i = 0; y < Radar->NY; y++)
    for (x = 0; x < Radar->NX; x++, i++)
      RadarMap[y][x].Precip = ((float *) Array)[i];

  free(Array);
}
Ejemplo n.º 2
0
void InitPrecipLapseMap(char *PrecipLapseFile, int NY, int NX,
			float ***PrecipLapseMap)
{
  const char *Routine = "InitPrecipLapseMap";
  int NumberType;
  int x;			/* counter */
  int y;			/* counter */
  float *Array = NULL;

  if (!((*PrecipLapseMap) = (float **) calloc(NY, sizeof(float *))))
    ReportError((char *) Routine, 1);

  for (y = 0; y < NY; y++) {
    if (!((*PrecipLapseMap)[y] = (float *) calloc(NX, sizeof(float))))
      ReportError((char *) Routine, 1);
  }

  if (!(Array = (float *) calloc(NY * NX, sizeof(float))))
    ReportError((char *) Routine, 1);
  NumberType = NC_FLOAT;

  Read2DMatrix(PrecipLapseFile, Array, NumberType, NY, NX, 0);
  for (y = 0; y < NY; y++) {
    for (x = 0; x < NX; x++) {
      (*PrecipLapseMap)[y][x] = Array[y * NX + x];
    }
  }

  free(Array);
}
Ejemplo n.º 3
0
/*****************************************************************************
  InitVegMap()
*****************************************************************************/
void InitVegMap(OPTIONSTRUCT * Options, LISTPTR Input, MAPSIZE * Map, VEGPIX *** VegMap)
{
  const char *Routine = "InitVegMap";
  char VarName[BUFSIZE + 1];
  char VegMapFileName[BUFSIZE + 1];
  int i;			/* counter */
  int x;			/* counter */
  int y;			/* counter */
  int flag;
  int NumberType;		/* number type */
  unsigned char *Type;		/* Vegetation type */

  /* Get the map filename from the [VEGETATION] section */
  GetInitString("VEGETATION", "VEGETATION MAP FILE", "", VegMapFileName,
		(unsigned long) BUFSIZE, Input);
  if (!VegMapFileName)
    ReportError("VEGETATION MAP FILE", 51);

  /* Read the vegetation type */
  GetVarName(005, 0, VarName);
  GetVarNumberType(005, &NumberType);
  if (!(Type = (unsigned char *) calloc(Map->NX * Map->NY,
					SizeOfNumberType(NumberType))))
    ReportError((char *) Routine, 1);
  flag = Read2DMatrix(VegMapFileName, Type, NumberType, Map->NY, Map->NX, 0, VarName, 0);

  /* Assign the attributes to the correct map pixel */
  if (!(*VegMap = (VEGPIX **) calloc(Map->NY, sizeof(VEGPIX *))))
    ReportError((char *) Routine, 1);
  for (y = 0; y < Map->NY; y++) {
    if (!((*VegMap)[y] = (VEGPIX *) calloc(Map->NX, sizeof(VEGPIX))))
      ReportError((char *) Routine, 1);
  }

  if ((Options->FileFormat == NETCDF && flag == 0) 
	  || (Options->FileFormat == BIN))
  {
  for (y = 0, i = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++, i++) {
      (*VegMap)[y][x].Veg = Type[i];
      (*VegMap)[y][x].Tcanopy = 0.0;
		  }
	  }
  }
  else if (Options->FileFormat == NETCDF && flag == 1){
	  for (y = Map->NY - 1, i = 0; y >= 0; y--) {
		  for (x = 0; x < Map->NX; x++, i++) {
			  (*VegMap)[y][x].Veg = Type[i]; 
			  (*VegMap)[y][x].Tcanopy = 0.0;
		  }
	  }
  }
  else ReportError((char *) Routine, 57);

  free(Type);
}
Ejemplo n.º 4
0
void InitShadeMap(OPTIONSTRUCT * Options, int NDaySteps, int NY, int NX,
		  unsigned char ****ShadowMap, float ***SkyViewMap)
{
  const char *Routine = "InitShadeMap";
  char VarName[BUFSIZE + 1];	/* Variable name */
  int x;			/* counter */
  int y;			/* counter */
  int n;
  int NumberType;
  float *Array = NULL;

  if (!((*ShadowMap) =
       (unsigned char ***) calloc(NDaySteps, sizeof(unsigned char **))))
    ReportError((char *) Routine, 1);
  for (n = 0; n < NDaySteps; n++) {
    if (!((*ShadowMap)[n] =
	 (unsigned char **) calloc(NY, sizeof(unsigned char *))))
      ReportError((char *) Routine, 1);
    for (y = 0; y < NY; y++) {
      if (!((*ShadowMap)[n][y] =
	   (unsigned char *) calloc(NX, sizeof(unsigned char))))
	ReportError((char *) Routine, 1);
    }
  }

  if (!((*SkyViewMap) = (float **) calloc(NY, sizeof(float *))))
    ReportError((char *) Routine, 1);
  for (y = 0; y < NY; y++) {
    if (!((*SkyViewMap)[y] = (float *) calloc(NX, sizeof(float))))
      ReportError((char *) Routine, 1);
  }

  for (y = 0; y < NY; y++) {
    for (x = 0; x < NX; x++) {
      (*SkyViewMap)[y][x] = 1.0;
    }
  }

  GetVarName(305, 0, VarName);
  GetVarNumberType(305, &NumberType);
  if (!(Array = (float *) calloc(NY * NX, sizeof(float))))
    ReportError((char *) Routine, 1);
  Read2DMatrix(Options->SkyViewDataPath, Array, NumberType, NY, NX, 0, 
	       VarName);
  for (y = 0; y < NY; y++) {
    for (x = 0; x < NX; x++) {
      (*SkyViewMap)[y][x] = Array[y * NX + x];
    }
  }

  free(Array);
}
Ejemplo n.º 5
0
/*******************************************************************************
  InitWindModelMaps()
*******************************************************************************/
void InitWindModelMaps(char *WindPath, int NY, int NX, float ****WindModel)
{
  char *Routine = "InitWindModelMaps";
  char InFileName[NAMESIZE + 1];
  char Str[NAMESIZE + 1];
  int NumberType;
  int n;
  int x;
  int y;
  float *Array = NULL;

  if (!((*WindModel) = (float ***) calloc(NWINDMAPS, sizeof(float **))))
    ReportError(Routine, 1);

  for (n = 0; n < NWINDMAPS; n++) {
    if (!((*WindModel)[n] = (float **) calloc(NY, sizeof(float *))))
      ReportError(Routine, 1);
    for (y = 0; y < NY; y++) {
      if (!((*WindModel)[n][y] = (float *) calloc(NX, sizeof(float))))
	ReportError(Routine, 1);
    }
  }

  if (!(Array = (float *) calloc(NY * NX, sizeof(float))))
    ReportError((char *) Routine, 1);
  NumberType = NC_FLOAT;

  /* Read the wind model maps */
  for (n = 0; n < NWINDMAPS; n++) {
    sprintf(Str, "%02d", n + 1);
    sprintf(InFileName, "%s%s%s", WindPath, Str, fileext);
    Read2DMatrix(InFileName, Array, NumberType, NY, NX, 0);
    for (y = 0; y < NY; y++) {
      for (x = 0; x < NX; x++) {
	(*WindModel)[n][y][x] = Array[y * NX + x];
      }
    }
  }
  free(Array);
}
Ejemplo n.º 6
0
/*****************************************************************************
  Function name: InitModelState()

  Purpose      : Initialize the state of the model using initial conditions
                 or a saved state from an earlier model run

  Required     :

  Returns      : void

  Modifies     :

  Comments : 
    Initialize the model state, by reading the state variables from a series
    of files.  This allows restarts of the model from any timestep for which
    the model state is known.  These model states can be stored using the
    routine StoreModelState().  Timesteps at which to dump the model state
    can be specified in the file with dump information.

*****************************************************************************/
void InitModelState(DATE * Start, MAPSIZE * Map, OPTIONSTRUCT * Options,
		    PRECIPPIX ** PrecipMap, SNOWPIX ** SnowMap,
		    SOILPIX ** SoilMap, LAYER Soil, SOILTABLE * SType,
		    VEGCHEMPIX ** VegChemMap, LAYER Veg, VEGTABLE * VType, char *Path,
		    SNOWTABLE * SnowAlbedo, TOPOPIX ** TopoMap,
		    ROADSTRUCT ** Network, UNITHYDRINFO * HydrographInfo,
		    float *Hydrograph)
{
  const char *Routine = "InitModelState";
  char Str[NAMESIZE + 1];
  char FileName[NAMESIZE + 1];
  FILE *HydroStateFile;
  int i;			/* counter */
  int x;			/* counter */
  int y;			/* counter */
  int NSet;			/* Number of dataset to be read */
  int NSoil;			/* Number of soil layers for current pixel */
  int NVeg;			/* Number of veg layers for current pixel */
  void *Array;
  MAPDUMP DMap;			/* Dump Info */
  float remove;

 
  /* Restore canopy interception */

  NSet = 0;

  if (DEBUG)
    printf("Restoring canopy conditions\n");

  sprintf(Str, "%02d.%02d.%02d.%02d.%02d.%02d", Start->Month, Start->Day,
	  Start->Year, Start->Hour, Start->Min, Start->Sec);

  printf("Reading %sInterception.State.%s%s...\n",Path, Str, fileext);
  sprintf(FileName, "%sInterception.State.%s%s", Path, Str, fileext);

  DMap.ID = 202;
  DMap.Layer = 0;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);
  if (!(Array = calloc(Map->NY * Map->NX, SizeOfNumberType(DMap.NumberType))))
    ReportError((char *) Routine, 1);

  for (i = 0; i < Veg.MaxLayers; i++) {

    DMap.ID = 202;
    DMap.Layer = i;
    DMap.Resolution = MAP_OUTPUT;
    strcpy(DMap.FileName, "");
    GetVarAttr(&DMap);

    Read2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, NSet++,
		 DMap.Name);

    for (y = 0; y < Map->NY; y++) {
      for (x = 0; x < Map->NX; x++) {
	if (INBASIN(TopoMap[y][x].Mask)) {
	  PrecipMap[y][x].IntRain[i] = 0.0;
	  NVeg = Veg.NLayers[(VegChemMap[y][x].Veg - 1)];
	  if (i < NVeg) {
	    PrecipMap[y][x].IntRain[i] = ((float *) Array)[y * Map->NX + x];
	    if (PrecipMap[y][x].IntRain[i] < 0.0) {
	      fprintf(stderr, "InitModelState at (x, y) is (%d, %d):\n", x, y);
	      fprintf(stderr,
		      "\tRain interception negative on layer %d of max %d ... reset to 0\n",
		      i, Veg.MaxLayers);
	      PrecipMap[y][x].IntRain[i] = 0.0;
	    }
	  }
	}
      }
    }
  }

  for (i = 0; i < Veg.MaxLayers; i++) {

    DMap.ID = 203;
    DMap.Layer = i;
    DMap.Resolution = MAP_OUTPUT;
    strcpy(DMap.FileName, "");
    GetVarAttr(&DMap);

    Read2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, NSet++,DMap.Name);

    for (y = 0; y < Map->NY; y++) {
      for (x = 0; x < Map->NX; x++) {
	if (INBASIN(TopoMap[y][x].Mask)) {
	  PrecipMap[y][x].IntSnow[i] = 0.0;
	  NVeg = Veg.NLayers[(VegChemMap[y][x].Veg - 1)];
	  if (i < NVeg) {
	    PrecipMap[y][x].IntSnow[i] = ((float *) Array)[y * Map->NX + x];
	    if (PrecipMap[y][x].IntSnow[i] < 0.0) {
	      fprintf(stderr, "InitModelState at (x, y) is (%d, %d):\n", x, y);
	      fprintf(stderr,"Snow interception negative on layer %d of max %d ... reset to 0\n",i, Veg.MaxLayers);
	      PrecipMap[y][x].IntSnow[i] = 0.0;
	    }
	  }
	}
      }
    }
  }

  DMap.ID = 204;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);

  Read2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, NSet++,DMap.Name);
  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask)) {
	PrecipMap[y][x].TempIntStorage = ((float *) Array)[y * Map->NX + x];
	if (PrecipMap[y][x].TempIntStorage < 0.0) {
	  fprintf(stderr, "InitModelState at (x, y) is (%d, %d):\n", x, y);
	  fprintf(stderr,
		  "Total intercepted precipitation negative on layer %d of max %d ... reset to 0\n",
		  i, Veg.MaxLayers);
	  PrecipMap[y][x].TempIntStorage = 0.0;
	}
      }
    }
  }

  free(Array);

  /* Restore snow pack conditions */

  NSet = 0;

  if (DEBUG)
    printf("Restoring snow pack conditions\n");

  printf("Reading %sSnow.State.%s%s...\n",Path, Str, fileext);
  sprintf(FileName, "%sSnow.State.%s%s", Path, Str, fileext);

  DMap.ID = 401;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);

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

  Read2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, NSet++,
	       DMap.Name);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask)) {
	SnowMap[y][x].HasSnow =
	  (unsigned char) ((float *) Array)[y * Map->NX + x];
      }
    }
  }

  DMap.ID = 403;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);

  Read2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, NSet++,
	       DMap.Name);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask)) {
	SnowMap[y][x].LastSnow =
	  (unsigned short) ((float *) Array)[y * Map->NX + x];
      }
    }
  }

  DMap.ID = 404;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);

  Read2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, NSet++,
	       DMap.Name);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask)) {
	SnowMap[y][x].Swq = ((float *) Array)[y * Map->NX + x];
	//if ( SnowMap[y][x].Swq > 100 ) SnowMap[y][x].Swq = 100;
      }
    }
  }

  DMap.ID = 406;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);

  Read2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, NSet++,
	       DMap.Name);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask)) {
	SnowMap[y][x].PackWater = ((float *) Array)[y * Map->NX + x];
      }
    }
  }

  DMap.ID = 407;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);

  Read2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, NSet++,
	       DMap.Name);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask)) {
	SnowMap[y][x].TPack = ((float *) Array)[y * Map->NX + x];
      }
    }
  }

  DMap.ID = 408;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);

  Read2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, NSet++,
	       DMap.Name);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask)) {
	SnowMap[y][x].SurfWater = ((float *) Array)[y * Map->NX + x];
      }
    }
  }

  DMap.ID = 409;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);

  Read2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, NSet++,
	       DMap.Name);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask)) {
	SnowMap[y][x].TSurf = ((float *) Array)[y * Map->NX + x];
      }
    }
  }

  DMap.ID = 410;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);

  Read2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, NSet++,
	       DMap.Name);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask)) {
	SnowMap[y][x].ColdContent = ((float *) Array)[y * Map->NX + x];
      }
    }
  }
	free(Array);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask)) {
	if (SnowMap[y][x].HasSnow) {
	  SnowMap[y][x].Albedo = CalcSnowAlbedo(SnowMap[y][x].TSurf,
						SnowMap[y][x].LastSnow,
						SnowAlbedo);
	} else {
	  SnowMap[y][x].Albedo = 0;
	}
	SnowMap[y][x].ShearStress = 0.0;
	SnowMap[y][x].IceA = 0.0;
	SnowMap[y][x].IceFlux = 0.0;
	SnowMap[y][x].IceVelocity = 0.0;
      } else {
        SnowMap[y][x].ShearStress = NA;
	SnowMap[y][x].IceA = NA;
	SnowMap[y][x].IceFlux = NA;
	SnowMap[y][x].Albedo = NA;
	SnowMap[y][x].IceVelocity = NA;
      }
    }
  }

  /* Restore soil conditions */

  NSet = 0;

  printf("Reading %sSoil.State.%s%s...\n",Path, Str, fileext);
  sprintf(FileName, "%sSoil.State.%s%s", Path, Str, fileext);

  DMap.ID = 501;
  DMap.Layer = 0;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);

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

  for (i = 0; i < Soil.MaxLayers + 1; i++) {

    DMap.ID = 501;
    DMap.Layer = i;
    DMap.Resolution = MAP_OUTPUT;
    strcpy(DMap.FileName, "");
    GetVarAttr(&DMap);

    Read2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, NSet++,DMap.Name);
    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) {			 
				SoilMap[y][x].Moist_m_m[i] = ((float *) Array)[y * Map->NX + x];
				if (SoilMap[y][x].Moist_m_m[i] < 0.0) {
					fprintf(stderr, "InitModelState at (x, y) is (%d, %d):\n", x, y);
					fprintf(stderr,"Soil moisture negative in layer %d of max %d ... reset to 0, was %f\n",
						i, Soil.MaxLayers, SoilMap[y][x].Moist_m_m[i]);
				SoilMap[y][x].Moist_m_m[i] = 0.0;
				}
				}
		/* this appears to be redundant - value also set by init-state file jsb 3/4/09
	  if (i == NSoil) {
	    if (SoilMap[y][x].Moist_m_m[i] < SType[SoilMap[y][x].Soil - 1].FCap[NSoil - 1]) 
	      SoilMap[y][x].Moist_m_m[i] = SType[SoilMap[y][x].Soil - 1].FCap[NSoil - 1];
	    
	  }
	  if (i < NSoil) {
	    if (SoilMap[y][x].Moist_m_m[i] <SType[SoilMap[y][x].Soil - 1].WP[NSoil - 1]) {
	      SoilMap[y][x].Moist_m_m[i] = SType[SoilMap[y][x].Soil - 1].WP[NSoil - 1];
	    }
	  }*/
	}
      }
    }
  }

  DMap.ID = 505;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);

  Read2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, NSet++,
	       DMap.Name);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask)) {
	SoilMap[y][x].TSurf = ((float *) Array)[y * Map->NX + x];
      }
    }
  }

  for (i = 0; i < Soil.MaxLayers; i++) {

    DMap.ID = 511;
    DMap.Layer = i;
    DMap.Resolution = MAP_OUTPUT;
    strcpy(DMap.FileName, "");
    GetVarAttr(&DMap);

    Read2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, NSet++,
		 DMap.Name);
    printf("Reading Soil Temperature State[%d]\n",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)
	    SoilMap[y][x].Temp[i] = ((float *) Array)[y * Map->NX + x];
	}
      }
    }
  }

  DMap.ID = 510;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);

  Read2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, NSet++,
	       DMap.Name);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask)) {
	SoilMap[y][x].Qst = ((float *) Array)[y * Map->NX + x];
      }
    }
  }

  DMap.ID = 512;
  DMap.Resolution = MAP_OUTPUT;
  strcpy(DMap.FileName, "");
  GetVarAttr(&DMap);

  Read2DMatrix(FileName, Array, DMap.NumberType, Map->NY, Map->NX, NSet++,
	       DMap.Name);

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {
      if (INBASIN(TopoMap[y][x].Mask)) {
	SoilMap[y][x].Runoff_m = ((float *) Array)[y * Map->NX + x];
	//DISABLEDTEST: BURPTEST((SoilMap[y][x].Runoff_m<1),"( SoilMap[y][x].Runoff_m <1)");
      }
    }
  }
  free(Array);

  /* Calculate the water table depth at each point based on the soil 
     moisture profile. Give an error message if the water ponds on the
     surface since that should not be allowed at this point */
  remove = 0.0;

  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) {

      /* SatFlow_m needs to be initialized properly in the future.  For now it 
         will just be set to zero here */
      SoilMap[y][x].SatFlow_m = 0.0;
      if (INBASIN(TopoMap[y][x].Mask)) {
	if ((SoilMap[y][x].TableDepth_m =
		WaterTableDepth((Soil.NLayers[SoilMap[y][x].Soil - 1]),SoilMap[y][x].Depth,VType[VegChemMap[y][x].Veg - 1].RootDepth_m,
		SType[SoilMap[y][x].Soil - 1].Porosity, SType[SoilMap[y][x].Soil - 1].FCap,Network[y][x].Adjust, SoilMap[y][x].Moist_m_m))< 0.0){ 
			remove -= SoilMap[y][x].TableDepth_m * Map->DX * Map->DY;
			SoilMap[y][x].TableDepth_m = 0.0;
		}
      }
      else SoilMap[y][x].TableDepth_m = 0;
      
    }
  }
  if (remove > 0.0) {
    printf("WARNING:excess water in soil profile is %f m^3 \n", remove);
    printf("Expect possible large flood wave during first timesteps \n");
  }

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

  if (Options->Extent == BASIN && Options->HasNetwork == FALSE) {
    sprintf(FileName, "%sHydrograph.State.%s", Path, Str);
    OpenFile(&HydroStateFile, FileName, "r", FALSE);
    for (i = 0; i < HydrographInfo->TotalWaveLength; i++)
		fscanf(HydroStateFile, "%f\n", &(Hydrograph[i]));
    fclose(HydroStateFile);
  }
}
Ejemplo n.º 7
0
/*****************************************************************************
  InitFineMaps()
*****************************************************************************/
void InitFineMaps(LISTPTR Input, OPTIONSTRUCT *Options, MAPSIZE *Map, 
		     LAYER *Soil, TOPOPIX ***TopoMap, SOILPIX ***SoilMap, 
		     FINEPIX ****FineMap)
{
 
  const char *Routine = "InitFineMaps";
  char VarName[BUFSIZE+1];	/* Variable name */
  int i, k, x, y;		/* Counters */
  int ii, jj, xx, yy, xy;            /* Counters */
  int NumberType;		/* Number type of data set */
  float *Elev;                   /* Surface elevation */
  int MASKFLAG;
  unsigned char *Mask = NULL;          /* Fine resolution mask */

  STRINIENTRY StrEnv[] = {
    {"FINEDEM", "DEM FILE"        , ""  , ""},
    {"FINEDEM", "MASK FILE"        , ""  , ""},
    {NULL       , NULL            , ""  , NULL}
  };
  
  printf("Initializing mass wasting resolution maps\n");
  
  /* Process the [FINEDEM] section in the input file */
  
  /* Read the key-entry pair for the dem from the input file */
  GetInitString(StrEnv[0].SectionName, StrEnv[0].KeyName, StrEnv[0].Default,
		StrEnv[0].VarStr, (unsigned long) BUFSIZE, Input);
  if (IsEmptyStr(StrEnv[0].VarStr))
    ReportError(StrEnv[0].KeyName, 51);
  
  /* Read the elevation dataset */ 
  
  GetVarName(001, 0, VarName);
  GetVarNumberType(001, &NumberType);
  if (!(Elev = (float *) calloc(Map->NXfine * Map->NYfine, 
				SizeOfNumberType(NumberType)))) 
    ReportError((char *) Routine, 1);
  Read2DMatrix(StrEnv[demfile].VarStr, Elev, NumberType, Map->NYfine, Map->NXfine, 0,
	       VarName);
  
  /* Read the key-entry pair for the mask from the input file */
  GetInitString(StrEnv[1].SectionName, StrEnv[1].KeyName, StrEnv[1].Default,
		StrEnv[1].VarStr, (unsigned long) BUFSIZE, Input);
  if (IsEmptyStr(StrEnv[1].VarStr)) {
    printf("\nWARNING: Fine resolution mask not provided, will be set equal to \n");
    printf("coarse resolution mask.\n\n");
    MASKFLAG = FALSE;
  }
  else {
    printf("fine mask = %s\n",StrEnv[1].VarStr);
    /* Read the mask */
    GetVarName(002, 0, VarName);
    GetVarNumberType(002, &NumberType);
    if (!(Mask = (unsigned char *) calloc(Map->NXfine * Map->NYfine,
					  SizeOfNumberType(NumberType))))
      ReportError((char *) Routine, 1);
    Read2DMatrix(StrEnv[maskfile].VarStr, Mask, NumberType, Map->NYfine, Map->NXfine, 0,
		 VarName);
    MASKFLAG = TRUE;
    
  }
  
  /* Assign the attributes to the correct map pixel */
  if (!(*FineMap = (FINEPIX ***) calloc(Map->NYfine, sizeof(FINEPIX **))))
    ReportError((char *) Routine, 1);
  for (y = 0; y < Map->NYfine; y++) {
    if (!((*FineMap)[y] = (FINEPIX **) calloc(Map->NXfine, sizeof(FINEPIX *))))
      ReportError((char *) Routine, 1);
  }
  // Only allocate a FINEPIX structure for a fine grid cell if that grid cell
  // is in the coarse grid mask
  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) { 
      if (INBASIN((*TopoMap)[y][x].Mask)) {
	for (ii=0; ii< Map->DY/Map->DMASS; ii++) { 
 	  for (jj=0; jj< Map->DX/Map->DMASS; jj++) { 
	    yy = (int) y*Map->DY/Map->DMASS + ii; 
 	    xx = (int) x*Map->DX/Map->DMASS + jj; 
            if (!((*FineMap)[yy][xx] = (FINEPIX *) malloc(sizeof(FINEPIX)))) {
	      printf("error allocating FineMap[%d][%d]\n",yy,xx);
              ReportError((char *) Routine, 1);
            }
	  }
	}
      }
    }
  }
  
  for (y = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++) { 
      if (INBASIN((*TopoMap)[y][x].Mask)) {
	for (ii=0; ii< Map->DY/Map->DMASS; ii++) { 
 	  for (jj=0; jj< Map->DX/Map->DMASS; jj++) { 
	    yy = (int) y*Map->DY/Map->DMASS + ii; 
 	    xx = (int) x*Map->DX/Map->DMASS + jj; 
	    xy = (int) yy*Map->NXfine + xx;
	    (*(*FineMap)[yy][xx]).Dem  = Elev[xy]; 
	  }
	}
      }
    }
  }
  
  free(Elev);
  
  if(MASKFLAG == TRUE){
    for (y = 0; y < Map->NY; y++) {
      for (x = 0; x < Map->NX; x++) { 
	if (INBASIN((*TopoMap)[y][x].Mask)) {
	  for (ii=0; ii< Map->DY/Map->DMASS; ii++) { 
	    for (jj=0; jj< Map->DX/Map->DMASS; jj++) { 
	      yy = (int) y*Map->DY/Map->DMASS + ii; 
	      xx = (int) x*Map->DX/Map->DMASS + jj; 
	      xy = (int) yy*Map->NXfine + xx;
	      (*(*FineMap)[yy][xx]).Mask  = Mask[xy]; 
	    }
	  }
	}
      }
    }
  }

 free(Mask);  
  /* Create fine resolution mask, sediment and bedrock maps.
   Initialize other variables*/
  
  for (y = 0, i = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++, i++) {
      for (ii=0; ii< Map->DY/Map->DMASS; ii++) {
	for (jj=0; jj< Map->DX/Map->DMASS; jj++) {
	  if (INBASIN((*TopoMap)[y][x].Mask)) {
	    yy = (int) y*Map->DY/Map->DMASS + ii;
	    xx = (int) x*Map->DX/Map->DMASS + jj;
	    if(MASKFLAG==FALSE)
	      (*(*FineMap)[yy][xx]).Mask = (*TopoMap)[y][x].Mask;
	    /*   else { */
	      // Don't allow fine mask to extend beyond edges of coarse mask.
	      // This means that failures may still try to leave the basin if the coarse 
	      // mask isn't wide enough because some of the drainage area may be cropped.
	/*       if (!INBASIN((*TopoMap)[y][x].Mask)) */
/* 		(*(*FineMap)[yy][xx]).Mask = (*TopoMap)[y][x].Mask; */
	 /*    } */
	    (*(*FineMap)[yy][xx]).bedrock = (*(*FineMap)[yy][xx]).Dem - (*SoilMap)[y][x].Depth;
	    (*(*FineMap)[yy][xx]).sediment = (*SoilMap)[y][x].Depth;
	    (*(*FineMap)[yy][xx]).SatThickness = 0.;
	    (*(*FineMap)[yy][xx]).DeltaDepth = 0.;
	    (*(*FineMap)[yy][xx]).Probability = 0.;
	    (*(*FineMap)[yy][xx]).MassWasting = 0.;
	    (*(*FineMap)[yy][xx]).MassDeposition = 0.;
	    (*(*FineMap)[yy][xx]).SedimentToChannel = 0.;
	    (*(*FineMap)[yy][xx]).TopoIndex = 0.;
	  }
	}
      }
    }
  }
  
  Map->NumFineIn = (Map->DX/Map->DMASS) * (Map->DY/Map->DMASS);

 /* NumCellsfine is used in CalcTopoIndex.  The topo index is calculated for every 
     fine cell within the boundary of the coarse mask, so this number may exceed the 
     number of pixels within the fine resolution mask.  */

  Map->NumCellsfine = Map->NumCells*Map->NumFineIn;

  printf("Basin has %d active pixels in the mass wasting resolution map\n",
	 Map->NumCellsfine);
  
  /* Calculate the topographic index */
  CalcTopoIndex(Map, *FineMap, *TopoMap);
  
  for (y = 0; y < Map->NY; y++) {
    for (x  = 0; x < Map->NX; x++) {
      if (INBASIN((*TopoMap)[y][x].Mask)) {
	if (!((*TopoMap)[y][x].OrderedTopoIndex = (ITEM *) calloc(Map->NumFineIn, sizeof(ITEM))))
	  ReportError((char *) Routine, 1);
      }
    }
  }
  
  for (y = 0; y < Map->NY; y++) {
    for (x  = 0; x < Map->NX; x++) {
      if (INBASIN((*TopoMap)[y][x].Mask)) {
	k = 0;
	for(ii=0; ii< Map->DY/Map->DMASS; ii++) {
	  for(jj=0; jj< Map->DX/Map->DMASS; jj++) {
	    yy = (int) y*Map->DY/Map->DMASS + ii;
	    xx = (int) x*Map->DX/Map->DMASS + jj;
	    (*TopoMap)[y][x].OrderedTopoIndex[k].Rank = (*(*FineMap)[yy][xx]).TopoIndex;
	    (*TopoMap)[y][x].OrderedTopoIndex[k].y = yy;
	    (*TopoMap)[y][x].OrderedTopoIndex[k].x = xx;
	    k++;
	  }
	}
       	quick((*TopoMap)[y][x] .OrderedTopoIndex, Map->NumFineIn);
      }
    }
  }
}
Ejemplo n.º 8
0
/*****************************************************************************
  Function name: InitUnitHydrograph()

  Purpose      :

  Required     :

  Returns      : void

  Modifies     :

  Comments     :
*****************************************************************************/
void InitUnitHydrograph(LISTPTR Input, MAPSIZE * Map, TOPOPIX ** TopoMap,
			UNITHYDR *** UnitHydrograph, float **Hydrograph,
			UNITHYDRINFO * HydrographInfo)
{
  const char *Routine = "InitUnitHydrograph()";
  char VarName[BUFSIZE + 1];	/* Variable name */
  FILE *HydrographFile;
  int MaxTravelTime;
  int NumberType;
  int TravelTimeStep;
  int WaveLength;
  int i;
  int j;
  int x;
  int y;
  STRINIENTRY StrEnv[] = {
    {"ROUTING", "TRAVEL TIME FILE", "", NULL},
    {"ROUTING", "UNIT HYDROGRAPH FILE", "", NULL},
    {NULL, NULL, "", NULL}
  };
  unsigned short *Travel;

  printf("Initializing unit hydrograph\n");

  /* Read the key-entry pairs from the [ROUTING] section of the input file */
  for (i = 0; StrEnv[i].SectionName; i++) {
    GetInitString(StrEnv[i].SectionName, StrEnv[i].KeyName, StrEnv[i].Default,
		  StrEnv[i].VarStr, (unsigned long) BUFSIZE, Input);
    if (!StrEnv[i].VarStr)
      ReportError(StrEnv[i].KeyName, 51);
  }

  /* Read the travel times */
  GetVarName(006, 0, VarName);
  GetVarNumberType(006, &NumberType);
  if (!(Travel = (unsigned short *) calloc(Map->NX * Map->NY,
					   SizeOfNumberType(NumberType))))
    ReportError((char *) Routine, 1);

  Read2DMatrix(StrEnv[travel_file].VarStr, Travel, NumberType, Map->NY,
	       Map->NX, 0, VarName);

  /* Assign the travel times to the correct pixels */
  for (y = 0, i = 0; y < Map->NY; y++)
    for (x = 0; x < Map->NX; x++, i++)
      TopoMap[y][x].Travel = Travel[i];
  free(Travel);

  /* Read the unit hydrograph file */
  OpenFile(&HydrographFile, StrEnv[hydrograph_file].VarStr, "r", FALSE);
  fscanf(HydrographFile, "%d", &MaxTravelTime);
  HydrographInfo->MaxTravelTime = MaxTravelTime;

  if (!(HydrographInfo->WaveLength =
	(int *) calloc(MaxTravelTime, sizeof(int))))
    ReportError((char *) Routine, 1);
  if (!(*UnitHydrograph =
	(UNITHYDR **) calloc(MaxTravelTime, sizeof(UNITHYDR *))))
    ReportError((char *) Routine, 1);

  for (i = 0; i < MaxTravelTime; i++) {

    fscanf(HydrographFile, "%d %d", &TravelTimeStep, &WaveLength);
    if (i != TravelTimeStep - 1)
      ReportError(StrEnv[hydrograph_file].VarStr, 46);
    HydrographInfo->WaveLength[i] = WaveLength;

    if (!((*UnitHydrograph)[i] =
	  (UNITHYDR *) calloc(WaveLength, sizeof(UNITHYDR))))
      ReportError((char *) Routine, 1);

    for (j = 0; j < WaveLength; j++) {
      fscanf(HydrographFile, "%d %f",
	     &((*UnitHydrograph)[i][j].TimeStep),
	     &((*UnitHydrograph)[i][j].Fraction));
    }
  }

  HydrographInfo->TotalWaveLength =
    (*UnitHydrograph)[MaxTravelTime - 1][WaveLength - 1].TimeStep + 1;

  if (!(*Hydrograph = (float *) calloc(HydrographInfo->TotalWaveLength,
				       sizeof(float))))
    ReportError((char *) Routine, 1);

  fclose(HydrographFile);
}
Ejemplo n.º 9
0
/*****************************************************************************
  InitTopoMap()
*****************************************************************************/
void InitTopoMap(LISTPTR Input, OPTIONSTRUCT * Options, MAPSIZE * Map,
		 TOPOPIX *** TopoMap)
{
  const char *Routine = "InitTopoMap";
  char VarName[BUFSIZE + 1];	/* Variable name */
  int i;			/* Counter */
  int x;			/* Counter */
  int y;			/* Counter */
  int flag;         /* either or not reverse the matrix */
  int NumberType;		/* Number type of data set */
  unsigned char *Mask = NULL;	/* Basin mask */
  float *Elev;			/* Surface elevation */
  STRINIENTRY StrEnv[] = {
    {"TERRAIN", "DEM FILE", "", ""},
    {"TERRAIN", "BASIN MASK FILE", "", ""},
    {NULL, NULL, "", NULL}
  };

  /* Process the [TERRAIN] section in the input file */
  if (!(*TopoMap = (TOPOPIX **) calloc(Map->NY, sizeof(TOPOPIX *))))
    ReportError((char *) Routine, 1);
  for (y = 0; y < Map->NY; y++) {
    if (!((*TopoMap)[y] = (TOPOPIX *) calloc(Map->NX, sizeof(TOPOPIX))))
      ReportError((char *) Routine, 1);
  }

  /* Read the key-entry pairs from the input file */
  for (i = 0; StrEnv[i].SectionName; i++) {
    GetInitString(StrEnv[i].SectionName, StrEnv[i].KeyName, StrEnv[i].Default,
		  StrEnv[i].VarStr, (unsigned long) BUFSIZE, Input);
    if (IsEmptyStr(StrEnv[i].VarStr))
      ReportError(StrEnv[i].KeyName, 51);
  }

  /* Read the elevation data from the DEM dataset */
  GetVarName(001, 0, VarName);
  GetVarNumberType(001, &NumberType);
  if (!(Elev = (float *) calloc(Map->NX * Map->NY,
				SizeOfNumberType(NumberType))))
    ReportError((char *) Routine, 1);

  flag = Read2DMatrix(StrEnv[demfile].VarStr, Elev, NumberType, Map->NY, Map->NX, 0,
	       VarName, 0);

  /* Assign the attributes to the map pixel */
  /* Reverse the matrix is flag = 1 & netcdf option is selected */
  if ((Options->FileFormat == NETCDF && flag == 0) || (Options->FileFormat == BIN)){
	for (y = 0, i = 0; y < Map->NY; y++) {
	  for (x = 0; x < Map->NX; x++, i++) {
	    (*TopoMap)[y][x].Dem = Elev[i]; }
	}
  }
  else if (Options->FileFormat == NETCDF && flag == 1){
	for (y = Map->NY - 1, i = 0; y >= 0; y--) {
	  for (x = 0; x < Map->NX; x++, i++) {
	    (*TopoMap)[y][x].Dem = Elev[i]; }
	}
  }
  else ReportError((char *) Routine, 57);
  free(Elev);
  
  /* find out the minimum grid elevation of the basin */
  MINELEV = 9999;
  for (y = 0, i = 0; y < Map->NY; y++) {
	for (x = 0; x < Map->NX; x++, i++) {
	  if ((*TopoMap)[y][x].Dem < MINELEV) {
		MINELEV = (*TopoMap)[y][x].Dem;
	  }
	}
  }

  /* Read the mask */
  GetVarName(002, 0, VarName);
  GetVarNumberType(002, &NumberType);
  if (!(Mask = (unsigned char *) calloc(Map->NX * Map->NY,
					SizeOfNumberType(NumberType))))
    ReportError((char *) Routine, 1);
  flag = Read2DMatrix(StrEnv[maskfile].VarStr, Mask, NumberType, Map->NY, Map->NX, 0,
	       VarName, 0);

  if ((Options->FileFormat == NETCDF && flag == 0) 
	  || (Options->FileFormat == BIN))
  {
	  for (y = 0, i = 0; y < Map->NY; y++) {


		  for (x = 0; x < Map->NX; x++, i++) {
			  (*TopoMap)[y][x].Mask = Mask[i]; }
	  }
  }
  else if (Options->FileFormat == NETCDF && flag == 1){
	  for (y = Map->NY - 1, i = 0; y >= 0; y--) {
		  for (x = 0; x < Map->NX; x++, i++) {
			  (*TopoMap)[y][x].Mask = Mask[i]; }
	  }
  }
  else ReportError((char *) Routine, 57);
  free(Mask);
  
  /* Calculate slope, aspect, magnitude of subsurface flow gradient, and 
     fraction of flow flowing in each direction based on the land surface 
     slope. */
  ElevationSlopeAspect(Map, *TopoMap);

  
  /* After calculating the slopes and aspects for all the points, reset the 
     mask if the model is to be run in point mode */
  if (Options->Extent == POINT) {
    for (y = 0; y < Map->NY; y++)
      for (x = 0; x < Map->NX; x++)
	(*TopoMap)[y][x].Mask = OUTSIDEBASIN;
    (*TopoMap)[Options->PointY][Options->PointX].Mask = (1 != OUTSIDEBASIN);
  }
}
Ejemplo n.º 10
0
/*****************************************************************************
  InitSoilMap()
*****************************************************************************/
void InitSoilMap(LISTPTR Input, OPTIONSTRUCT * Options, MAPSIZE * Map,
		 LAYER * Soil, TOPOPIX ** TopoMap, SOILPIX *** SoilMap)
{
  const char *Routine = "InitSoilMap";
  char VarName[BUFSIZE + 1];	/* Variable name */
  int i;			/* counter */
  int x;			/* counter */
  int y;			/* counter */
  int NumberType;		/* number type */
  unsigned char *Type;		/* Soil type */
  float *Depth;			/* Soil depth */
  int flag;
  STRINIENTRY StrEnv[] = {
    {"SOILS", "SOIL MAP FILE", "", ""},
    {"SOILS", "SOIL DEPTH FILE", "", ""},
    {NULL, NULL, "", NULL}
  };

  /* Process the filenames in the [SOILS] section in the input file */
  /* Assign the attributes to the correct map pixel */
  if (!(*SoilMap = (SOILPIX **) calloc(Map->NY, sizeof(SOILPIX *))))
    ReportError((char *) Routine, 1);
  for (y = 0; y < Map->NY; y++) {
    if (!((*SoilMap)[y] = (SOILPIX *) calloc(Map->NX, sizeof(SOILPIX))))
      ReportError((char *) Routine, 1);
  }

  /* Read the key-entry pairs from the input file */
  for (i = 0; StrEnv[i].SectionName; i++) {
    GetInitString(StrEnv[i].SectionName, StrEnv[i].KeyName, StrEnv[i].Default,
		  StrEnv[i].VarStr, (unsigned long) BUFSIZE, Input);
    if (IsEmptyStr(StrEnv[i].VarStr))
      ReportError(StrEnv[i].KeyName, 51);
  }

  /* Read the soil type */
  GetVarName(003, 0, VarName);
  GetVarNumberType(003, &NumberType);
  if (!(Type = (unsigned char *) calloc(Map->NX * Map->NY,
					SizeOfNumberType(NumberType))))
    ReportError((char *) Routine, 1);
  flag = Read2DMatrix(StrEnv[soiltype_file].VarStr, Type, NumberType, Map->NY,
	       Map->NX, 0, VarName, 0);

  if ((Options->FileFormat == NETCDF && flag == 0) 
	  || (Options->FileFormat == BIN))
  {
	  for (y = 0, i = 0; y < Map->NY; y++) {
		  for (x = 0; x < Map->NX; x++, i++) {
			  if (((int) Type[i]) > Soil->NTypes)
				  ReportError(StrEnv[soiltype_file].VarStr, 32);
			  (*SoilMap)[y][x].Soil = Type[i]; }
	  }
  }
  else if (Options->FileFormat == NETCDF && flag == 1){
	  for (y = Map->NY - 1, i = 0; y >= 0; y--) {
		  for (x = 0; x < Map->NX; x++, i++) {
			  if (((int) Type[i]) > Soil->NTypes)
				  ReportError(StrEnv[soiltype_file].VarStr, 32);
			  (*SoilMap)[y][x].Soil = Type[i]; }
	  }
  }
  else ReportError((char *) Routine, 57);

  /* Read the total soil depth  */
  GetVarName(004, 0, VarName);
  GetVarNumberType(004, &NumberType);
  if (!(Depth = (float *) calloc(Map->NX * Map->NY,
				 SizeOfNumberType(NumberType))))
    ReportError((char *) Routine, 1);
  flag = Read2DMatrix(StrEnv[soildepth_file].VarStr, Depth, NumberType, Map->NY,
	       Map->NX, 0, VarName, 0);

  /* Assign the attributes to the correct map pixel */
  if ((Options->FileFormat == NETCDF && flag == 0) 
	  || (Options->FileFormat == BIN))
  {
	  for (y = 0, i = 0; y < Map->NY; y++) {
		  for (x = 0; x < Map->NX; x++, i++) {
			  (*SoilMap)[y][x].Depth = Depth[i]; }
	  }
  }
  else if (Options->FileFormat == NETCDF && flag == 1){
	  for (y = Map->NY - 1, i = 0; y >= 0; y--) {
		  for (x = 0; x < Map->NX; x++, i++) {
			  (*SoilMap)[y][x].Depth = Depth[i]; }
	  }
  }
  else ReportError((char *) Routine, 57);

  for (y = 0, i = 0; y < Map->NY; y++) {
    for (x = 0; x < Map->NX; x++, i++) {
      if (Options->Infiltration == DYNAMIC)
		  (*SoilMap)[y][x].InfiltAcc = 0.; 
	  (*SoilMap)[y][x].MoistInit = 0.; 

     /* allocate memory for the number of root layers, plus an additional 
	  layer below the deepest root layer */
	  if (INBASIN(TopoMap[y][x].Mask)) {
		  if (!((*SoilMap)[y][x].Moist = 
			  (float *) calloc((Soil->NLayers[Type[i] - 1] + 1), sizeof(float))))
			  ReportError((char *) Routine, 1);
		  if (!((*SoilMap)[y][x].Perc =
			  (float *) calloc(Soil->NLayers[Type[i] - 1], sizeof(float))))
			  ReportError((char *) Routine, 1);
		  if (!((*SoilMap)[y][x].Temp =
			  (float *) calloc(Soil->NLayers[Type[i] - 1], sizeof(float))))
			  ReportError((char *) Routine, 1);
	  }
      else {
		  (*SoilMap)[y][x].Moist = NULL;
		  (*SoilMap)[y][x].Perc = NULL;
		  (*SoilMap)[y][x].Temp = NULL;
      }
    }
  }
  free(Type);
  free(Depth);
}
Ejemplo n.º 11
0
/*****************************************************************************
  InitNewMonth()
  At the start of a new month, read the new radiation files 
  (diffuse and direct beam), and potentially a new LAI value.
*****************************************************************************/
void InitNewMonth(TIMESTRUCT *Time, OPTIONSTRUCT *Options, MAPSIZE *Map,
		  TOPOPIX **TopoMap, float **PrismMap,
		  unsigned char ***ShadowMap, RADCLASSPIX **RadMap, 
		  INPUTFILES *InFiles, int NVegs, VEGTABLE *VType, int NStats,
		  METLOCATION *Stat, char *Path)
{
  const char *Routine = "InitNewMonth";
  char FileName[MAXSTRING + 1];
  char VarName[BUFSIZE + 1];	/* Variable name */
  int i;
  int j;
  int y, x;
  float a, b, l;
  int NumberType;
  float *Array = NULL;
  unsigned char *Array1 = NULL;

  if (DEBUG)
    printf("Initializing new month\n");

  /* If PRISM precipitation fields are being used to interpolate the 
     observed precipitation fields, then read in the new months field */

  if (Options->Prism == TRUE) {
    printf("reading in new PRISM field for month %d \n", Time->Current.Month);

    sprintf(FileName, "%s.%02d.%s", Options->PrismDataPath, 
	    Time->Current.Month, Options->PrismDataExt);

    GetVarName(205, 0, VarName);
    GetVarNumberType(205, &NumberType);
    if (!(Array = (float *) calloc(Map->NY * Map->NX, sizeof(float))))
      ReportError((char *) Routine, 1);

    Read2DMatrix(FileName, Array, NumberType, Map->NY, Map->NX, 0);

    for (y = 0; y < Map->NY; y++) {
      for (x = 0; x < Map->NX; x++) {
	PrismMap[y][x] = Array[y * Map->NX + x];
      }
    }

    free(Array);

  }

  if (Options->Shading == TRUE) {
    printf("reading in new shadow map for month %d \n", Time->Current.Month);
    sprintf(FileName, "%s.%02d.%s", Options->ShadingDataPath, 
	    Time->Current.Month, Options->ShadingDataExt);
    GetVarName(304, 0, VarName);
    GetVarNumberType(304, &NumberType);

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

    for (i = 0; i < Time->NDaySteps; i++) {

      Read2DMatrix(FileName, Array1, NumberType, Map->NY, Map->NX, i, 
		   VarName);

      for (y = 0; y < Map->NY; y++) {
	for (x = 0; x < Map->NX; x++) {
	  ShadowMap[i][y][x] = Array1[y * Map->NX + x];
	}
      }
    }
    free(Array1);
  }

  printf("changing LAI, albedo and diffuse transmission parameters\n");
  for (i = 0; i < NVegs; i++) {
    for (j = 0; j < VType[i].NVegLayers; j++) {
      VType[i].LAI[j] = VType[i].LAIMonthly[j][Time->Current.Month - 1];
      VType[i].MaxInt[j] = VType[i].LAI[j] * VType[i].Fract[j] *
	LAI_WATER_MULTIPLIER;
      VType[i].Albedo[j] = VType[i].AlbedoMonthly[j][Time->Current.Month - 1];
    }
    if (VType[i].OverStory) {
      a = VType[i].LeafAngleA;
      b = VType[i].LeafAngleB;
      l = VType[i].LAI[0] / VType[i].ClumpingFactor;
      if (l == 0)
	VType[i].Taud = 1.0;
      else
	VType[i].Taud =
	  exp(-b * l) * ((1 - a * l) * exp(-a * l) +
			 (a * l) * (a * l) * evalexpint(1, a * l));
    }
    else {
      VType[i].Taud = 0.0;
    }
  }
  

}
Ejemplo n.º 12
0
/*****************************************************************************
  Function name: InitNewStep()

  Purpose      : Initialize Earth-Sun geometry and meteorological data at the
                 beginning of each timestep

  Required     :
    MAPSIZE Map              - Structure with information about location
    TIMESTRUCT Time          - Structure with time information
    int PrecipType           - Type of precipitation input, RADAR, STATION or
                               OROGRAPHIC
    int FlowGradient         - Type of FlowGradient calculation
    int NStats               - Number of meteorological stations
    METLOCATION *Stat        - Structure with information about the
                               meteorological stations in or near the study
                               area 
    char *RadarFileName      - Name of file with radar images
    MAPSIZE Radar            - Structure with information about the
                               precipitation radar coverage
    RADCLASSPIX **RadMap     - Structure with radiation data for each pixel
    RADARPIX **RadarMap      - Structure with precipitation information for
                               each radar pixel
    SOLARGEOMETRY *SolarGeo  - structure with information about Earth-Sun 
                               geometry
    SOILPIX **SoilMap        - structure with soil information
    float ***MM5Input        - MM5 input maps
    float ***WindModel       - Wind model maps
                           
  Returns      : void

  Modifies     :

  Comments     : To be executed at the beginning of each time step
*****************************************************************************/
void InitNewStep(INPUTFILES *InFiles, MAPSIZE *Map, TIMESTRUCT *Time,
		 int NSoilLayers, OPTIONSTRUCT *Options, int NStats,
		 METLOCATION *Stat, char *RadarFileName, MAPSIZE *Radar,
		 RADARPIX **RadarMap, SOLARGEOMETRY *SolarGeo, 
		 TOPOPIX **TopoMap, RADCLASSPIX **RadMap, SOILPIX **SoilMap,
		 float ***MM5Input, float ***WindModel, MAPSIZE *MM5Map)
{
  const char *Routine = "InitNewStep";
  int i;			/* counter */
  int j;			/* counter */
  int x;			/* counter */
  int y;			/* counter */
  int NumberType;		/* number type in MM5 input */
  int Step;			/* Step in the MM5 Input */
  float *Array = NULL;
  int MM5Y, MM5X;

  /* Calculate variables related to the position of the sun above the
     horizon, this is only necessary if shading is TRUE */

  SolarHour(SolarGeo->Latitude,
	    (Time->DayStep + 1) * ((float) Time->Dt) / SECPHOUR,
	    ((float) Time->Dt) / SECPHOUR, SolarGeo->NoonHour,
	    SolarGeo->Declination, SolarGeo->Sunrise, SolarGeo->Sunset,
	    SolarGeo->TimeAdjustment, SolarGeo->SunEarthDistance,
	    &(SolarGeo->SineSolarAltitude), &(SolarGeo->DayLight),
	    &(SolarGeo->SolarTimeStep), &(SolarGeo->SunMax),
	    &(SolarGeo->SolarAzimuth));

/*printf("SunMax is %f\n",SolarGeo->SunMax);*/
  if (Options->MM5 == TRUE) {

    /* Read the data from the MM5 files */

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

    Step = NumberOfSteps(&(Time->StartMM5), &(Time->Current), Time->Dt);

    Read2DMatrix(InFiles->MM5Temp, Array, NumberType, MM5Map->NY,
		 MM5Map->NX, Step);
    for (y = 0; y < Map->NY; y++)
      for (x = 0; x < Map->NX; x++) {
	MM5Y = (int) ((y + MM5Map->OffsetY) * Map->DY / MM5Map->DY);
	MM5X = (int) ((x - MM5Map->OffsetX) * Map->DX / MM5Map->DY);
	MM5Input[MM5_temperature - 1][y][x] = Array[MM5Y * MM5Map->NX + MM5X];
      }

    Read2DMatrix(InFiles->MM5Humidity, Array, NumberType, MM5Map->NY,
		 MM5Map->NX, Step);

    for (y = 0; y < Map->NY; y++)
      for (x = 0; x < Map->NX; x++) {
	MM5Y = (int) ((y + MM5Map->OffsetY) * Map->DY / MM5Map->DY);
	MM5X = (int) ((x - MM5Map->OffsetX) * Map->DX / MM5Map->DY);
	MM5Input[MM5_humidity - 1][y][x] = Array[MM5Y * MM5Map->NX + MM5X];
      }

    Read2DMatrix(InFiles->MM5Wind, Array, NumberType, MM5Map->NY,
		 MM5Map->NX, Step);
    for (y = 0; y < Map->NY; y++)
      for (x = 0; x < Map->NX; x++) {
	MM5Y = (int) ((y + MM5Map->OffsetY) * Map->DY / MM5Map->DY);
	MM5X = (int) ((x - MM5Map->OffsetX) * Map->DX / MM5Map->DY);
	MM5Input[MM5_wind - 1][y][x] = Array[MM5Y * MM5Map->NX + MM5X];
      }

    Read2DMatrix(InFiles->MM5ShortWave, Array, NumberType, MM5Map->NY,
		 MM5Map->NX, Step);
    for (y = 0; y < Map->NY; y++)
      for (x = 0; x < Map->NX; x++) {
	MM5Y = (int) ((y + MM5Map->OffsetY) * Map->DY / MM5Map->DY);
	MM5X = (int) ((x - MM5Map->OffsetX) * Map->DX / MM5Map->DY);
	MM5Input[MM5_shortwave - 1][y][x] = Array[MM5Y * MM5Map->NX + MM5X];
      }

    Read2DMatrix(InFiles->MM5LongWave, Array, NumberType, MM5Map->NY,
		 MM5Map->NX, Step);
    for (y = 0; y < Map->NY; y++)
      for (x = 0; x < Map->NX; x++) {
	MM5Y = (int) ((y + MM5Map->OffsetY) * Map->DY / MM5Map->DY);
	MM5X = (int) ((x - MM5Map->OffsetX) * Map->DX / MM5Map->DY);
	MM5Input[MM5_longwave - 1][y][x] = Array[MM5Y * MM5Map->NX + MM5X];
      }

    Read2DMatrix(InFiles->MM5Precipitation, Array, NumberType, MM5Map->NY,
		 MM5Map->NX, Step);
    for (y = 0; y < Map->NY; y++)
      for (x = 0; x < Map->NX; x++) {
	MM5Y = (int) ((y + MM5Map->OffsetY) * Map->DY / MM5Map->DY);
	MM5X = (int) ((x - MM5Map->OffsetX) * Map->DX / MM5Map->DY);
	MM5Input[MM5_precip - 1][y][x] = Array[MM5Y * MM5Map->NX + MM5X];
	if (MM5Input[MM5_precip - 1][y][x] < 0.0) {
	  printf("Warning: MM5 precip is less than zero %f\n",
		 MM5Input[MM5_precip - 1][y][x]);
	  MM5Input[MM5_precip - 1][y][x] = 0.0;
	}
      }
    Read2DMatrix(InFiles->MM5Terrain, Array, NumberType, MM5Map->NY,
		 MM5Map->NX, Step);
    for (y = 0; y < Map->NY; y++)
      for (x = 0; x < Map->NX; x++) {
	MM5Y = (int) ((y + MM5Map->OffsetY) * Map->DY / MM5Map->DY);
	MM5X = (int) ((x - MM5Map->OffsetX) * Map->DX / MM5Map->DY);
	MM5Input[MM5_terrain - 1][y][x] = Array[MM5Y * MM5Map->NX + MM5X];
      }
    Read2DMatrix(InFiles->MM5Lapse, Array, NumberType, MM5Map->NY,
		 MM5Map->NX, Step);
    for (y = 0; y < Map->NY; y++)
      for (x = 0; x < Map->NX; x++) {
	MM5Y = (int) ((y + MM5Map->OffsetY) * Map->DY / MM5Map->DY);
	MM5X = (int) ((x - MM5Map->OffsetX) * Map->DX / MM5Map->DY);
	MM5Input[MM5_lapse - 1][y][x] = Array[MM5Y * MM5Map->NX + MM5X];
      }

    if (Options->HeatFlux == TRUE) {

      for (i = 0, j = MM5_lapse; i < NSoilLayers; i++, j++) {
	Read2DMatrix(InFiles->MM5SoilTemp[i], Array, NumberType, MM5Map->NY,
		     MM5Map->NX, Step);
	for (y = 0; y < Map->NY; y++)
	  for (x = 0; x < Map->NX; x++) {
	    MM5Y = (int) ((y + MM5Map->OffsetY) * Map->DY / MM5Map->DY);
	    MM5X = (int) ((x - MM5Map->OffsetX) * Map->DX / MM5Map->DY);
	    MM5Input[j][y][x] = Array[MM5Y * MM5Map->NX + MM5X];
	  }
      }
    }
    free(Array);
  }
/*end if MM5*/

  /* if the flow gradient is based on the water table, recalculate the water
     table gradients.  Flow directions are now calculated in RouteSubSurface*/
  if (Options->FlowGradient == WATERTABLE) {
    /* Calculate the WaterLevel, i.e. the height of the water table above 
       some datum */
    for (y = 0; y < Map->NY; y++) {
      for (x = 0; x < Map->NX; x++) {
	if (INBASIN(TopoMap[y][x].Mask)) {
	  SoilMap[y][x].WaterLevel =
	    TopoMap[y][x].Dem - SoilMap[y][x].TableDepth;
	}
      }
    }
/*     HeadSlopeAspect(Map, TopoMap, SoilMap); */
  }

  if ((Options->MM5 == TRUE && Options->QPF == TRUE) || Options->MM5 == FALSE)
    GetMetData(Options, Time, NSoilLayers, NStats, SolarGeo->SunMax, Stat,
	       Radar, RadarMap, RadarFileName);
}