static int _MFModelParse (int argc, char *argv [],int argNum, int (*conf) ()) { FILE *inFile; bool testOnly = false; int argPos, ret, help = false; int i, varID, intVal; float floatVal; char *startDate = (char *) NULL, *endDate = (char *) NULL; MFVariable_t *var; int MFLoadConfig (const char *,int (*) (const char *, const char *, bool)); bool _MFOptionNew (char *,char *), _MFOptionTestInUse (); for (argPos = 1;argPos < argNum;) { if (CMargTest (argv [argPos],"-i","--input")) { if ((argNum = CMargShiftLeft (argPos,argv,argNum)) < 1) { CMmsgPrint (CMmsgUsrError,"Missing input argument!\n"); return (CMfailed); } for (i = 0;i < (int) strlen (argv[argPos]);++i) if (argv [argPos][i] == '=') break; if (i == (int) strlen (argv [argPos])) { CMmsgPrint (CMmsgUsrError,"Illformed input variable [%s] in: %s:%d\n",argv [argPos],__FILE__,__LINE__); return (CMfailed); } argv [argPos][i] = '\0'; if (MFVarSetPath (argv [argPos],argv [argPos] + i + 1, false, MFInput) == (MFVariable_t *) NULL) return (CMfailed); if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-o","--output")) { if ((argNum = CMargShiftLeft (argPos,argv,argNum)) < 1) { CMmsgPrint (CMmsgUsrError,"Missing _MFModelOutput argument!\n"); return (CMfailed); } for (i = 0;i < (int) strlen (argv[argPos]);++i) if (argv [argPos][i] == '=') break; if (i == (int) strlen (argv [argPos])) { CMmsgPrint (CMmsgUsrError,"Illformed _MFModelOutput variable [%s]!\n",argv [argPos]); return (CMfailed); } argv [argPos][i] = '\0'; if (_MFModelOutputNew (argv [argPos],argv [argPos] + i + 1,false) == CMfailed) return (CMfailed); if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-t","--state")) { if ((argNum = CMargShiftLeft (argPos,argv,argNum)) < 1) { CMmsgPrint (CMmsgUsrError,"Missing _MFModelOutput argument!\n"); return (CMfailed); } for (i = 0;i < (int) strlen (argv[argPos]);++i) if (argv [argPos][i] == '=') break; if (i == (int) strlen (argv [argPos])) { CMmsgPrint (CMmsgUsrError,"Illformed _MFModelOutput variable [%s]!\n",argv [argPos]); return (CMfailed); } argv [argPos][i] = '\0'; if (_MFModelOutputNew (argv [argPos],argv [argPos] + i + 1, true) == CMfailed) return (CMfailed); if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-s","--start")) { if ((argNum = CMargShiftLeft (argPos,argv,argNum)) < 1) { CMmsgPrint (CMmsgUsrError,"Missing start time!\n"); return (CMfailed); } startDate = argv [argPos]; if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-n","--end")) { if ((argNum = CMargShiftLeft (argPos,argv,argNum)) < 1) { CMmsgPrint (CMmsgUsrError,"Missing end time!\n"); return (CMfailed); } endDate = argv [argPos]; if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-T","--testonly")) { testOnly = true; if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos) break; } if (CMargTest (argv [argPos],"-m","--message")) { const char *types [] = { "sys_error", "app_error", "usr_error", "debug", "warning", "info", (char *) NULL }; CMmsgType msgTypes [] = { CMmsgSysError, CMmsgAppError, CMmsgUsrError, CMmsgDebug, CMmsgWarning, CMmsgInfo }; int type; const char *modes [] = { "file:", "on", "off", (char *) NULL }; if ((argNum = CMargShiftLeft (argPos,argv,argNum)) < 1) { CMmsgPrint (CMmsgUsrError,"Missing message argument!\n"); return (CMfailed); } if ((type = CMoptLookup (types,argv [argPos],false)) == CMfailed) { CMmsgPrint (CMmsgWarning,"Ignoring illformed message [%s]!\n",argv [argPos]); } else { switch (CMoptLookup (modes, argv [argPos] + strlen (types [type]) + 1, false)) { case 0: CMmsgSetStreamFile (msgTypes [type], argv [argPos] + strlen (types [type]) + 1 + strlen (modes [0])); case 1: CMmsgSetStatus (msgTypes [type], true); break; case 2: CMmsgSetStatus (msgTypes [type], false); break; default: CMmsgPrint (CMmsgWarning,"Ignoring illformed message [%s]!\n",argv [argPos]); break; } } if ((argNum = CMargShiftLeft(argPos,argv,argNum)) <= argPos) break; continue; } if (CMargTest (argv [argPos],"-h","--help")) { help = true; CMmsgPrint (CMmsgInfo,"%s [options] <domain>\n",CMfileName (argv [0])); CMmsgPrint (CMmsgInfo," -s, --start [start date in the form of \"yyyy-mm-dd\"]\n"); CMmsgPrint (CMmsgInfo," -n, --end [end date in the form of \"yyyy-mm-dd\"]\n"); CMmsgPrint (CMmsgInfo," -i, --input [variable=source]\n"); CMmsgPrint (CMmsgInfo," -o, --output [variable=destination]\n"); CMmsgPrint (CMmsgInfo," -r, --state [variable=statefile]\n"); CMmsgPrint (CMmsgInfo," -ol, --output-listfile [output listfile]\n"); CMmsgPrint (CMmsgInfo," -p, --option [option=content]\n"); CMmsgPrint (CMmsgInfo," -r, --route [variable]\n"); CMmsgPrint (CMmsgInfo," -T, --testonly\n"); CMmsgPrint (CMmsgInfo," -m, --message [sys_error|app_error|usr_error|debug|warning|info]=[on|off|file=<filename>]\n"); CMmsgPrint (CMmsgInfo," -h, --help\n"); break; } if ((argv [argPos][0] == '-') && (strlen (argv [argPos]) > 1)) { CMmsgPrint (CMmsgUsrError,"Unknown option [%s]!\n",argv [argPos]); return (CMfailed); } argPos++; } if (startDate == (char *) NULL) startDate = "XXXX-01-01"; if (endDate == (char *) NULL) endDate = "XXXX-12-31"; if (!MFDateSetStart (startDate)) { CMmsgPrint (CMmsgAppError,"Error: Invalid start date!\n"); ret = CMfailed; } if (!MFDateSetCurrent (startDate)) ret = CMfailed; if (!MFDateSetEnd (endDate)) { CMmsgPrint (CMmsgAppError,"Error: Invalid end date!\n"); ret = CMfailed; } if (ret == CMfailed) return (CMfailed); ret = conf (); if (help) { MFOptionPrintList (); return (CMfailed); } if (ret == CMfailed) return (CMfailed); if (testOnly) { CMmsgPrint (CMmsgInfo, "ID %10s %30s[%10s] %6s %5s NStep %3s %4s %8s Output", "Start_Date", "Variable","Unit","Type", "TStep", "Set", "Flux", "Initial"); for (var = MFVarGetByID (varID = 1);var != (MFVariable_t *) NULL;var = MFVarGetByID (++varID)) if ((strncmp (var->Name,"__",2) != 0) || var->Initial) CMmsgPrint (CMmsgInfo, "%3i %10s %30s[%10s] %6s %5s %5d %3s %4s %8s %6s", varID,var->Header.Date,var->Name,var->Unit,MFVarTypeString (var->Header.DataType),MFDateTimeStepString (var->TStep),var->NStep, CMyesNoString (var->Set),CMyesNoString (var->Flux),CMyesNoString (var->Initial), CMyesNoString (var->OutPath != (char *) NULL)); return (CMfailed); } if ((argNum) > 2) { CMmsgPrint (CMmsgUsrError,"Extra arguments!"); return (CMfailed); } if ((argNum) < 2) { CMmsgPrint (CMmsgUsrError,"Missing Template Coverage!"); return (CMfailed); } if ((inFile = strcmp (argv [1],"-") != 0 ? fopen (argv [1],"r") : stdin) == (FILE *) NULL) { CMmsgPrint (CMmsgAppError,"%s: Template Coverage [%s] Opening error!",CMfileName (argv [0]),argv [1]); return (CMfailed); } if ((_MFDomain = MFDomainGet (inFile)) == (MFDomain_t *) NULL) return (CMfailed); for (var = MFVarGetByID (varID = 1);var != (MFVariable_t *) NULL;var = MFVarGetByID (++varID)) { if (var->InStream == (MFDataStream_t *) NULL) var->TStep = MFTimeStepDay; else if ((var->Initial == false) && (MFDateSetCurrent (var->Header.Date) == false)) CMmsgPrint (CMmsgWarning,"Warning: Invalid date in input (%s)",var->Name); if (var->Flux) sprintf (var->Unit + strlen (var->Unit),"/%s",MFDateTimeStepUnit (var->TStep)); } MFDateRewind (); if (strcmp (startDate,MFDateGetCurrent ()) != 0)//zero. strings are equal CMmsgPrint (CMmsgWarning,"Warning: Adjusting start date (%s,%s)!",startDate, MFDateGetCurrent ()); for (var = MFVarGetByID (varID = 1);var != (MFVariable_t *) NULL;var = MFVarGetByID (++varID)) { switch (var->Header.DataType) { case MFInput: CMmsgPrint (CMmsgAppError, "Error: Unresolved variable (%s [%s] %s)!",var->Name,var->Unit, MFVarTypeString (var->Header.DataType)); ret = CMfailed; break; case MFRoute: case MFOutput: var->Header.DataType = MFFloat; var->Header.Missing.Float = MFDefaultMissingFloat; default: if (var->Data == (void *) NULL) { var->Header.ItemNum = _MFDomain->ObjNum; if ((var->Data = (void *) calloc (var->Header.ItemNum,MFVarItemSize (var->Header.DataType))) == (void *) NULL) { CMmsgPrint (CMmsgSysError,"Memory Allocation Error in: %s:%d",__FILE__,__LINE__); ret = CMfailed; } switch (var->Header.DataType) { case MFByte: case MFShort: case MFInt: intVal = var->InStream == (MFDataStream_t *) NULL ? 0 : var->InStream->Handle.Int; for (i = 0;i < var->Header.ItemNum;++i) MFVarSetInt (varID,i,intVal); break; case MFFloat: case MFDouble: floatVal = var->InStream == (MFDataStream_t *) NULL ? 0.0 : var->InStream->Handle.Float; for (i = 0;i < var->Header.ItemNum;++i) MFVarSetFloat (varID,i,floatVal); break; } } else { if (var->Header.ItemNum != _MFDomain->ObjNum) { CMmsgPrint (CMmsgAppError,"Error: Inconsistent data stream (%s [%s])!",var->Name,var->Unit); ret = CMfailed; } else if (var->Initial) { while (MFDataStreamRead (var) == MFContinue); MFDataStreamClose (var->InStream); var->InPath = (char *) NULL; var->InStream = (MFDataStream_t *) NULL; } switch (var->TStep) { default: var->NStep = 1; break; case MFTimeStepMonth: var->NStep = MFDateGetMonthLength (); break; case MFTimeStepYear: var->NStep = 365; break; } } if (!var->Initial && !var->Set) { CMmsgPrint (CMmsgWarning,"Warning: Ignoring unused variable (%s)!",var->Name); MFDataStreamClose (var->InStream); var->InPath = (char *) NULL; var->InStream = (MFDataStream_t *) NULL; var->Set = true; var->Initial = true; } break; } } for (i = 0;i < _MFModelOutNum;++i) MFVarSetPath (_MFModelOutput [i].Name, _MFModelOutput [i].Path, _MFModelOutput [i].State, MFOutput); free (_MFModelOutput); CMmsgPrint (CMmsgInfo, "ID %10s %30s[%10s] %6s %5s NStep %3s %4s %8s Output", "Start_Date", "Variable","Unit","Type", "TStep", "Set", "Flux", "Initial"); for (var = MFVarGetByID (varID = 1);var != (MFVariable_t *) NULL;var = MFVarGetByID (++varID)) if ((strncmp (var->Name,"__",2) != 0) || var->Initial) CMmsgPrint (CMmsgInfo, "%3i %10s %30s[%10s] %6s %5s %5d %3s %4s %8s %6s", varID,var->Header.Date,var->Name,var->Unit,MFVarTypeString (var->Header.DataType),MFDateTimeStepString (var->TStep),var->NStep, CMyesNoString (var->Set),CMyesNoString (var->Flux),CMyesNoString (var->Initial), CMyesNoString (var->OutPath != (char *) NULL)); if (ret == CMfailed) return (CMfailed); _MFOptionTestInUse (); return (ret); }
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); }