Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
Basic InformationTitle: A Watershed-Scale Biogeochemical Loading Model for Nitrogen and PhosphorusProject Number: G-12Start Date: 9/1/2000End Date: 8/31/2003Research Category: Water QualityFocus Category: Hydrology, Models, NutrientsDescriptors:denitrification, ecosystems, hydrologic models, geographic information systems,land-water interactions, land use, mathematical models, rainfall-runoffprocesses, watershed managementLead Institute: New York State Water Resources InstitutePrincipal Investigators: Robert W Howarth, Elizabeth W. Boyer, Dennis Swaney
Title: A Watershed-Scale Biogeochemical Loading Model for Nitrogen and PhosphorusPrincipal: Robert W. Howarth, Dept. of Ecology and Evolutionary Biology, Cornell University, Ithaca, NY, 14850 and The Ecosystems Center, Marine Biological Lab, Woods Hole, MA 02543
Problem and Research Objectives
Two recent reports from the National Academy of Sciences have concluded that eutrophication is the biggest pollution problem in the coastal marine waters of the United States (NRC 1993, NRC 2000). Eutrophication lowers biotic diversity, leads to hypoxic and anoxic conditions, facilitates harmful algal blooms, causes dieback of seagrass beds, and can lead to changes in ecological food webs that lower fishery production (NRC 2000). Over 40% of the estuaries in the country are degraded from eutrophication, with the problem being particularly severe in the northeastern and mid-Atlantic regions (Bricker 1999). For most estuaries in these regions, eutrophication is caused primarily from over-enrichment with nitrogen; phosphorus is a secondary contributor (Howarth 1988; Nixon 1995; NRC 2000). Most of the nitrogen delivered to coastal waters in the US, including the northeastern and mid-Atlantic regions, comes from non-point sources in the watershed (Howarth et al. 1996). Agricultural sources are important in some watersheds, dominating the flux in the Mississippi River basin and contributing to the flux of some estuaries in the mid-Atlantic region, but atmospheric deposition of nitrogen from fossil-fuel combustion is an even greater source of nitrogen to estuaries for most of the mid-Atlantic region and for the northeastern US (Howarth et al. 1996; Smith et al. 1997; Jaworski et al. 1997; Goolsby et al. 1999; NRC 2000).
In regions subject to changes in land use and in atmospheric deposition of nitrogen, the processes that control nutrient loads to the coastal zone are
complex. Variability of these hydrological and biogeochemical processes is increasing as weather and climate change. Understanding how these processes affect the magnitude and transformations of the nutrient loads is necessary in order to manage the environmental resources of the coastal zone. Further, it is important for those living in and managing coastal watersheds to understand the impacts of their activities and policies on these nutrient loads. A relativelysimple modeling tool that can estimate the impacts of various activities in the watersheds can greatly enhance, at low cost, our ability to manage these regions effectively and to communicate the effects of human activities and environmental processes on nutrient loads. The report of the National Academy of Science s Committee on Causes and Management of Coastal Eutrophication concluded that no model currently available to managers fulfills this need for estimating the controls on nitrogen loads (NRC 2000).
They noted in particular that most models used by watershed and estuarine managers fail to deal adequately with nitrogen deposition onto the landscape with subsequent export downstream, even though this is the number one input of nitrogen to many estuaries. The Committee further concluded that the development of such a model particularly one that deals with atmospheric deposition -- is one of the most pressing priorities for solving the problem of coastal eutrophication (NRC 2000). Our aim has been to develop such a model.
To mitigate the effects of human activities on the supply of nutrients to surface waters, managers are tasked with gaining an understanding of the landscape source areas delivering nutrients to receiving waters. We have developed an easy-to-use model for calculating loads of N and P to coastal watersheds, targeted toward management applications. The model describes transport of water, sediment and nutrients from the landscape to receiving waters. Our goal has been to create a model structure that will be used widely; thus we have developed the model in a commonplace platform: the Excel workbook. This version of the model, GWLFXL1.0, runs as a Visual Basic for Applications (VBA) program with an Excel interface.
Model Summary
In its current form, the model uses the event-based dynamics of a simple, lumped hydrologic model (Generalized Watershed Loading Function (GWLF) (Haith and Shoemaker, 1987) GWLF is a parsimonious, event-based model that has been used successfully to analyze the hydrology, sediment, and nutrient loads of several mixed watersheds in the United States, including the New York City reservoir system, the Hudson River (Howarth et al., 1991; Swaney et al., 1996), the Tar-Pamlico (Dodd and Tippett, 1994), and the Choptank River drainage of the Chesapeake Bay (Lee et al., 2000). We have added additional functionality to handle atmospheric deposition of nutrients, simple estimates of denitrification rate, and changes over time of the areas of different landuse/land cover categories. The original model used daily historic or synthetic temperature
and precipitation data to simulate monthly discharge, sediment load, and nutrient transport. We have developed a separate stand-alone weather generation package (also Excel/VBA based) to allow the user to generate alternate climate scenarios in a format compatible with the model.
New Features
Model Input/Output After the “port” of GWLF code to Excel was achieved, several features of i/o were radically redesigned in the interest of flexibility:
Model simulation options are now controlled primarily from an Excel pulldown menu (GWLFXL) which appears when the workbook is loaded.
Model parameters can now be read either from existing GWLF input files (ie text files) or from parameter worksheets embedded within the workbook.
Model output is now organized into several output worksheets, depending upon the time scale desired (ie annual, monthly, or daily). Worksheets that group the output by land use category are also generated at the option of the user. An advantage of organizing model output by worksheet is the ready creation of graphics within Excel from the tabulated values, or further user-generated statistical analyses of model scenarios.
Model Calibration Mode. A major addition to the package is the model calibration mode which utilizes the Solver addin feature of Excel to obtain a least-squares fit of a selection of model parameters to monthly streamflow, sediment flux, or nitrogen flux data. Model parameters are selected and calibration datasets are entered in the calibrate worksheet. The desired calibration mode is chosen from the pulldown menu. Solver then drives the model, iteratively changing the selected parameters, until model best matches the data in a least-squares sense. Up to 5 parameters may be selected, though as of this writing, the procedure appears to work best with one or 2 parameters at a time.
Parameter Uncertainty Analysis. Another new mode of using the model is parameter uncertainty analysis, in which the effect of uncertainty about parameter values on model output is estimated quantitatively. The process occurs in 3 steps:
In the “stochastic” worksheet, the model parameters to be investigated are assigned probability distributions, together with estimates of their mean and variance, etc
The user chooses the number of replicate runs desired for the analysis, and then draws the corresponding parameter values from their individual distributions; this option is selected from the pulldown menu
The user runs the model in uncertainty mode, repeating the simulation for each realization of the parameter values, and the mean and standard deviation of the model outputs are stored in the “uncertainty” worksheet.
When the runs are complete, the user can plot the time series of means and confidence intervals for any model variable corresponding to the selection of parameters evaluated.
Project updates and website
The current version of the model and associated documentation and tutorials can be downloaded from the project website: http://cfe.cornell.edu/biogeo/USGSWRI.htm. Model updates, fixes, and future documentation will be made available here as well. While the VBA module containing the code is currently password protected to prevent tampering, the code is provided in Appendix 1. Interested researchers can obtain the password by contacting Dennis Swaney at [email protected].
Current and future research directions
Although the USGS/WRRI funded phase of the project has ended, we have obtained additional funding from an EPA star grant to pursue model development. We are currently engaged in adding more functionality to the model, aiming in particular at refining the descriptions of watershed biogeochemistry and hydrology, writing a model description for publication in a peer-reviewed journal, and beginning to evaluate the model against estimates of nitrogen load for 16 northeast US watersheds (Boyer et al., 2002). Links to further progress with the model development will be reported at the above website.
Publications and presentations associated with the project, 2002-2003
Boyer, E.W., C. L. Goodale, N. A. Jaworski and R. W. Howarth. 2002. Anthropogenic nitrogen sources and relationships to riverine nitrogen export in the northeastern U.S.A. Biogeochemistry 57/58:137-169.
Boyer, E. W., R. W. Howarth, and J. N. Galloway. Riverine nitrogen export from the world’s watersheds. Presentation at the 17th Biennial conference of the Estuarine Research Federation, Seattle, WA, September 14-18, 2003.
Howarth, R.W., R. Marino, E.W. Boyer and D.P. Swaney. Potential consequences of climate change on delivery of nutrients to estuaries. Presentation at the 17th Biennial conference of the Estuarine Research Federation, Seattle, WA, September 14-18, 2003.
Howarth, R.W., A. Sharpley and D. Walker. 2002. Sources of Nutrient Pollution to Coastal Waters in the United States: Implications for Achieving Coastal Water Quality Goals. Estuaries 25:656-676.
Mayer, B., E.W. Boyer, C. Goodale, N. A. Jaworski, N.ico Van Breemen,
R.W. Howarth, S. Seitzinger, G. Billen, K. Lajtha, K. Nadelhoffer, D. Van Dam,L. J. Hetling, M. Nosal and K. Paustian. 2002. Sources of nitrate in rivers draining sixteen watersheds in the northeastern U.S.: Isotopic constraints. Biogeochemistry 57/58:171-197.
Seitzinger, S. P., R. V. Styles, E. W. Boyer, R. B. Alexander, G. Billen, R. W.Howarth, B. Mayer and N. Van Breemen. 2002. Nitrogen retention in rivers: model development and application to watersheds in the northeastern U.S.A. Biogeochemistry 57/58:199-237.
Swaney, D.P., R.W. Howarth and E.W. Boyer. Implementing a management oriented nutrient loading model in Excel/VBA. (in preparation for submission to Ecological Modeling)
Swaney, D.P., R. W. Howarth and E.W. Boyer. ReNuMa: A regional scale nutrient loading model for management. Poster presentation at the 17 th Biennial conference of the Estuarine Research Federation, Seattle, WA, September 14-18, 2003.
Van Breemen, N., E. W. Boyer, C. Goodale, N. A. Jaworski, K. Paustian, S. P. Seitzinger, K. Lajtha, B. Mayer, D. Van Dam, R. W. Howarth, J.J. Nadelhoffer, M. Eve, and G. Billen. 2002. Where did all the nitrogen go? Fate of nitrogen inputs to large watersheds in the northeastern USA. Biogeochemistry 57/58:267-293.
References
Boyer, E.W., C. L. Goodale, N. A. Jaworski and R. W. Howarth. 2002. Anthropogenic nitrogen sources and relationships to riverine nitrogen export in the northeastern U.S.A. Biogeochemistry 57/58:137-169.
Bricker, S. B., C. G. Clement, D. E. Pirhalla, S. P. Orland, and D. G. G. Farrow. 1999. National Estuarine Eutrophication Assessment: A Summary of Conditions, Historical Trends, and Future Outlook. National Ocean Service, National Oceanic and Atmospheric Administration, Silver Springs, MD.Dodd, R.C. and J.P. Tippett. 1994. Nutrient Modeling and Management in the Tar- Pamlico River Basin. Research Triangle Institute. Unpublished Report.
Goolsby, D. A., W. A. Battaglin, G. B. Lawrence, R. S. Artz, B. T. Aulenbach, and R. P. Hooper. 1999. Gulf of Mexico hypoxia assessment, Topic #3, Flux and sources of nutrient in the Mississippi-Atchafalaya River basin. Committee on Environmental and Natural Resources, Hypoxia Work Group for the Mississippi River/Gulf of Mexico Watershed Nutrient Task Force
Haith, D.A. and L.L. Shoemaker. 1987. Generalized watershed loading functions for stream flow nutrients. Wat. Res. Bull. 23(3):471-478.
Howarth, R. W. 1988. Nutrient limitation of net primary production in marine ecosystems. Ann. Rev. Ecol. & Syst. 19: 89-110.
Howarth, R.W, J.R. Fruci, and D. Sherman. 1991. Inputs of sediment and carbon to an estuarine ecosystem: Influence of land use. Ecol. Appl. 1:27-39
Howarth, R.W., G. Billen, D. P. Swaney, A. Townsend, N. Jaworski, K. Lajtha, J. A. Downing, R. Elmgren, N. Caraco, T. Jordan, F. Berendse, J. Freney, V. Kudeyarov, P. Murdoch, Zhu Zhao-liang. 1996. Riverine Inputs of Nitrogen to the North Atlantic Ocean: Fluxes and Human Influences. Biogeochemistry, 35:75-139.
Jaworski, N. A., R. W. Howarth, and L. J. Hetling. 1997. Atmospheric deposition of N oxides onto the landscape contributes to coastal eutrophication in the northeast United States. Environ. Sci. Technol. 31: 1995-2004.
Lee, Kuang-Yao, T. R. Fisher, T. E. Jordan, D. L. Correll, and D. E. Weller.2000. Modeling the hydrochemistry of the Choptank River basin using GWLFand Arc/Info: 1. Model calibration and validation. Biogeochemistry 49:143-173.
National Research Council. 1993. Managing Wastewater in Coastal Urban Areas. National Academy Press, Washington, DC.
National Research Council. 2000. Clean Coastal Waters: Understanding and Reducing the Effects of Nutrient Pollution. National Academy Press, Washington, DC.
Nixon, S. W. 1995. Coastal marine eutrophication: a definition, social causes, and future concerns. Ophelia 41: 199-219.
Smith, R. A., G. E. Schwarz, and R. B. Alexander. 1997. Regional interpretation of water-quality monitoring data. Wat. Resour. Res. 33(12):2781-2798.Swaney, D.P., D. Sherman, and R.W. Howarth. 1996. Modeling Water, Sediment, and Organic Carbon Discharges in the Hudson/Mohawk Basin: Coupling to Terrestrial Sources. Estuaries, 19(4):833-847.
Appendix 1. GWLFXL1.0 source code. October, 2003.
1) Module 1. Model
Option Base 1 ' Module-level variable declarations '******************************************************************* ' DIMENSIONS - MAIN PROG & OUTPUT '*******************************************************************Public iter As IntegerPublic STREAMFLOW(), PRECIPITATION(), evapotrans(), GROUNDWATER(), RUNOFF() As Single Public EROSION(), SEDYIELD() As SinglePublic GROUNDNITR(), GROUNDPHOS(), DISNITR(), TOTNITR(), DISPHOS(), TOTPHOS() As SinglePublic wetdepnitr(), drydepnitr(), lu_denit(), gwdenitfrac, gwdenit(), denitfrac(), denittot()Public lu_RUNOFF(), lu_EROSION(), lu_DISNITR(), lu_TOTNITR(), lu_DISPHOS(), lu_TOTPHOS() As Single '********************************************************************* ' DIMENSIONS - MAIN PROG & NUTRIENTS '*********************************************************************Public nitrconc(200), phosconc(200), mannitr(200), manphos(200), urbannitr(200), urbanphos(200), pointnitr(0 To 12) As Single, pointphos(0 To 12) As SinglePublic x(15, 12), Q(15, 12), SEDTRANS() As Single, BSED(12), URBANSED(200) As SinglePublic temp(), PREC(), tempav(), DAYSMONTH(12) As Single'***********************************************************************' DIMENSIONS - MAIN PROG, TRANSPORT,NUTRIENTS & 0UTPUT'***********************************************************************Public dayhrs(12), grow(12), AREA(200), KLSCP(200), CN(3, 200), ANTMOIST(5) As SinglePublic month$(12, 4), landuse$(15, 12), areafinal(200), annareainc(200), AREAINIT(200)Public dsN(4, 12), dsP(4, 12), a(4, 12), septicN(), septicP()'declare remaining global variablesPublic acoef(12), cv(12), foldername$Public RECESSCOEF, SEEPCOEF, UNSATSTOR, SATSTOR, snow, SEDELRATIO As SinglePublic sednitr, sedphos, grnitrconc, grphosconc As SinglePublic rain, melt, water, amc5, qtotal As SinglePublic pdry, ndeptot, ndepc() As SinglePublic nday() As IntegerPublic manuredareas, firstmanuremonth, lastmanuremonth, nrur, nurb, nlu, nyrs As IntegerPublic dormantseason As Boolean, growingseason As Boolean, trajectory As BooleanPublic optimize As Boolean, report As Boolean, unsens As BooleanPublic eloadN, eloadP, upN, upP, dsflag, inityr, initmoPublic etmult, erosmult ' multipliers used in calibrationPublic gout(120) As Single, outlen As Long, num As Integer 'num = number of params to be optimizedPublic nlist(20) As Integer ' row locations of optimization parameters offset by -1
Sub gwlf4(opt, Optional optimize As Boolean, Optional unsens As Boolean)Dim line1, line2, line3, Title, default, MyValue As StringDim lenmo(12) As Integerfoldername$ = Worksheets("notes and parameters").Cells(7, 4).valueDo While Not PathExists(foldername$)foldername$ = getstrg$("Old foldername doesn't exist. New foldername?", foldername$)If Len(foldername$) = 0 ThenMsgBox " Simulation halted...hit ok to continue", vbInformationEndEnd If
LoopWorksheets("notes and parameters").ActivateIf optimize Thenreport = Falseunsens = FalseElsereport = TrueWorksheets("notes and parameters").Cells(7, 4).value = foldername$End IfIf unsens Thenreport = Falseoptimize = FalseEnd If
az = 0
If Not optimize Then Worksheets("notes and parameters").Cells(3, 9).value = "Simulation beginning"
default = Str(opt) numday = 0 monthct = 0
410 If opt > 3 Then End
'************************************************************************ ' MAIN PROGRAM
'************************************************************************* ' SIMULATION INITIALIZATION '*************************************************************************
If Not optimize Then Simbegin.Show
DAYSMONTH(1) = 31DAYSMONTH(2) = 28DAYSMONTH(3) = 31DAYSMONTH(4) = 30DAYSMONTH(5) = 31DAYSMONTH(6) = 30DAYSMONTH(7) = 31DAYSMONTH(8) = 31DAYSMONTH(9) = 30DAYSMONTH(10) = 31DAYSMONTH(11) = 30DAYSMONTH(12) = 31
If Not optimize ThenWorksheets("notes and parameters").Cells(1, 9).value = 0Worksheets("notes and parameters").Cells(2, 9).value = 0End If'get parametersspinuplen = Worksheets("notes and parameters").Cells(5, 4).valueIf spinuplen > 0 ThenIf UCase$(Worksheets("notes and parameters").Cells(6, 4).value) = "Y" Thenspinuprepeat = True
Elsespinuprepeat = FalseEnd IfEnd If ' spinuplen > 0simtitle$ = Worksheets("notes and parameters").Cells(8, 4).value nyrs = Worksheets("notes and parameters").Cells(9, 4).value
If iter = 1 Or Not unsens ThenReDim temp(1 - spinuplen To nyrs, 12, 31), PREC(1 - spinuplen To nyrs, 12, 31)ReDim nday(1 - spinuplen To nyrs), ndepc(1 - spinuplen To nyrs)ElseReDim Preserve temp(1 - spinuplen To nyrs, 12, 31), PREC(1 - spinuplen To nyrs, 12, 31)ReDim Preserve nday(1 - spinuplen To nyrs), ndepc(1 - spinuplen To nyrs)End IfReDim STREAMFLOW(1 - spinuplen To nyrs, 0 To 12), PRECIPITATION(1 - spinuplen To nyrs, 0 To 12), evapotrans(1 - spinuplen To nyrs, 0 To 12), GROUNDWATER(1 - spinuplen To nyrs, 0 To 12), RUNOFF(1 - spinuplen To nyrs, 0 To 12) As Single ReDim EROSION(1 - spinuplen To nyrs, 0 To 12), SEDYIELD(1 - spinuplen To nyrs, 0 To 12) As SingleReDim GROUNDNITR(1 - spinuplen To nyrs, 0 To 12), GROUNDPHOS(1 - spinuplen To nyrs, 0 To 12), DISNITR(1 - spinuplen To nyrs, 0 To 12), TOTNITR(1 - spinuplen To nyrs, 0 To 12), DISPHOS(1 - spinuplen To nyrs, 0 To 12), TOTPHOS(1 - spinuplen To nyrs, 0 To 12) As SingleReDim wetdepnitr(1 - spinuplen To nyrs, 0 To 12), drydepnitr(1 - spinuplen To nyrs, 0 To 12)ReDim lu_RUNOFF(1 - spinuplen To nyrs, 200), lu_EROSION(1 - spinuplen To nyrs, 200), lu_DISNITR(1 - spinuplen To nyrs, 200), lu_TOTNITR(1 - spinuplen To nyrs, 200), lu_DISPHOS(1 - spinuplen To nyrs, 200), lu_TOTPHOS(1 - spinuplen To nyrs, 200) As SingleReDim SEDTRANS(1 - spinuplen To nyrs, 0 To 12) As SingleReDim septicN(1 - spinuplen To nyrs, 0 To 12), septicP(1 - spinuplen To nyrs, 0 To 12)ReDim tempav(1 - spinuplen To nyrs, 0 To 12), denittot(1 - spinuplen To nyrs, 0 To 12)ReDim gwdenit(1 - spinuplen To nyrs, 0 To 12), lu_denit(1 - spinuplen To nyrs, 0 To 12)If nyrs = 0 Then StopCall initializeall(nyrs)If UCase$(Worksheets("notes and parameters").Cells(15, 4).value) = "Y" Then trajectory = True Else trajectory = FalseIf UCase$((Worksheets("notes and parameters").Cells(2, 4).value) = "F") ThenCall readtransportfile(foldername$ & Worksheets("notes and parameters").Cells(27, 4).value) ' transport filenameCall clearworksheet("transport")Call reporttransporttoworksheet("transport", 0, 0)Call readnutrientfile(foldername$ & Worksheets("notes and parameters").Cells(28, 4).value) ' nutrient filenameCall clearworksheet("nutrient")Call reportnutrienttoworksheet("nutrient", "watershed", 1, 0, 0)ElseCall readtransportworksheet("transport")Call readnutrientworksheet("nutrient")End If
zzzz = denitfrac(4)' use latitude to calculate dayhrs?latyn$ = Worksheets("notes and parameters").Cells(16, 4)If UCase$(latyn$) = "Y" Then uselat = True Else uselat = Falselat = Worksheets("notes and parameters").Cells(17, 4) ' latitude, degreesIf uselat Then Call hrs(dayhrs, lat)' read weather datainityr = Worksheets("notes and parameters").Cells(20, 4)
initmo = Worksheets("notes and parameters").Cells(21, 4)
If Not (unsens And iter > 1) Then ' read weather data if not repeating stochastic analysis wfilename = Worksheets("notes and parameters").Cells(29, 4).value
Open foldername$ & wfilename For Input As #1 ' weather filename
' if the weather data in the spinup period is simply repeating the initial period of the simulation' then we will enter it separately below, and so we start reading weather at y=1...if the spinup period weather data is simply the first n yrs' of the weather record, then we start reading at y = 1-spinuplen...in either case, weather data' starting at y = 1-spinuplen should be created If spinuprepeat Then nyrstart = 1 Else nyrstart = 1 - spinuplen For y = nyrstart To nyrs psum = 0: nday(y) = 0 For i = 1 To 12 If y < 1 Then yy = y + spinuplen Else yy = y For j = 1 To lenmonth(yy, inityr, i, initmo) Input #1, temp(y, i, j), PREC(y, i, j) 'celsius,cm psum = psum + PREC(y, i, j) nday(y) = nday(y) + 1 If spinuprepeat And y <= spinuplen Then temp(y - spinuplen, i, j) = temp(y, i, j) ' repeat the weather data over the PREC(y - spinuplen, i, j) = PREC(y, i, j) ' appropriate number of years before year 1 End If Next j Next i ndepc(y) = 10 * (1 - pdry) * ndeptot / psum 'mg/l If spinuprepeat And y <= spinuplen Then ndepc(y - spinuplen) = ndepc(y) End If Next y Close #1End If ' not (unsens and iter >1)
If Not trajectory Then areatotal = 0 denitav = 0 End If For l = 1 To nlu If trajectory Then annareainc(l) = (areafinal(l) - AREA(l)) / (nyrs - 1) Else areatotal = areatotal + AREA(l) denitav = denitav + AREA(l) * denitfrac(l) End If Next l If Not trajectory Then denitav = denitav / areatotal5250 amc5 = 05260 For k = 1 To 55270 amc5 = amc5 + ANTMOIST(k)5280 Next k
'******************************************************************************* monum = 0 If report And UCase$(Worksheets("notes and parameters").Cells(10, 4).value) = "Y" Then Call clearworksheet("daily") If optimize Then setparms2 ElseIf unsens Then getstochparms (iter) End If For y = 1 - spinuplen To nyrs ' simulation loop -yrs If Not (optimize Or unsens) Then Worksheets("notes and parameters").Cells(1, 9).value = y ' report year End If SLN1 = 0 ' initialize annual septic system N discharge (P = 0 for type 1 systems) pointnitr(0) = 0 pointphos(0) = 0 If trajectory Then areatotal = 0 denitav = 0 For l = 1 To nlu If y > 1 Then AREA(l) = AREA(l) + annareainc(l) Else AREAINIT(l) = AREA(l) areatotal = areatotal + AREA(l) denitav = denitav + denitfrac(l) * AREA(l) Next l denitav = denitav / areatotal End If 'trajectory For i = 1 To 12 'month loop If Not (optimize Or unsens) Then Worksheets("notes and parameters").Cells(2, 9).value = i ' report month dsN(3, i) = 0 dsP(3, i) = 0 For l = 1 To nlu Q(l, i) = 0: x(l, i) = 0 Next l
If y < 1 Then yy = y + spinuplen Else yy = y lenmo(i) = lenmonth(yy, inityr, i, initmo) For j = 1 To lenmo(i) ' days in month loop
'********* DAILY WEATHER ANALYSIS************************************ melt = 0: rain = 0: water = 0: EROSIV = 0: qtotal = 0: et = 0 numday = numday + 1 PRECIPITATION(y, i) = PRECIPITATION(y, i) + PREC(y, i, j) tempav(y, i) = tempav(y, i) + temp(y, i, j) '-----------------------RAIN,SNOWMELT,EVAPOTRANSPIRATION------------- If temp(y, i, j) <= 0 Then snow = snow + PREC(y, i, j) Else ' temp > 0 rain = PREC(y, i, j) If snow >= 0.001 Then melt = 0.45 * temp(y, i, j) If melt > snow Then melt = snow
snow = snow - melt End If water = rain + melt If opt > 1 Then If rain > 0 And snow < 0.001 Then EROSIV = 6.46 * acoef(i) * rain ^ 1.81 End If ' opt > 1 If water > 0.01 Then
'------------RUNOFF, EROSION, SEDIMENT WASHOFF--------------------
For l = 1 To nlu
If grow(i) > 0 Then growingseason = True dormantseason = False Else growingseason = False dormantseason = True End If
qrun = qrunoff(growingseason, CN(2, l), amc5, water, melt) If qrun > 0 Then qtotal = qtotal + qrun * AREA(l) Q(l, i) = Q(l, i) + qrun ' monthly runoff by landuse End If 'qrun > 0 If opt > 1 Then '-----------------------EROSION, SEDIMENT WASHOFF------------------ '-------X() IS RURAL EROSION, UNIT URBAN DUMMY SEDIMENT WASHOFF-------- If l <= nrur Then XEROS = 1.292 * EROSIV * KLSCP(l) * AREA(l) EROSION(y, i) = EROSION(y, i) + XEROS x(l, i) = x(l, i) + XEROS End If If opt < 3 Then GoTo 890 WASHOFF = (1 - Exp(-1.81 * qrun)) * URBANSED(l) URBANSED(l) = URBANSED(l) - WASHOFF x(l, i) = x(l, i) + WASHOFF End If ' opt > 1890 Next l
'-------------------------WATERSHED TOTALS----------------------------- qtotal = qtotal / areatotal SEDTRANS(y, i) = SEDTRANS(y, i) + qtotal ^ 1.67 RUNOFF(y, i) = RUNOFF(y, i) + qtotal
End If 'water > 0.01 End If ' (else) temp > 0 amc5 = amc5 - ANTMOIST(5) + water For k = 1 To 4 ANTMOIST(6 - k) = ANTMOIST(5 - k) Next k ANTMOIST(1) = water et = etfunc(dayhrs(i), temp(y, i, j), cv(i)) evapotrans(y, i) = evapotrans(y, i) + et If report And y > 0 Then
' report daily data if requested If UCase$(Worksheets("notes and parameters").Cells(10, 4).value) = "Y" Then 'Call clearworksheet("daily") Call reportdailytoworksheet("daily", numday, y, i, j) End If End If ' report'********* End of DAILY WEATHER ANALYSIS (but not day loop)*****************
'--------------------------WATERSHED WATER BALANCE--------------------- INFILTRATION = water - qtotal GRFLOW = RECESSCOEF * SATSTOR DEEPSEEP = SEEPCOEF * SATSTOR UNSATSTOR = UNSATSTOR + INFILTRATION - et If UNSATSTOR <= 0 Then percolation = 0 Else percolation = UNSATSTOR UNSATSTOR = 0 End If
SATSTOR = SATSTOR + percolation - GRFLOW - DEEPSEEP
If SATSTOR < 0 Then SATSTOR = 0
FLOW = qtotal + GRFLOW STREAMFLOW(y, i) = STREAMFLOW(y, i) + FLOW GROUNDWATER(y, i) = GROUNDWATER(y, i) + GRFLOW If opt = 3 Then '---------------------------URBAN NUTRIENT ACCUMULATION--------------- For l = nrur + 1 To nlu URBANSED(l) = URBANSED(l) + 1 Next l '---------------------------Septic system ACCUMULATION---------------- If dsflag = 1 Or dsflag = 3 Then SLN1 = SLN1 + 0.001 * a(1, i) * (eloadN - upN * grow(i))
If snow > 0 Or temp(y, i, j) <= 0 Then fnN = fnN + a(3, i) * eloadN fnP = fnP + a(3, i) * eloadP pnN = 0 pnP = 0 Else pnN = fnN + a(3, i) * (eloadN - upN * grow(i)) pnP = fnP + a(3, i) * (eloadP - upP * grow(i)) fnN = 0 fnP = 0 End If dsN(3, i) = dsN(3, i) + pnN dsP(3, i) = dsP(3, i) + pnP End If 'dsflag End If 'opt = 31520 Next j ' end of days in month loop
tempav(y, i) = tempav(y, i) / lenmo(i) ' avg temp, for month i, year y1530 Next i 'end of month loop
1540 '---------------------------ANNUAL WATER BALANCE-----------------------1550 For i = 1 To 12 'now monthly loop for both hydrology and sediment
monum = monum + 11560 For l = 1 To nlu1570 lu_RUNOFF(y, l) = lu_RUNOFF(y, l) + Q(l, i)1580 Next l1590 PRECIPITATION(y, 0) = PRECIPITATION(y, 0) + PRECIPITATION(y, i) tempav(y, 0) = tempav(y, 0) + tempav(y, i) / 121600 STREAMFLOW(y, 0) = STREAMFLOW(y, 0) + STREAMFLOW(y, i)1610 RUNOFF(y, 0) = RUNOFF(y, 0) + RUNOFF(y, i)1620 GROUNDWATER(y, 0) = GROUNDWATER(y, 0) + GROUNDWATER(y, i)1630 evapotrans(y, 0) = evapotrans(y, 0) + evapotrans(y, i)
1650 If opt < 2 Then GoTo 18011660 '----------------------------SEDIMENT YIELDS----------------------------
1680 BSED(i) = 01690 For m = i To 121700 BSED(i) = BSED(i) + SEDTRANS(y, m)1710 Next m1720 For m = 1 To i1730 If EROSION(y, m) > 0 Then SEDYIELD(y, i) = SEDYIELD(y, i) + (EROSION(y, m) / BSED(m)) ' if condition added by dps 8/9/02 to eliminate prob with bsed=01740 Next m1750 SEDYIELD(y, i) = SEDELRATIO * SEDTRANS(y, i) * SEDYIELD(y, i)1760 SEDYIELD(y, 0) = SEDYIELD(y, 0) + SEDYIELD(y, i) 'total1770 EROSION(y, 0) = EROSION(y, 0) + EROSION(y, i) 'total1780 For l = 1 To nrur1790 lu_EROSION(y, l) = lu_EROSION(y, l) + x(l, i)1800 Next l1801 If opt < 3 Then If y > 0 Then monthct = monthct + 1 If report Then If monthct = 1 Then Call clearworksheet("monthly") ' clear monthly worksheet
Else ' ie optimizing If optimize Then Select Case opt Case Is = 1 gout(monthct) = STREAMFLOW(y, i) Case Is = 2 gout(monthct) = SEDYIELD(y, i) / 1000# End Select End If 'optimize End If ' report
End If 'opt < 31810 Next i ' end of month loop sedIf report And y > 0 Then
' report monthly data if requested If UCase$(Worksheets("notes and parameters").Cells(11, 4).value) = "Y" Then Call reportmonthlytoworksheet("monthly", opt, y) End If End If ' report
1820 If opt < 3 Then GoTo 2170 1830 '--------------------------NUTRIENT FLUXES-----------------------------1840 For i = 1 To 12 ' month loop nutrient summary If y > 0 Then monthct = monthct + 11850 For l = 1 To nrur '--------------------------------RURAL DISSOLVED NUTRIENTS----------- NCONC = nitrconc(l) PCONC = phosconc(l) If l <= manuredareas And i >= firstmanuremonth And i <= lastmanuremonth Then NCONC = mannitr(l): PCONC = manphos(l) End If denit = 0.1 * NCONC * Q(l, i) * AREA(l) * denitfrac(l) 'kg NRUNOFF = 0.1 * NCONC * Q(l, i) * AREA(l) - denit 'kg PRUNOFF = 0.1 * PCONC * Q(l, i) * AREA(l) 'kg DISNITR(y, i) = DISNITR(y, i) + NRUNOFF DISPHOS(y, i) = DISPHOS(y, i) + PRUNOFF denittot(y, i) = denittot(y, i) + denit If Not optimize Then ' no need to calculate these if optimizing lu_TOTNITR(y, l) = lu_TOTNITR(y, l) + NRUNOFF lu_TOTPHOS(y, l) = lu_TOTPHOS(y, l) + PRUNOFF lu_DISNITR(y, l) = lu_DISNITR(y, l) + NRUNOFF lu_DISPHOS(y, l) = lu_DISPHOS(y, l) + PRUNOFF lu_denit(y, l) = lu_denit(y, l) + denit '---------------------------------ADD SOLID RURAL NUTRIENTS----------- lu_TOTNITR(y, l) = lu_TOTNITR(y, l) + 0.001 * SEDELRATIO * x(l, i) * sednitr lu_TOTPHOS(y, l) = lu_TOTPHOS(y, l) + 0.001 * SEDELRATIO * x(l, i) * sedphos End If 'not optimize1960 Next l
TOTNITR(y, i) = DISNITR(y, i) + 0.001 * sednitr * SEDYIELD(y, i) 'kg TOTPHOS(y, i) = DISPHOS(y, i) + 0.001 * sedphos * SEDYIELD(y, i) 'kg '-----------------------------------ADD URBAN NUTRIENTS----------------2000 For l = nrur + 1 To nlu denit = urbannitr(l) * x(l, i) * AREA(l) * denitfrac(l) NRUNOFF = urbannitr(l) * x(l, i) * AREA(l) - denit PRUNOFF = urbanphos(l) * x(l, i) * AREA(l) If Not optimize Then ' no need to calculate these if optimizing lu_TOTNITR(y, l) = lu_TOTNITR(y, l) + NRUNOFF lu_TOTPHOS(y, l) = lu_TOTPHOS(y, l) + PRUNOFF lu_denit(y, l) = lu_denit(y, l) + denit End If 'not optimize TOTNITR(y, i) = TOTNITR(y, i) + NRUNOFF TOTPHOS(y, i) = TOTPHOS(y, i) + PRUNOFF denittot(y, i) = denittot(y, i) + denit2040 Next l
'-----------------------------ADD GROUNDWATER, POINT SOURCES, dep----------- ' molen = lenmonth(y, inityr, i, initmo) gwdenit(y, i) = 0.1 * grnitrconc * GROUNDWATER(y, i) * areatotal * gwdenitfrac 'kg GROUNDNITR(y, i) = 0.1 * grnitrconc * GROUNDWATER(y, i) * areatotal - gwdenit(y, i) 'kg GROUNDPHOS(y, i) = 0.1 * grphosconc * GROUNDWATER(y, i) * areatotal 'kg wetdepnitr(y, i) = 0.1 * ndepc(y) * PRECIPITATION(y, i) * areatotal 'kg drydepnitr(y, i) = pdry * lenmo(i) / nday(y) * ndeptot * areatotal 'kg BNITR = GROUNDNITR(y, i) + pointnitr(i) + (wetdepnitr(y, i) + drydepnitr(y, i)) * (1 - denitav) BPHOS = GROUNDPHOS(y, i) + pointphos(i)
DISNITR(y, i) = DISNITR(y, i) + BNITR DISPHOS(y, i) = DISPHOS(y, i) + BPHOS TOTNITR(y, i) = TOTNITR(y, i) + BNITR TOTPHOS(y, i) = TOTPHOS(y, i) + BPHOS denittot(y, i) = denittot(y, i) + (wetdepnitr(y, i) + drydepnitr(y, i)) * denitav + gwdenit(y, i) '---------ADD On-Site waste disposal (septic system) SOURCES----------- If (dsflag = 1 Or dsflag = 3) Then ' molen = lenmonth(y, inityr, i, initmo) dsN(1, i) = SLN1 * GROUNDWATER(y, i) / GROUNDWATER(y, 0) dsP(1, i) = 0# dsN(2, i) = 0.001 * a(2, i) * lenmo(i) * (eloadN - upN * grow(i)) dsP(2, i) = 0.001 * a(2, i) * lenmo(i) * (eloadP - upP * grow(i))
dsN(4, i) = 0.001 * a(4, i) * lenmo(i) * eloadN dsP(4, i) = 0.001 * a(4, i) * lenmo(i) * eloadP For ii = 1 To 4 septicN(y, i) = septicN(y, i) + dsN(ii, i) septicP(y, i) = septicP(y, i) + dsP(ii, i) Next ii End If DISNITR(y, i) = DISNITR(y, i) + septicN(y, i) ' dps added 12/2002 DISPHOS(y, i) = DISPHOS(y, i) + septicP(y, i) ' dps added 12/2002 TOTNITR(y, i) = TOTNITR(y, i) + septicN(y, i) ' dps added 12/2002 TOTPHOS(y, i) = TOTPHOS(y, i) + septicP(y, i) ' dps added 12/2002 If optimize Then gout(monthct) = TOTNITR(y, i) / 1000 '-----------------------------ANNUAL TOTALS---------------------------- DISNITR(y, 0) = DISNITR(y, 0) + DISNITR(y, i) DISPHOS(y, 0) = DISPHOS(y, 0) + DISPHOS(y, i) TOTNITR(y, 0) = TOTNITR(y, 0) + TOTNITR(y, i) 'kg TOTPHOS(y, 0) = TOTPHOS(y, 0) + TOTPHOS(y, i) 'kg GROUNDNITR(y, 0) = GROUNDNITR(y, 0) + GROUNDNITR(y, i) GROUNDPHOS(y, 0) = GROUNDPHOS(y, 0) + GROUNDPHOS(y, i) septicN(y, 0) = septicN(y, 0) + septicN(y, i) septicP(y, 0) = septicP(y, 0) + septicP(y, i) wetdepnitr(y, 0) = wetdepnitr(y, 0) + wetdepnitr(y, i) drydepnitr(y, 0) = drydepnitr(y, 0) + drydepnitr(y, i) denittot(y, 0) = denittot(y, 0) + denittot(y, i) gwdenit(y, 0) = gwdenit(y, 0) + gwdenit(y, i) If dsflag = 2 Or dsflag = 3 Then pointnitr(0) = pointnitr(0) + pointnitr(12 * (y - 1) + i) pointphos(0) = pointphos(0) + pointphos(12 * (y - 1) + i) Else pointnitr(0) = pointnitr(0) + pointnitr(i) pointphos(0) = pointphos(0) + pointphos(i) End If ' monthct = monthct + 1 If report Then If monthct = 1 Then Call clearworksheet("monthly") ' clear monthly worksheet End If ' report
2160 Next i ' end of month loop nutrient summary
' clear summary report worksheets2170 'continue If report And y > 0 Then If y = 1 Then
Call clearworksheet("annual") Call clearworksheet("bycategory") Call clearworksheet("bycategory2") End If ' report monthly data if requested If UCase$(Worksheets("notes and parameters").Cells(11, 4).value) = "Y" Then Call reportmonthlytoworksheet("monthly", opt, y) End If ' report annual data if requested If UCase$(Worksheets("notes and parameters").Cells(12, 4).value) = "Y" Then Call reportannualtoworksheet("annual", y) End If Call reportannualbycategorytoworksheet("bycategory", y) Call reportannualbycategorytoworksheet2("bycategory2", y) End If 'report Next y ' end of year loop
If report ThenCall writeresultsfile(foldername$ & "results.txt")Worksheets("notes and parameters").Cells(3, 9).value = "Simulation complete"End If
5600End SubSub reportval(vblname$, value)Dim stringval$stringval$ = Str(value)Dim Msg1, msg2, Style, Title1, Help1, Ctxt, Response, MyStringMsg1 = vblname$ & " = " & stringval$msg2 = "Do you want to continue ?" ' Define message.Style = vbYesNo + vbQuestion + vbDefaultButton2 ' Define buttons.Title1 = "Parameter value report" ' Define title.Help1 = "DEMO.HLP" ' Define Help file.Ctxt = 1000 ' Define topic ' context. ' Display message. Response = MsgBox(Msg1 & Chr(13) & msg2, Style, Title1, Help1, Ctxt)If Response = vbYes Then ' User chose Yes.
MyString = "Yes" ' Perform some action.Else ' User chose No. MyString = "No" ' Perform some action. StopEnd If
End SubFunction getval(prompt$, default)Dim Title, MyValue
Title = "Enter a parameter value" ' Set title.If (Len(Str(default)) > 0) Then line2 = "(default = " & Str(default) & " )"
' Display message, title, and default value.MyValue = InputBox(prompt$ & Chr(13) & Chr(13) & line2, Title, default)getval = Val(MyValue)
End FunctionFunction getstrg$(promptstr$, defaultstr$)Dim Title, MyValueTitle = "Enter a name" ' Set title.line2 = ""If (Len(defaultstr$) > 0) Then line2 = "(default = " & defaultstr$ & " )"
' Display message, title, and default value.MyValue = InputBox(promptstr$ & Chr(13) & Chr(13) & line2, Title, defaultstr$)getstrg$ = MyValueEnd Function
Function etfunc(dayhrs, temp, cv)Dim satvapIf temp < 0 Thenetfunc = 0#Elsesatvap = 33.8639 * ((0.00738 * temp + 0.8072) ^ 8 - 0.000019 * Abs(1.8 * temp + 48) + 0.001316)etfunc = cv * 0.021 * dayhrs ^ 2 * satvap / (temp + 273)End If
End FunctionStatic Sub readnutrientworksheet(nutrientworksheetname$)' note: need to read transport data first to get nrur, nurb,nluOffsetX = 3OffsetY = 0
sednitr = Worksheets(nutrientworksheetname$).Cells(OffsetX + 1, OffsetY + 2).valuesedphos = Worksheets(nutrientworksheetname$).Cells(OffsetX + 1, OffsetY + 3).valuegrnitrconc = Worksheets(nutrientworksheetname$).Cells(OffsetX + 1, OffsetY + 4).valuegrphosconc = Worksheets(nutrientworksheetname$).Cells(OffsetX + 1, OffsetY + 5).valuendeptot = Worksheets(nutrientworksheetname$).Cells(OffsetX + 1, OffsetY + 6).value 'kg/ha/yr;not original gwlf parameterpdry = Worksheets(nutrientworksheetname$).Cells(OffsetX + 1, OffsetY + 7).value ' fraction; not original gwlf parameter
manuredareas = Worksheets(nutrientworksheetname$).Cells(OffsetX + 2 + 1, OffsetY + 2).valuefirstmanuremonth = Worksheets(nutrientworksheetname$).Cells(OffsetX + 2 + 1, OffsetY + 3).valuelastmanuremonth = Worksheets(nutrientworksheetname$).Cells(OffsetX + 2 + 1, OffsetY + 4).value'Open foldername$&"carbon.txt" For Input As #1'5550 Input #1, sednitr, sedphos, GRNITRCONC, grphosconc'5560 Input #1, MANUREDAREAS, FIRSTMANUREMONTH, LASTMANUREMONTHFor l = 1 To nrurnitrconc(l) = Worksheets(nutrientworksheetname$).Cells(OffsetX + 5 + l, OffsetY + 2).valuephosconc(l) = Worksheets(nutrientworksheetname$).Cells(OffsetX + 5 + l, OffsetY + 3).value5590 Next l
5600 For l = nrur + 1 To nlu
urbannitr(l) = Worksheets(nutrientworksheetname$).Cells(OffsetX + 5 + l, OffsetY + 2).valueurbanphos(l) = Worksheets(nutrientworksheetname$).Cells(OffsetX + 5 + l, OffsetY + 3).value5620 Next l
5630 For l = 1 To manuredareas
mannitr(l) = Worksheets(nutrientworksheetname$).Cells(OffsetX + 5 + nlu + l, OffsetY + 2).value
manphos(l) = Worksheets(nutrientworksheetname$).Cells(OffsetX + 5 + nlu + l, OffsetY + 3).value
5650 Next l5660 For i = 1 To 12
pointnitr(i) = Worksheets(nutrientworksheetname$).Cells(OffsetX + 6 + nlu + manuredareas + i, OffsetY + 2).valuepointphos(i) = Worksheets(nutrientworksheetname$).Cells(OffsetX + 6 + nlu + manuredareas + i, OffsetY + 3).value
5680 Next idsflag = Worksheets(nutrientworksheetname$).Cells(OffsetX + 19 + nlu + manuredareas, OffsetY + 2).valueIf dsflag = 1 Or dsflag = 3 Then For i = 1 To 12
For j = 1 To 4 a(j, i) = Worksheets(nutrientworksheetname$).Cells(OffsetX + 21 + nlu + manuredareas + i, OffsetY + 1 + j).value Next j Next i eloadN = Worksheets(nutrientworksheetname$).Cells(OffsetX + 34 + nlu + manuredareas, OffsetY + 3).value eloadP = Worksheets(nutrientworksheetname$).Cells(OffsetX + 35 + nlu + manuredareas, OffsetY + 3).value upN = Worksheets(nutrientworksheetname$).Cells(OffsetX + 36 + nlu + manuredareas, OffsetY + 3).value upP = Worksheets(nutrientworksheetname$).Cells(OffsetX + 37 + nlu + manuredareas, OffsetY + 3).valueEnd IfIf dsflag = 2 Or dsflag = 3 ThenIf dsflag = 2 Thendsoff = OffsetX + 20 + nlu + manuredareasElsedsoff = OffsetX + 38 + nlu + manuredareasEnd If
For y = 2 To nyrs For i = 1 To 12
pointnitr(12 * (y - 1) + i) = Worksheets(nutrientworksheetname$).Cells(dsoff + 12 * (y - 2) + i, OffsetY + 1).value pointphos(12 * (y - 1) + i) = Worksheets(nutrientworksheetname$).Cells(dsoff + 12 * (y - 2) + i, OffsetY + 2).value
Next i Next y End If
End SubSub reportnutrienttoworksheet(reportworksheetname$, Optional watershedname$ = "watershed 1", Optional watershednum = 1, Optional offsetxt = 0, Optional offsetyt = 0)
OffsetX = 2OffsetY = 1Worksheets(reportworksheetname$).Cells(offsetxt + 1, offsetyt + OffsetY + 1).value = "Watershed Name:"Worksheets(reportworksheetname$).Cells(offsetxt + 1, offsetyt + OffsetY + 2).value = watershedname$Worksheets(reportworksheetname$).Cells(offsetxt + 1, offsetyt + OffsetY + 3).value = "Watershed Number:"
Worksheets(reportworksheetname$).Cells(offsetxt + 1, offsetyt + OffsetY + 4).value = watershednumWorksheets(reportworksheetname$).Cells(offsetxt + 2, offsetyt + OffsetY + 1).value = "sediment N"Worksheets(reportworksheetname$).Cells(offsetxt + 2, offsetyt + OffsetY + 2).value = "sediment P"Worksheets(reportworksheetname$).Cells(offsetxt + 2, offsetyt + OffsetY + 3).value = "GW N conc"Worksheets(reportworksheetname$).Cells(offsetxt + 3, offsetyt + OffsetY + 3).value = " (mg/L)"Worksheets(reportworksheetname$).Cells(offsetxt + 2, offsetyt + OffsetY + 4).value = "GW P conc"Worksheets(reportworksheetname$).Cells(offsetxt + 3, offsetyt + OffsetY + 4).value = " (mg/L)"Worksheets(reportworksheetname$).Cells(offsetxt + 2, offsetyt + OffsetY + 5).value = "Atmos N dep"Worksheets(reportworksheetname$).Cells(offsetxt + 3, offsetyt + OffsetY + 5).value = " (kg/ha/yr)"Worksheets(reportworksheetname$).Cells(offsetxt + 2, offsetyt + OffsetY + 6).value = "fraction dry dep"
Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 2, offsetyt + OffsetY + 1).value = sednitrWorksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 2, offsetyt + OffsetY + 2).value = sedphosWorksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 2, offsetyt + OffsetY + 3).value = grnitrconcWorksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 2, offsetyt + OffsetY + 4).value = grphosconcIf IsNumeric(ndeptot) Then Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 2, offsetyt + OffsetY + 5).value = ndepttotIf IsNumeric(pdry) Then Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 2, offsetyt + OffsetY + 6).value = pdry
Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 3, offsetyt + OffsetY + 1).value = "# manured areas"Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 3, offsetyt + OffsetY + 2).value = "firstmanuremonth"Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 3, offsetyt + OffsetY + 3).value = "lastmanuremonth"
Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 4, offsetyt + OffsetY + 1).value = manuredareasWorksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 4, offsetyt + OffsetY + 2).value = firstmanuremonthWorksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 4, offsetyt + OffsetY + 3).value = lastmanuremonth
Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 6, offsetyt + 1).value = "landuse category"Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 6, offsetyt + 2).value = "N conc"Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 6, offsetyt + 3).value = "P conc"
For l = 1 To nrurWorksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 6 + l, offsetyt + 1).value = lWorksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 6 + l, offsetyt + OffsetY + 1).value = nitrconc(l)Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 6 + l, offsetyt + OffsetY + 2).value = phosconc(l)5590 Next l
5600 For l = nrur + 1 To nluWorksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 6 + l, offsetyt + 1).value = lWorksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 6 + l, offsetyt + OffsetY + 1).value = urbannitr(l)Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 6 + l, offsetyt + OffsetY + 2).value = urbanphos(l)5620 Next l
Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 7 + nlu, offsetyt + 1).value = "manured area"Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 7 + nlu, offsetyt + 2).value = "man N load"Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 7 + nlu, offsetyt + 3).value = "man P load"
5630 For l = 1 To manuredareasWorksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 7 + nlu + l, offsetyt + OffsetY + 1).value = lWorksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 7 + nlu + l, offsetyt + OffsetY + 1).value = mannitr(l)Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 7 + nlu + l, offsetyt + OffsetY + 2).value = manphos(l)5650 Next l
Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 7 + manuredareas + nlu, offsetyt + 1).value = "month"Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 7 + manuredareas + nlu, offsetyt + 2).value = "Pt src N load"Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 7 + manuredareas + nlu, offsetyt + 3).value = "Pt src P load"
5660 For i = 1 To 12Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 7 + nlu + manuredareas + i, offsetyt + 1).value = iWorksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 7 + nlu + manuredareas + i, offsetyt + OffsetY + 1).value = pointnitr(i)Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 7 + nlu + manuredareas + i, offsetyt + OffsetY + 2).value = pointphos(i)
5680 Next iWorksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 20 + nlu + manuredareas, offsetyt + 1).value = "dsflag"Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 20 + nlu + manuredareas, offsetyt + 2).value = dsflagIf dsflag = 1 Or dsflag = 3 Then Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 21 + nlu + manuredareas, offsetyt + 1).value = "Septic systems" Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 21 + nlu + manuredareas, offsetyt + 3).value = "Population served by type of system" Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 22 + nlu + manuredareas, offsetyt + 1).value = "Month" Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 22 + nlu + manuredareas, offsetyt + 2).value = "Normal systems" Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 22 + nlu + manuredareas, offsetyt + 3).value = "Short circuit systems" Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 22 + nlu + manuredareas, offsetyt + 4).value = "Ponding systems" Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 22 + nlu + manuredareas, offsetyt + 5).value = "Direct Discharge" For i = 1 To 12 Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 22 + nlu + manuredareas + i, offsetyt + 1).value = i
For j = 1 To 4 Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 22 + nlu + manuredareas + i, offsetyt + 1 + j).value = a(j, i) Next j Next i Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 35 + nlu + manuredareas, offsetyt + 1).value = "Percapita tank effluent - N (g/day)" Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 36 + nlu + manuredareas, offsetyt + 1).value = "Percapita tank effluent - P (g/day)"
Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 37 + nlu + manuredareas, offsetyt + 1).value = "Per capita growing season uptake - N(g/day)" Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 38 + nlu + manuredareas, offsetyt + 1).value = "Per capita growing season uptake - P(g/day)"Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 35 + nlu + manuredareas, offsetyt + 3).value = eloadN Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 36 + nlu + manuredareas, offsetyt + 3).value = eloadP Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 37 + nlu + manuredareas, offsetyt + 3).value = upN Worksheets(reportworksheetname$).Cells(offsetxt + OffsetX + 38 + nlu + manuredareas, offsetyt + 3).value = upPEnd IfIf dsflag = 2 Or dsflag = 3 ThenIf dsflag = 2 Thendsoff = offsetxt + OffsetX + 21 + nlu + manuredareasElsedsoff = offsetxt + OffsetX + 39 + nlu + manuredareasEnd IfWorksheets(reportworksheetname$).Cells(dsoff, offsetyt + 1).value = "month"Worksheets(reportworksheetname$).Cells(dsoff, offsetyt + 2).value = "Pt src N load"Worksheets(reportworksheetname$).Cells(dsoff, offsetyt + 3).value = "Pt src P load"
For y = 2 To nyrs For i = 1 To 12
Worksheets(reportworksheetname$).Cells(dsoff + 12 * (y - 2) + i, offsetyt + 1).value = 12 * (y - 1) + i Worksheets(reportworksheetname$).Cells(dsoff + 12 * (y - 2) + i, offsetyt + OffsetY + 1).value = pointnitr(12 * (y - 1) + i) Worksheets(reportworksheetname$).Cells(dsoff + 12 * (y - 2) + i, offsetyt + OffsetY + 2).value = pointphos(12 * (y - 1) + i)
Next i Next y End IfWorksheets(reportworksheetname$).Cells.NumberFormat = "General"End SubSub readnutrientfile(filename$)5510 '********************************************************************5520 ' READ NUTRIENT.DAT FILE5530 '*******************************************************************5540 Open filename$ For Input As #15550 Input #1, sednitr, sedphos, grnitrconc, grphosconc5560 Input #1, manuredareas, firstmanuremonth, lastmanuremonth5570 For l = 1 To nrur5580 Input #1, nitrconc(l), phosconc(l)5590 Next l5600 For l = nrur + 1 To nlu5610 Input #1, urbannitr(l), urbanphos(l)5620 Next l5630 For l = 1 To manuredareas5640 Input #1, mannitr(l), manphos(l)5650 Next l5660 For i = 1 To 125670 Input #1, pointnitr(i), pointphos(i)5680 Next i
Input #1, dsflag If dsflag = 1 Or dsflag = 3 Then For i = 1 To 12 Input #1, a(1, i), a(2, i), a(3, i), a(4, i) Next i Input #1, eloadN, eloadP, upN, upP End If If dsflag = 2 Or dsflag = 3 Then For y = 2 To nyrs For i = 1 To 12 Input #1, pointnitr(12 * (y - 1) + i), pointphos(12 * (y - 1) + i) Next i Next y End If5690 Close #1End SubSub readtransportfile(filename$)'********************************************************************' READ TRANSPRT.DAT FILE'*********************************************************************Open filename$ For Input As #1Input #1, nrur, nurbnlu = nrur + nurbInput #1, RECESSCOEF, SEEPCOEF, UNSATSTOR, SATSTOR, snow, SEDELRATIOFor k = 1 To 5 Input #1, ANTMOIST(k)Next kFor i = 1 To 12 Input #1, month$(i, 4), cv(i), dayhrs(i), grow(i), acoef(i)Next iFor l = 1 To nlu 'Input #1, landuse$(l, 12), AREA(l), CN(2, l), KLSCP(l), areafinal(l) - areafinal is not an original standard gwlf parameter Input #1, landuse$(l, 12), AREA(l), CN(2, l), KLSCP(l)Next lClose #1End SubSub writetransportfile(filename$)'********************************************************************' Write TRANSPRT.DAT FILE'*********************************************************************Open filename$ For Output As #2Write #2, nrur, nurbWrite #2, RECESSCOEF, SEEPCOEF, UNSATSTOR, SATSTOR, snow, SEDELRATIO For k = 1 To 5 Write #2, ANTMOIST(k) Next k For i = 1 To 12 Write #2, month$(i, 4), cv(i), dayhrs(i), grow(i), acoef(i) Next i For l = 1 To nlu' Write #2, landuse$(l, 12), AREA(l), CN(2, l), KLSCP(l), areafinal(l)- areafinal is not an original standard gwlf parameter Write #2, landuse$(l, 12), AREA(l), CN(2, l), KLSCP(l) Next lClose #2
End SubSub writenutrientfile(filename$)'********************************************************************' Write NUTRIENT.DAT FILE'*********************************************************************4470 Open filename$ For Output As #14480 Write #1, sednitr, sedphos, grnitrconc, grphosconc4490 Write #1, manuredareas, firstmanuremonth, lastmanuremonth4500 For l = 1 To nrur4510 Write #1, nitrconc(l), phosconc(l)4520 Next l4530 For l = nrur + 1 To nlu4540 Write #1, urbannitr(l), urbanphos(l)4550 Next l4560 For l = 1 To manuredareas4570 Write #1, mannitr(l), manphos(l)4580 Next l4590 For i = 1 To 124600 Write #1, pointnitr(i), pointphos(i)4610 Next i Write #1, dsflag If dsflag = 1 Or dsflag = 3 Then For i = 1 To 12 Write #1, a(1, i), a(2, i), a(3, i), a(4, i) Next i Write #1, eloadN, eloadP, upN, upP End If If dsflag = 2 Or dsflag = 3 Then For y = 2 To nyrs For i = 1 To 12 Write #1, pointnitr(12 * (y - 1) + i), pointphos(12 * (y - 1) + i) Next i Next y End If4620 Close #1End SubSub readtransportworksheet(transportworksheetname$)OffsetX = 1 'offset number of rowsOffsetY = 0 ' offset number of columns
nrur = Worksheets(transportworksheetname$).Cells(OffsetX + 1, OffsetY + 1).valuenurb = Worksheets(transportworksheetname$).Cells(OffsetX + 1, OffsetY + 2).value
nlu = nrur + nurbReDim denitfrac(nlu)RECESSCOEF = Worksheets(transportworksheetname$).Cells(OffsetX + 4, OffsetY + 1).valueSEEPCOEF = Worksheets(transportworksheetname$).Cells(OffsetX + 4, OffsetY + 2).valueUNSATSTOR = Worksheets(transportworksheetname$).Cells(OffsetX + 4, OffsetY + 3).valueSATSTOR = Worksheets(transportworksheetname$).Cells(OffsetX + 4, OffsetY + 4).valuesnow = Worksheets(transportworksheetname$).Cells(OffsetX + 4, OffsetY + 5).valueSEDELRATIO = Worksheets(transportworksheetname$).Cells(OffsetX + 4, OffsetY + 6).valuegwdenitfrac = Worksheets(transportworksheetname$).Cells(OffsetX + 4, OffsetY + 7).value 'Input #1, RECESSCOEF, SEEPCOEF, UNSATSTOR, SATSTOR, snow, SEDELRATIOFor k = 1 To 5'Input #1, ANTMOIST(k)
ANTMOIST(k) = Worksheets(transportworksheetname$).Cells(OffsetX + 5 + k, OffsetY + 1).valueNext k For i = 1 To 12 'Input #1, month$(i, 4), cv(i), dayhrs(i), GROW(i), ACOEF(i) month$(i, 4) = Worksheets(transportworksheetname$).Cells(OffsetX + i + 13, OffsetY + 1).value cv(i) = Worksheets(transportworksheetname$).Cells(OffsetX + i + 13, OffsetY + 2).value dayhrs(i) = Worksheets(transportworksheetname$).Cells(OffsetX + i + 13, OffsetY + 3).value grow(i) = Worksheets(transportworksheetname$).Cells(OffsetX + i + 13, OffsetY + 4).value acoef(i) = Worksheets(transportworksheetname$).Cells(OffsetX + i + 13, OffsetY + 5).value Next i For l = 1 To nlu 'Input #1, landuse$(l, 12), AREA(l), CN(2, l), KLSCP(l) landuse$(l, 12) = Worksheets(transportworksheetname$).Cells(OffsetX + l + 27, OffsetY + 1).value AREA(l) = Worksheets(transportworksheetname$).Cells(OffsetX + l + 27, OffsetY + 2).value CN(2, l) = Worksheets(transportworksheetname$).Cells(OffsetX + l + 27, OffsetY + 3).value KLSCP(l) = Worksheets(transportworksheetname$).Cells(OffsetX + l + 27, OffsetY + 4).value areafinal(l) = Worksheets(transportworksheetname$).Cells(OffsetX + l + 27, OffsetY + 5).value denitfrac(l) = Worksheets(transportworksheetname$).Cells(OffsetX + l + 27, OffsetY + 6).value Next l 'Close #1End SubSub reporttransporttoworksheet(reportworksheetname$, Optional offsetxt = 0, Optional offsetyt = 0)Dim range1 As RangeDim arrayout() As VariantOffsetX = 1OffsetY = 0ReDim arrayout(OffsetX + 27 + nlu, OffsetY + 7) As VariantSet range1 = Range(Cells(offsetxt + 1, offsetyt + OffsetY + 1), Cells(offsetxt + OffsetX + 27 + nlu, offsetyt + OffsetY + 7))arrayout(1, OffsetY + 1) = "Number of rural landuses"arrayout(1, OffsetY + 2) = "Number of urban landuses"
arrayout(OffsetX + 1, OffsetY + 1) = nrurarrayout(OffsetX + 1, OffsetY + 2) = nurb
arrayout(OffsetX + 2, OffsetY + 1) = "recession coefficient"arrayout(OffsetX + 2, OffsetY + 2) = "seepage coefficient"arrayout(OffsetX + 2, OffsetY + 3) = "init unsat storage"arrayout(OffsetX + 3, OffsetY + 3) = " (cm)"arrayout(OffsetX + 2, OffsetY + 4) = "init sat storage"arrayout(OffsetX + 3, OffsetY + 4) = " (cm)"arrayout(OffsetX + 2, OffsetY + 5) = "init snow"arrayout(OffsetX + 3, OffsetY + 5) = " (cm)"arrayout(OffsetX + 2, OffsetY + 6) = "sed delivery ratio"arrayout(OffsetX + 2, OffsetY + 7) = "fractional gw denit"
arrayout(OffsetX + 4, OffsetY + 1) = RECESSCOEFarrayout(OffsetX + 4, OffsetY + 2) = SEEPCOEFarrayout(OffsetX + 4, OffsetY + 3) = UNSATSTORarrayout(OffsetX + 4, OffsetY + 4) = SATSTORarrayout(OffsetX + 4, OffsetY + 5) = snowarrayout(OffsetX + 4, OffsetY + 6) = SEDELRATIOIf (IsNumeric(gwdenitfrac)) Then arrayout(OffsetX + 4, OffsetY + 7) = gwdenitfracarrayout(OffsetX + 5, OffsetY + 1) = "antecedent moisture for initial 5 days"
For k = 1 To 5arrayout(OffsetX + 5 + k, OffsetY + 1) = ANTMOIST(k)Next karrayout(OffsetX + 11, OffsetY + 1) = "month"arrayout(OffsetX + 11, OffsetY + 2) = "ET cover factor"arrayout(OffsetX + 11, OffsetY + 3) = "daylight hrs"arrayout(OffsetX + 11, OffsetY + 4) = "growing season"arrayout(OffsetX + 12, OffsetY + 4) = "(1=growing, 0=dormant)"arrayout(OffsetX + 11, OffsetY + 5) = "erosivity coeff."
For i = 1 To 12arrayout(OffsetX + 13 + i, OffsetY + 1) = month$(i, 4)arrayout(OffsetX + 13 + i, OffsetY + 2) = cv(i)arrayout(OffsetX + 13 + i, OffsetY + 3) = dayhrs(i)arrayout(OffsetX + 13 + i, OffsetY + 4) = grow(i)arrayout(OffsetX + 13 + i, OffsetY + 5) = acoef(i)Next i
arrayout(OffsetX + 26, OffsetY + 1) = "landuse type"arrayout(OffsetX + 26, OffsetY + 2) = "Initial land area"arrayout(OffsetX + 27, OffsetY + 2) = " (ha) "arrayout(OffsetX + 26, OffsetY + 3) = "runoff curve no"arrayout(OffsetX + 26, OffsetY + 4) = "USLE parameters"arrayout(OffsetX + 26, OffsetY + 5) = "Final land area"arrayout(OffsetX + 27, OffsetY + 5) = " (ha) "arrayout(OffsetX + 26, OffsetY + 6) = "Fractional denit loss"For l = 1 To nluarrayout(OffsetX + 27 + l, OffsetY + 1) = landuse$(l, 12)arrayout(OffsetX + 27 + l, OffsetY + 2) = AREA(l)arrayout(OffsetX + 27 + l, OffsetY + 3) = CN(2, l)arrayout(OffsetX + 27 + l, OffsetY + 4) = KLSCP(l)If (IsNumeric(areafinal(l))) Then arrayout(OffsetX + 27 + l, OffsetY + 5) = areafinal(l)If (IsNumeric(denitfrac(l))) Then arrayout(OffsetX + 27 + l, OffsetY + 6) = denitfrac(l)Next lrange1.value = arrayoutrange1.NumberFormat = "General"
End SubSub reportannualtoworksheet(reportworksheetname$, y)Dim range1 As Range, annrange As RangeDim annarray()offsetxavg = 5OffsetX = 2OffsetY = 0Worksheets(reportworksheetname$).Activate
' set header font to boldWorksheets(reportworksheetname$).Range(Cells(offsetxavg + 1, OffsetY + 1), Cells(offsetxavg + 2, OffsetY + 26 + nlu)).Font.Bold = TrueWorksheets(reportworksheetname$).Range(Cells(1, OffsetY + 3), Cells(3, OffsetY + 26 + nlu)).Font.Bold = True
If y = 1 ThenIf trajectory ThenSet annrange = Range(Cells(offsetxavg + 1, OffsetY + 1), Cells(offsetxavg + 2, OffsetY + 26 + nlu))ReDim annarray(2, 26 + nlu)
ElseReDim annarray(2, 26)Set annrange = Range(Cells(offsetxavg + 1, OffsetY + 1), Cells(offsetxavg + 2, OffsetY + 26))End Ifannarray(1, 1) = "year"annarray(1, 2) = "calendar year"annarray(1, 3) = "precip"annarray(1, 4) = "et"annarray(1, 5) = "runoff"annarray(1, 6) = "streamflow"annarray(1, 7) = "groundwater"annarray(1, 8) = "erosion"annarray(1, 9) = "sediment yield"annarray(1, 10) = "Total N flux"annarray(1, 11) = "Diss. N flux"annarray(1, 12) = "Part. N flux"annarray(1, 13) = "GW N flux"annarray(1, 14) = "Pt. Src. N flux"annarray(1, 15) = "Septic N flux"annarray(1, 16) = "Wet dep N flux"annarray(1, 17) = "Dry dep N flux"annarray(1, 18) = "Total denitrification flux"annarray(1, 19) = "GW denit flux"annarray(1, 20) = "Total P flux"annarray(1, 21) = "Diss. P flux"annarray(1, 22) = "Part. P flux"annarray(1, 23) = "GW P flux"annarray(1, 24) = "Pt. Src. P flux"annarray(1, 25) = "Septic P flux"annarray(1, 26) = "Air Temperature" If trajectory Then For l = 1 To nlu annarray(1, 26 + l) = landuse$(l, 12) Next l End If
annarray(2, 2) = "(at start)"annarray(2, 3) = "(cm)"annarray(2, 4) = "(cm)"annarray(2, 5) = "(cm)"annarray(2, 6) = "(cm)"annarray(2, 7) = "(cm)"annarray(2, 8) = "(1E6 kg)"annarray(2, 9) = "(1E6 kg)"annarray(2, 10) = "(1E3 kg)"annarray(2, 11) = "(1E3 kg)"annarray(2, 12) = "(1E3 kg)"annarray(2, 13) = "(1E3 kg) (diss)"annarray(2, 14) = "(1E3 kg) (diss)"annarray(2, 15) = "(1E3 kg)"annarray(2, 16) = "(1E3 kg)"annarray(2, 17) = "(1E3 kg)"annarray(2, 18) = "(1E3 kg)"annarray(2, 19) = "(1E3 kg)"annarray(2, 20) = "(1E3 kg)"annarray(2, 21) = "(1E3 kg)"
annarray(2, 22) = "(1E3 kg)"annarray(2, 23) = "(1E3 kg) (diss)"annarray(2, 24) = "(1E3 kg) (diss)"annarray(2, 25) = "(1E3 kg)"annarray(2, 26) = "(C)" If trajectory Then
For l = 1 To nlu annarray(2, 26 + l) = "(ha)" Next l End If annrange.value = annarrayEnd If
If trajectory ThenReDim annarray(1, nlu + 26)Set annrange = Range(Cells(offsetxavg + OffsetX + y, OffsetY + 1), Cells(offsetxavg + OffsetX + y, OffsetY + 26 + nlu))
ElseSet annrange = Range(Cells(offsetxavg + OffsetX + y, OffsetY + 1), Cells(offsetxavg + OffsetX + y, OffsetY + 26))
ReDim annarray(1, 26)End Ifannarray(1, 1) = yannarray(1, 2) = y + inityr - 1annarray(1, 3) = PRECIPITATION(y, 0)annarray(1, 4) = evapotrans(y, 0)annarray(1, 5) = RUNOFF(y, 0)annarray(1, 6) = STREAMFLOW(y, 0)annarray(1, 7) = GROUNDWATER(y, 0)annarray(1, 8) = EROSION(y, 0) / 1000#annarray(1, 9) = SEDYIELD(y, 0) / 1000#annarray(1, 10) = TOTNITR(y, 0) / 1000#annarray(1, 11) = DISNITR(y, 0) / 1000#annarray(1, 12) = (TOTNITR(y, 0) - DISNITR(y, 0)) / 1000#' note that groundwater and pointsources are both included in dissolved formsannarray(1, 13) = GROUNDNITR(y, 0) / 1000#annarray(1, 14) = pointnitr(0) / 1000#annarray(1, 15) = septicN(y, 0) / 1000#annarray(1, 16) = wetdepnitr(y, 0) / 1000#annarray(1, 17) = drydepnitr(y, 0) / 1000#annarray(1, 18) = denittot(y, 0) / 1000#annarray(1, 19) = gwdenit(y, 0) / 1000#annarray(1, 20) = TOTPHOS(y, 0) / 1000#annarray(1, 21) = DISPHOS(y, 0) / 1000#annarray(1, 22) = (TOTPHOS(y, 0) - DISPHOS(y, 0)) / 1000#annarray(1, 23) = GROUNDPHOS(y, 0) / 1000#annarray(1, 24) = pointphos(0) / 1000#annarray(1, 25) = septicP(y, 0) / 1000#annarray(1, 26) = tempav(y, 0)If trajectory ThenFor l = 1 To nluannarray(1, 26 + l) = AREA(l)Next l
End Ifannrange.value = annarray
If y = nyrs ThenWorksheets(reportworksheetname$).Activate
If trajectory ThenReDim annarray(3, nlu + 26)Set annrange = Range(Cells(2, OffsetY + 1), Cells(4, OffsetY + 26 + nlu))Range(Cells(1, OffsetY + 3), Cells(1, OffsetY + 23 + nlu)).value = "Average"ElseSet annrange = Range(Cells(2, OffsetY + 1), Cells(4, OffsetY + 26))Range(Cells(1, OffsetY + 3), Cells(1, OffsetY + 23)).value = "Average"ReDim annarray(3, 26)End If
For jj = 3 To 26 + nluSet range1 = Worksheets(reportworksheetname$).Range(Cells(offsetxavg + OffsetX + 1, OffsetY + jj), Cells(offsetxavg + OffsetX + nyrs, OffsetY + jj))If Application.WorksheetFunction.Count(range1) > 0 Thenannarray(3, jj) = Application.WorksheetFunction.Average(range1)End IfNext jjannarray(1, 3) = "precip"annarray(1, 4) = "et"annarray(1, 5) = "runoff"annarray(1, 6) = "streamflow"annarray(1, 7) = "groundwater"annarray(1, 8) = "erosion"annarray(1, 9) = "sediment yield"annarray(1, 10) = "Total N flux"annarray(1, 11) = "Diss. N flux"annarray(1, 12) = "Part. N flux"annarray(1, 13) = "GW N flux"annarray(1, 14) = "Pt. Src. N flux"annarray(1, 15) = "Septic N flux"annarray(1, 16) = "Wet dep N flux"annarray(1, 17) = "Dry dep N flux"annarray(1, 18) = "Total denitrification flux"annarray(1, 19) = "GW denit flux"annarray(1, 20) = "Total P flux"annarray(1, 21) = "Diss. P flux"annarray(1, 22) = "Part. P flux"annarray(1, 23) = "GW P flux"annarray(1, 24) = "Pt. Src. P flux"annarray(1, 25) = "Septic P flux"annarray(1, 26) = "Air Temperature"
If trajectory Then For l = 1 To nlu annarray(1, 26 + l) = landuse$(l, 12) Next l End Ifannarray(2, 3) = "(cm)"
annarray(2, 4) = "(cm)"annarray(2, 5) = "(cm)"annarray(2, 6) = "(cm)"annarray(2, 7) = "(cm)"annarray(2, 8) = "(1E6 kg)"annarray(2, 9) = "(1E6 kg)"annarray(2, 10) = "(1E3 kg)"annarray(2, 11) = "(1E3 kg)"annarray(2, 12) = "(1E3 kg)"annarray(2, 13) = "(1E3 kg) (diss)"annarray(2, 14) = "(1E3 kg) (diss)"annarray(2, 15) = "(1E3 kg)"annarray(2, 16) = "(1E3 kg)"annarray(2, 17) = "(1E3 kg)"annarray(2, 18) = "(1E3 kg)"annarray(2, 19) = "(1E3 kg)"annarray(2, 20) = "(1E3 kg)"annarray(2, 21) = "(1E3 kg)"annarray(2, 22) = "(1E3 kg)"annarray(2, 23) = "(1E3 kg) (diss)"annarray(2, 24) = "(1E3 kg) (diss)"annarray(2, 25) = "(1E3 kg)"annarray(2, 26) = "(C)" If trajectory Then For l = 1 To nlu annarray(2, 26 + l) = "(ha)" Next l End If annrange.value = annarrayEnd If ' y = nyrsannrange.NumberFormat = "General"End Sub
Sub reportannualbycategorytoworksheet(reportworksheetname$, y)Dim myRange As Range' nparams = the number of parameters to be plottednparams = 7offsetxavg = 5OffsetX = 2OffsetY = 0offsetlux = 1Worksheets(reportworksheetname$).Activate' set header font to boldWorksheets(reportworksheetname$).Range(Cells(offsetlux - 1 + offsetxavg + 1, OffsetY + 1), Cells(offsetlux - 1 + offsetxavg + 3, OffsetY + 3 + (nlu - 1) * (nparams + 1) + nparams)).Font.Bold = True
2260 For l = 1 To nlu
If y = 1 ThenWorksheets(reportworksheetname$).Cells(offsetlux - 1 + 1, OffsetY + 3 + (l - 1) * (nparams + 1) + 1).value = landuse$(l, 12)
If l = 1 Then Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 1, OffsetY + 3).value = "Year"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 1, OffsetY + 3 + (l - 1) * (nparams + 1) + 1).value = "area"
Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 1, OffsetY + 3 + (l - 1) * (nparams + 1) + 2).value = "runoff"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 1, OffsetY + 3 + (l - 1) * (nparams + 1) + 3).value = "erosion"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 1, OffsetY + 3 + (l - 1) * (nparams + 1) + 4).value = "Diss. N flux"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 1, OffsetY + 3 + (l - 1) * (nparams + 1) + 5).value = "Total N flux"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 1, OffsetY + 3 + (l - 1) * (nparams + 1) + 6).value = "Diss. P flux"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 1, OffsetY + 3 + (l - 1) * (nparams + 1) + 7).value = "Total P flux"
Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 2, OffsetY + 3 + (l - 1) * (nparams + 1) + 1).value = "(ha)"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 2, OffsetY + 3 + (l - 1) * (nparams + 1) + 2).value = "(cm)"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 2, OffsetY + 3 + (l - 1) * (nparams + 1) + 3).value = "(1E3 kg)"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 2, OffsetY + 3 + (l - 1) * (nparams + 1) + 4).value = "(1E3 kg) (diss)"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 2, OffsetY + 3 + (l - 1) * (nparams + 1) + 5).value = "(1E3 kg)"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 2, OffsetY + 3 + (l - 1) * (nparams + 1) + 6).value = "(1E3 kg) (diss)"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 2, OffsetY + 3 + (l - 1) * (nparams + 1) + 7).value = "(1E3 kg)"
End IfIf l = 1 Then Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + OffsetX + y, OffsetY + 3).value = yWorksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + OffsetX + y, OffsetY + 3 + (l - 1) * (nparams + 1) + 1).value = AREA(l)Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + OffsetX + y, OffsetY + 3 + (l - 1) * (nparams + 1) + 2).value = lu_RUNOFF(y, l)Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + OffsetX + y, OffsetY + 3 + (l - 1) * (nparams + 1) + 3).value = lu_EROSION(y, l) / 1000#Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + OffsetX + y, OffsetY + 3 + (l - 1) * (nparams + 1) + 4).value = lu_DISNITR(y, l) / 1000#Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + OffsetX + y, OffsetY + 3 + (l - 1) * (nparams + 1) + 5).value = lu_TOTNITR(y, l) / 1000#Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + OffsetX + y, OffsetY + 3 + (l - 1) * (nparams + 1) + 6).value = lu_DISPHOS(y, l) / 1000#Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + OffsetX + y, OffsetY + 3 + (l - 1) * (nparams + 1) + 7).value = lu_TOTPHOS(y, l) / 1000#
If y = nyrs Then'set the format of the column headings for the average values to boldWorksheets(reportworksheetname$).Range(Cells(offsetlux - 1 + 1, OffsetY + 1), Cells(offsetlux - 1 + 4, OffsetY + 3 + (nlu - 1) * (nparams + 1) + nparams)).Font.Bold = True
For jj = 1 To nparams
Worksheets(reportworksheetname$).Cells(offsetlux + 1, OffsetY + 3 + (l - 1) * (nparams + 1) + jj).value = "Average"'for each column, establish the range over which to calculate a time average
Set myRange = Worksheets(reportworksheetname$).Range(Cells(offsetlux + offsetxavg + OffsetX + 1, OffsetY + 3 + (l - 1) * (nparams + 1) + jj), Cells(offsetlux + offsetxavg + OffsetX + nyrs, OffsetY + 3 + (l - 1) * (nparams + 1) + jj))
' write the average value in the appropriate column if it exists
If Application.WorksheetFunction.Count(myRange) > 0 ThenWorksheets(reportworksheetname$).Cells(offsetlux + 4, 3 + (l - 1) * (nparams + 1) + jj).value = Application.WorksheetFunction.Average(myRange)End IfNext jjWorksheets(reportworksheetname$).Cells(offsetlux + 2, OffsetY + 3 + (l - 1) * (nparams + 1) + 1).value = "area"Worksheets(reportworksheetname$).Cells(offsetlux + 2, OffsetY + 3 + (l - 1) * (nparams + 1) + 2).value = "runoff"Worksheets(reportworksheetname$).Cells(offsetlux + 2, OffsetY + 3 + (l - 1) * (nparams + 1) + 3).value = "erosion"Worksheets(reportworksheetname$).Cells(offsetlux + 2, OffsetY + 3 + (l - 1) * (nparams + 1) + 4).value = "Diss. N flux"Worksheets(reportworksheetname$).Cells(offsetlux + 2, OffsetY + 3 + (l - 1) * (nparams + 1) + 5).value = "Total N flux"Worksheets(reportworksheetname$).Cells(offsetlux + 2, OffsetY + 3 + (l - 1) * (nparams + 1) + 6).value = "Diss. P flux"Worksheets(reportworksheetname$).Cells(offsetlux + 2, OffsetY + 3 + (l - 1) * (nparams + 1) + 7).value = "Total P flux"
Worksheets(reportworksheetname$).Cells(offsetlux + 3, OffsetY + 3 + (l - 1) * (nparams + 1) + 1).value = "(ha)"Worksheets(reportworksheetname$).Cells(offsetlux + 3, OffsetY + 3 + (l - 1) * (nparams + 1) + 2).value = "(cm)"Worksheets(reportworksheetname$).Cells(offsetlux + 3, OffsetY + 3 + (l - 1) * (nparams + 1) + 3).value = "(1E3 kg)"Worksheets(reportworksheetname$).Cells(offsetlux + 3, OffsetY + 3 + (l - 1) * (nparams + 1) + 4).value = "(1E3 kg) (diss)"Worksheets(reportworksheetname$).Cells(offsetlux + 3, OffsetY + 3 + (l - 1) * (nparams + 1) + 5).value = "(1E3 kg)"Worksheets(reportworksheetname$).Cells(offsetlux + 3, OffsetY + 3 + (l - 1) * (nparams + 1) + 6).value = "(1E3 kg) (diss)"Worksheets(reportworksheetname$).Cells(offsetlux + 3, OffsetY + 3 + (l - 1) * (nparams + 1) + 7).value = "(1E3 kg)"
End If2280 Next lWorksheets(reportworksheetname$).Cells.NumberFormat = "General"End Sub
Sub reportannualbycategorytoworksheet2(reportworksheetname$, y)Dim myRange As Range' nparams = the number of parameters to be plottednparams = 7offsetxavg = 5OffsetX = 2OffsetY = 0offsetlux = 1Worksheets(reportworksheetname$).Activate
' set header font to boldWorksheets(reportworksheetname$).Range(Cells(offsetlux - 1 + offsetxavg + 1, OffsetY + 1), Cells(offsetlux - 1 + offsetxavg + 3, OffsetY + 3 + (nlu - 1) * (nparams + 1) + nparams)).Font.Bold = True'Worksheets(reportworksheetname$).Range(Cells(1, OffsetY + 3), Cells(3, OffsetY + 19)).Font.Bold = True'Worksheets("Sheet1").Range(Cells(1, 1), Cells(5, 3)).Font.Italic = True
2260 For l = 1 To nlu
If y = 1 ThenFor jj = 1 To nparamsWorksheets(reportworksheetname$).Cells(offsetlux - 1 + 1, OffsetY + 3 + (l - 1) + (nlu + 1) * (jj - 1)).value = landuse$(l, 12)Next jjIf l = 1 ThenWorksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 1, OffsetY + 2).value = "Year"
Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 1, OffsetY + 3 + (l - 1)).value = "runoff"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 1, OffsetY + 3 + (l - 1) + (nlu + 1) * 1).value = "erosion"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 1, OffsetY + 3 + (l - 1) + (nlu + 1) * 2).value = "Diss. N flux"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 1, OffsetY + 3 + (l - 1) + (nlu + 1) * 3).value = "Total N flux"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 1, OffsetY + 3 + (l - 1) + (nlu + 1) * 4).value = "Diss. P flux"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 1, OffsetY + 3 + (l - 1) + (nlu + 1) * 5).value = "Total P flux"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 1, OffsetY + 3 + (l - 1) + (nlu + 1) * 6).value = "Area"
Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 2, OffsetY + 3 + (l - 1)).value = "(cm)"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 2, OffsetY + 3 + (l - 1) + (nlu + 1) * 1).value = "(1E3 kg)"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 2, OffsetY + 3 + (l - 1) + (nlu + 1) * 2).value = "(1E3 kg) (diss)"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 2, OffsetY + 3 + (l - 1) + (nlu + 1) * 3).value = "(1E3 kg)"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 2, OffsetY + 3 + (l - 1) + (nlu + 1) * 4).value = "(1E3 kg) (diss)"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 2, OffsetY + 3 + (l - 1) + (nlu + 1) * 5).value = "(1E3 kg)"Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + 2, OffsetY + 3 + (l - 1) + (nlu + 1) * 6).value = "(ha)"End IfEnd IfIf l = 1 Then Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + OffsetX + y, OffsetY + 2).value = y
Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + OffsetX + y, OffsetY + 3 + (l - 1)).value = lu_RUNOFF(y, l)Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + OffsetX + y, OffsetY + 3 + (l - 1) + (nlu + 1) * 1).value = lu_EROSION(y, l) / 1000#Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + OffsetX + y, OffsetY + 3 + (l - 1) + (nlu + 1) * 2).value = lu_DISNITR(y, l) / 1000#Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + OffsetX + y, OffsetY + 3 + (l - 1) + (nlu + 1) * 3).value = lu_TOTNITR(y, l) / 1000#
Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + OffsetX + y, OffsetY + 3 + (l - 1) + (nlu + 1) * 4).value = lu_DISPHOS(y, l) / 1000#Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + OffsetX + y, OffsetY + 3 + (l - 1) + (nlu + 1) * 5).value = lu_TOTPHOS(y, l) / 1000#Worksheets(reportworksheetname$).Cells(offsetlux + offsetxavg + OffsetX + y, OffsetY + 3 + (l - 1) + (nlu + 1) * 6).value = AREA(l)'2270 Write #1, lu_RUNOFF(y, l), lu_EROSION(y, l), lu_DISNITR(y, l), lu_TOTNITR(y, l), lu_DISPHOS(y, l), lu_TOTPHOS(y, l)
If y = nyrs Then'set the format of the column headings for the average values to boldWorksheets(reportworksheetname$).Range(Cells(offsetlux - 1 + 1, OffsetY + 1), Cells(offsetlux - 1 + 4, OffsetY + 3 + (nlu - 1) * (nparams + 1) + nparams)).Font.Bold = True
For jj = 1 To nparamsWorksheets(reportworksheetname$).Cells(offsetlux + 1, OffsetY + 3 + (nlu + 1) * (jj - 1)).value = "Average"'for each column, establish the range over which to calculate a time average
Set myRange = Worksheets(reportworksheetname$).Range(Cells(offsetlux + offsetxavg + OffsetX + 1, OffsetY + 3 + (l - 1) + (nlu + 1) * (jj - 1)), Cells(offsetlux + offsetxavg + OffsetX + nyrs, OffsetY + 3 + (l - 1) + (nlu + 1) * (jj - 1)))
' write the average value in the appropriate column if it exists
If Application.WorksheetFunction.Count(myRange) > 0 ThenWorksheets(reportworksheetname$).Cells(offsetlux + 4, OffsetY + 3 + (l - 1) + (nlu + 1) * (jj - 1)).value = Application.WorksheetFunction.Average(myRange)End IfNext jjIf l = 1 ThenWorksheets(reportworksheetname$).Cells(offsetlux + 2, OffsetY + 3).value = "runoff"Worksheets(reportworksheetname$).Cells(offsetlux + 2, OffsetY + 3 + (nlu + 1) * 1).value = "erosion"Worksheets(reportworksheetname$).Cells(offsetlux + 2, OffsetY + 3 + (nlu + 1) * 2).value = "Diss. N flux"Worksheets(reportworksheetname$).Cells(offsetlux + 2, OffsetY + 3 + (nlu + 1) * 3).value = "Total N flux"Worksheets(reportworksheetname$).Cells(offsetlux + 2, OffsetY + 3 + (nlu + 1) * 4).value = "Diss. P flux"Worksheets(reportworksheetname$).Cells(offsetlux + 2, OffsetY + 3 + (nlu + 1) * 5).value = "Total P flux"Worksheets(reportworksheetname$).Cells(offsetlux + 2, OffsetY + 3 + (nlu + 1) * 6).value = "Area"
Worksheets(reportworksheetname$).Cells(offsetlux + 3, OffsetY + 3).value = "(cm)"Worksheets(reportworksheetname$).Cells(offsetlux + 3, OffsetY + 3 + (nlu + 1) * 1).value = "(1E3 kg)"Worksheets(reportworksheetname$).Cells(offsetlux + 3, OffsetY + 3 + (nlu + 1) * 2).value = "(1E3 kg) (diss)"Worksheets(reportworksheetname$).Cells(offsetlux + 3, OffsetY + 3 + (nlu + 1) * 3).value = "(1E3 kg)"Worksheets(reportworksheetname$).Cells(offsetlux + 3, OffsetY + 3 + (nlu + 1) * 4).value = "(1E3 kg) (diss)"Worksheets(reportworksheetname$).Cells(offsetlux + 3, OffsetY + 3 + (nlu + 1) * 5).value = "(1E3 kg)"Worksheets(reportworksheetname$).Cells(offsetlux + 3, OffsetY + 3 + (nlu + 1) * 6).value = "(ha)"End IfEnd If2280 Next lWorksheets(reportworksheetname$).Cells.NumberFormat = "General"End Sub
Sub reportmonthlytoworksheet(reportworksheetname$, opt, y)OffsetX = 2
OffsetY = 2Dim monarray(), range1 As Range, monrange As RangeDim monthct As Integer
Worksheets(reportworksheetname$).ActivateIf y = 1 ThenReDim monarray(2, OffsetY + 26 + nlu)
Set range1 = Range(Cells(1, 1), Cells(2, OffsetY + 26 + nlu))' set header font to boldrange1.Font.Bold = Truemonarray(1, 1) = "year"monarray(1, 2) = "calendar year"monarray(1, OffsetY + 1) = "month"monarray(1, OffsetY + 2) = "monthname"monarray(1, OffsetY + 3) = "# of days"monarray(1, OffsetY + 4) = "precip"monarray(1, OffsetY + 5) = "et"monarray(1, OffsetY + 6) = "runoff"monarray(1, OffsetY + 7) = "streamflow"monarray(1, OffsetY + 8) = "groundwater"If opt > 1 Thenmonarray(1, OffsetY + 9) = "erosion"monarray(1, OffsetY + 10) = "sediment yield"If opt > 2 Thenmonarray(1, OffsetY + 11) = "Total N flux"monarray(1, OffsetY + 12) = "Diss. N flux"monarray(1, OffsetY + 13) = "Part. N flux"monarray(1, OffsetY + 14) = "GW N flux"monarray(1, OffsetY + 15) = "Pt. Src. N flux"monarray(1, OffsetY + 16) = "Septic N flux"monarray(1, OffsetY + 17) = "Wet dep N flux"monarray(1, OffsetY + 18) = "Dry dep N flux"monarray(1, OffsetY + 19) = "Tot denit flux"monarray(1, OffsetY + 20) = "GW denit flux"monarray(1, OffsetY + 21) = "Total P flux"monarray(1, OffsetY + 22) = "Diss. P flux"monarray(1, OffsetY + 23) = "Part. P flux"monarray(1, OffsetY + 24) = "GW P flux"monarray(1, OffsetY + 25) = "Pt. Src. P flux"monarray(1, OffsetY + 26) = "Septic P flux"End If ' opt>2End If ' opt > 1If trajectory ThenFor l = 1 To nlumonarray(1, OffsetY + 26 + l) = landuse$(l, 12)Next lEnd If 'trajectory
monarray(2, OffsetY + 4) = "(cm)"monarray(2, OffsetY + 5) = "(cm)"monarray(2, OffsetY + 6) = "(cm)"monarray(2, OffsetY + 7) = "(cm)"monarray(2, OffsetY + 8) = "(cm)"If opt > 1 Thenmonarray(2, OffsetY + 9) = "(1E6 kg)"
monarray(2, OffsetY + 10) = "(1E6 kg)"If opt > 2 Thenmonarray(2, OffsetY + 11) = "(1E3 kg)"monarray(2, OffsetY + 12) = "(1E3 kg)"monarray(2, OffsetY + 13) = "(1E3 kg)"monarray(2, OffsetY + 14) = "(1E3 kg) (diss)"monarray(2, OffsetY + 15) = "(1E3 kg) (diss)"monarray(2, OffsetY + 16) = "(1E3 kg)"monarray(2, OffsetY + 17) = "(1E3 kg)"monarray(2, OffsetY + 18) = "(1E3 kg)"monarray(2, OffsetY + 19) = "(1E3 kg)"monarray(2, OffsetY + 20) = "(1E3 kg)"monarray(2, OffsetY + 21) = "(1E3 kg)"monarray(2, OffsetY + 22) = "(1E3 kg)"monarray(2, OffsetY + 23) = "(1E3 kg)"monarray(2, OffsetY + 24) = "(1E3 kg) (diss)"monarray(2, OffsetY + 25) = "(1E3 kg) (diss)"monarray(2, OffsetY + 26) = "(1E3 kg)"End If ' opt>2End If ' opt > 1If trajectory ThenFor l = 1 To nlumonarray(2, OffsetY + 26 + l) = "(ha)"Next lEnd If 'trajectoryrange1.value = monarrayEnd If ' y=1ReDim monarray(12, OffsetY + 26 + nlu)Set monrange = Range(Cells(OffsetX + 1 + 12 * (y - 1), 1), Cells(OffsetX + 12 * y, OffsetY + 26 + nlu))
For mo = 1 To 12monthct = mo + 12 * (y - 1)
monarray(mo, 1) = yIf initmo + mo - 1 > 12 Thenyrcal = inityr + yElseyrcal = inityr + y - 1End Ifmonarray(mo, 2) = yrcalmonarray(mo, OffsetY + 1) = monthctmonarray(mo, OffsetY + 2) = month$(mo, 4)monarray(mo, OffsetY + 3) = lenmonth(y, inityr, mo, initmo)monarray(mo, OffsetY + 4) = PRECIPITATION(y, mo)monarray(mo, OffsetY + 5) = evapotrans(y, mo)monarray(mo, OffsetY + 6) = RUNOFF(y, mo)monarray(mo, OffsetY + 7) = STREAMFLOW(y, mo)monarray(mo, OffsetY + 8) = GROUNDWATER(y, mo)If opt > 1 Thenmonarray(mo, OffsetY + 9) = EROSION(y, mo) / 1000#monarray(mo, OffsetY + 10) = SEDYIELD(y, mo) / 1000#If opt > 2 Thenmonarray(mo, OffsetY + 11) = TOTNITR(y, mo) / 1000#monarray(mo, OffsetY + 12) = DISNITR(y, mo) / 1000#monarray(mo, OffsetY + 13) = (TOTNITR(y, mo) - DISNITR(y, mo)) / 1000#' note that groundwater and pointsources are both included in dissolved forms
monarray(mo, OffsetY + 14) = GROUNDNITR(y, mo) / 1000#monarray(mo, OffsetY + 15) = pointnitr(mo) / 1000#monarray(mo, OffsetY + 16) = septicN(y, mo) / 1000#monarray(mo, OffsetY + 17) = wetdepnitr(y, mo) / 1000#monarray(mo, OffsetY + 18) = drydepnitr(y, mo) / 1000#monarray(mo, OffsetY + 19) = denittot(y, mo) / 1000#monarray(mo, OffsetY + 20) = gwdenit(y, mo) / 1000#monarray(mo, OffsetY + 21) = TOTPHOS(y, mo) / 1000#monarray(mo, OffsetY + 22) = DISPHOS(y, mo) / 1000#monarray(mo, OffsetY + 23) = (TOTPHOS(y, mo) - DISPHOS(y, mo)) / 1000#monarray(mo, OffsetY + 24) = GROUNDPHOS(y, mo) / 1000#monarray(mo, OffsetY + 25) = pointphos(mo) / 1000#monarray(mo, OffsetY + 26) = septicP(y, mo) / 1000#If trajectory ThenFor l = 1 To nlumonarray(mo, OffsetY + 26 + l) = AREA(l)Next lEnd IfEnd IfEnd IfNext momonrange.value = monarrayIf monthct = 12 * nyrs Then Worksheets(reportworksheetname$).Cells.NumberFormat = "General"
End SubSub reportdailytoworksheet(reportworksheetname$, day, y, i, j)OffsetX = 2OffsetY = 0Worksheets(reportworksheetname$).ActivateWorksheets(reportworksheetname$).Range(Cells(OffsetX, OffsetY + 1), Cells(OffsetX, OffsetY + 10)).Font.Bold = True
If (day = 1) Then
Worksheets(reportworksheetname$).Cells(1, OffsetY + 1).value = "day"Worksheets(reportworksheetname$).Cells(1, OffsetY + 2).value = "month"Worksheets(reportworksheetname$).Cells(1, OffsetY + 3).value = "precip"Worksheets(reportworksheetname$).Cells(1, OffsetY + 4).value = "rain"Worksheets(reportworksheetname$).Cells(1, OffsetY + 5).value = "melt"Worksheets(reportworksheetname$).Cells(1, OffsetY + 6).value = "water"Worksheets(reportworksheetname$).Cells(1, OffsetY + 7).value = "snow"Worksheets(reportworksheetname$).Cells(1, OffsetY + 8).value = "qtotal"Worksheets(reportworksheetname$).Cells(1, OffsetY + 9).value = "temp"Worksheets(reportworksheetname$).Cells(1, OffsetY + 10).value = "amc5"
Worksheets(reportworksheetname$).Cells(OffsetX, OffsetY + 3).value = "(cm)"Worksheets(reportworksheetname$).Cells(OffsetX, OffsetY + 4).value = "(cm)"Worksheets(reportworksheetname$).Cells(OffsetX, OffsetY + 5).value = "(cm)"Worksheets(reportworksheetname$).Cells(OffsetX, OffsetY + 6).value = "(cm)"Worksheets(reportworksheetname$).Cells(OffsetX, OffsetY + 7).value = "(cm)"Worksheets(reportworksheetname$).Cells(OffsetX, OffsetY + 8).value = "(cm)"Worksheets(reportworksheetname$).Cells(OffsetX, OffsetY + 9).value = "(C)"Worksheets(reportworksheetname$).Cells(OffsetX, OffsetY + 10).value = "(cm)"End IfWorksheets(reportworksheetname$).Cells(OffsetX + day, OffsetY + 1).value = day
Worksheets(reportworksheetname$).Cells(OffsetX + day, OffsetY + 2).value = month$(i, 4)Worksheets(reportworksheetname$).Cells(OffsetX + day, OffsetY + 3).value = PREC(y, i, j)Worksheets(reportworksheetname$).Cells(OffsetX + day, OffsetY + 4).value = rainWorksheets(reportworksheetname$).Cells(OffsetX + day, OffsetY + 5).value = meltWorksheets(reportworksheetname$).Cells(OffsetX + day, OffsetY + 6).value = waterWorksheets(reportworksheetname$).Cells(OffsetX + day, OffsetY + 7).value = snowWorksheets(reportworksheetname$).Cells(OffsetX + day, OffsetY + 8).value = qtotalWorksheets(reportworksheetname$).Cells(OffsetX + day, OffsetY + 9).value = temp(y, i, j)Worksheets(reportworksheetname$).Cells(OffsetX + day, OffsetY + 10).value = amc5End SubFunction lenmonth(y, yinit, mo, moinit)yr = yinit + y - 1mon = moinit + mo - 1If mon > 12 Thenyr = yr + 1mo2 = mon - 12Elsemo2 = monEnd IfIf (yr Mod 4) = 0 And mo2 = 2 Then lenmonth = 29 Else lenmonth = DAYSMONTH(mo2)End FunctionFunction cnum(growingseason As Boolean, amc5, cn2, melt)If cn2 = 0 Thencnum = 0
Else CN1 = cn2 / (2.334 - 0.01334 * cn2) cn3 = cn2 / (0.4036 + 0.0059 * cn2) If melt > 0 Then cnum = cn3 'curve number = 3 if there is snowmelt Else If Not growingseason Then '----------------------DORMANT SEASON-------------------------------- If amc5 >= 2.79 Then cnum = cn3 ElseIf amc5 < 1.27 Then cnum = CN1 + (cn2 - CN1) * amc5 / 1.27 Else cnum = cn2 + (cn3 - cn2) * (amc5 - 1.27) / 1.52 End If ElseIf growingseason Then '-----------------------GROWING SEASON--------------------------------- If amc5 >= 5.33 Then cnum = cn3 ElseIf amc5 < 3.56 Then cnum = CN1 + (cn2 - CN1) * amc5 / 3.56 Else cnum = cn2 + (cn3 - cn2) * (amc5 - 3.56) / 1.77 End If End If End If End If
If cnum > 100 Then cnum = 100#
End FunctionFunction qrunoff(growingseason As Boolean, cn2, amc5, water, melt)If cn2 = 0 Thenqrunoff = 0Else retention = 2540 / cnum(growingseason, amc5, cn2, melt) - 25.4 If water < 0.2 * retention Then qrunoff = 0 Else qrunoff = (water - 0.2 * retention) ^ 2 / (water + 0.8 * retention) End IfEnd IfEnd FunctionSub writeresultsfile(filename$)2180 '----------------------------WRITE OUTPUT TO RESULTS.DAT--------------2190 Open filename$ For Output As #12200 Write #1, simtitle$, "nyrs= ", nyrs, nrur, nurb, nlu, opt2210 For y = 1 To nyrs2220 For i = 0 To 122230 Write #1, y, i, STREAMFLOW(y, i), PRECIPITATION(y, i), evapotrans(y, i), GROUNDWATER(y, i), RUNOFF(y, i), EROSION(y, i) / 1000#, SEDYIELD(y, i) / 1000#, GROUNDNITR(y, i) / 1000#, GROUNDPHOS(y, i) / 1000#, DISNITR(y, i) / 1000#, TOTNITR(y, i) / 1000#, DISPHOS(y, i) / 1000#, TOTPHOS(y, i) / 1000#2250 Next i Next y For y = 1 To nyrs2260 For l = 1 To nlu2270 Write #1, lu_RUNOFF(y, l), lu_EROSION(y, l) / 1000#, lu_DISNITR(y, l) / 1000#, lu_TOTNITR(y, l) / 1000#, lu_DISPHOS(y, l) / 1000#, lu_TOTPHOS(y, l) / 1000#2280 Next l2290 Next y2300 Close #1End Sub
Sub clearworksheet(reportworksheetname$)Worksheets(reportworksheetname$).Cells.ClearContentsEnd SubSub makenewchart(sheetname$, chartvbl, datarange)Set chartvbl = Sheets(sheetname$).ChartObjects.Add(50, 40, 200, 100)chartvbl.Chart.ChartWizard Source:=Worksheets(sheetname$).Range(datarange), _ Gallery:=xlColumn, Format:=5, PlotBy:=xlColumns, _ CategoryLabels:=0, SeriesLabels:=0, CategoryTitle:="Year", ValueTitle:="Sales"
End Sub 'Option Explicit Sub CreateMenu()' This sub should be executed when the workbook is opened.' NOTE: There is no error handling in this subroutine
Dim MenuSheet As Worksheet Dim MenuObject As CommandBarPopup
Dim MenuItem As Object Dim SubMenuItem As CommandBarButton Dim Row As Integer Dim MenuLevel, NextLevel, PositionOrMacro, Caption, Divider, FaceId
''''''''''''''''''''''''''''''''''''''''''''''''''''' Location for menu data Set MenuSheet = ThisWorkbook.Sheets("MenuSheet")''''''''''''''''''''''''''''''''''''''''''''''''''''
' Make sure the menus aren't duplicated Call DeleteMenu ' Initialize the row counter Row = 2
' Add the menus, menu items and submenu items using' data stored on MenuSheet Do Until IsEmpty(MenuSheet.Cells(Row, 1)) With MenuSheet MenuLevel = .Cells(Row, 1) Caption = .Cells(Row, 2) PositionOrMacro = .Cells(Row, 3) Divider = .Cells(Row, 4) FaceId = .Cells(Row, 5) NextLevel = .Cells(Row + 1, 1) End With Select Case MenuLevel Case 1 ' A Menu' Add the top-level menu to the Worksheet CommandBar Set MenuObject = Application.CommandBars(1). _ Controls.Add(Type:=msoControlPopup, _ Before:=PositionOrMacro, _ Temporary:=True) MenuObject.Caption = Caption Case 2 ' A Menu Item If NextLevel = 3 Then Set MenuItem = MenuObject.Controls.Add(Type:=msoControlPopup) Else Set MenuItem = MenuObject.Controls.Add(Type:=msoControlButton) MenuItem.OnAction = PositionOrMacro End If MenuItem.Caption = Caption If FaceId <> "" Then MenuItem.FaceId = FaceId If Divider Then MenuItem.BeginGroup = True Case 3 ' A SubMenu Item Set SubMenuItem = MenuItem.Controls.Add(Type:=msoControlButton) SubMenuItem.Caption = Caption SubMenuItem.OnAction = PositionOrMacro If FaceId <> "" Then SubMenuItem.FaceId = FaceId
If Divider Then SubMenuItem.BeginGroup = True Case 4 ' a sub-sub menu item Set SubMenuItem = MenuItem.Controls.Add(Type:=msoControlButton) SubMenuItem.Caption = Caption SubMenuItem.OnAction = PositionOrMacro If FaceId <> "" Then SubMenuItem.FaceId = FaceId If Divider Then SubMenuItem.BeginGroup = True End Select Row = Row + 1 LoopEnd Sub
Sub DeleteMenu()' This sub should be executed when the workbook is closed' Deletes the Menus Dim MenuSheet As Worksheet Dim Row As Integer Dim Caption As String On Error Resume Next Set MenuSheet = ThisWorkbook.Sheets("MenuSheet") Row = 2 Do Until IsEmpty(MenuSheet.Cells(Row, 1)) If MenuSheet.Cells(Row, 1) = 1 Then Caption = MenuSheet.Cells(Row, 2) Application.CommandBars(1).Controls(Caption).Delete End If Row = Row + 1 Loop On Error GoTo 0End Sub
Sub NotYet() MsgBox "This feature is not yet implemented."End Sub
Sub AboutGWLFXL() MsgBox ("GWLF for EXCEL (GWLFXL) is based on GWLF, a loading model" _ & Chr(13) & "which was originally developed by Douglas A. Haith Cornell University." _ & Chr(13) & "This application was developed by Dennis P. Swaney, Cornell University.")End SubSub units() MsgBox ("The standard unit of output for reporting hydrologic variables used in" _ & Chr(13) & "the model is cm of water (= 100 m3/ha of land surface)" _ & Chr(13) & "For erosion and sediment, the unit is millions of kg (1000 metric tons)" _ & Chr(13) & "For nutrients, the unit is 1000s of kg (metric tons)." _ & Chr(13) & "The use of 'per area' units for hydrologic variables " _ & Chr(13) & "and absolute units for other variables is a feature of the original model, " _ & Chr(13) & "and is maintained here for consistency. Units may be modified in future versions. ")End Sub
Sub CallHelp() MsgBox ("Dennis says, 'Don't even THINK of calling me!'" _ & Chr(13) & "(But you can try him by email at [email protected])")End SubSub rung1()
Call gwlf4(1)End SubSub rung2()
Call gwlf4(2)End SubSub rung3()
Call gwlf4(3)End SubSub frung3()Worksheets("notes and parameters").Cells(2, 4).value = "f"Call gwlf4(3)End SubSub srung3()Worksheets("notes and parameters").Cells(2, 4).value = "s"Call gwlf4(3)End SubSub createparamfile(numfil)foldername$ = Worksheets("notes and parameters").Cells(7, 4).valuest1$ = "Old filename already exists. "If numfil = 1 Thenfilename$ = "nutrient.txt"Elsefilename$ = "transport.txt"End IfDo While FileExists(foldername$ & filename$) Or Len(filename$) = 0filename2$ = getstrg$(st1$ & "New filename?", filename$)If filename2$ = filename$ And Len(filename2$) > 0 Then Exit DoIf Len(filename2$) > 0 Then filename$ = filename2$
LoopCall readtransportworksheet("transport") ' need to read this one regardlessIf numfil = 1 Then
Call readnutrientworksheet("nutrient")Call writenutrientfile(foldername$ & filename$)ElseCall writetransportfile(foldername$ & filename$)End IfMsgBox filename$ & " created...hit ok to continue", vbInformation
End SubSub createparameterworksheet(num)foldername$ = Worksheets("notes and parameters").Cells(7, 4).valuest1$ = " worksheet already exists. "transfilename$ = Worksheets("notes and parameters").Cells(27, 4).value ' transport fileIf num = 1 Thenfilename$ = Worksheets("notes and parameters").Cells(28, 4).value ' nutrient filesheetname$ = "nutrient"Else
sheetname$ = "transport"End IfDo While SheetExists(sheetname$) Or Len(sheetname$) = 0
sheetname2$ = getstrg$(sheetname$ & st1$ & "New worksheet name?", sheetname$)If sheetname2$ = sheetname$ And Len(sheetname2$) > 0 Then Exit DoIf Len(sheetname2$) > 0 Then sheetname$ = sheetname2$LoopIf SheetExists(sheetname$) Then ' if you elect to use the existing worksheet, clear it before overwritingCall clearworksheet(sheetname$)Else ' otherwise, create a new oneWorksheets.Add after:=Sheets(1)Sheets(2).Name = sheetname$Worksheets(sheetname$).ActivateEnd If
Call readtransportfile(foldername$ & transfilename$) ' need to read this one regardlessIf num = 1 Then
Call readnutrientfile(foldername$ & filename$)Call reportnutrienttoworksheet(sheetname$)ElseCall reporttransporttoworksheet(sheetname$)End IfMsgBox sheetname$ & " created...hit ok to continue", vbInformation
End SubSub create1()' creates GWLF nutrient fileCall createparamfile(1)End SubSub create2()' creates GWLF transport fileCall createparamfile(2)End SubSub create3()' creates nutrient worksheetCall createparameterworksheet(1)End SubSub create4()' creates transport worksheetCall createparameterworksheet(2)End SubSub makegwlftemplate()
n = Worksheets.CountFor ii = 1 To nIf Sheets(ii).Name = "template" Thenresp1$ = getstrg$("The template sheet already exists. Overwrite (y/n)?)", "y")
If UCase$(resp1$) = "Y" Then Worksheets("template").Delete Else oldname$ = "sheet" & Str(n + 1) resp1$ = MsgBox("The old template sheet is being renamed " & oldname$) Sheets(ii).Name = oldname$ End IfGoTo 200End If100 Next ii
200 Worksheets.Add after:=Sheets(1)Sheets(2).Name = "template"Worksheets("template").Activateresp = getval("Number of subwatersheds:", Worksheets("notes and parameters").Cells(1, 4).value)Worksheets("template").Cells(1, 1).value = "Number of subwatersheds:"Worksheets("template").Cells(1, 2).value = respmaxnlu = getval("Maximum number of landuse types:", 4)maxma = getval("Maximum number of manured areas:", 4)Call readtransportworksheet("transport")Call readnutrientworksheet("nutrient")
For j = 1 To resp
Call reportnutrienttoworksheet("template", "Hudson1", j, 0, (j - 1) * 8)Call reporttransporttoworksheet("template", 40 + maxnlu + maxma, (j - 1) * 8)Next jEnd SubPrivate Sub KillTheForm()Unload SimbeginEnd SubPrivate Function FileExists(fname) As Boolean' returns true if file exists (Walkenbach, 1999)Dim x As Stringx = Dir(fname)If x <> "" Then FileExists = True Else FileExists = FalseEnd FunctionPrivate Function PathExists(pname) As Boolean' returns true if path exists (Walkenbach, 1999)Dim x As StringOn Error Resume Nextx = GetAttr(pname) And 0If Err = 0 Then PathExists = True Else PathExists = FalseEnd FunctionPrivate Function SheetExists(sname) As Boolean' returns true if sheet exists (Walkenbach, 1999)Dim x As ObjectOn Error Resume NextSet x = ActiveWorkbook.Sheets(sname)If Err = 0 Then SheetExists = True Else SheetExists = FalseEnd FunctionFunction acos(x)acos = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)End Function
Sub hrs(dayhrs, lat) ' pass latitude in degreesdayct = 0Pi = 3.14159265lrad = lat * Pi / 180 ' latitude in radiansFor m = 1 To 12 h = 0 For d = 1 To DAYSMONTH(m) dayct = dayct + 1 f = 0.4093 * Sin(2 * Pi / 365 * dayct - 1.405) h = h + 24 * acos(-Tan(f) * Tan(lrad)) / Pi Next d dayhrs(m) = h / DAYSMONTH(m) ' average max daylight hrs for month m for latitude
Next mEnd Sub
Sub initializeall(nyrs)
For y = 1 - spinuplen To nyrs For m = 0 To 12 STREAMFLOW(y, m) = 0 PRECIPITATION(y, m) = 0 evapotrans(y, m) = 0 GROUNDWATER(y, m) = 0 RUNOFF(y, m) = 0 EROSION(y, m) = 0 SEDYIELD(y, m) = 0 GROUNDNITR(y, m) = 0 GROUNDPHOS(y, m) = 0 DISNITR(y, m) = 0 TOTNITR(y, m) = 0 DISPHOS(y, m) = 0 TOTPHOS(y, m) = 0 SEDTRANS(y, m) = 0 septicN(y, m) = 0 septicP(y, m) = 0 tempav(y, m) = 0 wetdepnitr(y, m) = 0 drydepnitr(y, m) = 0 gwdenit(y, m) = 0 denittot(y, m) = 0 Next m For l = 1 To nlu URBANSED(l) = 0# lu_RUNOFF(y, l) = 0 lu_EROSION(y, l) = 0 lu_DISNITR(y, l) = 0 lu_TOTNITR(y, l) = 0 lu_DISPHOS(y, l) = 0 lu_TOTPHOS(y, l) = 0 lu_denit(y, l) = 0 Next lNext y End Sub
Module 2. Optimization Sub solve1()solvessq (1)End SubSub solve2()solvessq (2)End SubSub solve3()solvessq (3)End Sub
Sub solvessq(opt)'Dim zzz(20) As String, objectivefunccell As String
' solve1 Macro'
' SolverOk' setcell is the cell containing the objective function'maxminval = 1 means maximize'maxminval = 2 means minimize'maxminval = 3 means match specific value' bychange = refers to the cells containing the parameters to optimize' (must be in active worksheet)' solverAdd' allows constraints on parameters to be added' relation = 1 is <=' relation = 2 is =' relation = 3 is >=' formulatext is the constraint valueWorksheets("Calibrate").ActivateCall setparms2 ' need to call this to initialize num and nlistzzz(1) = Cells(nlist(1) + 1, 2).AddressLocal
If num > 1 ThenFor nn = 2 To numzzz(nn) = Cells(nlist(nn) + 1, 2).AddressLocalNext nnEnd If Application.ScreenUpdating = False Select Case opt Case 1 objectivefunccell = "$e$2" Case 2 objectivefunccell = "$e$3" Case 3 objectivefunccell = "$e$4" Case ElseMsgBox "invalid option" & Chr(13) & "...Optimization halted...hit ok to continue", vbInformation: EndEnd Select
SolverReset 'num = # of parameters being optimized If num = 1 Then SolverOk SetCell:=objectivefunccell, MaxMinVal:=2, ValueOf:="0", ByChange:=Range(zzz(1)) If num = 2 Then SolverOk SetCell:=objectivefunccell, MaxMinVal:=2, ValueOf:="0", ByChange:=Union(Range(zzz(1)), Range(zzz(2))) If num = 3 Then SolverOk SetCell:=objectivefunccell, MaxMinVal:=2, ValueOf:="0", ByChange:=Union(Range(zzz(1)), Range(zzz(2)), Range(zzz(3))) If num = 4 Then SolverOk SetCell:=objectivefunccell, MaxMinVal:=2, ValueOf:="0", ByChange:=Union(Range(zzz(1)), Range(zzz(2)), Range(zzz(3)), Range(zzz(4))) If num = 5 Then SolverOk SetCell:=objectivefunccell, MaxMinVal:=2, ValueOf:="0", ByChange:=Union(Range(zzz(1)), Range(zzz(2)), Range(zzz(3)), Range(zzz(4)), Range(zzz(5))) For nn = 1 To num ' SolverAdd CellRef:="$b$2", Relation:=3, FormulaText:="0.000001" SolverAdd CellRef:=zzz(nn), Relation:=3, FormulaText:="0.0" Next nn SolverOptions MaxTime:=100, Iterations:=100, Precision:=0.000001, AssumeLinear _ :=False, StepThru:=False, Estimates:=1, Derivatives:=1, SearchOption:=1, _ IntTolerance:=5, Scaling:=False, Convergence:=0.0001, AssumeNonNeg:=True SolverSolve UserFinish = True Application.ScreenUpdating = TrueEnd Sub
Sub setparms2()Dim cell As Range, prange As RangeDim plist(20) As Double
'Cells(2, 2).Select'Range(Selection, Selection.End(xlDown)).Select'Set prange = Selection.Offset(0, 1)'Selection = Selection.Offset(0, 1)num = 0
For i = 2 To 16 ' need to change if parameters are added
If Not Worksheets("Calibrate").Cells(i, 3).value = "" Thennum = num + 1nlist(num) = i - 1plist(num) = Worksheets("Calibrate").Cells(i, 2).valueEnd IfNext iIf num > 5 Then MsgBox "Maximum of 5 parameters allowed" & Chr(13) & "...Optimization halted...hit ok to continue", vbInformation: EndIf num = 0 Then MsgBox "No parameters selected" & Chr(13) & "...Optimization halted...hit ok to continue", vbInformation: EndFor i = 1 To numIf nlist(i) = 1 Then RECESSCOEF = plist(i)If nlist(i) = 2 Then SEEPCOEF = plist(i)If nlist(i) = 3 Then UNSATSTOR = plist(i)If nlist(i) = 4 Then SATSTOR = plist(i)If nlist(i) = 5 Then snow = plist(i)If nlist(i) = 6 Then SEDELRATIO = plist(i)If nlist(i) = 7 Then etmult = plist(i): For mo = 1 To 12: cv(mo) = cv(mo) * etmult: Next mo
If nlist(i) = 8 Then erosmult = plist(i): For mo = 1 To 12: acoef(mo) = acoef(mo) * erosmult: Next moIf nlist(i) = 9 Then sednitr = plist(i)If nlist(i) = 10 Then sedphos = plist(i)If nlist(i) = 11 Then grnitrconc = plist(i)If nlist(i) = 12 Then grphosconc = plist(i)If nlist(i) = 13 Then ndeptot = plist(i)If nlist(i) = 14 Then pdry = plist(i)If nlist(i) = 15 Then gwdenitfrac = plist(i)If nlist(i) = 16 Then RECESSCOEF = plist(i)If nlist(i) = 17 Then RECESSCOEF = plist(i)If nlist(i) = 18 Then RECESSCOEF = plist(i)If nlist(i) = 19 Then RECESSCOEF = plist(i)If nlist(i) = 20 Then RECESSCOEF = plist(i)
Next i
End Sub
Function objfunc(range1 As Range, opt As Integer, range2 As Range)' Function objfunc(range1 As Range, opt As Integer, p1 As Single)' range1 contains the array of data to compare with model results in the calibration process'opt = 1, 2 or 3 depending upon whether only water, sediment or nutrients (respectively) are being simulated' note also that range2 is a dummy range which must encompass the complete potential list of parameters'which are being varied for the optimization - this is a quirk of solver, apparently
Dim p1 As Single
c1 = range1.Countp1 = 1#
For i = 1 To c1gout(i) = 0Next iCall gwlf4(opt, True, False) ' optimize=true, unsens=false
ssq = 0i = 0For Each c In range1 'modeli = i + 1
If IsNumeric(c.value) Then ssq = ssq + (c.value - gout(i)) ^ 2 Next c objfunc = ssq
End Function
Module 3. Printout
Sub printdaily()printoutdata ("daily")End SubSub printmonthly()printoutdata ("monthly")End SubSub printannual()printoutdata ("annual")End SubSub printtransport()Call printrange("transport", "a1:g32")End SubSub printnutrient()Call printrange("nutrient", "a1:g44")End SubSub printoutdata(sheetname$)
Sheets(sheetname$).Select Range("A8").Select vd = Selection.End(xlDown).Address ' find the bottom line of the current range of cells Range(vd).Select vr = Selection.End(xlToRight).Address ' find the rightmost cell of the bottom line Range("a1", vr).Select ' select the rectangle bounded by a1 and bottom right nn = Application.WorksheetFunction.CountA(Range("a3", vr)) ' counts number of nonblank cells If nn = 0 Then MsgBox " No data! Printout halted...hit ok to continue", vbInformation: End Selection.Printout preview:=True ' print selection with preview (can opt out from there)End SubSub printrange(sheetname$, prangeaddress$) Sheets(sheetname$).Select Range(prangeaddress$).Select nn = Application.WorksheetFunction.CountA(Selection) ' counts number of nonblank cells If nn = 0 Then MsgBox " No data! Printout halted...hit ok to continue", vbInformation: End Selection.Printout preview:=True ' print selection with preview (can opt out from there)End SubSub OpenWord(documentname$)' modified from http://www.experts-exchange.com/Developer/Applications/Q_10889061.htmlDim wdApp As Word.ApplicationDim wdDoc As Word.Documentfoldername$ = Worksheets("notes and parameters").Cells(7, 4).value'First part of code is to create a reference to the open Word instance'or to create a new Word instance (=start Word)On Error Resume NextSet wdApp = GetObject(, "word.application")If Err.Number <> 0 Then Set wdApp = CreateObject("word.application") wdApp.Visible = TrueEnd IfOn Error GoTo 0
With wdApp
On Error GoTo OpenError
Set wdDoc = .Documents.Open(foldername$ & documentname$) wdDoc.Activate
On Error GoTo OpenError
End WithExit Sub
OpenError:If Err.Number = 5174 ThenMsgBox "Users manual file could not be found in" & Chr(13) & foldername$ElseMsgBox "There was an unspecified error in trying to open the document."End If
End SubSub opendocumentation()Call OpenWord("gwlfusersmanual")End Sub
Module 4. Stochastic
'module level declarationsOption Base 1Dim vblarray() As Double, vblmean() As Double, vblm2() As Double, vout() As Double, vlabel() As String
Sub gensens()Call setrealizations(True, 3)End SubSub stoch1()Call drivestoch(1)End SubSub stoch2()Call drivestoch(2)End SubSub stoch3()Call drivestoch(3)End SubSub genstoch()Dim nreal As Integernreal = getval("Number of realizations:", Worksheets("notes and parameters").Cells(34, 4).value)Call setrealizations(False, nreal)End SubFunction recursemean(mean As Double, x As Double, n) ' used to calculate the mean of n values in terms of the mean of n-1 valuesDim p As Doublep = (n - 1) / nrecursemean = p * mean + (1# - p) * xEnd FunctionFunction svarest(meansq As Double, mean As Double, n) ' returns sample variance for sample of size n, mean, and mean of squaressvarest = n * (meansq - mean ^ 2) / (n - 1)End FunctionSub setrealizations(sensitivity As Boolean, nreal As Integer)Dim cell As Range, prange As RangeDim plist() As DoubleDim rangeout As Range, rangecode As RangeWorksheets("Stochastic").Activatenum = 3
Donum = num + 1Loop Until Worksheets("Stochastic").Cells(num, 2).value = ""num = num - 1Set rangeout = Range(Cells(3, 7), Cells(num, 256))rangeout.ClearIf sensitivity Then ' fill code column with s to indicate sensitivity analysisSet rangecode = Range(Cells(3, 6), Cells(num, 6))rangecode.value = "s"End IfReDim plist(num - 2, nreal)For i = 1 To num - 2pcode = UCase$(Worksheets("Stochastic").Cells(i + 2, 6).value)p1 = Worksheets("Stochastic").Cells(i + 2, 3).valuep2 = Worksheets("Stochastic").Cells(i + 2, 4).valueFor j = 1 To nreal
If p2 = 0 Or p2 = "" Thenpval = p1Else p = Rnd() Select Case pcode Case "TN" pval = Application.WorksheetFunction.NormInv(p, p1, p2) ' p1 = mean, p2= stdev Case "LN" If p1 = 0 Then MsgBox "zero mean value in lognormal distribution": End pval = Application.WorksheetFunction.LogInv(p, Log(p1 ^ 4 / (p1 ^ 2 + p2 ^ 2)) / 2, Log(1 + (p2 / p1) ^ 2) / 2) ' p1 = mean, p2= stdev of data, not log of data Case "N" pval = Application.WorksheetFunction.NormInv(p, p1, p2) ' p1 = mean, p2= stdev Case "EXP" pval = Application.WorksheetFunction.GammaInv(p, 1, p1) ' p1 = mean; note that this follows from excels def of gamma function Case "G" pval = Application.WorksheetFunction.GammaInv(p, (p1 / p2) ^ 2, p2 ^ 2 / p1) ' p1 = mean, p2= stdev; note the excel definition of beta is the inverse of that of Haith et al 1984. Case "U" pval = p1 + p * (p2 - p1) ' p1 = lower limit of uniform, p2= upper limit of uniform Case "S" If sensitivity Then If j = 1 Then pval = p1 If j = 2 Then pval = p1 - p2 If j = 3 Then pval = p1 + p2 End If Case Else pval = p1 End SelectEnd If 'p2 = 0 (std dev = 0)plist(i, j) = pval
Next j
Next i
Set rangeout = Range(Cells(3, 7), Cells(num, nreal + 6))rangeout.value = plistEnd Sub
Sub calcmeans(n, nreal) ' n is realization numberDim prange As Range
Dim var As DoubleDim rangeout As Range, labelout As RangeReDim vout(nyrs, 2 * (25 + nlu)) As DoubleFor y = 1 To nyrsvblarray(y, 1) = yvblarray(y, 2) = y + inityr - 1vblarray(y, 3) = PRECIPITATION(y, 0)vblarray(y, 4) = evapotrans(y, 0)vblarray(y, 5) = RUNOFF(y, 0)vblarray(y, 6) = STREAMFLOW(y, 0)vblarray(y, 7) = GROUNDWATER(y, 0)vblarray(y, 8) = EROSION(y, 0)
vblarray(y, 9) = SEDYIELD(y, 0)vblarray(y, 10) = TOTNITR(y, 0)vblarray(y, 11) = DISNITR(y, 0)vblarray(y, 12) = TOTNITR(y, 0) - DISNITR(y, 0)' note that groundwater and pointsources are both included in dissolved formsvblarray(y, 13) = GROUNDNITR(y, 0)vblarray(y, 14) = pointnitr(0)vblarray(y, 15) = septicN(y, 0)vblarray(y, 16) = wetdepnitr(y, 0)vblarray(y, 17) = drydepnitr(y, 0)vblarray(y, 18) = denittot(y, 0)vblarray(y, 19) = gwdenit(y, 0)vblarray(y, 20) = TOTPHOS(y, 0)vblarray(y, 21) = DISPHOS(y, 0)vblarray(y, 22) = TOTPHOS(y, 0) - DISPHOS(y, 0)vblarray(y, 23) = GROUNDPHOS(y, 0)vblarray(y, 24) = pointphos(0)vblarray(y, 25) = septicP(y, 0)
If n = 1 Then ' only need to evaluate on first pass of uncertainty analysisFor i = 1 To 25vblmean(y, i) = vblarray(y, i)vblm2(y, i) = vblarray(y, i) ^ 2Next i If trajectory Then For l = 1 To nlu vblarray(y, 25 + l) = AREA(l) vblmean(y, 25 + l) = vblarray(y, 25 + l)vblm2(y, 25 + l) = vblarray(y, 25 + l) ^ 2 Next l End If Else ' n> 1For i = 1 To 25 + nluvblmean(y, i) = recursemean(vblmean(y, i), vblarray(y, i), n)vblm2(y, i) = recursemean(vblm2(y, i), vblarray(y, i) ^ 2, n)vout(y, 1 + 2 * (i - 1)) = vblmean(y, i)Next iEnd If 'n
If n = nreal Then For i = 1 To 25 + nlu
'calc stdevs and store in the sum of squares array var = svarest(vblm2(y, i), vblmean(y, i), nreal)
If var >= 0 Then vblm2(y, i) = Sqr(var) ElseIf var > -0.000001 Then vblm2(y, i) = 0# Else vblm2(y, i) = -99999 ' error code End If vout(y, 2 * i) = vblm2(y, i)
Next i
If y = nyrs Thenvlabel(1, 1 * 2) = ""vlabel(1, 2 * 2) = ""vlabel(1, 3 * 2) = "precip sd"vlabel(1, 4 * 2) = "et sd"vlabel(1, 5 * 2) = "runoff sd"vlabel(1, 6 * 2) = "streamflow sd"vlabel(1, 7 * 2) = "groundwater sd"vlabel(1, 8 * 2) = "erosion sd"vlabel(1, 9 * 2) = "sediment yield sd"vlabel(1, 10 * 2) = "Total N flux sd"vlabel(1, 11 * 2) = "Diss. N flux sd"vlabel(1, 12 * 2) = "Part. N flux sd"vlabel(1, 13 * 2) = "GW N flux sd"vlabel(1, 14 * 2) = "Pt. Src. N flux sd"vlabel(1, 15 * 2) = "Septic N flux sd"vlabel(1, 16 * 2) = "Wet dep N flux sd"vlabel(1, 17 * 2) = "Dry dep N flux sd"vlabel(1, 18 * 2) = "Total denit flux sd"vlabel(1, 19 * 2) = "GW denit flux sd"vlabel(1, 20 * 2) = "Total P flux sd"vlabel(1, 21 * 2) = "Diss. P flux sd"vlabel(1, 22 * 2) = "Part. P flux sd"vlabel(1, 23 * 2) = "GW P flux sd"vlabel(1, 24 * 2) = "Pt. Src. P flux sd"vlabel(1, 25 * 2) = "Septic P flux sd"vlabel(1, 1 * 2 - 1) = ""vlabel(1, 2 * 2 - 1) = ""vlabel(1, 3 * 2 - 1) = "precip"vlabel(1, 4 * 2 - 1) = "et"vlabel(1, 5 * 2 - 1) = "runoff"vlabel(1, 6 * 2 - 1) = "streamflow"vlabel(1, 7 * 2 - 1) = "groundwater"vlabel(1, 8 * 2 - 1) = "erosion"vlabel(1, 9 * 2 - 1) = "sediment yield"vlabel(1, 10 * 2 - 1) = "Total N flux"vlabel(1, 11 * 2 - 1) = "Diss. N flux"vlabel(1, 12 * 2 - 1) = "Part. N flux"vlabel(1, 13 * 2 - 1) = "GW N flux"vlabel(1, 14 * 2 - 1) = "Pt. Src. N flux"vlabel(1, 15 * 2 - 1) = "Septic N flux"vlabel(1, 16 * 2 - 1) = "Wet dep N flux"vlabel(1, 17 * 2 - 1) = "Dry dep N flux"vlabel(1, 18 * 2 - 1) = "Total denit flux"vlabel(1, 19 * 2 - 1) = "GW denit flux"vlabel(1, 20 * 2 - 1) = "Total P flux"vlabel(1, 21 * 2 - 1) = "Diss. P flux"vlabel(1, 22 * 2 - 1) = "Part. P flux"vlabel(1, 23 * 2 - 1) = "GW P flux"vlabel(1, 24 * 2 - 1) = "Pt. Src. P flux"vlabel(1, 25 * 2 - 1) = "Septic P flux"vlabel(2, 1 * 2) = " "vlabel(2, 2 * 2) = " "vlabel(2, 3 * 2) = "(cm)"vlabel(2, 4 * 2) = "(cm)"
vlabel(2, 5 * 2) = "(cm)"vlabel(2, 6 * 2) = "(cm)"vlabel(2, 7 * 2) = "(cm)"vlabel(2, 8 * 2) = "(1E6 kg)"vlabel(2, 9 * 2) = "(1E6 kg)"vlabel(2, 10 * 2) = "(1E3 kg)"vlabel(2, 11 * 2) = "(1E3 kg)"vlabel(2, 12 * 2) = "(1E3 kg)"vlabel(2, 13 * 2) = "(1E3 kg) (diss)"vlabel(2, 14 * 2) = "(1E3 kg) (diss)"vlabel(2, 15 * 2) = "(1E3 kg)"vlabel(2, 16 * 2) = "(1E3 kg)"vlabel(2, 17 * 2) = "(1E3 kg)"vlabel(2, 18 * 2) = "(1E3 kg)"vlabel(2, 19 * 2) = "(1E3 kg)"vlabel(2, 20 * 2) = "(1E3 kg)"vlabel(2, 21 * 2) = "(1E3 kg)"vlabel(2, 22 * 2) = "(1E3 kg)"vlabel(2, 23 * 2) = "(1E3 kg) (diss)"vlabel(2, 24 * 2) = "(1E3 kg) (diss)"vlabel(2, 25 * 2) = "(1E3 kg)"vlabel(2, 1 * 2 - 1) = " "vlabel(2, 2 * 2 - 1) = " "vlabel(2, 3 * 2 - 1) = "(cm)"vlabel(2, 4 * 2 - 1) = "(cm)"vlabel(2, 5 * 2 - 1) = "(cm)"vlabel(2, 6 * 2 - 1) = "(cm)"vlabel(2, 7 * 2 - 1) = "(cm)"vlabel(2, 8 * 2 - 1) = "(1E6 kg)"vlabel(2, 9 * 2 - 1) = "(1E6 kg)"vlabel(2, 10 * 2 - 1) = "(1E3 kg)"vlabel(2, 11 * 2 - 1) = "(1E3 kg)"vlabel(2, 12 * 2 - 1) = "(1E3 kg)"vlabel(2, 13 * 2 - 1) = "(1E3 kg) (diss)"vlabel(2, 14 * 2 - 1) = "(1E3 kg) (diss)"vlabel(2, 15 * 2 - 1) = "(1E3 kg)"vlabel(1, 16 * 2 - 1) = "(1E3 kg)"vlabel(1, 17 * 2 - 1) = "(1E3 kg)"vlabel(2, 18 * 2 - 1) = "(1E3 kg)"vlabel(2, 19 * 2 - 1) = "(1E3 kg)"vlabel(2, 20 * 2 - 1) = "(1E3 kg)"vlabel(2, 21 * 2 - 1) = "(1E3 kg)"vlabel(2, 22 * 2 - 1) = "(1E3 kg)"vlabel(2, 23 * 2 - 1) = "(1E3 kg) (diss)"vlabel(2, 24 * 2 - 1) = "(1E3 kg) (diss)"vlabel(2, 25 * 2 - 1) = "(1E3 kg)" If trajectory Then For l = 1 To nlu vlabel(1, (25 + l) * 2 - 1) = landuse$(l, 12) vlabel(2, (25 + l) * 2 - 1) = "(ha)" Next l End IfWorksheets("uncertainty").ActivateSet labelout = Range(Cells(1, 2), Cells(2, (25 + nlu) * 2 - 1))labelout.value = vlabelSet rangeout = Range(Cells(3, 2), Cells(nyrs + 3 - 1, (25 + nlu) * 2 - 1))
rangeout.value = voutWorksheets("uncertainty").Cells.NumberFormat = "General"End If 'y=nyrsEnd If ' n=nrealNext yEnd Sub
Sub drivestoch(opt As Integer)Dim p1 As Singlestarttime = Timernyrs = Worksheets("notes and parameters").Cells(9, 4).valueWorksheets("Stochastic").ActivateCells(3, 7).SelectRange(Selection, Selection.End(xlToRight)).Selectnreal = Selection.Countp1 = 1#For iter = 1 To nreal
Worksheets("notes and parameters").Cells(1, 10).value = "iteration #:"Worksheets("notes and parameters").Cells(1, 11).value = iterCall gwlf4(opt, False, True) ' optimize=false, unsens=trueWorksheets("uncertainty").Cells(1, 1).value = "finished iteration:"Worksheets("uncertainty").Cells(2, 1).value = iterCall calcmeans(iter, nreal)Next iterendtime = TimerWorksheets("uncertainty").Cells(1, 3) = "runtime="Worksheets("uncertainty").Cells(1, 4) = Format(endtime - starttime, "0.0")End Sub
Sub getstochparms(n)Dim cell As Range, prange As RangeDim plist(20) As Double
Worksheets("Stochastic").Activate
nrur = Worksheets("stochastic").Cells(3, n + 6).value nurb = Worksheets("stochastic").Cells(4, n + 6).valuenlu = nrur + nurbIf n = 1 ThenReDim vblarray(nyrs, 21 + nlu) As Double, vblmean(nyrs, 21 + nlu) As DoubleReDim vblm2(nyrs, 21 + nlu) As Double, vlabel(2, (21 + nlu) * 2 - 1) As StringEnd IfRECESSCOEF = Worksheets("stochastic").Cells(5, n + 6).valueSEEPCOEF = Worksheets("stochastic").Cells(6, n + 6).valueUNSATSTOR = Worksheets("stochastic").Cells(7, n + 6).valueSATSTOR = Worksheets("stochastic").Cells(8, n + 6).valuesnow = Worksheets("stochastic").Cells(9, n + 6).valueSEDELRATIO = Worksheets("stochastic").Cells(10, n + 6).valuegwdenitfrac = Worksheets("stochastic").Cells(11, n + 6).valueFor k = 1 To 5
ANTMOIST(k) = Worksheets("stochastic").Cells(11 + k, n + 6).valueNext k
For i = 1 To 12month$(i, 4) = Worksheets("stochastic").Cells(16 + i, 1).value
cv(i) = Worksheets("stochastic").Cells(16 + i, n + 6).valuedayhrs(i) = Worksheets("stochastic").Cells(16 + 12 + i, n + 6).valuegrow(i) = Worksheets("stochastic").Cells(16 + 24 + i, n + 6).valueacoef(i) = Worksheets("stochastic").Cells(16 + 36 + i, n + 6).valueNext iFor l = 1 To nlulanduse$(l, 12) = Worksheets("stochastic").Cells(64 + l, 1).value
AREA(l) = Worksheets("stochastic").Cells(64 + l, n + 6).valueCN(2, l) = Worksheets("stochastic").Cells(64 + nlu + l, n + 6).valueKLSCP(l) = Worksheets("stochastic").Cells(64 + 2 * nlu + l, n + 6).valueareafinal(l) = Worksheets("stochastic").Cells(64 + 3 * nlu + l, n + 6).valuedenitfrac(l) = Worksheets("stochastic").Cells(64 + 4 * nlu + l, n + 6).valueNext l
watershedname$ = Worksheets("stochastic").Cells(65 + 5 * nlu, 2).value
sednitr = Worksheets("stochastic").Cells(66 + 5 * nlu, n + 6).valuesedphos = Worksheets("stochastic").Cells(67 + 5 * nlu, n + 6).valuegrnitrconc = Worksheets("stochastic").Cells(68 + 5 * nlu, n + 6).valuegrphosconc = Worksheets("stochastic").Cells(69 + 5 * nlu, n + 6).valuendeptot = Worksheets("stochastic").Cells(70 + 5 * nlu, n + 6).valuepdry = Worksheets("stochastic").Cells(71 + 5 * nlu, n + 6).valuemanuredareas = Worksheets("stochastic").Cells(72 + 5 * nlu, n + 6).valuefirstmanuremonth = Worksheets("stochastic").Cells(73 + 5 * nlu, n + 6).valuelastmanuremonth = Worksheets("stochastic").Cells(74 + 5 * nlu, n + 6).value
For l = 1 To nlu
If l <= nrur Thennitrconc(l) = Worksheets("stochastic").Cells(74 + 5 * nlu + l, n + 6).valuephosconc(l) = Worksheets("stochastic").Cells(74 + 6 * nlu + l, n + 6).valueElseurbannitr(l) = Worksheets("stochastic").Cells(74 + 5 * nlu + l, n + 6).valueurbanphos(l) = Worksheets("stochastic").Cells(74 + 6 * nlu + l, n + 6).valueEnd IfNext l
manuredareas = Worksheets("stochastic").Cells(75 + 7 * nlu, n + 6).value
For l = 1 To manuredareasmannitr(l) = Worksheets("stochastic").Cells(75 + 7 * nlu + l, n + 6).valuemanphos(l) = Worksheets("stochastic").Cells(75 + 7 * nlu + l + manuredareas, n + 6).valueNext l
For i = 1 To 12
pointnitr(i) = Worksheets("stochastic").Cells(75 + 7 * nlu + 2 * manuredareas + i, n + 6).valuepointphos(i) = Worksheets("stochastic").Cells(87 + 7 * nlu + 2 * manuredareas + i, n + 6).valueNext i
dsflag = Worksheets("stochastic").Cells(100 + 7 * nlu + 2 * manuredareas, n + 6).value
If dsflag = 1 Or dsflag = 3 Then ' handle septic system data and any multiyear point source data
For i = 1 To 12 For j = 1 To 4 a(j, i) = Worksheets("stochastic").Cells(100 + (j - 1) * 12 + 7 * nlu + 2 * manuredareas + i, n + 6).value Next j Next i
eloadN = Worksheets("stochastic").Cells(149 + 7 * nlu + 2 * manuredareas, n + 6).valueeloadP = Worksheets("stochastic").Cells(150 + 7 * nlu + 2 * manuredareas, n + 6).valueupN = Worksheets("stochastic").Cells(151 + 7 * nlu + 2 * manuredareas, n + 6).valueupP = Worksheets("stochastic").Cells(152 + 7 * nlu + 2 * manuredareas, n + 6).valueEnd If
If dsflag = 2 Or dsflag = 3 Then ' handle any multiyear point source data dsoff = 152 + 7 * nlu + 2 * manuredareas For y = 2 To nyrs For i = 1 To 12
pointnitr(12 * (y - 1) + i) = Worksheets("stochastic").Cells(dsoff + 12 * (y - 2) + i, n + 6).value pointphos(12 * (y - 1) + i) = Worksheets("stochastic").Cells(dsoff + 12 * (nyrs - 2) + 12 * (y - 2) + i, n + 6).value
Next i Next yEnd If
End SubSub fillmeanparamsinstoch()Dim rangeout As RangeCall readtransportworksheet("transport")Call readnutrientworksheet("nutrient")Worksheets("Stochastic").ActivateSet rangeout = Range(Cells(3, 1), Cells(257, 5))rangeout.ClearWorksheets("stochastic").Cells(3, 2).value = "Number of rural landuses"Worksheets("stochastic").Cells(4, 2).value = "Number of urban landuses"
Worksheets("stochastic").Cells(3, 3).value = nrurWorksheets("stochastic").Cells(4, 3).value = nurb
Worksheets("stochastic").Cells(5, 2).value = "recession coefficient"Worksheets("stochastic").Cells(6, 2).value = "seepage coefficient"Worksheets("stochastic").Cells(7, 2).value = "init unsat storage,(cm)"
Worksheets("stochastic").Cells(8, 2).value = "init sat storage,(cm)"
Worksheets("stochastic").Cells(9, 2).value = "init snow,(cm)"
Worksheets("stochastic").Cells(10, 2).value = "sed delivery ratio"Worksheets("stochastic").Cells(11, 2).value = "gw denitrification frac"
Worksheets("stochastic").Cells(5, 3).value = RECESSCOEFWorksheets("stochastic").Cells(6, 3).value = SEEPCOEFWorksheets("stochastic").Cells(7, 3).value = UNSATSTORWorksheets("stochastic").Cells(8, 3).value = SATSTORWorksheets("stochastic").Cells(9, 3).value = snowWorksheets("stochastic").Cells(10, 3).value = SEDELRATIOWorksheets("stochastic").Cells(11, 3).value = gwdenitfracFor k = 1 To 5Worksheets("stochastic").Cells(11 + k, 2).value = "antecedent moisture for initial 5 days"Worksheets("stochastic").Cells(11 + k, 3).value = ANTMOIST(k)Next k
For i = 1 To 12Worksheets("stochastic").Cells(16 + i, 1).value = month$(i, 4)Worksheets("stochastic").Cells(16 + i, 2).value = "ET cover factor"Worksheets("stochastic").Cells(16 + 12 + i, 1).value = month$(i, 4)Worksheets("stochastic").Cells(16 + 12 + i, 2).value = "daylight hrs"Worksheets("stochastic").Cells(16 + 24 + i, 1).value = month$(i, 4)Worksheets("stochastic").Cells(16 + 24 + i, 2).value = "growing season"Worksheets("stochastic").Cells(16 + 36 + i, 1).value = month$(i, 4)Worksheets("stochastic").Cells(16 + 36 + i, 2).value = "erosivity coeff."
Worksheets("stochastic").Cells(16 + i, 3).value = cv(i)Worksheets("stochastic").Cells(16 + 12 + i, 3).value = dayhrs(i)Worksheets("stochastic").Cells(16 + 24 + i, 3).value = grow(i)Worksheets("stochastic").Cells(16 + 36 + i, 3).value = acoef(i)Next iFor l = 1 To nluWorksheets("stochastic").Cells(64 + l, 1).value = landuse$(l, 12)Worksheets("stochastic").Cells(64 + l, 2).value = "Initial land area"Worksheets("stochastic").Cells(64 + nlu + l, 1).value = landuse$(l, 12)Worksheets("stochastic").Cells(64 + nlu + l, 2).value = "runoff curve no"Worksheets("stochastic").Cells(64 + 2 * nlu + l, 1).value = landuse$(l, 12)Worksheets("stochastic").Cells(64 + 2 * nlu + l, 2).value = "USLE parameters"Worksheets("stochastic").Cells(64 + 3 * nlu + l, 1).value = landuse$(l, 12)Worksheets("stochastic").Cells(64 + 3 * nlu + l, 2).value = "Final land area"Worksheets("stochastic").Cells(64 + 4 * nlu + l, 1).value = landuse$(l, 12)Worksheets("stochastic").Cells(64 + 4 * nlu + l, 2).value = "fraction denitrification"
Worksheets("stochastic").Cells(64 + l, 3).value = AREA(l)Worksheets("stochastic").Cells(64 + nlu + l, 3).value = CN(2, l)Worksheets("stochastic").Cells(64 + 2 * nlu + l, 3).value = KLSCP(l)Worksheets("stochastic").Cells(64 + 3 * nlu + l, 3).value = areafinal(l)Worksheets("stochastic").Cells(64 + 4 * nlu + l, 3).value = denitfrac(l)Next l
If watershedname$ <> "" ThenWorksheets("stochastic").Cells(65 + 5 * nlu, 2).value = watershedname$ElseWorksheets("stochastic").Cells(65 + 5 * nlu, 2).value = "stochastic analysis"EndWorksheets("stochastic").Cells(66 + 5 * nlu, 2).value = "sediment N"
Worksheets("stochastic").Cells(67 + 5 * nlu, 2).value = "sediment P"Worksheets("stochastic").Cells(68 + 5 * nlu, 2).value = "GW N conc,(mg/L)"Worksheets("stochastic").Cells(69 + 5 * nlu, 2).value = "GW P conc,(mg/L)"Worksheets("stochastic").Cells(70 + 5 * nlu, 2).value = "Atmos N dep,(kg/ha/yr)"Worksheets("stochastic").Cells(71 + 5 * nlu, 2).value = "fraction dry dep"
Worksheets("stochastic").Cells(66 + 5 * nlu, 3).value = sednitrWorksheets("stochastic").Cells(67 + 5 * nlu, 3).value = sedphosWorksheets("stochastic").Cells(68 + 5 * nlu, 3).value = grnitrconcWorksheets("stochastic").Cells(69 + 5 * nlu, 3).value = grphosconcWorksheets("stochastic").Cells(70 + 5 * nlu, 3).value = ndeptotWorksheets("stochastic").Cells(71 + 5 * nlu, 3).value = pdry
Worksheets("stochastic").Cells(72 + 5 * nlu, 2).value = "# manured areas"Worksheets("stochastic").Cells(73 + 5 * nlu, 2).value = "firstmanuremonth"Worksheets("stochastic").Cells(74 + 5 * nlu, 2).value = "lastmanuremonth"
Worksheets("stochastic").Cells(72 + 5 * nlu, 3).value = manuredareasWorksheets("stochastic").Cells(73 + 5 * nlu, 3).value = firstmanuremonthWorksheets("stochastic").Cells(74 + 5 * nlu, 3).value = lastmanuremonth
For l = 1 To nlu
If l <= nrur ThenWorksheets("stochastic").Cells(74 + 5 * nlu + l, 1).value = landuse$(l, 12)Worksheets("stochastic").Cells(74 + 5 * nlu + l, 2).value = "N conc"Worksheets("stochastic").Cells(74 + 5 * nlu + l, 3).value = nitrconc(l)Worksheets("stochastic").Cells(74 + 6 * nlu + l, 1).value = landuse$(l, 12)Worksheets("stochastic").Cells(74 + 6 * nlu + l, 2).value = "P conc"Worksheets("stochastic").Cells(74 + 6 * nlu + l, 3).value = phosconc(l)ElseWorksheets("stochastic").Cells(74 + 5 * nlu + l, 1).value = landuse$(l, 12)Worksheets("stochastic").Cells(74 + 5 * nlu + l, 2).value = "N conc"Worksheets("stochastic").Cells(74 + 5 * nlu + l, 3).value = urbannitr(l)Worksheets("stochastic").Cells(74 + 6 * nlu + l, 1).value = landuse$(l, 12)Worksheets("stochastic").Cells(74 + 6 * nlu + l, 2).value = "P conc"Worksheets("stochastic").Cells(74 + 6 * nlu + l, 3).value = urbanphos(l)End IfNext l
Worksheets("stochastic").Cells(75 + 7 * nlu, 2).value = "manured areas"Worksheets("stochastic").Cells(75 + 7 * nlu, 3).value = manuredareas
For l = 1 To manuredareasWorksheets("stochastic").Cells(75 + 7 * nlu + l, 1).value = "man N load"Worksheets("stochastic").Cells(75 + 7 * nlu + l, 2).value = lWorksheets("stochastic").Cells(75 + 7 * nlu + l, 3).value = mannitr(l)Worksheets("stochastic").Cells(75 + 7 * nlu + l + manuredareas, 1).value = "man P load"Worksheets("stochastic").Cells(75 + 7 * nlu + l + manuredareas, 2).value = lWorksheets("stochastic").Cells(75 + 7 * nlu + l + manuredareas, 3).value = manphos(l)Next l
For i = 1 To 12Worksheets("stochastic").Cells(75 + 7 * nlu + 2 * manuredareas + i, 1).value = month$(i, 4)Worksheets("stochastic").Cells(75 + 7 * nlu + 2 * manuredareas + i, 2).value = "Pt src N load"Worksheets("stochastic").Cells(75 + 7 * nlu + 2 * manuredareas + i, 3).value = pointnitr(i)
Worksheets("stochastic").Cells(87 + 7 * nlu + 2 * manuredareas + i, 1).value = month$(i, 4)Worksheets("stochastic").Cells(87 + 7 * nlu + 2 * manuredareas + i, 2).value = "Pt src P load"Worksheets("stochastic").Cells(87 + 7 * nlu + 2 * manuredareas + i, 3).value = pointphos(i)Next i
Worksheets("stochastic").Cells(100 + 7 * nlu + 2 * manuredareas, 2).value = "dsflag"Worksheets("stochastic").Cells(100 + 7 * nlu + 2 * manuredareas, 3).value = dsflag
'If dsflag = 1 Or dsflag = 3 Then ' handle septic system data and any multiyear point source data
For i = 1 To 12Worksheets("stochastic").Cells(100 + 7 * nlu + 2 * manuredareas + i, 1).value = month$(i, 4)Worksheets("stochastic").Cells(100 + 7 * nlu + 2 * manuredareas + i, 2).value = "Pop served by Normal septic systems"Worksheets("stochastic").Cells(100 + 7 * nlu + 2 * manuredareas + i, 3).value = a(1, i)Worksheets("stochastic").Cells(112 + 7 * nlu + 2 * manuredareas + i, 1).value = month$(i, 4)Worksheets("stochastic").Cells(112 + 7 * nlu + 2 * manuredareas + i, 2).value = "Pop served by Short circuit systems"Worksheets("stochastic").Cells(112 + 7 * nlu + 2 * manuredareas + i, 3).value = a(2, i)Worksheets("stochastic").Cells(124 + 7 * nlu + 2 * manuredareas + i, 1).value = month$(i, 4)Worksheets("stochastic").Cells(124 + 7 * nlu + 2 * manuredareas + i, 2).value = "Pop served by Ponding septic systems"Worksheets("stochastic").Cells(124 + 7 * nlu + 2 * manuredareas + i, 3).value = a(3, i)Worksheets("stochastic").Cells(136 + 7 * nlu + 2 * manuredareas + i, 1).value = month$(i, 4)Worksheets("stochastic").Cells(136 + 7 * nlu + 2 * manuredareas + i, 2).value = "Pop served by Direct Discharge systems"Worksheets("stochastic").Cells(136 + 7 * nlu + 2 * manuredareas + i, 3).value = a(4, i)
Next i Worksheets("stochastic").Cells(149 + 7 * nlu + 2 * manuredareas, 2).value = "Percapita tank effluent - N (g/day)" Worksheets("stochastic").Cells(150 + 7 * nlu + 2 * manuredareas, 2).value = "Percapita tank effluent - P (g/day)" Worksheets("stochastic").Cells(151 + 7 * nlu + 2 * manuredareas, 2).value = "Per capita growing season uptake - N(g/day)" Worksheets("stochastic").Cells(152 + 7 * nlu + 2 * manuredareas, 2).value = "Per capita growing season uptake - P(g/day)"Worksheets("stochastic").Cells(149 + 7 * nlu + 2 * manuredareas, 3).value = eloadN Worksheets("stochastic").Cells(150 + 7 * nlu + 2 * manuredareas, 3).value = eloadP Worksheets("stochastic").Cells(151 + 7 * nlu + 2 * manuredareas, 3).value = upN Worksheets("stochastic").Cells(152 + 7 * nlu + 2 * manuredareas, 3).value = upP'End If
If dsflag = 2 Or dsflag = 3 Then ' handle any multiyear point source data dsoff = 152 + 7 * nlu + 2 * manuredareas For y = 2 To nyrs For i = 1 To 12 Worksheets("stochastic").Cells(dsoff + 12 * (y - 2) + i, 1).value = month$(i, 4) Worksheets("stochastic").Cells(dsoff + 12 * (y - 2) + i, 2).value = "Pt src N load"
Worksheets("stochastic").Cells(dsoff + 12 * (y - 2) + i, 3).value = pointnitr(12 * (y - 1) + i) Worksheets("stochastic").Cells(dsoff + 12 * (nyrs - 2) + 12 * (y - 2) + i, 1).value = month$(i, 4) Worksheets("stochastic").Cells(dsoff + 12 * (nyrs - 2) + 12 * (y - 2) + i, 2).value = "Pt src P load" Worksheets("stochastic").Cells(dsoff + 12 * (nyrs - 2) + 12 * (y - 2) + i, 3).value = pointphos(12 * (y - 1) + i)
Next i Next yEnd If
Worksheets("stochastic").Cells.NumberFormat = "General"
End SubSub fillmeanparamsinstoch2()Dim rangeout As RangeDim temparray(260, 5) As Variant
Call readtransportworksheet("transport")Call readnutrientworksheet("nutrient")Worksheets("Stochastic").ActivateSet rangeout = Range(Cells(3, 1), Cells(262, 5))rangeout.Cleartemparray(1, 2) = "Number of rural landuses"temparray(2, 2) = "Number of urban landuses"
temparray(1, 3) = nrurtemparray(2, 3) = nurb
temparray(3, 2) = "recession coefficient"temparray(4, 2) = "seepage coefficient"temparray(5, 2) = "init unsat storage,(cm)"
temparray(6, 2) = "init sat storage,(cm)"
temparray(7, 2) = "init snow,(cm)"
temparray(8, 2) = "sed delivery ratio"temparray(9, 2) = "gw denit fraction"
temparray(3, 3) = RECESSCOEFtemparray(4, 3) = SEEPCOEFtemparray(5, 3) = UNSATSTORtemparray(6, 3) = SATSTORtemparray(7, 3) = snowtemparray(8, 3) = SEDELRATIOtemparray(9, 3) = gwdenitfracFor k = 1 To 5temparray(9 + k, 2) = "antecedent moisture for initial 5 days"temparray(9 + k, 3) = ANTMOIST(k)Next k
For i = 1 To 12temparray(14 + i, 1) = month$(i, 4)temparray(14 + i, 2) = "ET cover factor"temparray(14 + 12 + i, 1) = month$(i, 4)temparray(14 + 12 + i, 2) = "daylight hrs"temparray(14 + 24 + i, 1) = month$(i, 4)
temparray(14 + 24 + i, 2) = "growing season"temparray(14 + 36 + i, 1) = month$(i, 4)temparray(14 + 36 + i, 2) = "erosivity coeff."
temparray(14 + i, 3) = cv(i)temparray(14 + 12 + i, 3) = dayhrs(i)temparray(14 + 24 + i, 3) = grow(i)temparray(14 + 36 + i, 3) = acoef(i)Next iFor l = 1 To nlutemparray(62 + l, 1) = landuse$(l, 12)temparray(62 + l, 2) = "Initial land area"temparray(62 + nlu + l, 1) = landuse$(l, 12)temparray(62 + nlu + l, 2) = "runoff curve no"temparray(62 + 2 * nlu + l, 1) = landuse$(l, 12)temparray(62 + 2 * nlu + l, 2) = "USLE parameters"temparray(62 + 3 * nlu + l, 1) = landuse$(l, 12)temparray(62 + 3 * nlu + l, 2) = "Final land area"temparray(62 + 4 * nlu + l, 1) = landuse$(l, 12)temparray(62 + 4 * nlu + l, 2) = "Denit fraction"
temparray(62 + l, 3) = AREA(l)temparray(62 + nlu + l, 3) = CN(2, l)temparray(62 + 2 * nlu + l, 3) = KLSCP(l)temparray(62 + 3 * nlu + l, 3) = areafinal(l)temparray(62 + 4 * nlu + l, 3) = denitfrac(l)Next l
If watershedname$ <> "" Thentemparray(63 + 5 * nlu, 2) = watershedname$Elsetemparray(63 + 5 * nlu, 2) = "stochastic analysis"End Iftemparray(64 + 5 * nlu, 2) = "sediment N"temparray(65 + 5 * nlu, 2) = "sediment P"temparray(66 + 5 * nlu, 2) = "GW N conc,(mg/L)"temparray(67 + 5 * nlu, 2) = "GW P conc,(mg/L)"temparray(68 + 5 * nlu, 2) = "Atmos dep N,(kg/ha/yr)"temparray(69 + 5 * nlu, 2) = "fraction dry dep"
temparray(64 + 5 * nlu, 3) = sednitrtemparray(65 + 5 * nlu, 3) = sedphostemparray(66 + 5 * nlu, 3) = grnitrconctemparray(67 + 5 * nlu, 3) = grphosconctemparray(68 + 5 * nlu, 3) = ndeptottemparray(69 + 5 * nlu, 3) = pdry
temparray(70 + 5 * nlu, 2) = "# manured areas"temparray(71 + 5 * nlu, 2) = "firstmanuremonth"temparray(72 + 5 * nlu, 2) = "lastmanuremonth"
temparray(70 + 5 * nlu, 3) = manuredareastemparray(71 + 5 * nlu, 3) = firstmanuremonthtemparray(72 + 5 * nlu, 3) = lastmanuremonth
For l = 1 To nlu
If l <= nrur Thentemparray(72 + 5 * nlu + l, 1) = landuse$(l, 12)temparray(72 + 5 * nlu + l, 2) = "N conc"temparray(72 + 5 * nlu + l, 3) = nitrconc(l)temparray(72 + 6 * nlu + l, 1) = landuse$(l, 12)temparray(72 + 6 * nlu + l, 2) = "P conc"temparray(72 + 6 * nlu + l, 3) = phosconc(l)Elsetemparray(72 + 5 * nlu + l, 1) = landuse$(l, 12)temparray(72 + 5 * nlu + l, 2) = "N conc"temparray(72 + 5 * nlu + l, 3) = urbannitr(l)temparray(72 + 6 * nlu + l, 1) = landuse$(l, 12)temparray(72 + 6 * nlu + l, 2) = "P conc"temparray(72 + 6 * nlu + l, 3) = urbanphos(l)End IfNext l
temparray(73 + 7 * nlu, 2) = "manured areas"temparray(73 + 7 * nlu, 3) = manuredareas
For l = 1 To manuredareastemparray(73 + 7 * nlu + l, 1) = "man N load"temparray(73 + 7 * nlu + l, 2) = ltemparray(73 + 7 * nlu + l, 3) = mannitr(l)temparray(73 + 7 * nlu + l + manuredareas, 1) = "man P load"temparray(73 + 7 * nlu + l + manuredareas, 2) = ltemparray(73 + 7 * nlu + l + manuredareas, 3) = manphos(l)Next l
For i = 1 To 12temparray(73 + 7 * nlu + 2 * manuredareas + i, 1) = month$(i, 4)temparray(73 + 7 * nlu + 2 * manuredareas + i, 2) = "Pt src N load"temparray(73 + 7 * nlu + 2 * manuredareas + i, 3) = pointnitr(i)
temparray(85 + 7 * nlu + 2 * manuredareas + i, 1) = month$(i, 4)temparray(85 + 7 * nlu + 2 * manuredareas + i, 2) = "Pt src P load"temparray(85 + 7 * nlu + 2 * manuredareas + i, 3) = pointphos(i)Next i
temparray(98 + 7 * nlu + 2 * manuredareas, 2) = "dsflag"temparray(98 + 7 * nlu + 2 * manuredareas, 3) = dsflag
'If dsflag = 1 Or dsflag = 3 Then ' handle septic system data and any multiyear point source data
For i = 1 To 12temparray(98 + 7 * nlu + 2 * manuredareas + i, 1) = month$(i, 4)temparray(98 + 7 * nlu + 2 * manuredareas + i, 2) = "Pop served by Normal septic systems"temparray(98 + 7 * nlu + 2 * manuredareas + i, 3) = a(1, i)temparray(110 + 7 * nlu + 2 * manuredareas + i, 1) = month$(i, 4)
temparray(110 + 7 * nlu + 2 * manuredareas + i, 2) = "Pop served by Short circuit systems"temparray(110 + 7 * nlu + 2 * manuredareas + i, 3) = a(2, i)temparray(122 + 7 * nlu + 2 * manuredareas + i, 1) = month$(i, 4)temparray(122 + 7 * nlu + 2 * manuredareas + i, 2) = "Pop served by Ponding septic systems"temparray(122 + 7 * nlu + 2 * manuredareas + i, 3) = a(3, i)temparray(134 + 7 * nlu + 2 * manuredareas + i, 1) = month$(i, 4)temparray(134 + 7 * nlu + 2 * manuredareas + i, 2) = "Pop served by Direct Discharge systems"temparray(134 + 7 * nlu + 2 * manuredareas + i, 3) = a(4, i)
Next i temparray(147 + 7 * nlu + 2 * manuredareas, 2) = "Percapita tank effluent - N (g/day)" temparray(148 + 7 * nlu + 2 * manuredareas, 2) = "Percapita tank effluent - P (g/day)" temparray(149 + 7 * nlu + 2 * manuredareas, 2) = "Per capita growing season uptake - N(g/day)" temparray(150 + 7 * nlu + 2 * manuredareas, 2) = "Per capita growing season uptake - P(g/day)" temparray(147 + 7 * nlu + 2 * manuredareas, 3) = eloadN temparray(148 + 7 * nlu + 2 * manuredareas, 3) = eloadP temparray(149 + 7 * nlu + 2 * manuredareas, 3) = upN temparray(150 + 7 * nlu + 2 * manuredareas, 3) = upP'End If
If dsflag = 2 Or dsflag = 3 Then ' handle any multiyear point source data dsoff = 150 + 7 * nlu + 2 * manuredareas For y = 2 To nyrs For i = 1 To 12 temparray(dsoff + 12 * (y - 2) + i, 1) = month$(i, 4) temparray(dsoff + 12 * (y - 2) + i, 2) = "Pt src N load" temparray(dsoff + 12 * (y - 2) + i, 3) = pointnitr(12 * (y - 1) + i) temparray(dsoff + 12 * (nyrs - 2) + 12 * (y - 2) + i, 1) = month$(i, 4) temparray(dsoff + 12 * (nyrs - 2) + 12 * (y - 2) + i, 2) = "Pt src P load" temparray(dsoff + 12 * (nyrs - 2) + 12 * (y - 2) + i, 3) = pointphos(12 * (y - 1) + i)
Next i Next yEnd Ifrangeout.value = temparraySet rangeout = Range(Cells(3, 1), Cells(156 + 6 * nlu + 2 * manuredareas, 5))Call colorrange(rangeout, 36)Worksheets("stochastic").Cells.NumberFormat = "General"
End SubSub colorrange(rangetofill As Range, fillcolor As Integer, Optional lineweight As Integer)' lineweight must be 1,2,3 or 4 and increases in proportion to magnitude' color = 0 is white'xlthin = 2'xlNone=-4142'xlsolid = 6'xlautomatic = -4105
rangetofill.Borders(xlDiagonalDown).LineStyle = xlNone rangetofill.Borders(xlDiagonalUp).LineStyle = xlNone rangetofill.Borders(xlEdgeLeft).LineStyle = xlNone rangetofill.Borders(xlEdgeTop).LineStyle = xlNone rangetofill.Borders(xlEdgeBottom).LineStyle = xlNone rangetofill.Borders(xlEdgeRight).LineStyle = xlNone rangetofill.Borders(xlInsideVertical).LineStyle = xlNone rangetofill.Borders(xlInsideHorizontal).LineStyle = xlNone
With rangetofill.Interior .ColorIndex = fillcolor .Pattern = xlSolid .PatternColorIndex = xlAutomatic End With LS = 1 '=xlcontinuousIf lineweight = 0 Then ' ie optional lineweight is missing
Call clearborders(rangetofill)Else With rangetofill.Borders(xlEdgeLeft) .LineStyle = LS .Weight = lineweight .ColorIndex = xlAutomatic End With With rangetofill.Borders(xlEdgeTop) .LineStyle = LS .Weight = lineweight .ColorIndex = xlAutomatic End With With rangetofill.Borders(xlEdgeBottom) .LineStyle = LS .Weight = lineweight .ColorIndex = xlAutomatic End With With rangetofill.Borders(xlEdgeRight) .LineStyle = LS .Weight = lineweight .ColorIndex = xlAutomatic End With With rangetofill.Borders(xlInsideVertical) .LineStyle = LS .Weight = lineweight .ColorIndex = xlAutomatic End With With rangetofill.Borders(xlInsideHorizontal) .LineStyle = LS .Weight = lineweight .ColorIndex = xlAutomatic End With End IfEnd SubSub clearborders(rangetoclear As Range)' removes any lines in borders from indicated range rangetoclear.Borders(xlDiagonalDown).LineStyle = xlNone rangetoclear.Borders(xlDiagonalUp).LineStyle = xlNone rangetoclear.Borders(xlEdgeLeft).LineStyle = xlNone rangetoclear.Borders(xlEdgeTop).LineStyle = xlNone rangetoclear.Borders(xlEdgeBottom).LineStyle = xlNone rangetoclear.Borders(xlEdgeRight).LineStyle = xlNone rangetoclear.Borders(xlInsideVertical).LineStyle = xlNone rangetoclear.Borders(xlInsideHorizontal).LineStyle = xlNoneEnd Sub