<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://grasswiki.osgeo.org/w/index.php?action=history&amp;feed=atom&amp;title=User%3ANikosA%2FLandsat</id>
	<title>User:NikosA/Landsat - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://grasswiki.osgeo.org/w/index.php?action=history&amp;feed=atom&amp;title=User%3ANikosA%2FLandsat"/>
	<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=User:NikosA/Landsat&amp;action=history"/>
	<updated>2026-07-08T15:20:04Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://grasswiki.osgeo.org/w/index.php?title=User:NikosA/Landsat&amp;diff=19067&amp;oldid=prev</id>
		<title>⚠️NikosA: Modified existing Landsat import script to take care about Landsat8</title>
		<link rel="alternate" type="text/html" href="https://grasswiki.osgeo.org/w/index.php?title=User:NikosA/Landsat&amp;diff=19067&amp;oldid=prev"/>
		<updated>2013-06-30T21:37:00Z</updated>

		<summary type="html">&lt;p&gt;Modified existing Landsat import script to take care about Landsat8&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Processing Landsat scenes =&lt;br /&gt;
&lt;br /&gt;
== Importing Landsat scenes in independent Mapsets ==&lt;br /&gt;
&lt;br /&gt;
A modification of the script found at [http://grasswiki.osgeo.org/wiki/LANDSAT#Automated_data_import LANDSAT#Automated_data_import] to take care for newer Landsat 8 scenes.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
&lt;br /&gt;
# source: &amp;lt;http://grasswiki.osgeo.org/wiki/LANDSAT#Automated_data_import&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# imports&lt;br /&gt;
import os&lt;br /&gt;
import shutil&lt;br /&gt;
import sys&lt;br /&gt;
import glob&lt;br /&gt;
import grass.script as grass&lt;br /&gt;
&lt;br /&gt;
def copy_metafile(mapset):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;!Copies the *MTL.txt metadata file in the cell_misc directory inside the&lt;br /&gt;
    Landsat scene's independent Mapset&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    # get the metadata file&lt;br /&gt;
    try:&lt;br /&gt;
        metafile = glob.glob(mapset + '/*MTL.txt')[0]&lt;br /&gt;
        print '\nThe identified metadata file is &amp;lt;%s&amp;gt;.' % metafile.split('/')[1]&lt;br /&gt;
&lt;br /&gt;
    except IndexError:&lt;br /&gt;
        return&lt;br /&gt;
&lt;br /&gt;
    # get environment variables &amp;amp; define path to &amp;quot;cell_misc&amp;quot;&lt;br /&gt;
    gisenv=grass.gisenv()&lt;br /&gt;
    CELL_MISC_DIR = gisenv['GISDBASE'] + '/' + gisenv['LOCATION_NAME'] + '/' + gisenv['MAPSET'] + '/cell_misc'&lt;br /&gt;
    print 'It will be copied at: &amp;lt;%s&amp;gt;.\n' % CELL_MISC_DIR # better check if really copied!&lt;br /&gt;
&lt;br /&gt;
    # copy the metadata file&lt;br /&gt;
    shutil.copy (metafile, CELL_MISC_DIR)&lt;br /&gt;
&lt;br /&gt;
def get_timestamp(mapset):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;!Gets the timestamp for each band of a Landsat scene from its metadata&lt;br /&gt;
&lt;br /&gt;
    Returns the date of acquisition for each band of a Landsat scene from &lt;br /&gt;
    its metadata file (*MTL.txt)&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
    try:&lt;br /&gt;
        metafile = glob.glob(mapset + '/*MTL.txt')[0]&lt;br /&gt;
&lt;br /&gt;
    except IndexError:&lt;br /&gt;
        return&lt;br /&gt;
&lt;br /&gt;
    result = dict()&lt;br /&gt;
&lt;br /&gt;
    try:&lt;br /&gt;
        fd = open(metafile)&lt;br /&gt;
        for line in fd.readlines():&lt;br /&gt;
            line = line.rstrip('\n')&lt;br /&gt;
            if len(line) == 0:&lt;br /&gt;
                continue&lt;br /&gt;
            if any(x in line for x in ('DATE_ACQUIRED', 'ACQUISITION_DATE')):&lt;br /&gt;
                result['date'] = line.strip().split('=')[1].strip()&lt;br /&gt;
    finally:&lt;br /&gt;
        fd.close()&lt;br /&gt;
 &lt;br /&gt;
    return result&lt;br /&gt;
 &lt;br /&gt;
def import_tifs(mapset):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;!Imports all bands (TIF format) of a Landsat scene&lt;br /&gt;
&lt;br /&gt;
    Imports all bands (TIF format) of a Landsat scene, be it Landsat 5, 7 or 8.&lt;br /&gt;
    All known naming conventions are respected, such as &amp;quot;VCID&amp;quot; and &amp;quot;QA&amp;quot; found&lt;br /&gt;
    in newer Landsat scenes for temperature channels and Quality... respectively.&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
  &lt;br /&gt;
    #grass.message ('Importing... \n') # why is it printed twice?&lt;br /&gt;
    &lt;br /&gt;
    meta = get_timestamp(mapset)&lt;br /&gt;
    &lt;br /&gt;
    for file in os.listdir(mapset):&lt;br /&gt;
      &lt;br /&gt;
        if os.path.splitext(file)[-1] != '.TIF':&lt;br /&gt;
            continue&lt;br /&gt;
        &lt;br /&gt;
        ffile = os.path.join(mapset, file)&lt;br /&gt;
        # if correctly handled below, usr the &amp;quot;any&amp;quot; instruction!&lt;br /&gt;
        if ('QA') in ffile:&lt;br /&gt;
            name = &amp;quot;&amp;quot;.join((os.path.splitext(file)[0].split('_'))[1::2])&lt;br /&gt;
        elif ('VCID') in ffile:&lt;br /&gt;
            name = &amp;quot;&amp;quot;.join((os.path.splitext(file)[0].split('_'))[1::2])&lt;br /&gt;
        else:&lt;br /&gt;
            name = os.path.splitext(file)[0].split('_')[-1]&lt;br /&gt;
        &lt;br /&gt;
        # found a wrongly named *MTL.TIF file in LE71610432005160ASN00 &amp;gt; issue Warning!&lt;br /&gt;
        # really break the import process?&lt;br /&gt;
        if ('MTL') in ffile: &lt;br /&gt;
            print(&amp;quot;Found a wrongly named *MTL.TIF file!&amp;quot;)&lt;br /&gt;
            print(&amp;quot;Please rename the extension to .txt&amp;quot;)&lt;br /&gt;
            break&lt;br /&gt;
        elif ('QA') in ffile:&lt;br /&gt;
            band = name&lt;br /&gt;
        elif len(name) == 3 and name[-1] == '0':&lt;br /&gt;
            band = int(name[1:2])&lt;br /&gt;
        elif len(name) == 3 and name[-1] != '0':&lt;br /&gt;
            band = int(name[1:3])&lt;br /&gt;
        else:&lt;br /&gt;
            band = int(name[-1:])&lt;br /&gt;
        &lt;br /&gt;
        grass.message('%s &amp;gt; %s@%s...' % (file, name, mapset))&lt;br /&gt;
&lt;br /&gt;
        # create mapset of interest&lt;br /&gt;
        grass.run_command('g.mapset',&lt;br /&gt;
                          flags = 'c',&lt;br /&gt;
                          mapset = mapset,&lt;br /&gt;
                          quiet = True,&lt;br /&gt;
                          stderr = open(os.devnull, 'w'))&lt;br /&gt;
&lt;br /&gt;
        # import bands&lt;br /&gt;
        if isinstance(band, str):&lt;br /&gt;
            grass.run_command('r.in.gdal',&lt;br /&gt;
                              input = ffile,&lt;br /&gt;
                              output = name,&lt;br /&gt;
                              quiet = True,&lt;br /&gt;
                              overwrite = True,&lt;br /&gt;
                              title = 'band %s' % band)&lt;br /&gt;
        else:&lt;br /&gt;
            grass.run_command('r.in.gdal',&lt;br /&gt;
                              input = ffile,&lt;br /&gt;
                              output = name,&lt;br /&gt;
                              quiet = True,&lt;br /&gt;
                              overwrite = True,&lt;br /&gt;
                              title = 'band %d' % band)&lt;br /&gt;
&lt;br /&gt;
        if meta:&lt;br /&gt;
            year, month, day = meta['date'].split('-')&lt;br /&gt;
            if month == '01':&lt;br /&gt;
                month = 'jan'&lt;br /&gt;
            elif month == '02':&lt;br /&gt;
                month = 'feb'&lt;br /&gt;
            elif month == '03':&lt;br /&gt;
                month = 'mar'&lt;br /&gt;
            elif month == '04':&lt;br /&gt;
                month = 'apr'&lt;br /&gt;
            elif month == '05':&lt;br /&gt;
                month = 'may'&lt;br /&gt;
            elif month == '06':&lt;br /&gt;
                month = 'jun'&lt;br /&gt;
            elif month == '07':&lt;br /&gt;
                month = 'jul'&lt;br /&gt;
            elif month == '08':&lt;br /&gt;
                month = 'aug'&lt;br /&gt;
            elif month == '09':&lt;br /&gt;
                month = 'sep'&lt;br /&gt;
            elif month == '10':&lt;br /&gt;
                month = 'oct'&lt;br /&gt;
            elif month == '11':&lt;br /&gt;
                month = 'nov'&lt;br /&gt;
            elif month == '12':&lt;br /&gt;
                month = 'dec'&lt;br /&gt;
 &lt;br /&gt;
            grass.run_command('r.timestamp',&lt;br /&gt;
                              map = name,&lt;br /&gt;
                              date = ' '.join((day, month, year)))&lt;br /&gt;
    # copy MTL file&lt;br /&gt;
    copy_metafile(mapset)&lt;br /&gt;
 &lt;br /&gt;
def main():&lt;br /&gt;
    if len(sys.argv) == 1:&lt;br /&gt;
        for directory in filter(os.path.isdir, os.listdir(os.getcwd())):&lt;br /&gt;
            import_tifs(directory)&lt;br /&gt;
    else:&lt;br /&gt;
        import_tifs(sys.argv[1])&lt;br /&gt;
    &lt;br /&gt;
 &lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>⚠️NikosA</name></author>
	</entry>
</feed>