static void _MDGrossRadianceOtto (int itemID) { // Input int day; float lambda; // Output float grossRad; // Local int hour; double eta, sigma,sinphi,sp,sbb,sotd; day = MFDateGetDayOfYear (); lambda = MFModelGetLatitude (itemID) * DTOR; sp = 1360.0 * 3600.0 * 24.0 * 0.041841 / 41860.0; // FBM 0.041841 conversion from cal/cm2 to MJ/m2 grossRad = 0.0; sigma = -23.4856 * cos (2.0 * M_PI * (day + 11.0) / 365.25) * DTOR; for (hour = 0; hour < 24; hour++) { eta = (double) ((hour + 1)) * M_PI / 12.0; sinphi = sin (lambda) * sin (sigma) + cos (lambda) * cos (sigma) * cos (eta); sotd = 1 - (0.016729 * cos (0.9856 * (day - 4.0) * DTOR)); sbb = sp * sinphi / pow (sotd,2.0); if (sbb >= 0) grossRad += sbb; } MFVarSetFloat (_MDOutGrossRadID, itemID, grossRad / 24.0); }
static void _MDSmallReservoirCapacity (int itemID) { // Input float irrAreaFraction; // Irrigated arrea fraction float accumSurfRunoff; // Accumulated surface runoff [mm] float accumIrrDemand; // Accumulated irrigational water demand [mm] // Output float smallResCapacity; // maximum storage, m3 // Local float potResCapacity; if ((irrAreaFraction = MFVarGetFloat (_MDInIrrAreaID, itemID, 0.0)) > 0.0) { accumSurfRunoff = MFVarGetFloat (_MDInRainSurfRunoffID, itemID, 0.0) * MFVarGetFloat (_MDInSmallResStorageFractionID, itemID, 1.0); accumIrrDemand = MFVarGetFloat (_MDInIrrGrossDemandID, itemID, 0.0); smallResCapacity = MFVarGetFloat (_MDOutSmallResCapacityID, itemID, 0.0); if (MFDateGetDayOfYear () > 1) { accumSurfRunoff += MFVarGetFloat (_MDOutRainSurfRunoffAccumulatedID, itemID, 0.0); accumIrrDemand += MFVarGetFloat (_MDOutIrrGrossDemandAccumulatedID, itemID, 0.0); } potResCapacity = accumSurfRunoff < accumIrrDemand ? accumSurfRunoff : accumIrrDemand; smallResCapacity = smallResCapacity > potResCapacity ? smallResCapacity : potResCapacity; } else accumSurfRunoff = accumIrrDemand = smallResCapacity = 0.0; MFVarSetFloat (_MDOutRainSurfRunoffAccumulatedID, itemID, accumSurfRunoff); MFVarSetFloat (_MDOutIrrGrossDemandAccumulatedID, itemID, accumIrrDemand); MFVarSetFloat (_MDOutSmallResCapacityID, itemID, smallResCapacity); }
static void _MDGrossRadianceStd (int itemID) { // Input int day; float lambda; // Output float grossRad; // Local int hour; double eta,sigma,sinphi,sp,sbb; day = MFDateGetDayOfYear (); lambda = MFModelGetLatitude (itemID) * DTOR; sp = 1360.0 * 3600.0 * 24.0 * 0.041841 / 41860.0; // FBM 0.041841 conversion from cal/cm2 to MJ/m2 grossRad = 0; sigma = -23.4 * cos (2.0 * M_PI * (day + 11.0) / 365.0) * DTOR; for (hour = 0; hour < 24; hour++) { eta = (hour + 1) * M_PI / 12.0; sinphi = sin (lambda) * sin (sigma) + cos (lambda) * cos (sigma) * cos (eta); sbb = sp * sinphi * pow ((double) _MDGrossRadStdTAU,(double) (1.0 / sinphi)); if (sbb > 0) grossRad += sbb; } MFVarSetFloat (_MDOutGrossRadID, itemID, grossRad / 24.0); }
static void _MDSPackChg (int itemID) { // Input float airT; float precip; float winterOnsetDoY; float initialDensity = 150; float sDensitySlope = 3; if( _MDCalculateSoilTempID==1)winterOnsetDoY = MFVarGetFloat(_MDInWinterOnsetID, itemID,1.0); // printf ("Anf SnowPackChange \n"); // Local float sPack; float sPackChg = 0.0; float sDensity=0.0; float sDepth = 0.0; int snowAge=0; float densityOfWater =1000; if (MFDateGetDayOfYear() - winterOnsetDoY > 0){ snowAge = MFDateGetDayOfYear() - winterOnsetDoY; }else{ snowAge = 365 - winterOnsetDoY + MFDateGetDayOfYear(); } sPack = MFVarGetFloat (_MDOutSnowPackID, itemID, 0.0); if (MFVarTestMissingVal (_MDInAtMeanID,itemID) || MFVarTestMissingVal (_MDInPrecipID, itemID)) { MFVarSetFloat (_MDOutSnowFallID, itemID, 0.0); MFVarSetFloat (_MDOutSnowMeltID, itemID, 0.0); MFVarSetFloat (_MDOutSnowPackID, itemID, sPack); MFVarSetFloat (_MDOutSPackChgID, itemID, 0.0); return; } //printf ("SnowMeltThreshold= %f SnoFall %f\n",_MDSnowMeltThreshold, _MDFallThreshold); airT = MFVarGetFloat (_MDInAtMeanID, itemID, 0.0); precip = MFVarGetFloat (_MDInPrecipID, itemID, 0.0); if (airT < _MDFallThreshold) { /* Accumulating snow pack */ MFVarSetFloat (_MDOutSnowFallID, itemID, precip); MFVarSetFloat (_MDOutSnowMeltID, itemID, 0.0); MFVarSetFloat (_MDOutSnowPackID, itemID, sPack + precip); MFVarSetFloat (_MDOutSPackChgID, itemID, precip); } else if (airT > _MDSnowMeltThreshold) { /* Melting snow pack */ sPackChg = 2.63 + 2.55 * airT + 0.0912 * airT * precip; sPackChg = - (sPack < sPackChg ? sPack : sPackChg); MFVarSetFloat (_MDOutSnowFallID, itemID, 0.0); MFVarSetFloat (_MDOutSnowMeltID, itemID, fabs(sPackChg)); MFVarSetFloat (_MDOutSPackChgID, itemID, sPackChg); MFVarSetFloat (_MDOutSnowPackID, itemID, sPack + sPackChg); } else { /* No change when air temperature is in [-1.0,1.0] range */ MFVarSetFloat (_MDOutSnowFallID, itemID, 0.0); MFVarSetFloat (_MDOutSnowMeltID, itemID, 0.0); MFVarSetFloat (_MDOutSnowPackID, itemID, sPack); MFVarSetFloat (_MDOutSPackChgID, itemID, 0.0); } sDensity = (initialDensity + (snowAge * sDensitySlope)); if (sPack > 0.0 ) sDepth = sPack * densityOfWater / sDensity; //in mm // printf ("sAge %i sDens %f sPack %f sDepth %f \n", snowAge, sDensity, sPack, sDepth); MFVarSetFloat(_MDOutSnowDensityID,itemID,sDensity); MFVarSetFloat(_MDOutSnowDepthID,itemID, sDepth); // printf ("Ende SnowPackChange \n"); // if (itemID == 54914) printf("-- m = %d, d = %d, SF = %f, SM = %f, airT = %f, precip = %f, sPack = %f, sPackChg = %f\n", MFDateGetCurrentMonth(), MFDateGetCurrentDay(), _MDFallThreshold, _MDSnowMeltThreshold, airT, precip, sPack, sPackChg); }
static void _MDIrrGrossDemand (int itemID) { //Input float irrEffeciency; float dailyPrecip = 0.0; float dailyEffPrecip = 0.0; float refETP; float cropFraction [_MDNumberOfIrrCrops+1]; float snowpackChg = 0; int seasStart[3]; float dailyPercolation; float wltPnt; float fldCap; //Output: float totalNetIrrDemand = 0.0; float totalIrrPercolation = 0.0; // Local int i; float ReqpondingDepth ; float pondingDepth; float irrIntensity; float cropDepletionFactor; float meanSMChange; float meanSMoist; int daysSincePlanted; float totAvlWater,readAvlWater, curDepl, prevSoilMstDepl; float addBareSoil = 0.0; float bareSoilBalance = 0.0; float cropCoeff; float croppedArea = 0.0; float cropWR = 0.0; float deepPercolation = 0.0; float loss = 0.0; float netIrrDemand = 0.0; float relCropFraction = 0.0; float rootDepth; float sumOfCropFractions = 0.0; float totalCropETP = 0.0; float totGrossDemand = 0.0; float riceWaterBalance; float nonRiceWaterBalance; float smChange; int numGrowingSeasons; float curCropFraction; float returnFlow; float CropETPlusEPloss; float debug = 0.0; float OUT; float IN; int stage; int curDay; curDay = MFDateGetDayOfYear(); // printf ("IrrIsDistrubted = %i\n", _MDIntensityIsDistributed ); ReqpondingDepth=MFVarGetFloat(_MDRicePoindingDepthID,itemID,2.0); irrAreaFrac = MFVarGetFloat(_MDInIrrAreaFracID, itemID, 0.0); //printf ("pond%fi\n",ReqpondingDepth); switch (_MDIrrigatedAreaMap) { case FAO_ID: irrIntensity = MFVarGetFloat (_MDInIrrIntensityID, itemID, 100.0) / 100.0; break; case IWMI_ID: irrIntensity = 1.0; _MDIntensityDistributed = true; break; } seasStart [0]= MFVarGetFloat (_MDGrowingSeason1ID, itemID, -100); seasStart [1]= MFVarGetFloat (_MDGrowingSeason2ID, itemID, -100); if ((seasStart[0] < 0) || (seasStart[0]< 0)) printf ("Growing Season! \n"); if (irrAreaFrac > 0.0) { for (i = 0;i < _MDNumberOfIrrCrops + 1; ++i) { cropFraction[i] = 0.0; } for (i = 0;i < _MDNumberOfIrrCrops; ++i) { if (MFVarTestMissingVal (_MDInCropFractionIDs [i],itemID)) { MFVarSetFloat(_MDInCropFractionIDs [i],itemID,0.0); } } irrEffeciency = MFVarGetFloat(_MDInIrrEfficiencyID, itemID, 38); dailyPrecip = MFVarGetFloat(_MDInPrecipID, itemID, 0.0); refETP = MFVarGetFloat(_MDInIrrRefEvapotransID, itemID, 0.0); if (irrEffeciency == 0) irrEffeciency =38; if (refETP == 0.0) refETP = 0.01; snowpackChg = MFVarGetFloat (_MDInSPackChgID, itemID, 0.0); if (snowpackChg > 0.0) dailyPrecip = 0.0; //Snow Accumulation, no liquid precipitation if (snowpackChg <= 0.0) dailyPrecip = dailyPrecip + fabs (snowpackChg); //Add Snowmelt dailyEffPrecip = dailyPrecip; dailyPercolation = MFVarGetFloat(_MDRicePercolationRateID,itemID,3.0); wltPnt = MFVarGetFloat (_MDInWltPntID, itemID, 0.15); fldCap = MFVarGetFloat (_MDInFldCapaID, itemID, 0.25); if (fldCap == 0.0) { fldCap=0.35; wltPnt=0.2; } // printf ("FC %f WP %f \n",fldCap,wltPnt); if (irrIntensity < 1.2 && irrIntensity > 1.0)irrIntensity=1.0; if (irrIntensity > 2.0) irrIntensity=2.0; curDepl=0; // printf("Num irr crops %i\n",_MDNumberOfIrrCrops); sumOfCropFractions=0; for (i = 0; i < _MDNumberOfIrrCrops; i++) { sumOfCropFractions += MFVarGetFloat(_MDInCropFractionIDs [i],itemID, 0.0); // printf ("hier\n, numcrops= %i\n",_MDNumberOfIrrCrops); } if (sumOfCropFractions==0) { // No Cropdata for irrigated cell: default to some cereal crop MFVarSetFloat(_MDInCropFractionIDs [2],itemID, 0.3); } sumOfCropFractions=0; for (i = 0; i < _MDNumberOfIrrCrops; i++) {sumOfCropFractions += MFVarGetFloat(_MDInCropFractionIDs [i],itemID, 0.0); } meanSMChange=0;totalCropETP=0; for (i = 0; i < _MDNumberOfIrrCrops; i++) { // cropFraction[_MDNumberOfIrrCrops] is bare soil Area! numGrowingSeasons = 2; // getNumGrowingSeasons (irrIntensity); if (_MDIrrigatedAreaMap == 0.0) numGrowingSeasons=getNumGrowingSeasons(irrIntensity); //FAO MAP curCropFraction = MFVarGetFloat (_MDInCropFractionIDs [i], itemID, 0.0); if (curCropFraction > 0.0) {relCropFraction = curCropFraction / sumOfCropFractions; } else { relCropFraction = 0.0; } daysSincePlanted = getDaysSincePlanting (curDay, seasStart, numGrowingSeasons, &_MDirrigCropStruct [i]); if (_MDIntensityDistributed) { // printf ("Distributed\n"); if (daysSincePlanted > 0.0) { addBareSoil = relCropFraction - irrIntensity / ceil(irrIntensity) * relCropFraction; // printf ("BareSoil = %f\n",addBareSoil); if (relCropFraction > 0.0) cropFraction [i] = relCropFraction - addBareSoil; cropFraction [_MDNumberOfIrrCrops] += addBareSoil; } else { cropFraction [i] = 0.0; cropFraction [_MDNumberOfIrrCrops] += relCropFraction; } } else { // try to grow all crops in Growing Season 1 (always assumed to be the first season!) // printf("first Season!\n"); if (daysSincePlanted > 0.0) { // Growing season // printf("IrrIntensity %f\n",irrIntensity); if (curDay < seasStart [1] || (daysSincePlanted > seasStart[1]-seasStart[0])) { // First or perennial growing season if (irrIntensity < 1.0) addBareSoil = relCropFraction * (1.0 - irrIntensity); else addBareSoil = 0.0; // printf ("hier intensity = %f relCropFraction %f addBareSoil %f\n",irrIntensity, relCropFraction, addBareSoil); } else { // second crop if (irrIntensity > 1.0) addBareSoil = relCropFraction - (irrIntensity - 1.0) * relCropFraction; else if (irrIntensity < 1.0) addBareSoil = relCropFraction; } if (relCropFraction > 0.0) cropFraction [i] = relCropFraction - addBareSoil; cropFraction [_MDNumberOfIrrCrops] += addBareSoil; } else { // Non-growing season cropFraction [i] = 0.0; cropFraction [_MDNumberOfIrrCrops] += relCropFraction; } } } croppedArea = 0.0; for (i = 0; i < _MDNumberOfIrrCrops; i++) croppedArea+=cropFraction[i]; if (fabs ((croppedArea +cropFraction[_MDNumberOfIrrCrops] -1)) > 0.0001) printf ("Cropped %f bare %f \n", croppedArea ,cropFraction[_MDNumberOfIrrCrops]); for (i = 0; i < _MDNumberOfIrrCrops; i++) { netIrrDemand=0;cropWR=0;deepPercolation=0;smChange=0; relCropFraction = cropFraction[i]; numGrowingSeasons = 2; //getNumGrowingSeasons (irrIntensity); if (_MDIrrigatedAreaMap==0) numGrowingSeasons=getNumGrowingSeasons(irrIntensity);//FAO MAP if (relCropFraction > 0) { netIrrDemand=0; daysSincePlanted = getDaysSincePlanting(curDay, seasStart,numGrowingSeasons,&_MDirrigCropStruct[i]); if (daysSincePlanted > 0) { prevSoilMstDepl = MFVarGetFloat(_MDOutCropDeficitIDs [i],itemID, 0.0); // if (daysSincePlanted==1)prevSoilMstDepl=0; // if (itemID==22)printf ("DP %f\n",prevSoilMstDepl); stage = getCropStage(&_MDirrigCropStruct[i], daysSincePlanted); cropCoeff =getCropKc(&_MDirrigCropStruct[i], daysSincePlanted, stage); cropWR = refETP * cropCoeff; rootDepth = getCurCropRootingDepth (&_MDirrigCropStruct[i],daysSincePlanted); rootDepth = 400; // TODO cropDepletionFactor=getCorrDeplFactor(&_MDirrigCropStruct[i], cropWR); if (_MDirrigCropStruct[i].cropIsRice==1) { // printf ("Hier is rice %i\n",i); pondingDepth=prevSoilMstDepl+dailyEffPrecip-cropWR-dailyPercolation; if (pondingDepth>=ReqpondingDepth) { deepPercolation=pondingDepth -ReqpondingDepth; pondingDepth=ReqpondingDepth; } if (pondingDepth<ReqpondingDepth) { netIrrDemand=ReqpondingDepth-pondingDepth; pondingDepth=ReqpondingDepth; } curDepl=pondingDepth; //so that current ponding depth gets set.. smChange=curDepl-prevSoilMstDepl; deepPercolation+=dailyPercolation; riceWaterBalance=dailyEffPrecip+netIrrDemand-cropWR-deepPercolation-smChange; if (fabs (riceWaterBalance) > 0.0001) printf ("RiceWaterBalance %f prevSMM %f smChange %f cropWR %f \n",riceWaterBalance, prevSoilMstDepl,smChange,cropWR); } else { totAvlWater = (fldCap -wltPnt) * rootDepth; readAvlWater = totAvlWater * cropDepletionFactor; curDepl = prevSoilMstDepl - dailyEffPrecip + cropWR; if (curDepl < 0) { curDepl = 0; // if (itemID == 104) printf ("HIER\n"); // if (itemID == 104) printf ("sssaaaaaaaaaaaaaaaaaaaaaasssssssssssssss!\n"); deepPercolation = dailyEffPrecip - prevSoilMstDepl -cropWR; } if (curDepl >= totAvlWater) { // if (itemID == 104) printf ("HIER\n"); curDepl =totAvlWater; } if (curDepl >= readAvlWater) { netIrrDemand = curDepl; netIrrDemand = curDepl; // if (cropWR > readAvlWater)cropWR = readAvlWater; // deepPercolation = dailyEffPrecip - prevSoilMstDepl - cropWR + netIrrDemand; curDepl = prevSoilMstDepl - netIrrDemand-dailyEffPrecip+cropWR; // deepPercolation = dailyEffPrecip - prevSoilMstDepl - cropWR; curDepl = 0.0; if (fabs (curDepl) > 0.001) printf ("Schieses\n"); // if (netIrrDemand > readAvlWater) netIrrDemand = readAvlWater; // if (itemID == 104) printf ("sssssssssssssssssscur Dpl %f RAW %f TAW %f cropWR %f netDem %f!\n",curDepl, readAvlWater,totAvlWater,cropWR,netIrrDemand);// curDepl=0+cropWR; // if (itemID == 104) if (netIrrDemand > readAvlWater) printf ("crop NO %i curDepl %f RAW %f TAW %f cropWR %f netDem %f dsp %i Doy %i!seasStart[0] %i seasStart[1]%i refCropF %f\n",i,curDepl, readAvlWater,totAvlWater,cropWR,netIrrDemand,daysSincePlanted, MFDateGetDayOfYear(),seasStart[0],seasStart[1],relCropFraction);// curDepl=0+cropWR; } // if (curDepl > (fldCap -wltPnt) * rootDepth*cropDepletionFactor) printf ("F**k\n"); smChange=prevSoilMstDepl-curDepl; nonRiceWaterBalance=dailyEffPrecip+netIrrDemand-cropWR-deepPercolation-smChange; if (fabs(nonRiceWaterBalance) > 0.0001)printf ("NONRiceWaterBalance %f prevSMM %f smChange %f cropWR %f TAW %f FC %f WP %f \n",nonRiceWaterBalance, prevSoilMstDepl,smChange,cropWR,totAvlWater,fldCap,wltPnt ); smChange = dailyEffPrecip+netIrrDemand-cropWR-deepPercolation-nonRiceWaterBalance; } MFVarSetFloat(_MDOutCropDeficitIDs [i],itemID,curDepl); // if (itemID==104)printf ("hier %i frac %f %i deple %f depl[2] %f DP %f\n",itemID,relCropFraction,daysSincePlanted,curDepl, MFVarGetFloat(_MDOutCropDeficitIDs [i],itemID, 0.0),deepPercolation); } totalNetIrrDemand += netIrrDemand * relCropFraction; // if (itemID == 77) printf ("totalNetIrrDemand = %f cropFrac %f DSP %i\n",totalNetIrrDemand,relCropFraction,daysSincePlanted); totalCropETP += cropWR * relCropFraction; meanSMChange += smChange * relCropFraction; totalIrrPercolation += deepPercolation * relCropFraction; } MFVarSetFloat (_MDOutCropETIDs [i], itemID, netIrrDemand * relCropFraction * irrAreaFrac); MFVarSetFloat (_MDOutCropGrossDemandIDs[i],itemID,netIrrDemand * relCropFraction * irrAreaFrac/(irrEffeciency/100)); // printf ("Hier\n"); }//for all crops // Add Water Balance for bare soil cropWR = 0.0; relCropFraction = cropFraction [_MDNumberOfIrrCrops]; if (relCropFraction > 1.01) printf("Problem relCropFraction = %f\n",relCropFraction); if (irrAreaFrac > 1.01) printf("Problem irrAreaFraction = %f\n",relCropFraction); // if (itemID == 104) printf ("bareSoil = %f %i dsp %i inten %f\n", relCropFraction, itemID, daysSincePlanted, irrIntensity); curDepl=0; if (relCropFraction > 0.0) { //Crop is not currently grown. ET from bare soil is equal to ET (initial) // printf ("hier bare = %f\n",relCropFraction); netIrrDemand = 0.0; cropWR = 0.2*refETP; prevSoilMstDepl= MFVarGetFloat (_MDOutCropDeficitIDs [_MDNumberOfIrrCrops],itemID, 0.0); totAvlWater = (fldCap - wltPnt) * 250; //assumed RD = 0.25 m deepPercolation = 0.0; curDepl = prevSoilMstDepl - dailyEffPrecip + cropWR; if (curDepl < 0) { curDepl = 0; deepPercolation=dailyEffPrecip - prevSoilMstDepl -cropWR; } if (curDepl >= totAvlWater) { cropWR = totAvlWater - prevSoilMstDepl + dailyEffPrecip; deepPercolation = 0.0; curDepl = totAvlWater; if (cropWR < 0)printf ("CropWR, item %i TAW %f PrevSM %f EffP %f\n",itemID,totAvlWater,prevSoilMstDepl,dailyEffPrecip); // printf("her FC %f WP %f curDepl %f prevDepl %f totAvlWater %f cropWR %f dailyEffPrecip %f\n",fldCap, wltPnt,curDepl,prevSoilMstDepl, totAvlWater, cropWR, dailyEffPrecip); } smChange = prevSoilMstDepl - curDepl; bareSoilBalance=dailyEffPrecip -smChange- cropWR - netIrrDemand -deepPercolation; if (fabs(bareSoilBalance >0.0001)) printf ("bare SMBalance!! precip %f cropWR %f smchange %f dp %f\n ",dailyEffPrecip , cropWR ,smChange, deepPercolation ); MFVarSetFloat (_MDOutCropDeficitIDs [_MDNumberOfIrrCrops], itemID, curDepl); } else { cropWR = 0.0; smChange = 0.0; deepPercolation = 0.0; netIrrDemand = 0.0; curDepl = 0.0; } MFVarSetFloat (_MDOutCropETIDs [_MDNumberOfIrrCrops], itemID, cropWR); MFVarSetFloat (_MDNonIrrFractionID, itemID, cropFraction [_MDNumberOfIrrCrops]); totalNetIrrDemand += netIrrDemand * relCropFraction; totalCropETP += cropWR * relCropFraction; meanSMoist += curDepl * relCropFraction; meanSMChange += smChange * relCropFraction; totalIrrPercolation += deepPercolation * relCropFraction; totGrossDemand = getIrrGrossWaterDemand (totalNetIrrDemand, irrEffeciency); loss = (totGrossDemand - totalNetIrrDemand) + (dailyPrecip - dailyEffPrecip); returnFlow = totalIrrPercolation + loss * 0.1; CropETPlusEPloss = totalCropETP + loss * 0.9; OUT = CropETPlusEPloss + returnFlow + meanSMChange; IN = totGrossDemand + dailyPrecip; // if (itemID == 1) printf ("IrrSMChange in GrossDemand %f Doy %i \n",meanSMChange * irrAreaFrac, MFDateGetDayOfYear()); // if (itemID == 104) printf ("itemID %iGrossDemn %f DailyPrecip %f meanSMChange %f \n",itemID,totGrossDemand,dailyPrecip,meanSMChange); // if (itemID == 104) printf ("ItemID %i ppt %f CropET %f perc %f loss %f \n",itemID,dailyPrecip,totalCropETP,totalIrrPercolation,loss); // if (fabs (IN - OUT) > 0.1) CMmsgPrint (CMmsgAppError,"WaterBalance in MDIrrigation!!! IN %f OUT %f BALANCE %f LOSS %f %i DEMAND %f %i EffPrecip %f itemID %i \n", IN, OUT, IN-OUT, loss, itemID, totGrossDemand, itemID, dailyEffPrecip,itemID); // if (totGrossDemand > 0.1) if (fabs (IN - OUT) > 0.001) printf ("WaterBalance in MDIrrigation!!! IN %f OUT %f BALANCE %f LOSS %f %i DEMAND %f NET %f EffPrecip %f dailyPrecip %f cropped %f bare %f IrrARea %fitemID %i \n", IN, OUT, IN-OUT, loss, itemID, totGrossDemand, totalNetIrrDemand, dailyEffPrecip,dailyPrecip,croppedArea,cropFraction[_MDNumberOfIrrCrops],irrAreaFrac,itemID); // printf ("After = %f \n", MFVarGetFloat (_MDOutCropETIDs [_MDNumberOfIrrCrops], itemID, 88)); for (i = 0; i < _MDNumberOfIrrCrops; i++) { debug += MFVarGetFloat (_MDOutCropETIDs [i], itemID, 0.0); } if (fabs (debug - totalNetIrrDemand * irrAreaFrac) > 0.1 ) printf ("sum of et %f netdeman = %f item %i\n", debug, totalNetIrrDemand * irrAreaFrac, itemID); MFVarSetFloat(_MDOutIrrSMoistChgID, itemID, meanSMChange * irrAreaFrac); MFVarSetFloat(_MDOutIrrNetDemandID, itemID, totalNetIrrDemand * irrAreaFrac); MFVarSetFloat(_MDOutIrrGrossDemandID, itemID, totGrossDemand * irrAreaFrac); MFVarSetFloat(_MDOutIrrReturnFlowID, itemID, returnFlow * irrAreaFrac); MFVarSetFloat(_MDOutIrrEvapotranspID, itemID, CropETPlusEPloss * irrAreaFrac); } else { // cell is not irrigated MFVarSetFloat(_MDOutIrrSoilMoistID, itemID, 0.0); MFVarSetFloat(_MDOutIrrSMoistChgID, itemID, 0.0); MFVarSetFloat(_MDOutIrrNetDemandID, itemID, 0.0); MFVarSetFloat(_MDOutIrrGrossDemandID, itemID, 0.0); MFVarSetFloat(_MDOutIrrReturnFlowID, itemID, 0.0); MFVarSetFloat(_MDOutIrrEvapotranspID, itemID, 0.0); for (i = 0; i < _MDNumberOfIrrCrops; i++) {MFVarSetFloat(_MDOutCropETIDs[i],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); }