def setup_prjdir(self):
        # update the prj_dir for new storm event - TOO
        # <prj_dir>/parm/<event> - to exists 
        # <prj_dir>/fix/meshes/<event>   hsofs  hsofs_elevated20m ??
        # <prj_dir>/sorc/runupforecast.fd ??
        p = self.NWROOT
        print("Checking health of project directory: {}".format(p))
        if not nus.exist(p):
            try:
                subprocess.run(['mkdir', '-pv', p ], check=True)
                subprocess.check_call(['mkdir', '-v', self.SOURCE, self.SCRIPT, self.ECF, self.FIX,
                                       self.USH, self.EXEC, self.PARM, self.JOBS], cwd=p)
            except subprocess.CalledProcessError as err:
                print('Error in creating model home directory: ', err)
        else:
            for dir in self.prj_dir():
                if not nus.exist(dir):
                    try:
                      subprocess.run(['mkdir', '-vp', dir ], check=True)
                    except subprocess.CalledProcessError as err:
                      print('Error in creating %s directory: ' %(dir, err))
                #else:
                #    print("\n%s is healthy" %dir)

        # now check on subdirs:
        for dir in self.prj_subdir():
            if not nus.exist(dir):
                try:
                  subprocess.run(['mkdir', '-vp', dir ], check=True)
                except subprocess.CalledProcessError as err:
                  print('Error in creating %s directory: ' %(dir, err))
    def setup_comdir(self):
        p = self.COMIN
        print("Checking health of com directory: {}".format(p))
        if not nus.exist(p):
            try:
                subprocess.run(['mkdir', '-pv', p ], check=True)
            except subprocess.CalledProcessError as err:
                print('Error in creating model input directory: ', err)

        for dir in self.com_subdir():
            subdir = os.path.join(p, dir)
            if not nus.exist(subdir):
                try:
                  subprocess.run(['mkdir', '-vp', subdir ], check=True)
                except subprocess.CalledProcessError as err:
                  print('Error in creating %s directory: ' %(subdir, err))


        """created on script side with id
        p = self.COMOUT
        if not nus.exist(p):
            print("\nCreating model output directory: {}".format(p))
            try:
                subprocess.run(['mkdir', '-pv', p ], check=True)
            except subprocess.CalledProcessError as err:
                print('Error in creating model input directory: ', err)
        """

        p = self.GESIN
        print("Checking restart directory: {}".format(p))
        if not nus.exist(p):
            try:
                subprocess.run(['mkdir', '-pv', p ], check=True)
            except subprocess.CalledProcessError as err:
                print('Error in creating gesin directory: ', err)


        """created on script side with id
        p = self.GESOUT
        if not nus.exist(p):
            print("\nCreating gesout directory: {}".format(p))
            try:
                subprocess.run(['mkdir', '-pv', p ], check=True)
            except subprocess.CalledProcessError as err:
                print('Error in creating gesout directory: ', err)
        """

        # this is a dev logfile, so creating it here.
        # there is one also in ecf script to create, upon running!! One is extra!!
        p = self.jlogfile
        print("Checking logs directory: {}".format(p))
        if not nus.exist(p):
            try:
                subprocess.run(['mkdir', '-pv', p ], check=True)
            except subprocess.CalledProcessError as err:
                print('Error in creating logs directory: ', err)
    def write_build(self):

        user_module = self.nems_cfg.user_module
        nems_models = self.nems_cfg.nems_models

        p = os.path.join(self.source_dir,'build.sh')

        junk, modulefile = user_module.split(self.source_dir)

        lines = """#!/bin/bash\n
# Description : Script to compile NSEModel NEMS application 
# Usage       : ./build.sh
# Date        : Autogenerated by NSEM at {}\n
# Contacts    : [email protected]
#               [email protected]
#               [email protected]
#               [email protected]

# load modules
source {}

cd NEMS\n""".format(nus.now(2), modulefile[1:])    # remove the '/' from string

        lines += '\n#clean up\n'

        for model in nems_models():
            alias = model.get_alias().upper()
            if not nus.exist(os.path.join(self.source_dir, alias)):
                sys.exit(0)

            lines += 'make -f GNUmakefile distclean_' + \
                      alias + ' COMPONENTS=' + \
                      '"' + alias + '"\n'

        lines += 'make -f GNUmakefile distclean_NEMS COMPONENTS="NEMS"\n'

        lines += '\n#make\n'
        lines += 'make -f GNUmakefile build COMPONENTS="'

        for model in nems_models():
            alias = model.get_alias().upper()
            lines +=  alias+ ' '
        lines = lines[:-1] + '"\n'

        with open(p, 'w') as fptr:
            fptr.write(lines)

        # change mode
        subprocess.call(["chmod", "a+x", p])


        # save
        self.build_script = p
        print("Processed build script %s" %p)
    def storm_dir(self, which):
        # running two storms even for testing overrides ecf/jjob files!
        # TODO - let them know about this case!!
        if which == "ecf":
            which = self.ecf_dir()
        elif which == "job":
            which = self.jjob_dir()

        p = os.path.join(which, self.NET)
        if not nus.exist(p):
            print("\nCreating directory: {}".format(p))
            try:
                subprocess.run(['mkdir', '-pv', p ], check=True)
            except subprocess.CalledProcessError as err:
                print('Error in creating directory: ', err)
        return p
def git_nsem(repo, nco_sorc_dir):
    
    print("\nCloning {} to {}".format(repo, nco_sorc_dir)) 

    # dest_dir is the name part of the repo and so it becomes
    # the location of NEMS and all the models. This is one layer
    # below ROOTDIR, as mentioned in NEMS HOWTO.
    dest_dir = os.path.join(nco_sorc_dir, Path(repo).name)
    if nus.exist(dest_dir):
       print("\nRemoving directory: {}".format(dest_dir))
       try:
           subprocess.run(['rm', '-rf', dest_dir ], check=True)
       except subprocess.CalledProcessError as err:
           print(nus.colory("red", 'Error deleting directory {}\n'.format(err)))

    cmd = ['git', 'clone', '--recursive', repo]      # TO DO - add the user/pass
    try:   
      # must wait to finishe, so check_call or run
      return subprocess.check_call(cmd, cwd=nco_sorc_dir, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
      print(nus.colory("red", "Exception on process, rc=".format(e.returncode)))