Esempio n. 1
0
/*****************************************************************************
  Function name: InitNewDay()

  Purpose      : Initialize the Earth-Sun geometry variables at the beginning
                 of each day

  Required     : 
    int DayOfYear           - day of year (January 1 = 1)
    SOLARGEOMETRY *SolarGeo - structure with information about Earth-Sun 
                              geometry

  Returns      : void

  Modifies     : 
    SOLARGEOMETRY *SolarGeo

  Comments     : To be excuted at the beginning of each new day
*****************************************************************************/
void InitNewDay(int DayOfYear, SOLARGEOMETRY * SolarGeo)
{
  SolarDay(DayOfYear, SolarGeo->Longitude, SolarGeo->Latitude,
	   SolarGeo->StandardMeridian, &(SolarGeo->NoonHour),
	   &(SolarGeo->Declination), &(SolarGeo->HalfDayLength),
	   &(SolarGeo->Sunrise), &(SolarGeo->Sunset),
	   &(SolarGeo->TimeAdjustment), &(SolarGeo->SunEarthDistance));
}
Esempio n. 2
0
int main(int argc, char **argv)
{
  FILE   *demfile,*outfile,*outfile2;
  char   demfilename[255],outfilename[255],outfilename2[255];
  int    nRows;                    /* Number of rows */
  int    nCols;                    /* Number of columns */
  float  *temp;
  float  **elev,**slope,**aspect,**hillshade;
  unsigned char **outputgrid;
  int    i;
  int    ny,nx;
  float  lx,ly;
  double max_angle,angle;
  float  saz,sal;
  double theta;
  float  dx;
  float  x,y,sx,sy,dz,dist;
  float  mx,my;
  float  start_elev;
  float  max_elev,min_elev;
  float  target_row,target_col;
  int    stop_flag;
  float  a,b,c,d,e,f,g,h,j;
  float  dzdx,dzdy,rr;
  float  safe_distance;
  int    month,day,year,jday;
  float  hour;
  int    ihour;
  float  dt;
  int    count;
  float  outstep;
  int    stepsperday;
  int    daylight;
  int    sunlight;
  float  standardmeridian,latitude,longitude;
  float  noon_hour,  declination, halfdaylength, solar_hour;
  float  sunrise, sunset, timeadjustment, sunearthdistance;
  float  sinesolaraltitude, solartimestep, sunmax, solarazimuth;
  float  beam,diffuse;


  if(argc<13) {
    printf("usage is: make_dhsvm_shade_maps:  \n");
    printf("demfilename  \n");
    printf("outfilename  \n");
    printf("nrows, ncols \n");
    printf("cellsize (in the same units as the dem elevation)\n");
    printf("longitude and latitude of the site (dd)\n");
    printf("longitude of location for met file time stamp\n");
    printf("year month day output_time_step (hours)\n");
    exit(-1);
  }
  /* note: this program will loop over all time, starting at 0 and advancing */
  /* every hour */
  /* output images ranging from 0 to 255 are made at every output_time_step */
  /* these are in the proper format for DHSVM */

  strcpy(demfilename, argv[1]);   /* name of the binary flo  at dem input file - no header */
  strcpy(outfilename, argv[2]);   /* name of the binary float hillshade output file        */
  nRows = GetNumber(argv[3]);
  nCols = GetNumber(argv[4]);
  dx = GetFloat(argv[5]); /* the cellsize of the dem */                            
  longitude=GetFloat(argv[6])*RADPDEG;
  latitude=GetFloat(argv[7])*RADPDEG;
  standardmeridian=GetFloat(argv[8])*RADPDEG;
  year = GetNumber(argv[9]);
  month = GetNumber(argv[10]);
  day = GetNumber(argv[11]);
  outstep = GetFloat(argv[12]);

  printf("calculating shade map for %d / %d / %d \n",month,day,year);


  temp = calloc(nRows*nCols, sizeof(float));
  if (temp == NULL)
    exit(-1);

  if (!(demfile = fopen(demfilename, "rb"))){
    printf("dem file not found \n");
    exit(-1);
  }

  if (!(outfile = fopen(outfilename, "wb"))){
    printf("output file not opened \n");
    exit(-1);
  }
  /*
  sprintf(outfilename2,"%s.float",outfilename);
  if (!(outfile2 = fopen(outfilename2, "wb"))){
    printf("output file not opened \n");
    exit(-1);
  }
  */
  fread(temp, sizeof(float), nCols*nRows, demfile); 

  if (!((elev) = (float**) calloc(nRows, sizeof(float*))))
    exit(-1);
  for (ny = 0; ny < nRows; ny++) {
    if (!((elev)[ny] = (float*) calloc(nCols, sizeof(float))))
      exit(-1);
  }
  if (!((slope) = (float**) calloc(nRows, sizeof(float*))))
    exit(-1);
  for (ny = 0; ny < nRows; ny++) {
    if (!((slope)[ny] = (float*) calloc(nCols, sizeof(float))))
      exit(-1);
  }
  if (!((aspect) = (float**) calloc(nRows, sizeof(float*))))
    exit(-1);
  for (ny = 0; ny < nRows; ny++) {
    if (!((aspect)[ny] = (float*) calloc(nCols, sizeof(float))))
      exit(-1);
  }
  if (!((hillshade) = (float**) calloc(nRows, sizeof(float*))))
    exit(-1);
  for (ny = 0; ny < nRows; ny++) {
    if (!((hillshade)[ny] = (float*) calloc(nCols, sizeof(float))))
      exit(-1);
  }
  if (!((outputgrid) = (unsigned char**) calloc(nRows, sizeof(unsigned char*))))
    exit(-1);
  for (ny = 0; ny < nRows; ny++) {
    if (!((outputgrid)[ny] = (unsigned char*) calloc(nCols, sizeof(unsigned char))))
      exit(-1);
  }

  max_elev = 0.0;
  for (ny = 0; ny < nRows; ny++) {
    for (nx = 0; nx < nCols; nx++) {
      elev[ny][nx] = temp[ny*nCols + nx]; 
      if(elev[ny][nx]>max_elev) max_elev = elev[ny][nx];
    }
  }

  CalcSlopeAspect(nRows,nCols,dx,elev,&slope,&aspect);


  dt=outstep;
  stepsperday=(int)(24/outstep);
  for(i=0;i<stepsperday;i++){
    hour=(float)i*outstep;
    printf("working on hour %f \n",hour);
    jday=DayOfYear(year,month,day);
  
  SolarDay(jday, longitude, latitude,
           standardmeridian, &noon_hour, 
           &declination, &halfdaylength,
           &sunrise, &sunset, &timeadjustment, &sunearthdistance);

  SolarHour(latitude, hour+dt, dt, noon_hour, &solar_hour,
	    declination,sunrise, sunset,
	    timeadjustment, sunearthdistance,
	    &sinesolaraltitude, &daylight, &solartimestep,
	    &sunmax, &solarazimuth); 
  sal=asin(sinesolaraltitude);
  saz=solarazimuth;

  /*  printf("for %2d/%2d/%4d at met-file-time %5.2f and solar hour %5.2f \n",
	  month,day,year,hour+0.5*dt,solar_hour);
  printf(" sunrise is at %5.2f with sunset at %5.2f and solar alt: %f with azimuth %f \n",
  sunrise,sunset,sal*DEGPRAD,saz*DEGPRAD);*/

  CalcHillShadeWithTerrainBlocking(nRows,nCols,dx,max_elev,elev,
				   sal,saz,slope,aspect,&hillshade);

  /* at this point hillshade is between 0 and 255 */
  /* which is the standard arc-info for the hillshade command */
  /* we need to translate this to the proper dhsvm format */
  /* and output it as an unsigned char */

    for (ny = 0; ny < nRows; ny++) {
      for (nx = 0; nx < nCols; nx++) {
	if(sinesolaraltitude>0)
	if(hillshade[ny][nx]/255/sinesolaraltitude>11.47)
	  outputgrid[ny][nx]=255;
	else
	  outputgrid[ny][nx]=(unsigned char)(hillshade[ny][nx]/sinesolaraltitude/11.47);
	else outputgrid[ny][nx]=0;
      }
    }
 


    /*       for (ny = 0; ny < nRows; ny++) {
     fwrite(hillshade[ny],sizeof(float),nCols,outfile2); 
     }*/
       
	for (ny = 0; ny < nRows; ny++) {
      fwrite(outputgrid[ny],sizeof(unsigned char),nCols,outfile); 
	}
		

      }
    }