3

I'm trying to write a Python code that will import LANDSAT satellite images into Grass GIS by adapting this code: http://grass.osgeo.org/wiki/LANDSAT

LANDSAT tiles are downloaded as folders, each containing 7 tiff images (Band 1-7). I therefore have a directory which contains several subdirectories (one for each LANDSAT tile).

My code at present is as follows:

#!/usr/bin/python

import os
import sys
import glob
import grass.script as grass

def import_tifs(dirpath):

    for dirpath, dirname, filenames in os.walk(dirpath):
        for dirname in dirpath:

        dirname = os.path.join(dirpath,dirname)

            for file in os.listdir(dirname):
                if os.path.splitext(file)[-1] != '.TIF':
                    continue
                ffile = os.path.join(dirname, file)
                name = os.path.splitext(file)[0].split(dirname)[-1]

                grass.message('Importing %s -> %s@%s...' % (file, name, dirpath))

                grass.run_command('r.in.gdal',
                                  flags = 'o',
                                  input = ffile,
                                  output = name,
                                  quiet = True,
                                  overwrite = True)

def main():                                 
    if len(sys.argv) == 1:
        for directory in filter(os.path.isdir, os.listdir(os.getcwd())):
            import_tifs(directory)
    else:
        import_tifs(sys.argv[1])

if __name__ == "__main__":
    main()

I'm getting the following error:

Traceback (most recent call last):
  File "C:/Users/Simon/Documents/import_landsat2.py", line
40, in <module>
    main()
  File "C:/Users/Simon/Documents/import_landsat2.py", line
37, in main
    import_tifs(sys.argv[1])
  File "C:/Users/Simon/Documents/import_landsat2.py", line
17, in import_tifs
    for file in os.listdir(dirname):
WindowsError: [Error 3] The system cannot find the path
specified: 'dirpath\\C/*.*'

Can anyone explain what is happening and what I need to do to fix it, or suggest an alternative? Thanks.

www
  • 38,575
  • 12
  • 48
  • 84
si_2012
  • 649
  • 1
  • 6
  • 11

2 Answers2

2

I believe your main problem is that dirname in os.walk() returns a list (not a string), so your subsequent strings (namely dirname = os.path.join(dirpath,dirname)) are a bit malformed. Here is one possible alternative - to test this, I used the full path to the directory as sys.argv[1], but you can make it more dynamic to suit your case. Also, avoid using variable names such as file since they are Python keywords. I couldn't test out your grass.* functions, but hopefully this will be a clear enough example so you can tweak how you need. os.walk() natively handles a lot of standard parsing, so you can remove some of the directory-manipulating functions:

def import_tifs(dirpath):
  for dirpath, dirname, filenames in os.walk(dirpath):
    # Iterate through the files in the current dir returned by walk()
    for tif_file in filenames:
      # If the suffix is '.TIF', process
      if tif_file.upper().endswith('.tif'):
        # This will contain the full path to your file
        full_path = os.path.join(dirpath, tif_file)

        # tif_file will already contain the name, so you can call from here
        grass.message('Importing %s -> %s@%s...' % (full_path, tif_file, dirpath))

        grass.run_command('r.in.gdal',
                          flags = 'o',
                          input = full_path,
                          output = tif_file,
                          quiet = True,
                          overwrite = True)
RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
  • Great - thanks for taking the time to reply. I've used the codes rewritten by yourself and @jonhkr and have got my code to work, and hopefully made it more efficient at the same time. – si_2012 Oct 26 '12 at 08:12
  • @user1774932 Awesome! Good luck with everything. – RocketDonkey Oct 26 '12 at 08:15
2

I've just rewritten your code to list all the dir tree and find for a file extension, in this case '.tif',

#!/usr/bin/python

import os
import sys


def import_tifs(dirpath):

    for dirpath, dirname, filenames in os.walk(dirpath):
        for filename in filenames:
            name, extension = os.path.splitext(filename)
            if extension.lower() == ".tif":
                filepath = os.path.join(dirpath, filename)

                print(filepath, name, dirpath)


def main():
    if len(sys.argv) == 1:
            import_tifs(os.getcwd())
    else:
        import_tifs(sys.argv[1])


if __name__ == "__main__":
    main()

please check if that is what you are looking for...

jonhkr
  • 600
  • 5
  • 11