Esempio n. 1
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);
}
Esempio n. 2
0
/*****************************************************************************
  GetVarAttr()
*****************************************************************************/
void GetVarAttr(MAPDUMP * DMap)
{
    GetVarName(DMap->ID, DMap->Layer, DMap->Name);
    GetVarLongName(DMap->ID, DMap->Layer, DMap->LongName);
    GetVarFormat(DMap->ID, DMap->Format);
    GetVarUnits(DMap->ID, DMap->Units);
    GetVarFileName(DMap->ID, DMap->Layer, DMap->Resolution, DMap->FileName);
    GetVarFileLabel(DMap->ID, DMap->FileLabel);
    GetVarNumberType(DMap->ID, &(DMap->NumberType));
}
Esempio n. 3
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);
}
Esempio n. 4
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);
      }
    }
  }
}
Esempio n. 5
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);
}
Esempio n. 6
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);
  }
}
Esempio n. 7
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);
}
Esempio n. 8
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;
    }
  }
  

}