#include #include #include #include #include #include "delta.h" #include "kirkrand.h" FILE *fpin, *fpout; /*************************************************************************** CheckRunParam() This function checks the values of the parameters for a model run. Arguments: struct Run *runprm pointer to run parameter struct struct Basin *basin pointer to basin parameter struct struct Lifehist *lifehist pointer to life history struct Return Value: void ****************************************************************************/ void CheckRunParam(struct Basin *basin, struct Lifehist *lifehist, struct Run *run) { int i, errors, sperror, surverror, prerror, rherror, proderror, bankerror, germerror, minerror, maxerror, disperror; errors = sperror = surverror = prerror = rherror = proderror = bankerror = germerror = minerror = maxerror = disperror = 0; if(run->runlength[0] < 0) { errors++; printf("\n\trunlength > 0"); } if(run->waterfile[0] != 0 && run->waterfile[0] != 1) { errors++; printf("\n\twaterfile 0 or 1"); } if(run->basinfile[0] != 0 && run->basinfile[0] != 1) { errors++; printf("\n\tbasinfile 0 or 1"); } if(run->adultfile[0] != 0 && run->adultfile[0] != 1) { errors++; printf("\n\tadultfile 0 or 1"); } if(run->writeadult[0] < 0 || run->writeadult[0] > run->runlength[0]) { errors++; printf("\n\t0 <= writeadult <= runlength"); } if(run->writebasin[0] != 0 && run->writebasin[0] != 1) { errors++; printf("\n\twritebasin must be 0 or 1"); } if(run->maxrhiz[0] < basin->cellsize[0] || run->maxrhiz[0] > sqrt((double)basin->area[0])) { errors++; printf("\n\tcellsize <= maxrhiz <= sqrt(area)"); } if(run->showrun[0] != 0 && run->showrun[0] != 1) { errors++; printf("n\\tshowrun must be 0 or 1"); } if(run->showplant[0] != 0 && run->showplant[0] != 1) { errors++; printf("n\\tshowplant must be 0 or 1"); } if(run->showadult[0] != 0 && run->showadult[0] != 1) { errors++; printf("n\\tshowadult must be 0 or 1"); } if(run->showwater[0] != 0 && run->showwater[0] != 1) { errors++; printf("n\\tshowwater must be 0 or 1"); } if(run->showbasin[0] != 0 && run->showbasin[0] != 1) { errors++; printf("n\\tshowbasin must be 0 or 1"); } if(basin->cellsize[0] < 0) { errors++; printf("\n\tcellsize > 0"); } if(basin->area[0] < basin->cellsize[0] || basin->cellsize[0] <=0) { errors++; printf("\n\t0 < cellsize < area"); } if(basin->buffer[0] < 0) { errors++; printf("\n\tbuffer >= 0"); } if(basin->zmin[0] > 0 || basin->zmin[0] < -10) { errors++; printf("\n\t0 <= zmin <= 10"); } if(basin->zmax[0] < 0 || basin->zmax[0] > 5) { errors++; printf("\n\t0 <= zmax <= 5"); } if(basin->xyratio[0] < 0 || basin->xyratio[0] > 10) { errors++; printf("\n\t0.1 <= xyratio <= 10"); } if(basin->curve[0] < 0.5 || basin->curve[0] > 32) { errors++; printf("\n\t0.1 <= curve <= 10"); } if(basin->watercos[0] != 0 && basin->watercos[0] != 1) { errors++; printf("\n\twatercos must be = 0 or 1"); } if(basin->yearint[0] < 0) { errors++; printf("\n\tyearint >= 0"); } if(basin->drawfreq[0] <= 0 ) { errors++; printf("\n\t0 < drawfreq"); } if(basin->drawint[0] < 0) { errors++; printf("\n\tdrawint >= 0"); } for(i=0; i < MAXSP; i++) { if(lifehist->species[i] < 0) { errors++; sperror++; } if(lifehist->seedsurv[i] < 0||lifehist->seedsurv[i] > 1) { errors++; surverror++; } if(lifehist->germrate[i] < 0 || lifehist->germrate[i] > 1) { errors++; germerror++; } if(lifehist->seedprod[i] < 0) { errors++; proderror++; } if(lifehist->longdisp[i] != 0 && lifehist->longdisp[i] != 1) { errors++; disperror++; } if(lifehist->peren[i] != 0 && lifehist->peren[i] != 1) { errors++; prerror++; } if(lifehist->rhizome[i] != 0 && lifehist->rhizome[i] != 1) { errors++; rherror++; } if(lifehist->depthmean[i] < basin->zmin[0]-basin->zmax[0] || lifehist->depthmean[i] > -basin->zmin[0]) { errors++; minerror++; } if(lifehist->depthwidth[i] < 0) { errors++; maxerror++; } } if(sperror) printf("\n\tspecies no. > 0"); if(surverror) printf("\n\t0 < seedlife < 1"); if(rherror) printf("\n\tlongdisp = 0 or 1"); if(prerror) printf("\n\tperen = 0 or 1"); if(rherror) printf("\n\trhizome = 0 or 1"); if(minerror) printf("\n\t-zmax <= depthmean <= -zmin"); if(errors) { printf("\n\nThere are %d errors in the *.prm file", errors); exit(EXIT_FAILURE); } } /*************************************************************************** GetAdults() This function gets the adult distribution from a file. Arguments: struct Basin *basin pointer to basin parameters struct Plant ***plant pointer to plant data Return Value: void ****************************************************************************/ void GetAdults(struct Basin *basin, struct Lifehist *lifehist, struct Plant ***plant, struct Run *run, double **topo, double *water) { int x, y, i, adult; char buff[MAXSTRING]; double depth, mean,width,select,z,zsqr,prob; fgets(buff, MAXSTRING, fpin); if(toupper(buff[0]) !='D') /*check if input is NOT a surfer file*/ { fgets(buff, MAXSTRING, fpin); /*move file pointer an extra line for ARC/GRID files*/ } fgets(buff, MAXSTRING, fpin); /*move file pointer to begining of topo data*/ fgets(buff, MAXSTRING, fpin); fgets(buff, MAXSTRING, fpin); fgets(buff, MAXSTRING, fpin); for (y = 0; y < basin->ymax[0]; y++) { for (x = 0; x < basin->xmax[0]; x++) { for(i=0; i < MAXSTRING; i++) { buff[i]=fgetc(fpin); if(isspace(buff[i])){ /*test for whitespace*/ if(i) { /*test for adjacent whitespace*/ buff[i] = '\0'; break; } else { /*decrement for adjacent tabs or returns*/ i--; } } } if(buff[1] == '.') { adult = -9999; } else { sscanf(buff,"%d",&adult); } if(adult == 7) { /*make other species equal to open areas*/ adult = 0; } plant[x][y][0].adult[0] = adult; if(adult == -9999 || adult == 0) { /*end iteration if nodata value is encountered or open area is encountered*/ continue; } GetDepth(basin, run, topo, water, &depth, x, y); GetProb(&prob, &adult, &depth); mean = lifehist->depthmean[adult]; if((select=dr250()) < prob || depth < mean) { /*is above water depth tolerance*/ plant[x][y][0].reproduce[0] = 1; /*make plants reproductive*/ } } } } /*************************************************************************** GetBasinParams() This function gets the dimensions of the basin from a file. Arguments: struct Basin *basin pointer to basin file Return Value: void ****************************************************************************/ void GetBasinParams(struct Basin *basin) { char buff[MAXSTRING]; float f, g; int nx, ny; fgets(buff, MAXSTRING, fpin); /*check if input is surfer file*/ if(toupper(buff[0])=='D') { /*code for surfer files*/ fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d\t%d",&nx, &ny); basin->xmax[0] = nx; basin->ymax[0] = ny; fgets(buff, MAXSTRING, fpin); fgets(buff, MAXSTRING, fpin); fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f\t%f",&f, &g); basin->zmin[0] = (double)f; basin->zmax[0] = (double)g; } else { /*code for ARC/GRID files*/ sscanf(buff, "%*s%d",&nx); basin->xmax[0] = nx; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%*s%d",&ny); basin->ymax[0] = ny; } basin->cellsize[0] = 1; } /*************************************************************************** GetBasinTopo() This function gets the basin topography from a file. Elevations are standardized relative to the maximum flooding level in the basins (248.5 AMSL) so that positive values are never flooded. Normal level is 1 m (247.5 AMSL). Arguments: double **topo pointer to basin array struct Run *run pointer to run parameters Return Value: void ****************************************************************************/ void GetBasinTopo(double **topo, struct Basin *basin) { int x, y, i; int arc=0; int init=1; char buff[MAXSTRING]; float f; double zmax, zmin; fgets(buff, MAXSTRING, fpin); if(toupper(buff[0]) !='D') /*check if input is NOT a surfer file*/ { fgets(buff, MAXSTRING, fpin); /*move file pointer an extra line for ARC/GRID files*/ arc = 1; } fgets(buff, MAXSTRING, fpin); /*move file pointer to begining of topo data*/ fgets(buff, MAXSTRING, fpin); fgets(buff, MAXSTRING, fpin); fgets(buff, MAXSTRING, fpin); for (y = 0; y < basin->ymax[0]; y++) { for (x = 0; x < basin->xmax[0]; x++) { for(i=0; i < MAXSTRING; i++) { buff[i]=fgetc(fpin); if(isspace(buff[i])){ /*test for whitespace*/ if(i) { /*test for adjacent whitespace*/ buff[i] = '\0'; break; } else { /*decrement for adjacent whitespace*/ i--; } } } if(buff[1] == '.') { f = -9999; } else { sscanf(buff,"%f",&f); } topo[x][y] = (double)f; if(f==-9999) continue; if(arc==1) topo[x][y] -= 248.5; /*zero delta grids on max high water line if they are in arc format*/ if(init) { /*ck if zmax and zmin are initialized*/ zmax = zmin = topo[x][y]; init--; } if(zmax < topo[x][y]) zmax = topo[x][y]; if(zmin > topo[x][y]) zmin = topo[x][y]; } } basin->zmax[0] = zmax; basin->zmin[0] = zmin; } /*************************************************************************** GetFilename() This function gets a filename. Arguments: char pointer to filename Return Value: void ****************************************************************************/ void GetFilename(char *filename) { char buff[20]; int param; for (EVER) { gets(filename); printf("Input File: %s\n", filename); printf("\nIs this correct (y or n)?"); gets(buff); param = toupper(buff[0]) - 'N'; if (param) { break; } } } /*************************************************************************** GetFilenameDate(); This function outputs the adult distribution to a file. Arguments: struct Run *run pointer to run parameters char filename[] filename string int year current year of run Return Value: void ****************************************************************************/ void GetFilenameDate(struct Run *run, char filename[], int year) { char *cptr, s[5]; int c, i, j; i = 0; do { s[i++] = year % 10 + '0'; year /= 10; } while(i < 4); s[i] = '\0'; for(i = 0, j = strlen(s) -1; i < j; i++, j--) { c = s[i]; s[i] = s[j]; s[j] = c; } for(i = 0, cptr = filename; i < 4; i++, cptr++) { *cptr = run->runname[i]; } for(i = 0; i < 4; i++, cptr++) { *cptr = s[i]; } } /*************************************************************************** GetWaterRegime() This function reads in the data file. Arguments: double **data pointer to data array Return Value: void ****************************************************************************/ void GetNewRunlength(struct Run *run) { char buff[MAXSTRING]; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d%*s", run->h2olength); } /*************************************************************************** GetRunParam() This function gets the parameters for a model run. Arguments: struct Run *runprm pointer to run parameter struct struct Basin *basin pointer to basin parameter struct struct Lifehist *lifehist pointer to life history struct Return Value: void ****************************************************************************/ void GetRunParam(struct Basin *basin, struct Lifehist *lifehist, struct Run *run) { char buff[MAXSTRING]; float f; int i,j; fgets(buff, MAXSTRING, fpin); for(i=0; i < 4; i++) { run->runname[i] = buff[i]; } fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d%*s",run->runlength); memset(run->watername, '\0', strlen(run->watername) - 1); fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d%s",run->waterfile,run->watername); fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d\t%s",run->basinfile,run->basinname); fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d\t%s",run->adultfile,run->adultname); fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d%*s",run->writebasin); fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d%*s",run->writeadult); fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f%*s",&f); run->maxrhiz[0] = (double)f; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d%*s",run->showrun); fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d%*s",run->showplant); fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d%*s",run->showadult); fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d%*s",run->showwater); fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d%*s",run->showbasin); fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f%*s",&f); basin->cellsize[0] = (double)f; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%ld%*s",basin->area); fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d%*s",basin->buffer); fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f%*s",&f); basin->zmin[0] = (double)f; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f%*s",&f); basin->zmax[0] = (double)f; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f%*s",&f); basin->xyratio[0] = (double)f; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f%*s",&f); basin->curve[0] = (double)f; fgets(buff, MAXSTRING, fpin); sscanf(buff,"%d%*s", basin->watercos); fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f%*s",&f); basin->yearint[0] = (double)f; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f%*s",&f); if(toupper(buff[0])=='Z') basin->drawint[0] = -basin->zmin[0]; else basin->drawint[0] = (double)f; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d%*s",basin->drawfreq); for(i=0; ispecies[i] = j; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f%*s", &f); lifehist->seedsurv[i] = (double)f; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f%*s", &f); lifehist->germrate[i] = (double)f; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f%*s", &f); lifehist->seedvar[i] = (double)f; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f%*s", &f); lifehist->seedb0[i] = (double)f; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f%*s", &f); lifehist->seedb1[i] = (double)f; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f%*s", &f); lifehist->seedb2[i] = (double)f; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d%*s", &j); lifehist->seedprod[i] = j; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d%*s", &j); lifehist->longdisp[i] = j; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d%*s",&j); lifehist->peren[i] = j; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%d%*s",&j); lifehist->rhizome[i] = j; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f%*s", &f); lifehist->depthmean[i] = (double)f; fgets(buff, MAXSTRING, fpin); sscanf(buff, "%f%*s", &f); lifehist->depthwidth[i] = (double)f; } } /*************************************************************************** GetWaterRegime() This function reads in the data file. Arguments: double **data pointer to data array Return Value: void ****************************************************************************/ void GetWaterRegime(double *h2oregime, struct Run *run) { char buff[MAXSTRING]; float f; int t; fgets(buff, MAXSTRING, fpin); fgets(buff, MAXSTRING, fpin); for(t=0; t< run->h2olength[0]; t++) { fgets(buff, MAXSTRING, fpin); sscanf(buff,"%f", &f); h2oregime[t] = (double)f; } } /*************************************************************************** WriteAdult (); This function outputs the adult distribution to a file. Arguments: float *element pnter to first matrix element int maxrow number of rows in matrix int maxcol number of columns in matrix struct parameters *parab_ptr pointer to basin parameters Return Value: void ****************************************************************************/ void WriteAdult(struct Basin *basin, struct Plant ***plant, struct Run *run, int year, double **topo, double *water) { double i, j, depth; int x, y, adult; char filename[20]={"xxxxxxxx.grd"}, write[1] = {"w"}; if(run->writeadult[0]) { GetFilenameDate(run, filename, year); #ifdef SCREENIO printf("\nAdult data written to: %s",filename); #endif OpenFile(filename,write); j = (double) basin->cellsize[0]*basin->xmax[0]; i = (double) basin->cellsize[0]*basin->ymax[0]; if ((fprintf(fpout,"DSAA\n%d\t%d\n%d\t%f\n%d\t%f\n%d\t%d\n", basin->xmax[0], basin->ymax[0], 0, j, 0, i, 0, MAXSP))<0) { printf("\nDisk Write Error"); exit(EXIT_FAILURE); } for (y = 0; y < basin->ymax[0]; y++) { for (x = 0; x < basin->xmax[0]; x++) { if(plant[x][y][0].adult[0]==-9999) { if ((fprintf(fpout, "1.70141e+038\t")) < 0) { printf("\nDisk Write Error"); exit(EXIT_FAILURE); } } else { adult = plant[x][y][0].adult[0]; if(adult == 7) { /*make other species equal to open areas*/ adult = 0; } GetDepth(basin, run, topo, water, &depth, x, y); if(depth > 0.7) {/*don't show plant when depth is > 0.7 m */ adult = 0; } if ((fprintf(fpout, "%d\t",adult)) < 0) { printf("\nDisk Write Error"); exit(EXIT_FAILURE); } } } fprintf(fpout, "\n"); } } fclose(fpout); } /*************************************************************************** WriteBasin (); This function outputs the basin matrix to a file. Arguments: float *element pnter to first matrix element int maxrow number of rows in matrix int maxcol number of columns in matrix struct parameters *parab_ptr pointer to basin parameters Return Value: void ****************************************************************************/ void WriteBasin(struct Basin *basin, struct Run *run, double **topo) { double d, i, j; int x, y; char *cptr, filename[20]={"xxxxbasn.grd"}, write[1] = {"w"}; int k; if(run->writebasin[0]) { for(k=0, cptr = filename; k < 4; k++, cptr++) { *cptr = run->runname[k]; } OpenFile(filename,write); j = (double) basin->cellsize[0]*basin->xmax[0]; i = (double) basin->cellsize[0]*basin->ymax[0]; if ((fprintf(fpout,"DSAA\n%d\t%d\n%d\t%f\n%d\t%f\n%f\t%f\n", basin->xmax[0], basin->ymax[0], 0, j, 0, i, basin->zmin[0], basin->zmax[0]))<0) { printf("\nDisk Write Error"); exit(EXIT_FAILURE); } for (y = 0; y < basin->ymax[0]; y++) { for (x = 0; x < basin->xmax[0]; x++) { GetElev(basin, run, topo, &d, x, y); if(d==-9999) { if ((fprintf(fpout, "1.70141e+038\t")) < 0) { printf("\nDisk Write Error"); exit(EXIT_FAILURE); } } else { if ((fprintf(fpout, "%f\t",d )) < 0) { printf("\nDisk Write Error"); exit(EXIT_FAILURE); } } } fprintf(fpout, "\n"); } fclose(fpout); #ifdef SCREENIO printf("\nBasin Data written."); #endif } }