Exemple #1
0
int OutputMonitor::__MapBuffer()
{
    int ret;

    assert(this->m_hDBWinMapBuffer == NULL);
    assert(this->m_pDBWinBuffer == NULL);

    if (this->m_GlobalWin32) {
        this->m_hDBWinMapBuffer = CreateMapFile("Global\\DBWIN_BUFFER", sizeof(DBWIN_BUFFER_t), 0);
    } else {
        this->m_hDBWinMapBuffer = CreateMapFile("DBWIN_BUFFER", sizeof(DBWIN_BUFFER_t), 0);
    }
    if(this->m_hDBWinMapBuffer == NULL) {
        if (this->m_GlobalWin32) {
            this->m_hDBWinMapBuffer = CreateMapFile("Global\\DBWIN_BUFFER", sizeof(DBWIN_BUFFER_t), 1);
        } else {
            this->m_hDBWinMapBuffer = CreateMapFile("DBWIN_BUFFER", sizeof(DBWIN_BUFFER_t), 1);
        }
        if(this->m_hDBWinMapBuffer == NULL) {
            ret = GETERRNO();
            goto fail;
        }
    }

    this->m_pDBWinBuffer = MapFileBuffer(this->m_hDBWinMapBuffer,sizeof(DBWIN_BUFFER_t));
    if(this->m_pDBWinBuffer == NULL) {
        ret = GETERRNO();
        goto fail;
    }

    SETERRNO(0);
    return 0;
fail:
    this->__UnMapBuffer();
    SETERRNO(ret);
    return -ret;
}
Exemple #2
0
Map::Map()
{
	if (!font.loadFromFile("media/Consolas.ttf"))
		throw DataNotLoaded("Nie znaleziono czcionki");
	text[0].setFont(font);
	CreateMapFile("media/map.txt", "media/map3.txt");
	FillMap("media/map3.txt", "media/Tileset.png");
	getObjStatus("media/map3.txt");
	player[0] = new Player(1, 1, "media/Player1.png");
	player[1] = new Player(13, 11, "media/Player2.png");
	UpdateTime = clock();
	UpdateTime2 = clock();
	text[0].setFont(font);	
	text[0].setPosition(30.f, 420.f);
	text[0].setColor(sf::Color::Black);
	text[1].setFont(font);
	text[1].setPosition(30.f, 445.f);
	text[1].setColor(sf::Color::Black);
	text[2].setFont(font);
	text[2].setPosition(300.f, 480.f);
	text[2].setColor(sf::Color::Red);
	text[2].setString("Main menu");
}
Exemple #3
0
/*******************************************************************************
  Function name: InitMapDump()

  Purpose      : Initialize the map dumps.  This information is in the
         [OUTPUT] section of the input file

  Required     :
    LISTPTR Input         - Linked list with input strings
    MAPSIZE *MapDump      - Information about areal extent
    int MaxSoilLayers     - Maximum number of soil layers
    int MaxVegLayers      - Maximum number of vegetation layers
    char *Path            - Directory to write output to
    int NTotalMapImages   - Total number of maps and images to dump
    int NMaps             - Number of maps to dump
    MAPDUMP **DMap        - Array of maps and images to dump

  Returns      : void

  Modifies     : DMap and its members

  Comments     :
*******************************************************************************/
void InitMapDump(LISTPTR Input, MAPSIZE * Map, int MaxSoilLayers,
  int MaxVegLayers, char *Path, int TotalMapImages, int NMaps,
  MAPDUMP ** DMap)
{
  char *Routine = "InitMapDump";
  int i;			/* counter */
  int j;			/* counter */
  int MaxLayers;		/* Maximum number of layers allowed for this
                   variable */
  char KeyName[map_date + 1][BUFSIZE + 1];
  char *KeyStr[] = {
    "MAP VARIABLE",
    "MAP LAYER",
    "NUMBER OF MAPS",
    "MAP DATE",
  };
  char *SectionName = "OUTPUT";
  char VarStr[map_date + 1][BUFSIZE + 1];

  if (!(*DMap = (MAPDUMP *)calloc(TotalMapImages, sizeof(MAPDUMP))))
    ReportError(Routine, 1);

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

    /* Read the key-entry pairs from the input file */
    for (j = 0; j <= nmaps; j++) {
      sprintf(KeyName[j], "%s %d", KeyStr[j], i + 1);
      GetInitString(SectionName, KeyName[j], "", VarStr[j],
        (unsigned long)BUFSIZE, Input);
    }

    /* Assign the entries to the appropriate variables */
    if (!CopyInt(&((*DMap)[i].ID), VarStr[map_variable], 1))
      ReportError(KeyName[map_variable], 51);

    if (!IsValidID((*DMap)[i].ID))
      ReportError("Input Options File", 19);

    if (IsMultiLayer((*DMap)[i].ID)) {
      MaxLayers = GetVarNLayers((*DMap)[i].ID, MaxSoilLayers, MaxVegLayers);
      if (!CopyInt(&((*DMap)[i].Layer), VarStr[map_layer], 1))
        ReportError(KeyName[map_layer], 51);
      if ((*DMap)[i].Layer < 1 || (*DMap)[i].Layer > MaxLayers)
        ReportError("Input Options File", 20);
    }
    else
      (*DMap)[i].Layer = 1;

    (*DMap)[i].Resolution = MAP_OUTPUT;

    strncpy((*DMap)[i].FileName, Path, BUFSIZE);
    GetVarAttr(&((*DMap)[i]));

    CreateMapFile((*DMap)[i].FileName, (*DMap)[i].FileLabel, Map);

    if (!CopyInt(&((*DMap)[i].N), VarStr[nmaps], 1))
      ReportError(KeyName[nmaps], 51);

    if ((*DMap)[i].N < 1)
      ReportError("Input Options File", 22);

    if (!((*DMap)[i].DumpDate = (DATE *)calloc((*DMap)[i].N, sizeof(DATE))))
      ReportError(Routine, 1);

    for (j = 0; j < (*DMap)[i].N; j++) {
      sprintf(KeyName[map_date], "%s %d %d", KeyStr[map_date], j + 1, i + 1);
      GetInitString(SectionName, KeyName[map_date], "", VarStr[map_date],
        (unsigned long)BUFSIZE, Input);
      if (!SScanDate(VarStr[map_date], &((*DMap)[i].DumpDate[j])))
        ReportError(KeyName[map_date], 51);
    }

    (*DMap)[i].MinVal = 0.0;
    (*DMap)[i].MaxVal = 0.0;
  }
}
Exemple #4
0
/*******************************************************************************
  Function name: InitImageDump()

  Purpose      : Initialize the image dumps.  This information is in the
         [OUTPUT] section of the input file

  Required     :
    LISTPTR Input         - Linked list with input strings
    int Dt                - Model timestep in seconds
    MAPSIZE *MapDump      - Information about areal extent
    int MaxSoilLayers     - Maximum number of soil layers
    int MaxVegLayers      - Maximum number of vegetation layers
    char *Path            - Directory to write output to
    int NMaps             - Number of maps to dump
    int NImages           - Number of images to dump
    MAPDUMP **DMap        - Array of maps and images to dump

  Returns      : void

  Modifies     : Members of DMap

  Comments     : InitImageDump must be preceded by a call to InitMapDump, since
                 the necessary memory is allocated there
*******************************************************************************/
void InitImageDump(LISTPTR Input, int Dt, MAPSIZE * Map, int MaxSoilLayers,
  int MaxVegLayers, char *Path, int NMaps, int NImages,
  MAPDUMP ** DMap)
{
  char *Routine = "InitImageDump";
  DATE End;			/* End of low resolution map dump period */
  DATE Start;			/* Start of low resolution map dump period */
  int i;			/* counter */
  int j;			/* counter */
  int Interval;			/* Interval between low resolution map dumps */
  int MaxLayers;		/* Maximum number of layers allowed for this
                   variable */
  char KeyName[image_lower + 1][BUFSIZE + 1];
  char *KeyStr[] = {
    "IMAGE VARIABLE",
    "IMAGE LAYER",
    "IMAGE START",
    "IMAGE END",
    "IMAGE INTERVAL",
    "IMAGE UPPER LIMIT",
    "IMAGE LOWER LIMIT"
  };
  char *SectionName = "OUTPUT";
  char VarStr[image_lower + 1][BUFSIZE + 1];
  float tmpInterval;

  for (i = NMaps - NImages; i < NMaps; i++) {

    /* Read the key-entry pairs from the input file */
    for (j = 0; j <= image_lower; j++) {
      sprintf(KeyName[j], "%s %d", KeyStr[j], i - (NMaps - NImages) + 1);
      GetInitString(SectionName, KeyName[j], "", VarStr[j],
        (unsigned long)BUFSIZE, Input);
    }

    /* Assign the entries to the appropriate variables */
    if (!CopyInt(&((*DMap)[i].ID), VarStr[image_variable], 1))
      ReportError(KeyName[image_variable], 51);

    if (!IsValidID((*DMap)[i].ID))
      ReportError("Input Options File", 19);

    if (IsMultiLayer((*DMap)[i].ID)) {
      MaxLayers = GetVarNLayers((*DMap)[i].ID, MaxSoilLayers, MaxVegLayers);
      if (!CopyInt(&((*DMap)[i].Layer), VarStr[image_layer], 1))
        ReportError(KeyName[image_layer], 51);
      if ((*DMap)[i].Layer < 1 || (*DMap)[i].Layer > MaxLayers)
        ReportError("Input Options File", 20);
    }
    else
      (*DMap)[i].Layer = 1;

    (*DMap)[i].Resolution = IMAGE_OUTPUT;

    strncpy((*DMap)[i].FileName, Path, BUFSIZE);
    GetVarAttr(&((*DMap)[i]));
    (*DMap)[i].NumberType = NC_BYTE;
    strcpy((*DMap)[i].Format, "%d");

    CreateMapFile((*DMap)[i].FileName, (*DMap)[i].FileLabel, Map);

    if (!SScanDate(VarStr[image_start], &Start))
      ReportError(KeyName[image_start], 51);

    if (!SScanDate(VarStr[image_end], &End))
      ReportError(KeyName[image_end], 51);

    if (!CopyFloat(&tmpInterval, VarStr[image_interval], 1))
      ReportError(KeyName[image_interval], 51);
    Interval = SECPHOUR * tmpInterval;

    if (Interval % Dt != 0 || Interval <= 0)
      ReportError("Input Options File", 24);

    if (((*DMap)[i].N = NumberOfSteps(&Start, &End, Interval)) < 1)
      ReportError("Input Options File", 25);

    if (!((*DMap)[i].DumpDate = (DATE *)calloc((*DMap)[i].N, sizeof(DATE))))
      ReportError(Routine, 1);

    CopyDate(&((*DMap)[i].DumpDate[0]), &Start);

    for (j = 1; j < (*DMap)[i].N; j++)
      (*DMap)[i].DumpDate[j] =
      NextDate(&((*DMap)[i].DumpDate[j - 1]), Interval);

    if (!CopyFloat(&((*DMap)[i].MaxVal), VarStr[image_upper], 1))
      ReportError(KeyName[image_upper], 51);

    if (!CopyFloat(&((*DMap)[i].MinVal), VarStr[image_lower], 1))
      ReportError(KeyName[image_lower], 51);
  }
}
Exemple #5
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);
  }
}
Exemple #6
0
int main(int argc, char* argv[])
{
    int ret;
	int i;
    HANDLE hMapFile=NULL;
    unsigned char* pMemBase=NULL;
    FILE* fp=NULL;
	DWORD stick,etick,ctick;

	DEBUG_INFO("\n");
	SDEBUG("\n");
    ParseParam(argc,argv);
	SDEBUG("\n");
	DEBUG_INFO("\n");
    hMapFile = CreateMapFile(st_pShareName,st_MemSize,st_CreateMem);
    if(hMapFile== NULL)
    {
        ret = -(LAST_ERROR_CODE());
		SDEBUG("can not %s %s memsize(%d:0x%x) error(%d)\n",
			st_CreateMem ? "Create" : "Open",st_pShareName,st_MemSize,st_MemSize,ret);
		ERROR_INFO("\n");
        goto out;
    }
	DEBUG_INFO("\n");
	SDEBUG("\n");

    pMemBase = MapFileBuffer(hMapFile,st_MemSize);
    if(pMemBase == NULL)
    {
        ret = -(LAST_ERROR_CODE());
		SDEBUG("\n");
		ERROR_INFO("\n");
        goto out;
    }
	DEBUG_INFO("\n");

    /*now to read from the file*/
    if(st_pFromFile && st_WriteInit)
    {
        ret = fopen_s(&fp,st_pFromFile,"r");
        if(fp == NULL)
        {
            ret = -(LAST_ERROR_CODE());
			ERROR_INFO("\n");
            goto out;
        }

        ret = fread(st_pBuffer,st_BufSize,1,fp);
        if(ret != 1)
        {
            ret = -(LAST_ERROR_CODE());
            fprintf(stderr,"could not read %d from %s (%d)\n",st_BufSize,st_pFromFile,ret);
            goto out;
        }

        ret = WriteShareMem(pMemBase,st_WriteOffset,st_pBuffer,st_BufSize);
        if(ret < 0)
        {
            ret = -(LAST_ERROR_CODE());
            fprintf(stderr,"could not write at offset %d (0x%x) in mem (%d:0x%x) for size(%d:0x%x) error(%d)\n",st_WriteOffset,st_WriteOffset,st_MemSize,st_MemSize,
                    st_BufSize,st_BufSize,ret);
            goto out;
        }
    }
    else if(st_pToFile && st_ReadInit)
    {
        ret = fopen_s(&fp,st_pToFile,"w");
        if(fp == NULL)
        {
            ret = -(LAST_ERROR_CODE());
			ERROR_INFO("\n");
            goto out;
        }

        ret = ReadShareMem(pMemBase,st_ReadOffset,st_pBuffer,st_BufSize);
        if(ret < 0)
        {
            ret = -(LAST_ERROR_CODE());
            fprintf(stderr,"could not read at offset (%d:0x%x) in mem (%d:0x%x) for size(%d:0x%x) error(%d)\n",
                    st_ReadOffset,st_ReadOffset,st_MemSize,st_MemSize,
                    st_BufSize,st_BufSize,ret);
            goto out;
        }

        ret = fwrite(st_pBuffer,st_BufSize,1,fp);
        if(ret != 1)
        {
            ret = -(LAST_ERROR_CODE());
            fprintf(stderr,"could not write %s for size (%d:0x%x) error(%d)\n",st_pToFile,
                    st_BufSize,st_BufSize,ret);
            goto out;
        }
    }
    else if(st_WriteInit)
    {
		ret = WriteShareMem(pMemBase,st_WriteOffset,st_pBuffer,st_BufSize);
		if (ret < 0)
		{
			ret = -(LAST_ERROR_CODE());
			fprintf(stderr,"could not write (%s) at offset (%d:0x%x) in mem (%d:0x%x) for size (%d:0x%x) error (%d)\n",
				st_pShareName,st_WriteOffset,st_WriteOffset,st_MemSize,st_MemSize,st_BufSize,st_BufSize,ret);
			goto out;
		}

		fprintf(stdout,"Write(%s) offset(%d:0x%x):\n",st_pShareName,st_WriteOffset,st_WriteOffset);
		for(i=0;i<st_BufSize;i++)
		{
			if ((i%16)==0)
			{
				fprintf(stdout,"\n0x%08x:\t",i);
			}
			fprintf(stdout," 0x%02x",st_pBuffer[i]);
		}
		fprintf(stdout,"\nsuccess\n");
    }
    else if(st_ReadInit)
    {
		ret=  ReadShareMem(pMemBase,st_ReadOffset,st_pBuffer,st_BufSize);
		if (ret < 0)
		{
			ret = -(LAST_ERROR_CODE());
			fprintf(stderr,"could not read (%s) at offset (%d:0x%x) in mem (%d:0x%x) for size (%d:0x%x) error (%d)\n",
				st_pShareName,st_ReadOffset,st_ReadOffset,st_MemSize,st_MemSize,st_BufSize,st_BufSize,ret);
			goto out;
		}

		fprintf(stdout,"Read(%s) offset(%d:0x%x):\n",st_pShareName,st_ReadOffset,st_ReadOffset);
		for(i=0;i<st_BufSize;i++)
		{
			if ((i%16)==0)
			{
				fprintf(stdout,"\n0x%08x:\t",i);
			}
			fprintf(stdout," 0x%02x",st_pBuffer[i]);
		}
		fprintf(stdout,"\nsuccess\n");
    }

	stick = GetTickCount();
	etick = stick + st_Timeout * 1000;
	ctick = stick;

	while (st_Timeout == 0 || ctick >= etick)
	{
		Sleep(1000);
		ctick = GetTickCount();
	}


    ret = 0;

out:
    if(fp)
    {
        fclose(fp);
    }
    fp = NULL;
    if(pMemBase)
    {
        UnmapViewOfFile(pMemBase);
    }
    pMemBase = NULL;

    if(hMapFile)
    {
        CloseHandle(hMapFile);
    }
    hMapFile = NULL;

    if(st_pBuffer)
    {
        free(st_pBuffer);
    }
    st_pBuffer = NULL;
    st_BufSize = 0;

    return 0;
}