コード例 #1
0
 def run(self):
     """
     run the command
     """
     if self.args and self.args[0] in aaem.cli.AaemCommand.commands.keys():
         print aaem.cli.AaemCommand.commands[self.args[0]].usagestr
         print aaem.cli.AaemCommand.commands[self.args[0]].description
         return 0
     elif self.args:
         msg = ("command: [" + self.args[0] + "] not recognized.\n"
         "  available commands: " + str(aaem.cli.AaemCommand.commands.keys()) 
                 )
         cli_lib.print_error_message (msg)
     else:
         print aaem.cli.AaemCommand.usagestr
         print aaem.cli.AaemCommand.description    
コード例 #2
0
    def run(self):
        """
        run the command
        """
        if self.args and len(self.args) < 2:
            msg = "REFRESH ERROR: please provide a path to the model and " +\
                                  "a path to the aaem data repo"
            cli_lib.print_error_message(msg, RefreshCommand.usagestr)
            return 0

        if self.args and os.path.exists(self.args[0]):
            model_root = os.path.abspath(self.args[0])
        else:
            msg = "REFRESH ERROR: please provide a path to the model"
            cli_lib.print_error_message(msg, RefreshCommand.usagestr)
            return 0

        if self.args and os.path.exists(self.args[1]):
            repo = os.path.abspath(self.args[1])
        else:
            msg = "REFRESH ERROR: please provide a path to the aaem data repo"
            cli_lib.print_error_message(msg, RefreshCommand.usagestr)
            return 0

        if self.args:
            try:
                tag = self.args[2]
            except IndexError:
                tag = None

        force = True
        if self.flags.force is None:
            force = False

        if self.flags.dev:
            coms = __DEV_COMS__
            interties = False
        else:
            coms = read_csv(os.path.join(repo,'community_list.csv'),
                         comment="#",index_col=0).Community.tolist()


        make_globals = True
        if self.flags.make_globals is None:
            make_globals = False

        #~ coms = ['Brevig Mission']
        my_setup = driver.Setup(model_root, repo, sorted(coms), tag)
        if not my_setup.setup(
                    force = force,
                    ng_coms=['Barrow','Nuiqsut'],
                    make_globals = make_globals
                ):
            pth = os.path.join(model_root, my_setup.tag)
            msg = "REFRESH ERRO: " + pth + \
                    " exists. Use force flag (-f) to overwrite"
            cli_lib.print_error_message(msg, RefreshCommand.usagestr)
コード例 #3
0
    def run(self):
        """
        run the command
        """
        if self.args and os.path.exists(self.args[0]) \
                     and os.path.exists(self.args[1]):
            results1 = os.path.abspath(self.args[0])
            results2 = os.path.abspath(self.args[1])
        else:
            msg = "Compare Error: needs 2 existing runs"
            cli_lib.print_error_message(msg, CompareCommand.usagestr)
            return 0


        coms = self.args[2:]
        if len(coms) != 0 :
            cli_lib.compare_indepth(results1, results2, coms)
        else:
            cli_lib.compare_high_level(results1, results2)
コード例 #4
0
    def run(self):
        """
        run the command
        """
        # get arguments

        # log file?
        sout = sys.stdout
        if self.flags.log:
            sys.stdout = open(self.flags.log, 'w')

        if self.args and os.path.exists(self.args[0]):
            base = os.path.abspath(self.args[0])
        else:
            msg = "RUN ERROR: needs a directory"
            cli_lib.print_error_message(msg, RunCommand.usagestr)
            return 0

        force = True
        if self.flags.force is None:
            force = False

        if os.path.isfile(base):
            #run script
            #~ print 'Runnint script ... need to reimplement'

            try:
                script = driver.script_validator(base)
            except StandardError as e:
                cli_lib.print_error_message('SCRIPT ERROR:\n' + str(e))
                return 0

            ## check existing results
            res_dir = 'results'
            if script['global']['results tag']:
                res_dir += '_' + script['global']['results tag']
            base = script['global']['root']

            #~ ## print os.path.join(base, res_dir)
            if os.path.exists(os.path.join(base, res_dir)) and force:
                shutil.rmtree(os.path.join(base, res_dir))
            elif os.path.exists(os.path.join(base, res_dir)):
                msg =  "RUN ERROR: " + os.path.join(base, res_dir) + \
                            " exists. Use force flag (-f) to overwrite"
                cli_lib.print_error_message(msg, RunCommand.usagestr)
                return 0

            # run
            run_driver = driver.Driver(base)

            for com in script['communities']:
                print 'community:', com['community'], 'name:', com['ID']
                try:
                    #~ print com['config']
                    run_driver.run(
                        com['config'],
                        global_config=script['global']['global config'],
                        tag=script['global']['results tag'],
                        scalers=com['scalers'],
                        alt_save_name=com['ID'])
                except (RuntimeError, IOError) as e:
                    print e
                    msg = "RUN ERROR: "+ com['community'] + \
                                " not a configured community/project"
                    cli_lib.print_error_message(msg)

            # save Summaries
            try:
                run_driver.save_summaries(script['global']['results tag'])
            except IOError as e:
                #~ print e
                msg = "RUN ERROR: No valid communities/projects provided"
                cli_lib.print_error_message(msg)
                return 0
            run_driver.save_metadata(script['global']['results tag'])

        else:
            # run regular
            # Get communities to run
            if self.flags.dev:
                # Development coms
                coms = __DEV_COMS__
            elif len(self.args[1:]) != 0:
                # listed coms
                coms = self.args[1:]
                if len(coms) == 1:
                    if coms[0][-1] == '*':
                        coms = [c for c in cli_lib.get_config_coms(base) \
                            if c.find(coms[0][:-1]) != -1]
                        #~ print coms
                    else:
                        # Regional coms
                        region = coms[0]
                        coms = cli_lib.get_regional_coms(region, base)
                # model thinks its barrow
                if 'Utqiagvik' in coms:
                    coms[coms.index('Utqiagvik')] = 'Barrow'
            else:
                # ALL COMS
                try:
                    coms = cli_lib.get_config_coms(base)
                except OSError:
                    msg = ("RUN ERROR: structure to run model does "
                           "not exist at provided path")
                    cli_lib.print_error_message(msg)
                    return 0
            # other options
            #~ plot = False
            #~ img_dir = None
            #~ if not self.flags.plot is None:
            #~ plot = True
            #~ img_dir = self.flags.plot

            tag = ''
            if not self.flags.tag is None:
                tag = self.flags.tag
            if tag != '':
                rd = 'results_' + tag
            else:
                rd = 'results'

            global_config = None
            if not self.flags.global_config is None:
                global_config = self.flags.global_config
                if not os.path.isfile(global_config):
                    msg = 'FLAG ERROR: global config specified with option' + \
                        ' --global(-g) is not a file'
                    raise RuntimeError, msg
            if global_config is None:
                gc = os.path.join(base, 'config', '__global_config.yaml')
                if os.path.isfile(gc):
                    print 'Using ' + gc + ' ad global config'
                    global_config = gc

            scalers = driver.default_scalers
            if not self.flags.scalers is None:
                items = self.flags.scalers.replace('{','').\
                                           replace('}','').\
                                           strip().split(',')
                for i in items:
                    item = i.split(":")
                    key = item[0].strip().strip('"\'')
                    try:
                        #~ print scalers
                        scalers[key]
                    except KeyError:
                        msg = "SCALER ERROR: " + key + " is not a valid scaler"
                        cli_lib.print_error_message(msg)
                        return 0
                    scalers[key] = float(item[1])

            ## results exist?
            if os.path.exists(os.path.join(base, rd)) and force:
                shutil.rmtree(os.path.join(base, rd))
            elif os.path.exists(os.path.join(base, rd)):
                msg =  "RUN ERROR: " + os.path.join(base, rd) + \
                            " exists. Use force flag (-f) to overwrite"
                cli_lib.print_error_message(msg, RunCommand.usagestr)
                return 0

            ## Run
            #~ print sorted(coms)
            run_driver = driver.Driver(base)
            for com in sorted(coms):
                if com == 'Barrow':
                    print 'Utqiagvik'
                else:
                    print com
                try:
                    pth = os.path.join(base, 'config', com + '.yaml')
                    run_driver.run(pth,
                                   global_config=global_config,
                                   tag=tag,
                                   scalers=scalers)

                except (RuntimeError, IOError) as e:
                    print e
                    msg = "RUN ERROR: "+ com + \
                                    " not a configured community/project"
                    cli_lib.print_error_message(msg)

            # save summaries
            try:
                run_driver.save_summaries(tag)
            except IOError as e:
                print e
                msg = "RUN ERROR: No valid communities/projects provided"
                cli_lib.print_error_message(msg)
                return 0

            try:
                name = 'Utqiagvik'
                os.rename(os.path.join(base, rd, 'Barrow'),
                          os.path.join(base, rd, 'Utqiagvik_Barrow'))
                for f_name in os.listdir(
                        os.path.join(base, rd, 'Utqiagvik_Barrow')):
                    if f_name.find('.csv') != -1:
                        f = os.path.join(base, rd, 'Utqiagvik_Barrow', f_name)
                        with open(f, 'r') as in_f:
                            text = in_f.read()
                        with open(f, 'w') as out_f:
                            out_f.write(
                                text.replace('Barrow', 'Utqiagvik (Barrow)'))
                        #~ print f
                        os.rename(
                            f,
                            os.path.join(
                                base, rd, 'Utqiagvik_Barrow',
                                f_name.replace('Barrow', 'Utqiagvik_Barrow')))
                for f_name in os.listdir(
                        os.path.join(base, rd, 'Utqiagvik_Barrow',
                                     'component_outputs')):
                    #~ print f_name
                    if f_name.find('.csv') != -1:
                        f = os.path.join(base, rd, 'Utqiagvik_Barrow',
                                         'component_outputs', f_name)
                        with open(f, 'r') as in_f:
                            text = in_f.read()
                        with open(f, 'w') as out_f:
                            out_f.write(
                                text.replace('Barrow', 'Utqiagvik (Barrow)'))
                        #~ print f
                        os.rename(
                            f,
                            os.path.join(
                                base, rd, 'Utqiagvik_Barrow',
                                'component_outputs',
                                f_name.replace('Barrow', 'Utqiagvik_Barrow')))
            except StandardError as e:
                # no need to do any thing if 'Barrow','Utqiagvik' not necessary
                #~ print e
                pass
            run_driver.save_metadata(tag)

        sys.stdout = sout
コード例 #5
0
    def run(self):
        """
        run the command
        """
        # argument setup
        if len(self.args) < 2:
            msg = "SETUP ERROR: provide location to setup & the data repo"
            cli_lib.print_error_message(msg, SetupCommand.usagestr)
            return 0

        model_root = self.args[0]

        repo = os.path.abspath(self.args[1])

        if not os.path.exists(repo):
            msg = "SETUP ERROR: provided repo directory does not exist"
            cli_lib.print_error_message(msg, SetupCommand.usagestr)
            return 0

        try:
            tag = self.args[2]
        except IndexError:
            tag = None

        # option setup
        force = True
        if self.flags.force is None:
            force = False

        ## SET UP
        print repo
        try:
            if self.flags.dev:
                coms = __DEV_COMS__
            else:
                coms = read_csv(os.path.join(repo, 'community_list.csv'),
                                comment="#",
                                index_col=0).Community.tolist()
            print "Setting up ..."

            my_setup = driver.Setup(model_root, repo, sorted(coms), tag)
            if not my_setup.setup(force=force, ng_coms=['Barrow', 'Nuiqsut']):
                pth = os.path.join(model_root, my_setup.tag)
                msg = "SETUP ERROR: " + pth + \
                        " exists. Use force flag (-f) to overwrite"
                cli_lib.print_error_message(msg, SetupCommand.usagestr)
                return

        except IOError as e:
            print e
            msg = "SETUP ERROR: provided repo has missing files"
            cli_lib.print_error_message(msg, SetupCommand.usagestr)
            return

        ## Generate inital results
        print "Running ..."

        base = os.path.join(model_root, my_setup.tag)
        try:
            shutil.rmtree(os.path.join(base, 'results'))
        except OSError:
            pass

        my_driver = driver.Driver(base)
        for com in sorted(cli_lib.get_config_coms(base)):
            print com
            my_driver.run(com)
        my_driver.save_summaries()
        my_driver.save_metadata()
コード例 #6
0
    def run(self):
        """
        run the command
        """
        # get arguments

        # log file?
        if self.args and os.path.exists(self.args[0]):
            root = os.path.abspath(self.args[0])
        else:
            msg = "HTML ERROR: needs path to model folder"
            cli_lib.print_error_message(msg, self.usagestr)
            return 0

        try:
            tag = self.args[1]
        except IndexError:
            tag = ''

        if self.flags.alt_out is None:
            ot = tag
            if ot != '':
                ot = '_' + ot
            out = os.path.join(root, 'results' + ot, '__web_summaries')
        else:
            out = self.flags.alt_out

        force = True
        if self.flags.force is None:
            force = False

        if os.path.exists(out) and force:
            shutil.rmtree(out)
        elif os.path.exists(out):
            msg =  "HTML ERROR: " + out + \
                        " exists. Use force flag (-f) to overwrite"
            cli_lib.print_error_message(msg, self.usagestr)
            return 0

        print "Loading Results..."
        ws = web.WebSummary(root, out, tag)
        print "Generating Summaries..."

        #~ try:
        ws.generate_all()
        try:
            pth = os.path.join(out, 'Barrow')
            os.rename(pth, pth.replace('Barrow', 'Utqiagvik'))

            pth = os.path.join(out, 'Utqiagvik', 'csv')
            for f_name in os.listdir(pth):
                f = os.path.join(pth, f_name)
                #~ print f
                os.rename(f, f.replace('Barrow', 'Utqiagvik'))

            pth = os.path.join(out, 'Utqiagvik')
            for f_name in os.listdir(pth):
                if f_name.find('.html') != -1:
                    f = os.path.join(pth, f_name)
                    with open(f, 'r') as in_f:
                        text = in_f.read()
                    with open(f, 'w') as out_f:
                        out_f.write(
                            text.replace('Barrow', 'Utqiagvik').replace(
                                'ctobrw', 'Barrow'))
                        #~ out_f.write()

            f = os.path.join(out, 'map.js')
            with open(f, 'r') as in_f:
                text = in_f.read()
            with open(f, 'w') as out_f:
                out_f.write(text.replace('Barrow', 'Utqiagvik'))
            f = os.path.join(out, 'navbar.js')
            with open(f, 'r') as in_f:
                text = in_f.read()
            with open(f, 'w') as out_f:
                out_f.write(text.replace('Barrow', 'Utqiagvik'))
        except:
            pass

        print 'Complete'
コード例 #7
0
    def run(self):
        """
        run the command
        """
        # get arguments
        print "WARNING: this command may not produce desired results"
        if len(self.args) != 2:
            msg = ("GET-DATA ERROR: provide path to the repo, and a"
                   " path to output")
            cli_lib.print_error_message(msg, GetDataCommand.usagestr)
            return 0

        if self.args and os.path.exists(self.args[0]):
            repo = os.path.abspath(self.args[0])
            print repo
        else:
            msg = "GET-DATA ERROR: path to the repo must exist"
            cli_lib.print_error_message(msg, GetDataCommand.usagestr)
            return 0

        out = os.path.abspath(self.args[1])
        #~ else:
        #~ msg = "GET-DATA ERROR: needs an output directory"
        #~ cli_lib.print_error_message(msg, GetDataCommand.usagestr)
        #~ return 0
        print out

        force = True
        if self.flags.force is None:
            force = False

        if os.path.exists(out) and force:
            shutil.rmtree(out)
        elif not os.path.exists(out):
            pass
        else:
            msg =  "RUN ERROR: " + out + \
                            " exists. Use force flag (-f) to overwrite"
            cli_lib.print_error_message(msg, GetDataCommand.usagestr)
            return 0

        os.makedirs(out)

        with open(os.path.join(repo, 'VERSION'), 'r') as v:
            version = v.read().strip()

        formats = ['csv', 'yaml']
        files = [f for f in os.listdir(repo) if f.split('.')[-1] in formats]
        self.metadata = {f: 'repo - ' + version for f in files}
        #~ from pprint import pprint
        #~ pprint(metadata)

        for f in files:
            shutil.copy2(os.path.join(repo, f), out)

        try:
            self.id_data = self.get_id_data()
        except:
            self.id_data = None
            print 'Error retrieving community ID data. Using repo version'

        try:
            self.getpce_data(out)
        except:
            print 'Error retrieving PCE data. Using repo version'

        #~ self.geteia_data(out)# wont work api doesn't have proper forgien keys

        try:
            self.getdph_data(out)
        except:
            print 'Error retrieving power house data. Using repo version'

        try:
            self.getfps_data(out)
        except StandardError as e:
            print e
            print 'Error retrieving fuel price survey. Using repo version'

        with open(os.path.join(out, '__metadata.yaml'), 'w') as meta:
            meta.write(yaml.dump(self.metadata, default_flow_style=False))

        with open(os.path.join(out, 'VERSION'), 'w') as v:
            v.write('generated_from_command')