Example #1
0
    def startup(self):
        """The starting point of the workflow. It runs everytime. 
        It 1) grab project name if given
           2) grab and go to work directory
           3) get and read template(s) options
        """

        #1. Get projectName
        self.projectName = None
        if self.customTemplateFile:
            self.projectName = os.path.splitext(
                os.path.basename(self.customTemplateFile))[0]
            print('Project name:', self.projectName)

        #2. Go to the work directory
        #2.1 Get workDir
        if not self.workDir:
            if autoPath and 'SCRATCHDIR' in os.environ and self.projectName:
                self.workDir = os.path.join(os.getenv('SCRATCHDIR'),
                                            self.projectName, 'PYSAR')
            else:
                self.workDir = os.getcwd()
        self.workDir = os.path.abspath(self.workDir)

        #2.2 Go to workDir
        if not os.path.isdir(self.workDir):
            os.makedirs(self.workDir)
            print('create directory:', self.workDir)
        os.chdir(self.workDir)
        print("Go to work directory:", self.workDir)

        #3. Read templates
        #3.1 Get default template file
        lfile = os.path.join(os.path.dirname(__file__),
                             'defaults/pysarApp_template.txt')  #latest version
        cfile = os.path.join(self.workDir,
                             'pysarApp_template.txt')  #current version
        if not os.path.isfile(cfile):
            print('copy default template file {} to work directory'.format(
                lfile))
            shutil.copy2(lfile, self.workDir)
        else:
            #cfile is obsolete if any key is missing
            ldict = readfile.read_template(lfile)
            cdict = readfile.read_template(cfile)
            if any([key not in cdict.keys() for key in ldict.keys()]):
                print(
                    'obsolete default template detected, update to the latest version.'
                )
                shutil.copy2(lfile, self.workDir)
                #keep the existing option value from obsolete template file
                ut.update_template_file(cfile, cdict)
        self.templateFile = cfile

        # 3.2 read (custom) template files into dicts
        self._read_template()
        return
Example #2
0
def check_obsolete_default_template(inps):
    """Update pysarApp_template.txt file if it's obsolete, a.k.a. lack new option names"""
    template_file = os.path.join(inps.workDir, 'pysarApp_template.txt')
    obsolete_template = False
    current_dict = readfile.read_template(template_file)
    latest_dict = readfile.read_template(inps.autoTemplateFile)
    for key in latest_dict.keys():
        if key not in current_dict.keys():
            obsolete_template = True

    if obsolete_template:
        print('obsolete default template detected, update to the latest template options.')
        shutil.copy2(inps.autoTemplateFile, inps.workDir)
        template_file = ut.update_template_file(template_file, current_dict)
    else:
        print('latest template file detected:', template_file)
    return template_file
Example #3
0
def check_obsolete_default_template(template_file='./pysarApp_template.txt'):
    """Update pysarApp_template.txt file if it's obsolete, a.k.a. lack new option names"""
    obsolete_template = False
    current_dict = readfile.read_template(template_file)
    latest_dict = readfile.read_template(TEMPLATE)
    for key in latest_dict.keys():
        if key not in current_dict.keys():
            obsolete_template = True

    if obsolete_template:
        print(
            'obsolete default template detected, update to the latest template options.'
        )
        with open(template_file, 'w') as f:
            f.write(TEMPLATE)
        template_file = ut.update_template_file(template_file, current_dict)
    else:
        print('default template file exists: ' + template_file)
    return template_file
Example #4
0
    def _read_template(self):
        # read custom template, to:
        # 1) update default template
        # 2) add metadata to ifgramStack file and HDF-EOS5 file
        self.customTemplate = None
        if self.customTemplateFile:
            cfile = self.customTemplateFile
            # Copy custom template file to INPUTS directory for backup
            inputs_dir = os.path.join(self.workDir, 'INPUTS')
            if not os.path.isdir(inputs_dir):
                os.makedirs(inputs_dir)
                print('create directory:', inputs_dir)
            if ut.run_or_skip(out_file=os.path.join(inputs_dir,
                                                    os.path.basename(cfile)),
                              in_file=cfile,
                              check_readable=False) == 'run':
                shutil.copy2(cfile, inputs_dir)
                print('copy {} to INPUTS directory for backup.'.format(
                    os.path.basename(cfile)))

            # Read custom template
            print('read custom template file:', cfile)
            cdict = readfile.read_template(cfile)

            # correct some loose type errors
            standardValues = {
                'def': 'auto',
                'default': 'auto',
                'y': 'yes',
                'on': 'yes',
                'true': 'yes',
                'n': 'no',
                'off': 'no',
                'false': 'no'
            }
            for key, value in cdict.items():
                if value in standardValues.keys():
                    cdict[key] = standardValues[value]

            for key in ['pysar.deramp', 'pysar.troposphericDelay.method']:
                if key in cdict.keys():
                    cdict[key] = cdict[key].lower().replace('-', '_')

            if 'processor' in cdict.keys():
                cdict['pysar.load.processor'] = cdict['processor']

            # these metadata are used in load_data.py only, not needed afterwards
            # (in order to manually add extra offset when the lookup table is shifted)
            # (seen in ROI_PAC product sometimes)
            for key in ['SUBSET_XMIN', 'SUBSET_YMIN']:
                if key in cdict.keys():
                    cdict.pop(key)

            self.customTemplate = dict(cdict)

            # Update default template file based on custom template
            print('update default template based on input custom template')
            self.templateFile = ut.update_template_file(
                self.templateFile, self.customTemplate)

        print('read default template file:', self.templateFile)
        self.template = readfile.read_template(self.templateFile)
        self.template = ut.check_template_auto_value(self.template)

        # correct some loose setup conflicts
        if self.template['pysar.geocode'] is False:
            for key in ['pysar.save.hdfEos5', 'pysar.save.kmz']:
                if self.template[key] is True:
                    self.template['pysar.geocode'] = True
                    print('Turn ON pysar.geocode in order to run {}.'.format(
                        key))
                    break
        return
Example #5
0
def read_template(inps):
    print('\n**********  Read Template File  **********')
    # default template
    inps.templateFile = os.path.join(inps.workDir, 'pysarApp_template.txt')
    if not os.path.isfile(inps.templateFile):
        print('generate default template file: ' + inps.templateFile)
        with open(inps.templateFile, 'w') as f:
            f.write(TEMPLATE)
    else:
        inps.templateFile = check_obsolete_default_template(inps.templateFile)

    # custom template
    templateCustom = None
    if inps.templateFileCustom:
        # Copy custom template file to work directory
        if ut.update_file(os.path.basename(inps.templateFileCustom),
                          inps.templateFileCustom,
                          check_readable=False):
            shutil.copy2(inps.templateFileCustom, inps.workDir)
            print('copy {} to work directory'.format(
                os.path.basename(inps.templateFileCustom)))

        # Read custom template
        print('read custom template file: ' + inps.templateFileCustom)
        templateCustom = readfile.read_template(inps.templateFileCustom)
        # correct some loose type errors
        for key in templateCustom.keys():
            if templateCustom[key].lower() in ['default']:
                templateCustom[key] = 'auto'
            elif templateCustom[key].lower() in ['n', 'off', 'false']:
                templateCustom[key] = 'no'
            elif templateCustom[key].lower() in ['y', 'on', 'true']:
                templateCustom[key] = 'yes'
        for key in ['pysar.deramp', 'pysar.troposphericDelay.method']:
            if key in templateCustom.keys():
                templateCustom[key] = templateCustom[key].lower().replace(
                    '-', '_')
        if 'processor' in templateCustom.keys():
            templateCustom['pysar.load.processor'] = templateCustom[
                'processor']

        # Update default template with custom input template
        print('update default template based on input custom template')
        inps.templateFile = ut.update_template_file(inps.templateFile,
                                                    templateCustom)

    if inps.generate_template:
        raise SystemExit('Exit as planned after template file generation.')

    print('read default template file: ' + inps.templateFile)
    template = readfile.read_template(inps.templateFile)
    template = ut.check_template_auto_value(template)

    # Get existing files name: unavco_attributes.txt
    try:
        inps.unavcoMetadataFile = ut.get_file_list('unavco_attribute*txt',
                                                   abspath=True)[0]
    except:
        inps.unavcoMetadataFile = None
        print('No UNAVCO attributes file found.')

    return inps, template, templateCustom