void Regioner::run(){ //error initialization int errcount = 0; errout.errorid = 0; list<int>::iterator jj ; for ( jj=runchtlist.begin() ; jj!=runchtlist.end(); jj++){ int chtid = *jj; // clean-up and re-setup for the next cohort (Yuan: July 13, 2012) gd = GridData(); cd = CohortData(); ed = EnvData(); bd = BgcData(); fd = FirData(); //rgrid.~Grid(); //rgrid = Grid(); //runcht.cht.~Cohort(); runcht.cht = Cohort(); bd.setEnvData(&ed); rgrid.setEnvData(&ed); rgrid.setRegionData(&rd); rgrid.setGridData(&gd); runcht.cht.setTime(&timer); runcht.cht.setProcessData(&ed, &bd, &fd); runcht.cht.setModelData(&md); runcht.cht.setInputData(&rd, &gd, &cd); runcht.cht.setAtmData(&rgrid); if(!md.runeq) { runcht.cht.setRegnOutData(®nod); } runcht.cht.setRestartOutData(&resod); runcht.cht.init(); // starting run here errout.chtid = chtid; //get the eqchtid, spchtid/trchtid, restart-id, and cruid int eqcid = 0; //the record order in the input files, NOT the cohort ID (chtid) int cid = 0; //the record order in the input files, NOT the cohort ID (chtid) int rescid = 0; //the record order in the input files, NOT the cohort ID (chtid) try { //for regional run, only one of the following can be true; if(md.runeq){ cd.eqchtid = chtid; eqcid=cin.getEqRecID(cd.eqchtid); //needed for cruid searching cid=eqcid; // } if(md.runsp){ cd.spchtid = chtid; cid=cin.getSpRecID(cd.spchtid); cin.getEqchtid5SpFile(cd.eqchtid, cid); eqcid=cin.getEqRecID(cd.eqchtid); cd.reschtid = cd.eqchtid; if (md.initmode==3) rescid = resin.getRecordId(cd.reschtid); } if(md.runtr){ cd.trchtid = chtid; cid=cin.getTrRecID(cd.trchtid); cin.getSpchtid5TrFile(cd.spchtid, cid); int spcid=cin.getSpRecID(cd.spchtid); cin.getEqchtid5SpFile(cd.eqchtid, spcid); eqcid=cin.getEqRecID(cd.eqchtid); if (!md.runsc) { //Yuan: when md.runsc is true, md.runtr is also true cd.reschtid = cd.spchtid; } else { cd.reschtid = cd.trchtid; //restart id is of transient-run's, because sc-run is continuouity of tr-run } if (md.initmode==3) rescid = resin.getRecordId(cd.reschtid); } cin.getGrdID(cd.grdid, eqcid); //(eq/sp/tr)grid-id (for soil): starting from ZERO cin.getClmID(cd.clmid, cid); //Yuan: from cid to get its clmid, SO no more using CRUID as its climate data id } catch (Exception &exception){ errout.errorid = -1; errout.outputVariables(errcount); errcount+=1; if(md.consoledebug){ cout <<"problem in setting IDs in Regioner::run\n"; exception.mesg(); } } if(cd.grdid>=0 && cid>=0 && eqcid>=0 && rescid>=0){ int error = 0; //grid-level data for a cohort try { int grdrecid = gin.getGridRecID(cd.grdid); int clmrecid = gin.getClmRecID(cd.clmid); gd.gid=grdrecid; gin.getGridData(&gd, grdrecid, clmrecid); error = rgrid.reinit(grdrecid); //reinit for a new grid if (error!=0) { if(md.consoledebug){ cout <<"problem in grid data in Regioner::run\n"; } errout.errorid = -3; errout.outputVariables(errcount); errcount+=1; continue; //jump over to next cohort, due to grid-data error } } catch (Exception &exception){ exception.mesg(); if(md.consoledebug){ cout <<"problem in reinitializing grid in Regioner::run\n"; } errout.errorid = -3; errout.outputVariables(errcount); errcount+=1; continue; //jump over to next cohort, due to grid-data error } //cohort-level data for a cohort runcht.jcalifilein=true; // for reading Jcalinput.txt, the default is true (must be done before re-initiation) runcht.jcalparfile=""; runcht.ccdriverout=false; //don't change to true for regioner error = runcht.reinit(cid, eqcid, rescid); //reinit for a new cohort //run a cohort try { if (error!=0) { cout<<"Error for reinitializing cohort: "<<chtid<<" - SKIPPED! \n"; errout.errorid = -5; errout.outputVariables(errcount); errcount+=1; continue; //jump over to next cohort, due to cohort reinit error } else { if (md.consoledebug) cout<<"cohort: "<<chtid<<" @ "<<md.runstages <<" - running! \n"; error = runcht.run(); if (error!=0) { if(md.consoledebug){ cout<<"problem in running cohort in Regioner::run \n"; } rout.missingValues(MAX_OREGN_YR, runcht.cohortcount); errout.errorid = -4; errout.outputVariables(errcount); errcount+=1; continue; //jump over to next cohort, due to run cohort error } } } catch (Exception &exception){ exception.mesg(); if(md.consoledebug){ cout <<"problem in running cohort in Regioner::run\n"; } rout.missingValues(MAX_OREGN_YR, runcht.cohortcount); errout.errorid = -4; errout.outputVariables(errcount); errcount+=1; continue; //jump over to next cohort, due to run cohort error } } else { // end of cruid >=0 && other IDs>=0 cout<<"No grid exists for cohort: "<<chtid<<" - SKIPPED! \n"; rout.missingValues(MAX_OREGN_YR, runcht.cohortcount); errout.errorid = -2; errout.outputVariables(errcount); errcount+=1; } // end of cruid >=0 && errout.errorid ==0 runcht.cohortcount++; }// end of cohort loop };
void run_json(std::istream& is, std::ostream& os, const hfst::HfstTransducer& t, const msgmap& msgs) { json::sanity_test(); int pos = 0; std::u16string errtype = u"default"; bool first_err = true; LineType prevtype = BlankL; bool is_addcohort = true; std::ostringstream text; Cohort c; // TODO: could use http://utfcpp.sourceforge.net, but it's not in macports; // and ICU seems overkill just for iterating codepoints std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> utf16conv; os << "{" << json::key(u"errs") << "["; for (std::string line; std::getline(is, line);) { std::match_results<const char*> result; std::regex_match(line.c_str(), result, CG_LINE); bool appendsugg = is_addcohort && prevtype != WordformL && !c.err.empty(); if (!result.empty() && ((result[2].length() != 0 && !appendsugg) || result[6].length() != 0)) { proc_cohort(pos, first_err, c, text, os, t, msgs, utf16conv); c = Cohort(); } if (!result.empty() && result[2].length() != 0) { if(appendsugg) { c.err = sugg_append(utf16conv.from_bytes(result[2]), c.err); } is_addcohort = true; c.form = utf16conv.from_bytes(result[2]); prevtype = WordformL; } else if(!result.empty() && result[3].length() != 0) { // TODO: doesn't do anything with subreadings yet; needs to keep track of previous line(s) for that const auto& sugg = proc_line(t, line); if(!sugg.errtype.empty()) { errtype = sugg.errtype; } if(sugg.suggest) { c.err[errtype].insert(sugg.sforms.begin(), sugg.sforms.end()); } else { is_addcohort = false; // Seen at least one non-suggestion reading } prevtype = ReadingL; } else if(!result.empty() && result[6].length() != 0) { const auto blank = clean_blank(result[6]); pos += utf16conv.from_bytes(blank).size(); text << blank; prevtype = BlankL; } else { // Blank lines without the prefix don't go into text output! prevtype = BlankL; } } proc_cohort(pos, first_err, c, text, os, t, msgs, utf16conv); os << "]" << "," << json::key(u"text") << json::str(utf16conv.from_bytes(text.str())) << "}"; }