def from_csv(cls, speciesName, filename, row=0): """Construct Tree object using data from a csv which is structured like the master csv (tree_defaults.csv) Args: speciesName: name of species (can be anything) filename: csv file with the info row: row in the csv to be read (0-indexed) Returns: Tree object Raises: IndexError: if row > # rows in csv, or csv doesn't have 8 cols """ data = io_.read_csv(filename, cols=(2, 3, 4, 5, 6, 7, 8, 9)) data = np.atleast_2d(data) # to account for if there's only one row try: params = { 'species': speciesName, 'nitrogen': np.array([ data[row, 0], data[row, 1], data[row, 2], data[row, 3], data[row, 4] ]), 'carbon': data[row, 5], 'rootToShoot': data[row, 6], 'dens': data[row, 7] } tree = cls(params) except IndexError: log.exception("Couldn't find row %d in %s" % (row, filename)) sys.exit(1) return tree
def from_csv(cls, filename='climate.csv', order=('temp', 'rain', 'evap'), isEvap=True): """Construct Climate object from a csv file. Args: filename order: tuple of str specifying the column order in csv isEvap: whether data has evap or PET - convert to evap if not isEvap Returns: Climate object Raises: ValueError: if order doesn't contain 'temp,'rain', and 'evap' in some order """ data = io_.read_csv(filename) try: # Create clim array with the correct rows clim = np.zeros((3, 12)) correctOrder = ('temp', 'rain', 'evap') for i in range(3): clim[i] = data[:, order.index(correctOrder[i])] if not isEvap: # pet given, so convert to evap clim[2] /= 0.75 climate = cls(clim) except ValueError: log.exception("INCORRECT VALUE(S) IN ORDER ARGUMENT") sys.exit(1) except IndexError: log.exception("Data not in correct format") sys.exit(1) return climate
def from_csv(cls, speciesName, filename, row=0): """Construct Crop object using data from a csv which is structured like the master csv (crop_ipcc_defaults.csv). Args: speciesName: name of species (can be anything) filename: filename of csv with species info row: row in the csv to be read (0-indexed) Returns: Crop object Raises: IndexError: if row > number of rows in csv, or csv doesn't have 7 columns """ data = io_.read_csv(filename, cols=(2, 3, 4, 5, 6, 7, 8)) data = np.atleast_2d(data) # account for when only one row in file try: params = { 'species': speciesName, 'slope': data[row, 0], 'intercept': data[row, 1], 'nitrogenBelow': data[row, 2], 'nitrogenAbove': data[row, 3], 'carbonBelow': data[row, 4], 'carbonAbove': data[row, 5], 'rootToShoot': data[row, 6] } crop = cls(params) except IndexError: log.exception("CAN'T FIND ROW %d IN %s" % (row, filename)) sys.exit(1) return crop
def from_csv(cls, filename='soil.csv'): """Construct Soil object from csv data.""" data = io_.read_csv(filename) params = {'Cy0': data[0], 'clay': data[1]} return cls(params)
if not muRows: log.warning("COULD NOT FIND %d IN HWSD_DATA.csv", mu) return None # Weighted sum of SOC and clay cy0 = 0 clay = 0 for row in muRows: # weighted sum of SOC and clay cy0 += row[12] * row[2] clay += row[7] * row[2] cy0 /= 100 # account for percentages clay /= 100 return cy0, clay if __name__ == '__main__': data = io_.read_csv( os.path.join(cfg.SHAMBA_DIR, 'rasters', 'soil', 'muGlobalTestValues.csv')) long = data[:, 1] lat = data[:, 2] mu = data[:, 3] for i in range(len(mu)): a = Soil((lat[i], long[i])) print("\nlocation = %f, %f " % (lat[i], long[i])) print("mu actual= %d" % mu[i]) print("mu python= %d" % a.mu_global)
import os import sys import pdb import numpy as np from shamba.model import io_, cfg # ---------------------------------- # Read species data from csv # (run when this module is imported) # ---------------------------------- # abridged species list SPP_LIST = [1, 2, 3] TREE_SPP = {} _data = io_.read_csv('tree_defaults_cl.csv', cols=(2, 3, 4, 5, 6, 7, 8, 9)) _data = np.atleast_2d(_data) _nitrogen = np.zeros((len(SPP_LIST), 5)) for _i in range(len(SPP_LIST)): _nitrogen[_i] = np.array( [_data[_i, 0], _data[_i, 1], _data[_i, 2], _data[_i, 3], _data[_i, 4]]) _carbon = _data[:, 5] _rootToShoot = _data[:, 6] _dens = _data[:, 7] for _i in range(len(SPP_LIST)): _spp = SPP_LIST[_i] TREE_SPP[_spp] = { 'species': _spp, 'dens': _dens[_i],
# -------------------------- # Read species data from csv # -------------------------- SPP_LIST = [ 'grains', 'beans and pulses', 'tubers', 'root crops', 'n-fixing forages', 'non-n-fixing forages', 'perennial grasses', 'grass-clover mixtures', 'maize', 'wheat', 'winter wheat', 'spring wheat', 'rice', 'barley', 'oats', 'millet', 'sorghum', 'rye', 'soyabean', 'dry bean', 'potato', 'peanut', 'alfalfa', 'non-legume hay' ] # Read csv file with default crop data CROP_SPP = {} _data = io_.read_csv('crop_ipcc_defaults.csv', cols=(2, 3, 4, 5, 6, 7, 8)) _data = np.atleast_2d(_data) _slope = _data[:, 0] _intercept = _data[:, 1] _nitrogenBelow = _data[:, 2] _nitrogenAbove = _data[:, 3] _carbonBelow = _data[:, 4] _carbonAbove = _data[:, 5] _rootToShoot = _data[:, 6] for _i in range(len(SPP_LIST)): _spp = SPP_LIST[_i] CROP_SPP[_spp] = { 'species': _spp, 'slope': _slope[_i],
def main(n): """ ## STEP 1 ## Point SHAMBA towards location of the 'shamba' folder on your computer by specifying the BASE_PATH in the 'cfg.py' script (the script can be found in the 'shamba/shamba/model' folder). """ # Initial stuff cfg.args = io_.get_cl_args() """ ## STEP 2 ## Within the 'shamba/shamba_projects' folder, set up a project folder with a unique project name (e.g. UG_TS_2016). Within this folder, set up a folder titled 'input'. Within this 'input' folder directory add copies of the following four .csv files (you can copy from other existing'input' folder directories e.g. 'UG_TS_2016'): 1. crop_ipcc_baseline.csv 2. crop_ipcc.csv 3. climate.csv 4. soilInfo.csv The above .csv files can be updated with new input parmeteres if desired, but it may not be necesarry. """ """ ## STEP 3 ## Specify below the unique name of the new project folder in the shamba/shamba_projects folder """ project_name = 'UG_TS_2016' cfg.SAV_DIR = os.path.join(cfg.PROJ_DIR, project_name) # specifiying input and output files cfg.INP_DIR = os.path.join(cfg.SAV_DIR, 'input') cfg.OUT_DIR = os.path.join(cfg.SAV_DIR, 'output') """ ## STEP 4 ## Complete in full the Excel worksheet 'SHAMBA input output template v1', (located in the 'plan_vivo_approach_excel_templates' folder) including all references for information. The reviewer will reject the modelling unless it is fully referenced. See the instructions in the Excel worksheet. On the '_questionnaire' worksheet, you must enter a value in each of the blue cells in the 'Input data' column (column N) in response to each 'data collection question'. Otherwise the model will not run properly. If the question is not relevent to the land use you are modelling, enter zero. ## STEP 5 ### To run the model for a particular intervention, save the relevant '_input.csv' file into the new shamba/shamba_projects/"project"/input folder and specifiy the .csv name below """ input_csv = 'WL_input.csv' # ---------- # getting input data # ---------- ## creating dictionary of input data from input.csv data = list(csv.reader(open(cfg.INP_DIR + "/" + input_csv))) keys = data[0] values = data[n + 1] val = (dict(zip(keys, values))) # terms in coded below preceded by val are values being pulled in from dictionary # created above. Converting to float or interger as needed for each # key ## getting plot anlaysis number to name output st = int(val['analysis_no']) # ---------- # location information # ---------- loc = (float(val['lat']), float(val['lon'])) climate = Climate.from_location(loc) # ---------- # project length # ---------- # YEARS = length of tree data. ACCT = years in accounting period cfg.N_YEARS = (int(val['yrs_proj'])) cfg.N_ACCT = (int(val['yrs_acct'])) # ---------- # soil equilibrium solve # ---------- soil = SoilParams.from_location(loc) invRoth = InverseRothC(soil, climate) # ---------- # tree model # ---------- """ ## STEP 6 ## If nitrogen allocations, carbon, root/shoot and/or wood density attributes differ between tree cohorts, add a new row specifying these tree parametres to the the tree_defaults.csv at shamba/default_input folder and make sure the '_input.csv' file correctly attributes each tree cohort to the relevant parametres under 'trees in baseline' and 'trees in project' """ # linking tree cohort parameteres tree_par_base = TreeParams.from_species_index(int(val['species_base'])) tree_par1 = TreeParams.from_species_index(int(val['species1'])) tree_par2 = TreeParams.from_species_index(int(val['species2'])) tree_par3 = TreeParams.from_species_index(int(val['species3'])) # linking tree growth spp = int(val['species_base']) if spp is 1: growth_base = TreeGrowth.from_csv1(tree_par_base, n, filename=input_csv) elif spp is 2: growth_base = TreeGrowth.from_csv2(tree_par_base, n, filename=input_csv) else: growth_base = TreeGrowth.from_csv3(tree_par_base, n, filename=input_csv) spp = int(val['species1']) if spp is 1: growth1 = TreeGrowth.from_csv1(tree_par1, n, filename=input_csv) elif spp is 2: growth1 = TreeGrowth.from_csv2(tree_par1, n, filename=input_csv) else: growth1 = TreeGrowth.from_csv3(tree_par1, n, filename=input_csv) spp = int(val['species2']) if spp is 1: growth2 = TreeGrowth.from_csv1(tree_par2, n, filename=input_csv) elif spp is 2: growth2 = TreeGrowth.from_csv2(tree_par2, n, filename=input_csv) else: growth2 = TreeGrowth.from_csv3(tree_par2, n, filename=input_csv) spp = int(val['species3']) if spp is 1: growth3 = TreeGrowth.from_csv1(tree_par3, n, filename=input_csv) elif spp is 2: growth3 = TreeGrowth.from_csv2(tree_par3, n, filename=input_csv) else: growth3 = TreeGrowth.from_csv3(tree_par3, n, filename=input_csv) # specify thinning regime and fraction left in field (lif) # baseline thinning regime # (add line of thinning[yr] = % thinned for each event) thinning_base = np.zeros(cfg.N_YEARS + 1) thinning_base[int(val['thin_base_yr1'])] = float(val['thin_base_pc1']) thinning_base[int(val['thin_base_yr2'])] = float(val['thin_base_pc2']) # project thinning regime # (add need line of thinning[yr] = % thinned for each event) thinning_proj = np.zeros(cfg.N_YEARS + 1) thinning_proj[int(val['thin_proj_yr1'])] = float(val['thin_proj_pc1']) thinning_proj[int(val['thin_proj_yr2'])] = float(val['thin_proj_pc2']) thinning_proj[int(val['thin_proj_yr3'])] = float(val['thin_proj_pc3']) thinning_proj[int(val['thin_proj_yr4'])] = float(val['thin_proj_pc4']) # baseline fraction of thinning left in the field # specify vector = array[(leaf,branch,stem,course root,fine root)]. # 1 = 100% left in field. Leaf and roots assumed 100%. # (can specify for individual years) using above code for thinning_proj. thin_frac_lif_base = np.array( [1, float(val['thin_base_br']), float(val['thin_base_st']), 1, 1]) # project fraction of thinning left in the field # specify vector = array[(leaf,branch,stem,course root,fine root)]. # 1 = 100% left in field. Leaf and roots assumed 100%. # (can specify for individual years) using above code for thinning_proj. thin_frac_lif_proj = np.array( [1, float(val['thin_proj_br']), float(val['thin_proj_st']), 1, 1]) # specify mortality regime and fraction left in field (lif) # baseline yearly mortality mort_base = np.array((cfg.N_YEARS + 1) * [float(val['base_mort'])]) # project yearly mortality mort_proj = np.array((cfg.N_YEARS + 1) * [float(val['proj_mort'])]) # baseline fraction of dead biomass left in the field # specify vector = array[(leaf,branch,stem,course root,fine root)]. # 1 = 100% left in field. Leaf and roots assumed 100%. # (can specify for individual years) using above code for thinning_proj. mort_frac_lif_base = np.array( [1, float(val['mort_base_br']), float(val['mort_base_st']), 1, 1]) # project fraction of dead biomass left in the field # specify vector = array[(leaf,branch,stem,course root,fine root)]. # 1 = 100% left in field. Leaf and roots assumed 100%. # (can specify for individual years) using above code for thinning_proj. mort_frac_lif_proj = np.array( [1, float(val['mort_proj_br']), float(val['mort_proj_st']), 1, 1]) # run tree model # trees planted in baseline (standDens must be at least 1) tree_base = TreeModel.from_defaults(tree_params=tree_par1, tree_growth=growth_base, yearPlanted=0, standDens=int(val['base_plant_dens']), thin=thinning_base, thinFrac=thin_frac_lif_base, mort=mort_base, mortFrac=mort_frac_lif_base) # trees planted in project tree_proj1 = TreeModel.from_defaults( tree_params=tree_par1, tree_growth=growth1, yearPlanted=int(val['proj_plant_yr1']), standDens=int(val['proj_plant_dens1']), thin=thinning_proj, thinFrac=thin_frac_lif_proj, mort=mort_proj, mortFrac=mort_frac_lif_proj) tree_proj2 = TreeModel.from_defaults( tree_params=tree_par2, tree_growth=growth2, yearPlanted=int(val['proj_plant_yr2']), standDens=int(val['proj_plant_dens2']), thin=thinning_proj, thinFrac=thin_frac_lif_proj, mort=mort_proj, mortFrac=mort_frac_lif_proj) tree_proj3 = TreeModel.from_defaults( tree_params=tree_par3, tree_growth=growth3, yearPlanted=int(val['proj_plant_yr3']), standDens=int(val['proj_plant_dens3']), thin=thinning_proj, thinFrac=thin_frac_lif_proj, mort=mort_proj, mortFrac=mort_frac_lif_proj) # ---------- # fire model # ---------- #return interval of fire, [::2] = 1 is return interval of two years base_fire_interval = int(val['fire_int_base']) if base_fire_interval is 0: fire_base = np.zeros(cfg.N_YEARS) else: fire_base = np.zeros(cfg.N_YEARS) fire_base[::base_fire_interval] = int(val['fire_pres_base']) proj_fire_interval = int(val['fire_int_proj']) if proj_fire_interval is 0: fire_proj = np.zeros(cfg.N_YEARS) else: fire_proj = np.zeros(cfg.N_YEARS) fire_proj[::proj_fire_interval] = int(val['fire_pres_proj']) # ---------- # litter model # ---------- # baseline external organic inputs l_base = LitterModel.from_defaults(litterFreq=int(val['base_lit_int']), litterQty=float(val['base_lit_qty'])) # baseline synthetic fertiliser additions sf_base = LitterModel.synthetic_fert(freq=int(val['base_sf_int']), qty=float(val['base_sf_qty']), nitrogen=float(val['base_sf_n'])) # Project external organic inputs l_proj = LitterModel.from_defaults(litterFreq=int(val['proj_lit_int']), litterQty=float(val['proj_lit_qty'])) # Project synthetic fertiliser additions sf_proj = LitterModel.synthetic_fert(freq=int(val['proj_sf_int']), qty=float(val['proj_sf_qty']), nitrogen=float(val['proj_sf_n'])) # ---------- # Crop model # ---------- # Baseline specify crop, yield, and % left in field in csv file cropPar = io_.read_csv(input_csv) cropPar = np.atleast_2d(cropPar) crop_base = [] # list for crop objects crop_par_base = [] spp = int(val['crop_base_spp1']) harvYield = np.zeros(cfg.N_YEARS) harvYield[int(val['crop_base_start1']):int(val['crop_base_end1'])] = float( val['crop_base_yd1']) harvFrac = float(val['crop_base_left1']) ci = CropParams.from_species_index(spp) c = CropModel(ci, harvYield, harvFrac) crop_base.append(c) crop_par_base.append(ci) spp = int(val['crop_base_spp2']) harvYield = np.zeros(cfg.N_YEARS) harvYield[int(val['crop_base_start2']):int(val['crop_base_end2'])] = float( val['crop_base_yd2']) harvFrac = float(val['crop_base_left2']) ci = CropParams.from_species_index(spp) c = CropModel(ci, harvYield, harvFrac) crop_base.append(c) crop_par_base.append(ci) spp = int(val['crop_base_spp3']) harvYield = np.zeros(cfg.N_YEARS) harvYield[int(val['crop_base_start3']):int(val['crop_base_end3'])] = float( val['crop_base_yd3']) harvFrac = float(val['crop_base_left3']) ci = CropParams.from_species_index(spp) c = CropModel(ci, harvYield, harvFrac) crop_base.append(c) crop_par_base.append(ci) # Project specify crop, yield, and % left in field in csv file cropPar = io_.read_csv(input_csv) cropPar = np.atleast_2d(cropPar) crop_proj = [] crop_par_proj = [] spp = int(val['crop_proj_spp1']) harvYield = np.zeros(cfg.N_YEARS) harvYield[int(val['crop_proj_start1']):int(val['crop_proj_end1'])] = float( val['crop_proj_yd1']) harvFrac = float(val['crop_proj_left1']) ci = CropParams.from_species_index(spp) c = CropModel(ci, harvYield, harvFrac) crop_proj.append(c) crop_par_proj.append(ci) spp = int(val['crop_proj_spp2']) harvYield = np.zeros(cfg.N_YEARS) harvYield[int(val['crop_proj_start2']):int(val['crop_proj_end2'])] = float( val['crop_proj_yd2']) harvFrac = float(val['crop_proj_left2']) ci = CropParams.from_species_index(spp) c = CropModel(ci, harvYield, harvFrac) crop_proj.append(c) crop_par_proj.append(ci) spp = int(val['crop_proj_spp3']) harvYield = np.zeros(cfg.N_YEARS) harvYield[int(val['crop_proj_start3']):int(val['crop_proj_end3'])] = float( val['crop_proj_yd3']) harvFrac = float(val['crop_proj_left3']) ci = CropParams.from_species_index(spp) c = CropModel(ci, harvYield, harvFrac) crop_proj.append(c) crop_par_proj.append(ci) # soil cover for baseline cover_base = np.zeros(12) cover_base[int(val['base_cvr_mth_st']):int(val['base_cvr_mth_en'])] = int( val['base_cvr_pres']) # soil cover for project cover_proj = np.zeros(12) cover_proj[int(val['proj_cvr_mth_st']):int(val['proj_cvr_mth_en'])] = int( val['proj_cvr_pres']) # Solve to y=0 forRoth = ForwardRothC(soil, climate, cover_base, Ci=invRoth.eqC, crop=crop_base, fire=fire_base, solveToValue=True) # Soil carbon for baseline and project roth_base = ForwardRothC(soil, climate, cover_base, Ci=forRoth.SOC[-1], crop=crop_base, tree=[tree_base], litter=[l_base], fire=fire_base) roth_proj = ForwardRothC(soil, climate, cover_proj, Ci=forRoth.SOC[-1], crop=crop_proj, tree=[tree_proj1, tree_proj2, tree_proj3], litter=[l_proj], fire=fire_proj) # Emissions stuff emit_base = emit_cl.Emission(forRothC=roth_base, crop=crop_base, tree=[tree_base], litter=[l_base], fert=[sf_base], fire=fire_base) emit_proj = emit_cl.Emission(forRothC=roth_proj, crop=crop_proj, tree=[tree_proj1, tree_proj2, tree_proj3], litter=[l_proj], fert=[sf_proj], fire=fire_proj) # ---------- # Printing outputs # ---------- # print stuff print("location: ", loc) climate.print_() soil.print_() growth1.print_() growth2.print_() growth3.print_() tree_proj1.print_biomass() tree_proj1.print_balance() tree_proj2.print_biomass() tree_proj2.print_balance() tree_proj3.print_biomass() tree_proj3.print_balance() forRoth.print_() roth_base.print_() roth_proj.print_() # crop emissions print print("\n\nCROP EMISSIONS (t CO2)") print("=================\n") print("baseline project") crop_base_emit = emit_cl.Emission(crop=crop_base, fire=fire_base) crop_proj_emit = emit_cl.Emission(crop=crop_proj, fire=fire_proj) crop_diff = crop_proj_emit.emissions - crop_base_emit.emissions for i in range(len(crop_base_emit.emissions)): print(crop_base_emit.emissions[i], crop_proj_emit.emissions[i], crop_diff[i]) print("\nTotal crop difference: ", sum(crop_diff), " t CO2 ha^-1") print("Average crop difference: ", np.mean(crop_diff)) # fert emissions print print("\n\nFERTILISER EMISSIONS (t CO2)") print("=================\n") print("baseline project") fert_base_emit = emit_cl.Emission(fert=[sf_base]) fert_proj_emit = emit_cl.Emission(fert=[sf_proj]) fert_diff = fert_proj_emit.emissions - fert_base_emit.emissions for i in range(len(fert_base_emit.emissions)): print(fert_base_emit.emissions[i], fert_proj_emit.emissions[i], fert_diff[i]) print("\nTotal fertiliser difference: ", sum(fert_diff), " t CO2 ha^-1") print("Average fertiliser difference: ", np.mean(fert_diff)) # litter emissions print print("\n\nLITTER EMISSIONS (t CO2)") print("=================\n") print("baseline project") lit_base_emit = emit_cl.Emission(litter=[l_base], fire=fire_base) lit_proj_emit = emit_cl.Emission(litter=[l_proj], fire=fire_proj) lit_diff = lit_proj_emit.emissions - lit_base_emit.emissions for i in range(len(lit_base_emit.emissions)): print(lit_base_emit.emissions[i], lit_proj_emit.emissions[i], lit_diff[i]) print("\nTotal Litter difference: ", sum(lit_diff), " t CO2 ha^-1") print("Average Litter difference: ", np.mean(lit_diff)) # fire emissions print print("\n\nFIRE EMISSIONS (t CO2)") print("=================\n") print("baseline project") fire_base_emit = emit_cl.Emission(fire=fire_base) fire_proj_emit = emit_cl.Emission(fire=fire_proj) fire_diff = fire_proj_emit.emissions - fire_base_emit.emissions for i in range(len(fire_base_emit.emissions)): print(fire_base_emit.emissions[i], fire_proj_emit.emissions[i], fire_diff[i]) print("\nTotal Fire difference: ", sum(fire_diff), " t CO2 ha^-1") print("Average Fire difference: ", np.mean(fire_diff)) # tree emissions print print("\n\nTREE EMISSIONS (t CO2)") print("=================\n") print("baseline project") tree_base_emit = emit_cl.Emission(tree=[tree_base], fire=fire_base) tree_proj_emit = emit_cl.Emission( tree=[tree_proj1, tree_proj2, tree_proj3], fire=fire_proj) tree_diff = tree_proj_emit.emissions - tree_base_emit.emissions for i in range(len(tree_base_emit.emissions)): print(tree_base_emit.emissions[i], tree_proj_emit.emissions[i], tree_diff[i]) print("\nTotal tree difference: ", sum(tree_diff), " t CO2 ha^-1") print("Average tree difference: ", np.mean(tree_diff)) # soil diff emissions print print("\n\nSOIL EMISSIONS (t CO2)") print("=================\n") print("baseline project") soil_base_emit = emit_base.emissions - ( crop_base_emit.emissions + fert_base_emit.emissions + lit_base_emit.emissions + fire_base_emit.emissions + tree_base_emit.emissions) soil_proj_emit = emit_proj.emissions - ( crop_proj_emit.emissions + fert_proj_emit.emissions + lit_proj_emit.emissions + fire_proj_emit.emissions + tree_proj_emit.emissions) soil_diff = soil_proj_emit - soil_base_emit for i in range(len(emit_base.emissions)): print(soil_base_emit[i], soil_proj_emit[i], soil_diff[i]) print("\nTotal Soil difference: ", sum(soil_diff), " t CO2 ha^-1") print("Average Soil difference: ", np.mean(soil_diff)) # total emissions print print("\n\nTOTAL EMISSIONS (t CO2)") print("=================\n") print("baseline project") emit_diff = emit_proj.emissions - emit_base.emissions for i in range(len(emit_base.emissions)): print(emit_base.emissions[i], emit_proj.emissions[i], emit_diff[i]) print("\nTotal difference: ", sum(emit_diff), " t CO2 ha^-1") print("Average difference: ", np.mean(emit_diff)) # summary of GHG pools print("\n\nSUMMARY OF EMISSIONS (t CO2)") print("over " + str(val['yrs_acct']) + " years") print("=================\n") print("baseline project") print("\nTotal crop difference: ", sum(crop_diff), " t CO2 ha^-1") print("\nTotal fertiliser difference: ", sum(fert_diff), " t CO2 ha^-1") print("\nTotal litter difference: ", sum(lit_diff), " t CO2 ha^-1") print("\nTotal fire difference: ", sum(fire_diff), " t CO2 ha^-1") print("\nTotal tree difference: ", sum(tree_diff), " t CO2 ha^-1") print("\nTotal Soil difference: ", sum(soil_diff), " t CO2 ha^-1") print("\nTotal difference: ", sum(emit_diff), " t CO2 ha^-1") # Save stuff # starting plot output number st = 1 dir = cfg.OUT_DIR + "_" + mod_run + "\plot_" + str(n + st) if os.path.exists(dir): shutil.rmtree(dir) os.makedirs(dir) plot_name = dir + "\plot_" + str(n + st) climate.save_(plot_name + "_climate.csv") soil.save_(plot_name + "_soil.csv") growth1.save_(plot_name + "_growth1.csv") growth2.save_(plot_name + "_growth2.csv") growth3.save_(plot_name + "_growth3.csv") tree_proj1.save_(plot_name + "_tree_proj1.csv") tree_proj2.save_(plot_name + "_tree_proj2.csv") tree_proj3.save_(plot_name + "_tree_proj3.csv") i = 1 for i in range(len(crop_base)): crop_base[i].save_(plot_name + "_crop_model_base_" + str(i) + ".csv") crop_par_base[i].save_(plot_name + "_crop_params_base_" + str(i) + ".csv") crop_proj[i].save_(plot_name + "_crop_model_proj_" + str(i) + ".csv") crop_par_proj[i].save_(plot_name + "_crop_params_proj_" + str(i) + ".csv") invRoth.save_(plot_name + "_invRoth.csv") forRoth.save_(plot_name + "_forRoth.csv") roth_base.save_(plot_name + "_soil_model_base.csv") roth_proj.save_(plot_name + "_soil_model_proj.csv") emit_proj.save_(emit_base, emit_proj, plot_name + "_emit_proj.csv") with open( cfg.OUT_DIR + "_" + mod_run + "\plot_" + str(n + st) + "\plot_" + str(n + st) + "_emissions_all_pools_per_year.csv", 'w+') as csvfile: writer = csv.writer(csvfile, delimiter=',') writer.writerow([ "emit_base", "emit_proj", "emit_diff", "soil_base", "soil_proj", "soil_diff", "tree_base", "tree_proj", "tree_diff", "fire_base", "fire_proj", "fire_diff", "lit_base", "lit_proj", "lit_diff", "fert_base", "fert_proj", "fert_diff", "crop_base", "crop_proj", "crop_diff", ]) for i in range(len(emit_base.emissions)): writer.writerow([ emit_base.emissions[i], emit_proj.emissions[i], emit_diff[i], soil_base_emit[i], soil_proj_emit[i], soil_diff[i], tree_base_emit.emissions[i], tree_proj_emit.emissions[i], tree_diff[i], fire_base_emit.emissions[i], fire_proj_emit.emissions[i], fire_diff[i], lit_base_emit.emissions[i], lit_proj_emit.emissions[i], lit_diff[i], fert_base_emit.emissions[i], fert_proj_emit.emissions[i], fert_diff[i], crop_base_emit.emissions[i], crop_proj_emit.emissions[i], crop_diff[i] ]) # Plot stuff growth1.plot_(saveName=plot_name + "_growthFits.png") plt.close() tree_proj1.plot_biomass(saveName=plot_name + "_biomassPools.png") plt.close() tree_proj1.plot_balance(saveName=plot_name + "_massBalance.png") plt.close() tree_proj2.plot_biomass(saveName=plot_name + "_biomassPools.png") plt.close() tree_proj2.plot_balance(saveName=plot_name + "_massBalance.png") plt.close() tree_proj3.plot_biomass(saveName=plot_name + "_biomassPools.png") plt.close() tree_proj3.plot_balance(saveName=plot_name + "_massBalance.png") plt.close() forRoth.plot_(legendStr='initialisation') roth_base.plot_(legendStr='baseline') roth_proj.plot_(legendStr='project', saveName=plot_name + "_soilModel.png") plt.close() emit_base.plot_(legendStr='baseline') emit_proj.plot_(legendStr='project') emit_cl.Emission.ax.plot(emit_diff, label='difference') emit_cl.Emission.ax.legend(loc='best') plt.savefig(os.path.join(cfg.OUT_DIR, plot_name + "_emissions.png")) plt.close() emit_proj.save_(emit_base, emit_proj=emit_proj, file=plot_name + "_emissions.csv") return (sum(crop_diff), sum(fert_diff), sum(lit_diff), sum(fire_diff), sum(tree_diff), sum(soil_diff), sum(emit_diff))