123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- #include "config.h"
- #include "demoinput.h"
- #include "driver.h"
- #include "outputchannel.h"
- #include <stdio.h>
- REGISTER_INPUT_MODULE("demo", DemoInput)
- namespace {
- xtring file_temp,file_prec,file_sun,file_soil;
- int soilcode;
- void interp_climate(double* mtemp, double* mprec, double* msun, double* mdtr,
- double* dtemp, double* dprec, double* dsun, double* ddtr) {
- interp_monthly_means_conserve(mtemp, dtemp, date.is_leap(date.year));
- interp_monthly_totals_conserve(mprec, dprec, date.is_leap(date.year), 0);
- interp_monthly_means_conserve(msun, dsun, date.is_leap(date.year), 0, 100);
- interp_monthly_means_conserve(mdtr, ddtr, date.is_leap(date.year), 0);
- }
- }
- DemoInput::DemoInput()
- : nyear(1) {
-
- declare_parameter("nyear", &nyear, 1, 10000, "Number of simulation years to run after spinup");
- }
- bool DemoInput::read_from_file(Coord coord, xtring fname, const char* format,
- double monthly[12], bool soil ) {
- double dlon, dlat;
- int elev;
- FILE* in = fopen(fname, "r");
- if (!in) {
- fail("readenv: could not open %s for input", (char*)fname);
- }
- bool foundgrid = false;
- while (!feof(in) && !foundgrid) {
- if (!soil) {
- readfor(in, format, &dlon, &dlat, &elev, monthly);
- } else {
- readfor(in, format, &dlon, &dlat, &soilcode);
- }
- foundgrid = equal(coord.lon, dlon) && equal(coord.lat, dlat);
- }
- fclose(in);
- if (!foundgrid) {
- dprintf("readenv: could not find record for (%g,%g) in %s",
- coord.lon, coord.lat, (char*)fname);
- }
- return foundgrid;
- }
- bool DemoInput::readenv(Coord coord, long& seed) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- double mtemp[12];
- double mprec[12];
- double msun[12];
- double mwet[12]={31,28,31,30,31,30,31,31,30,31,30,31};
- double mdtr[12];
- for(int m=0; m<12; m++) {
- mdtr[m] = 0.;
- if (ifbvoc) {
- dprintf("WARNING: No data available for dtr in sample data set!\nNo daytime temperature correction for BVOC calculations applied.");
- }
- }
- bool gridfound = read_from_file(coord, file_temp, "f6.2,f5.2,i4,12f4.1", mtemp);
- if(gridfound)
- gridfound = read_from_file(coord, file_prec, "f6.2,f5.2,i4,12f4", mprec);
- if(gridfound)
- gridfound = read_from_file(coord, file_sun, "f6.2,f5.2,i4,12f3", msun);
- if(gridfound)
- gridfound = read_from_file(coord, file_soil, "f,f,i", msun, true);
- if(gridfound) {
-
-
-
- interp_climate(mtemp, mprec, msun, mdtr, dtemp, dprec, dsun, ddtr);
-
-
- prdaily(mprec, dprec, mwet, seed);
- }
- return gridfound;
- }
- void DemoInput::init() {
-
-
-
-
-
-
- double dlon,dlat;
- bool eof=false;
- xtring descrip;
-
-
- xtring file_gridlist=param["file_gridlist"].str;
- FILE* in_grid=fopen(file_gridlist,"r");
- if (!in_grid) fail("initio: could not open %s for input",(char*)file_gridlist);
- gridlist.killall();
- first_call = true;
- while (!eof) {
-
- eof=!readfor(in_grid,"f,f,a#",&dlon,&dlat,&descrip);
- if (!eof && !(dlon==0.0 && dlat==0.0)) {
- Coord& c=gridlist.createobj();
- c.lon=dlon;
- c.lat=dlat;
- c.descrip=descrip;
- }
- }
- fclose(in_grid);
-
- co2=param["co2"].num;
-
- ndep=param["ndep"].num;
-
- landcover_input.init();
-
- management_input.init();
-
- file_temp=param["file_temp"].str;
- file_prec=param["file_prec"].str;
- file_sun=param["file_sun"].str;
- file_soil=param["file_soil"].str;
-
- tprogress.init();
- tmute.init();
- tprogress.settimer();
- tmute.settimer(MUTESEC);
- }
- bool DemoInput::getgridcell(Gridcell& gridcell) {
-
-
- bool gridfound = false;
- bool LUerror = false;
-
-
- if (first_call) {
- gridlist.firstobj();
-
-
- first_call = false;
- }
- else gridlist.nextobj();
- if (gridlist.isobj) {
- while(!gridfound) {
-
- Coord& c = gridlist.getobj();
-
- if(run_landcover) {
- LUerror = landcover_input.loadlandcover(gridlist.getobj().lon, gridlist.getobj().lat);
- if(!LUerror)
- LUerror = management_input.loadmanagement(gridlist.getobj().lon, gridlist.getobj().lat);
- }
- if (!LUerror) {
- gridfound = readenv(c, gridcell.seed);
- } else {
- gridlist.nextobj();
- if(!gridlist.isobj)
- return false;
- }
- }
- dprintf("\nCommencing simulation for stand at (%g,%g)",gridlist.getobj().lon,
- gridlist.getobj().lat);
- if (gridlist.getobj().descrip!="") dprintf(" (%s)\n\n",
- (char*)gridlist.getobj().descrip);
- else dprintf("\n\n");
-
- gridcell.set_coordinates(gridlist.getobj().lon, gridlist.getobj().lat);
-
-
- gridcell.climate.instype=SUNSHINE;
-
- soilparameters(gridcell.soiltype,soilcode);
-
-
- clear_all_graphs();
- return true;
- }
- return false;
- }
- void DemoInput::getlandcover(Gridcell& gridcell) {
- landcover_input.getlandcover(gridcell);
- landcover_input.get_land_transitions(gridcell);
- }
- bool DemoInput::getclimate(Gridcell& gridcell) {
-
- double progress;
- Climate& climate = gridcell.climate;
-
- climate.dndep = ndep / (365.0 * 10000.0);
- climate.co2 = co2;
- climate.temp = dtemp[date.day];
- climate.prec = dprec[date.day];
- climate.insol = dsun[date.day];
-
- climate.dtr=ddtr[date.day];
-
- if (date.day == 0) {
-
- if (date.year==nyear_spinup+nyear) return false;
-
- if (tmute.getprogress()>=1.0) {
- progress=(double)(gridlist.getobj().id*(nyear_spinup+nyear)
- +date.year)/(double)(gridlist.nobj*(nyear_spinup+nyear));
- tprogress.setprogress(progress);
- dprintf("%3d%% complete, %s elapsed, %s remaining\n",(int)(progress*100.0),
- tprogress.elapsed.str,tprogress.remaining.str);
- tmute.settimer(MUTESEC);
- }
- }
- return true;
- }
- DemoInput::~DemoInput() {
-
- }
|