Code:Energy supply 1

From IMAGE
Jump to: navigation, search

The highlighting does not work for MyM code yet.

import sys, os, traceback
from datetime import datetime

import run_utils as ut
import parse_arguments
import make_config_files
# run_model handles all the processing of the coupled framework
import run_model

def make_time_dat(params):
    '''Make time.dat used in run_image_coupled for IMAGE and the interface'''
    try: 
        fp = open(os.path.join(params.outputdir,"time.dat"),"w")
        fp.write("# Basic time data\n")
        fp.write("# Start of simulation\n")
        fp.write("timestart = " + str(params.startyear) + "\n")
        fp.write("\n")
        fp.write("# Start of scenario\n")
        fp.write("timescen = " + str(params.timescen) + "\n")
        fp.write("\n")
        fp.write("# End of simulation\n")
        fp.write("timestop = "+ str(params.endyear) + "\n")
        fp.write("\n")
        fp.write("\n")
        fp.write("# Output frequency\n")
        fp.write("outfreq = " + str(params.outfreq) + "\n")
        fp.write("# Output frequency Gridded output\n")
        fp.write("outfreqGrid = " + str(params.outfreqgrid) + "\n")
        fp.close()
    except Exception as e:
        raise ("Error in creating time.dat! \n" + 
               "(def make_time_dat) " + str(e))

if __name__ == "__main__":

    try:
        # Store current working directory
        workdir = os.getcwd() 
        log = None
        logfile=""
        # store date and time 
        start_time = datetime.now()
        
        # read the run parameters
        input_args = parse_arguments.Input(sys.argv, start_time)
        params = input_args.get_params()
        
        
        # prepare log
        name =  "start_lpj_image"
        runlogdir = os.path.join(params.outputdir,"runlog") 
        loglevel = "DEBUG" if params.ldebug else "INFO"   
    
        log_inst = ut.Model_log(sys.argv, params, 
                                runlogdir,
                                "run_env", 
                                name,
                                loglevel)
        log = log_inst.getLog()
        
        if input_args.outputdirMoved:
            log.warning ("The output directory existed and has been moved!\n" + \
                          "From " + params.outputdir + " to " + params.outputdir + \
                          start_time.strftime("%y-%m-%d-%H-%M") )    

        # Make time.dat used  for IMAGE and the interface
        make_time_dat(params)
        log.info ("Created time.dat")
    
        # Create configuration file for LPJ and GridCells.txt for the interface
        make_config_files.make_config_files(params)       
        log.info ("Created lpj config files.")
        
        # run op grid        
        if not params.llocal:
            log.info ("start run on grid")
            log.info ("submit run_interface_image job,the lpj jobs and postprocessing jobs")  
            process = run_model.Run_grid(params)
            process.run()
            process.post_process()
            process.check_master()
        
        else:
            log.info ("start local run")
            log.info ("start subprocesses run_interface_image, lpj parallel  and postprocessing ")  
            process = run_model.Run_local(params)    
            process.run()   
            # if master =STOP raise an error
            process.check_master()
            process.post_process()
        
    except SystemExit as e:
        if e.code != 0:
            print("Something wrong!\n "+ \
                            "System exit!\n Use: python " + sys.argv[0] + " --help to see the help\n")
            traceback.print_exc()

    except Exception as e:
        if log is not None:
            print ("See logfiles in " + runlogdir )
            log.error(name + " failed.\n " +str(e) + "\n", exc_info=True)           
        else:
            print ("***** ERROR *****")
            print ("start_lpj_image.py failed.")
            print (str(e))
            traceback.print_exc()
    finally:
        os.chdir(workdir)
        if log is not None:
            log.info("End of start_lpj_image.py")
            log_inst.shutdown()