static void _MDDischMean (int itemID) { int nSteps; float accumDisch; float dischMean; accumDisch = MFVarGetFloat (_MDInAccumDischargeID, itemID, 0.0); nSteps = MFVarGetInt (_MDInAvgNStepsID, itemID, 0); dischMean = MFVarGetFloat (_MDOutDischMeanID, itemID, 0.0); dischMean = (float) (((double) dischMean * (double) nSteps + accumDisch) / ((double) (nSteps + 1))); MFVarSetFloat (_MDOutDischMeanID, itemID, dischMean); }
static void _MDRunoffMean (int itemID) { int nSteps; float runoff; float runoffMean; runoff = MFVarGetFloat (_MDInRunoffID, itemID, 0.0); nSteps = MFVarGetInt (_MDInAvgNStepsID, itemID, 0); runoffMean = MFVarGetFloat (_MDOutRunoffMeanID, itemID, 0.0); runoffMean = (float) (((double) runoffMean * (double) nSteps + runoff) / ((double) (nSteps + 1))); MFVarSetFloat (_MDOutRunoffMeanID, itemID, runoffMean); }
static void _MDCParamZ0g (int itemID) { // Input int cover; // Local static float lookup [] = { 0.02, 0.02, 0.02, 0.01, 0.01, 0.005, 0.001, 0.001 }; cover = MFVarGetInt (_MDInCoverID, itemID, 7); // defaulting missing value to water. if ((cover < 0) || (cover >= (int) (sizeof (lookup) / sizeof (lookup [0])))) { CMmsgPrint (CMmsgWarning,"Warning: Invalid cover [%d] in: %s:%d\n",cover,__FILE__,__LINE__); return; } MFVarSetFloat (_MDOutCParamZ0gID,itemID, lookup [cover]); }
static void _MDCParamAlbedo (int itemID) { // Input int cover; float snowPack; // Local static float albedo [] = { 0.14, 0.18, 0.18, 0.20, 0.20, 0.22, 0.26, 0.10 }; static float albedoSnow [] = { 0.14, 0.23, 0.35, 0.50, 0.50, 0.50, 0.50, 0.50 }; cover = MFVarGetInt (_MDInCoverID, itemID, 7); // defaulting missing value to water. if ((cover < 0) || (cover >= (int) (sizeof (albedo) / sizeof (albedo [0])))) { CMmsgPrint (CMmsgWarning,"Warning: Invalid cover [%d] in: %s:%d\n",cover,__FILE__,__LINE__); return; } snowPack = MFVarGetFloat (_MDInSnowPackID, itemID, 0.0); MFVarSetFloat (_MDOutCParamAlbedoID,itemID,snowPack > 0.0 ? albedoSnow[cover] : albedo[cover]); }
static void _MDRiverLight (int itemID) { float discharge; float channelDepth; float channelWidth; float channelWidthMean; float width_canopy; // mean width of overhanging trees from each bank float canopy_proportion = 0.7; // proportion of total bank length (stream length * 2) with canopy float LAI = 0.9; // proportion of light shaded out float DOC; int koppen; //light parameters float solarRad; //MJ/m2/ float Ecan; float rad2par = 0.5; // proprotion of total radiation as PAR float reflectance = 0.9; //proportion of PAR transmitted across air-water interface float shading; float turbidity = 10; //NTU float Kturb = 0.177; // from Julian et al. 2008 float Kdoc = 0.05; //assumption: assume every mg/l of DOC leads to 1% attenuation (m-1) float Kd; // m-1 float par2bottom; float par2reach; discharge = MFVarGetFloat ( _MDInDischargeID, itemID, 0.0); solarRad = MFVarGetFloat ( _MDInSolarRadID, itemID, 0.0); //MJ/m2/d - CHECK UNITS - already accounts for clouds channelDepth = MFVarGetFloat ( _MDInRiverDepthID, itemID, 0.0); channelWidth = MFVarGetFloat ( _MDInRiverWidthID, itemID, 0.0); channelWidthMean = MFVarGetFloat (_MDInRiverbedWidthMeanID, itemID, 0.0); //assume mean width = bankfull width DOC = MFVarGetFloat (_MDInConcDOCID, itemID, 0.0) * 1000; //converts kg/m3 to mg/l koppen = MFVarGetInt (_MDInKoppenID, itemID, 0.0); // % TODO I think this is not % but category that should be read as integer FBM // KoppenID's : 1=Taiga, 2=semi-arid,3=tundra,4=temperate,5=tropics // Canopy width (m), assumption: Tundra=0,Taiga=2,Temperate=5,Semi-arid=0, WetTropics=10, DryTropics=0 switch (koppen) { // TODO Wil originally read this as float and tested for nan which should never happen particularly, when it is read as integer. case 1: width_canopy = 2; break; case 2: width_canopy = 0; break; case 3: width_canopy = 0; break; case 4: width_canopy = 5; break; case 5: width_canopy = 10; break; } Ecan = solarRad * rad2par; if (!isnan(discharge) && (channelWidthMean > 0) && (channelWidth > 0)){ if ((channelWidthMean / 2) < width_canopy){ shading = canopy_proportion * MDMinimum(1, LAI); } else { //proportion with canopy * proportion of half width not shaded // does not include effect of variable width within channel; need to scale down based on actual water surface area shading = canopy_proportion * (width_canopy / (channelWidthMean / 2)) * MDMinimum(1,LAI); } if (discharge > 0){ // turbidity = turbidity_const * pow(Q, turbidity_slope); Kd = Kturb * turbidity + Kdoc * DOC; //Figure 5; Julian et al. 2008 par2bottom = Ecan * (1 - shading) * reflectance * pow(2.71828, -1 * Kd * channelDepth); if (isnan(par2bottom)){ printf("Light: Q %f solarRad %f DOC %f Ecan %f shading %f reflectance %f Kd %f channelWidthMean %f channelDepth %f par2bottom %f \n", discharge, solarRad, DOC, Ecan, shading, reflectance, Kd, channelWidthMean, channelDepth, par2bottom); } } else { par2bottom = Ecan * shading; } par2reach = par2bottom * channelWidth * MFModelGetLength(itemID); //MJ/reach MFVarSetFloat(_MDPAR2BottomID, itemID, par2bottom); MFVarSetFloat(_MDPAR2ReachID, itemID, par2reach); } else{ MFVarSetFloat(_MDPAR2BottomID, itemID, 0.0); MFVarSetFloat(_MDPAR2ReachID, itemID, 0.0); } }
static void _MDQBARTpreprocess (int itemID) { int TimeStep,p,i,d,j; float Qday, Qbar,Qacc, Qbar_km3y,Qbar_m3s; float Qmax = 0; float Tday,Tbar,Tacc,A,T_time,T_old; float TupSlop,PixSize_km2; int n, nA; float mu, muA, M2, M2A, M3, M3A, del; float logQmax; float dummy; /*static int Max_itemID = 0;*/ //static int year_count=1; /*static float *PixelmaxQ;*/ /*static float **dailyQ; */ /*if (itemID > Max_itemID) {*/ /*Max_itemID=itemID+1;*/ /*printf("Max_itemID:%d\n",Max_itemID);*/ /*PixelmaxQ = (float*)malloc(Max_itemID*sizeof(float));*/ /*dailyQ = (float**) malloc(Max_itemID*sizeof(float*));*/ /*for (i = 1; i < Max_itemID+1; i++)*/ /*dailyQ[i] = (float*) malloc(366*sizeof(float));*/ /*for (i = 1; i < 366; i++)*/ /*for (j = 1; j < Max_itemID; j++)*/ /*dailyQ[j][i] = 0.0; */ /*} */ //printf("sizeof(Max_itemID):%d\n",(sizeof(PixelmaxQ)/sizeof(float))); /*static int pix=1;*/ /*static int day=1;*/ /*FILE * textfile;*/ //printf ("itemID:%d\n ", itemID); //printf("Max_itemID:%d\n",Max_itemID); if (MFDateGetDayOfYear() == 1) { MFVarSetFloat(_MDOutDischMaxID, itemID, 0.0); } Qday = MFVarGetFloat (_MDInDischargeID , itemID, 0.0); // in m3/s Qmax = MFVarGetFloat(_MDOutDischMaxID, itemID, 0.0); if (Qday > Qmax) { MFVarSetFloat(_MDOutDischMaxID, itemID, Qday); } else { MFVarSetFloat(_MDOutDischMaxID, itemID, Qmax); } if (MFDateGetDayOfYear() == 365) { //MFDateGetYearLength()) { Qmax = MFVarGetFloat(_MDOutDischMaxID, itemID, 0.0); logQmax = 0; if (Qmax > 0) logQmax = log10(Qmax); // online (onepass) variance calculation, continues in MDBankfullQcalc.c (T.Terriberry) nA = MFVarGetInt(_MDOutYearCountID, itemID, 0); n = nA + 1; muA = MFVarGetFloat(_MDOutMeanLogQMaxID, itemID, 0.0); M2A = MFVarGetFloat(_MDOutLogQMaxM2ID, itemID, 0.0); M3A = MFVarGetFloat(_MDOutLogQMaxM3ID, itemID, 0.0); del = logQmax - muA; mu = muA + del / n; M2 = M2A + (del * del * nA / n); M3 = M3A + (del * del * del * nA * (nA - 1) / (n * n)) + (3 * -M2A * del / n); MFVarSetInt(_MDOutYearCountID, itemID, n); MFVarSetFloat(_MDOutMeanLogQMaxID, itemID, mu); MFVarSetFloat(_MDOutLogQMaxM2ID, itemID, M2); MFVarSetFloat(_MDOutLogQMaxM3ID, itemID, M3); // call this just to make bankfull calcs and save vals dummy = MFVarGetFloat(_MDInBankfullQ5ID, itemID, 0.0); } // don't need to set missing vals to anything, dsAggregate annual in // postproc should just skip them. /*else {*/ /*MFVarSetInt(_MDOutYearsCountID, itemID, MFVarGetInt(_MDOutYearsCountID, itemID, 0));*/ /*MFVarSetFloat(_MDOutDischMaxMeanID, itemID, MFVarGetFloat(_MDOutDischMaxMeanID, itemID, 0.0));*/ /*}*/ //printf ("pix:%d\n ", pix); /*dailyQ[pix][day]=Qday;*/ //if (pix==1) printf ("pix=1. Qday=:%f\n ", Qday); /*pix++;*/ /*if (itemID==1){ //last pixelID*/ /*//printf ("day:%d\n ", day);*/ /*[>if (day==365){<]*/ /*if (MFDateGetDayOfYear() == MFDateGetYearLength()) {*/ /*printf("year count\n ");*/ /*//year_count++;*/ /*textfile = fopen("year_max_logQ.txt","a");*/ /*printf ("Writing to Scripts/year_max_logQ.txt\n");*/ /*for (p=1;p<Max_itemID;p++){*/ /*PixelmaxQ[p] = dailyQ[p][1];*/ /*//printf ("PixelmaxQ[p]:%f\n ", PixelmaxQ[p]);*/ /*for (d=2; d<366; d++){*/ /*if (PixelmaxQ[p] < dailyQ[p][d]) PixelmaxQ[p] = dailyQ[p][d];*/ /*}*/ /*if (PixelmaxQ[p]>0) PixelmaxQ[p]=log10(PixelmaxQ[p]);*/ /*fprintf (textfile,"%f ", PixelmaxQ[p]);*/ /*}*/ /*fprintf (textfile,"\n");*/ /*fclose (textfile);*/ /*Max_itemID = 0;*/ /*day = 0;*/ /*}*/ /*day++;*/ /*pix=1;*/ /*} */ //Geting the values of these parameters Qbar = MFVarGetFloat (_MDInDischMeanID , itemID, 0.0); // in m3/s Tday = MFVarGetFloat (_MDInAirTempID , itemID, 0.0); // in C // A = MFVarGetFloat (_MDInContributingAreaID, itemID, 0.0); //in km2 A = MFVarGetFloat (_MDInContributingAreaAccID, itemID, 0.0) + (MFModelGetArea (itemID)/(pow(1000,2)));// convert from m2 to km2 //calculating the contributing area MFVarSetFloat (_MDInContributingAreaAccID, itemID, A); PixSize_km2 =(MFModelGetArea(itemID)/pow(1000,2)); // printf("PixSize_km2: %f\n",PixSize_km2); // printf("A: %f\n",A); //Calculate Relief // LocalElev = MFVarGetFloat (_MDInElevationID , itemID, 0.0)/1000;//convert to km // printf("LocalElev: %f\n",LocalElev); // MaxElev = MFVarGetFloat (_MDOutElevationMaxID, itemID, 0.0); // if (A == PixSize_km2) MaxElev=LocalElev; // MFVarSetFloat (_MDOutElevationMaxID, itemID, -MaxElev); // MFVarSetFloat (_MDOutElevationMaxID, itemID, LocalElev); // printf("MaxElev: %f\n",MaxElev); // R = MaxElev-LocalElev; // printf("R: %f\n",R); MFVarSetInt (_MDOutBQART_AID, itemID, A); //MFVarSetInt (_MDOutBQART_RID, itemID, R); //Accumulate temperature TupSlop = MFVarGetFloat (_MDInAirTempAcc_spaceID, itemID, 0.0); T_old = MFVarGetFloat (_MDInAirTempAcc_timeID, itemID, 0.0) ; T_time = T_old+ Tday; Tacc = TupSlop + (T_time * PixSize_km2); MFVarSetFloat (_MDInAirTempAcc_spaceID, itemID, Tacc); MFVarSetFloat (_MDInAirTempAcc_timeID, itemID, T_time); //Accumulate discharge Qacc = (MFVarGetFloat (_MDInDischargeAccID, itemID, 0.0)+ Qday);// in m3/s MFVarSetFloat (_MDInDischargeAccID, itemID, Qacc); // Accumulate time steps TimeStep = (MFVarGetInt (_MDInTimeStepsID, itemID, 0.0)+1); MFVarSetInt (_MDInTimeStepsID, itemID, TimeStep); //Calculate moving avarege temperature (Tbar) and discharge Tbar = Tacc/TimeStep/A; Qbar_m3s = Qacc/TimeStep; //in m3/s MFVarSetFloat (_MDOutBQART_Qbar_m3sID, itemID, Qbar_m3s); if (Qbar_m3s > 0){ //convert to km3/y Qbar_km3y = (Qbar_m3s/(pow(1000,3)))*(365*24*60*60); }else Qbar_km3y = 0; // exporting outputs flux to files MFVarSetFloat (_MDOutBQART_TID, itemID, Tbar); MFVarSetFloat (_MDOutBQART_Qbar_km3yID, itemID, Qbar_km3y); }