[GRASS-dev] Copying Landsat MTL file(s) inside the cell_misc directory -- Python scripting

Nikos Alexandris nik at nikosalexandris.net
Tue May 14 05:25:42 PDT 2013


Nikos Alexandris wrote:

> whose creation is the the import_landsat.py script featured in
> <http://grasswiki.osgeo.org/wiki/LANDSAT#Automated_data_import>?

This question is still valid :-). If my 2 proposed changes don't do something 
seriously wrong, I would like to update the script in the wiki.

> I would like to expand it a bit in copying the respective metadata
> (*MTL.txt) files over to the (respective) "cell_misc" directory?  I guess
> that using the "shutil" is the way to go, like:

1) I managed to integrate another small function (thanks to Luca D for off-
list support), actually a clone of one small function already found in the 
script. It works like:

def copy_metafile(mapset):

    # get the metafile
    try:
        metafile = glob.glob(mapset + '/*MTL.txt')[0]
        print '\nThe identified metadata file is:\n %s\n' % 
metafile.split('/')[1]

    except IndexError:
        return

    # get environment variables & define path to "cell_misc"
    gisenv=grass.gisenv()
    CELL_MISC_DIR = gisenv['GISDBASE'] + '/' + gisenv['LOCATION_NAME'] + '/' + 
gisenv['MAPSET'] + '/cell_misc'
    print 'The identified metadata file will be copied at:\n %s\n' % 
CELL_MISC_DIR

    # copy the metadata file
    shutil.copy (metafile, CELL_MISC_DIR)

I call this small function in the end of the "core" function 
"import_tifs(mapset)" to copy the MTL file in the respective "cell_misc" 
directory, i.e.:

copy_metafile(mapset)




2) The current script checks inside the MTL file for the string 
'ACQUISITION_DATE' to get the date of acquisition. The code (lines 56-57) goes 
like:

    if 'ACQUISITION_DATE' in line:
        result['date'] = line.strip().split('=')[1].strip()

Since newer MTL files contain the string 'DATE_ACQUIRED', another check is 
required. The script certainly works by adding an extra if statement, like:

    if 'DATE_ACQUIRED' in line:
        result['date'] = line.strip().split('=')[1].strip()

Nevertheless, I want to merge the two "if" statements in one. So, my attempt 
goes like

    if 'DATE_ACQUIRED' or 'ACQUISITION_DATE' in line:
        result['date'] = line.strip().split('=')[1].strip()

This is wrong. I have no idea, though, on why the former "simple" if 
statements return a single line, while the latter if statement, which contains 
the "or" operator, does return the complete MTL file content!

And, finally, the following works:

    if 'DATE_ACQUIRED' in line or 'ACQUISITION_DATE' in line:
        result['date'] = line.strip().split('=')[1].strip()

I know the following is a pure python question -- still, can someone shed some 
light on why is there such a difference between

    if 'DATE_ACQUIRED' or 'ACQUISITION_DATE' in line:

and

    if 'DATE_ACQUIRED' in line or 'ACQUISITION_DATE' in line:

?

Thanks, Nikos


More information about the grass-dev mailing list