extract_images.py 2.77 KB
Newer Older
Clément Haëck's avatar
Clément Haëck committed
1 2 3
from os import path
import logging
import subprocess
4
import tempfile
Clément Haëck's avatar
Clément Haëck committed
5

6
from filefinder.library import get_date
Clément Haëck's avatar
Clément Haëck committed
7

Clément Haëck's avatar
Clément Haëck committed
8 9
from lib import get_args, check_output_dir
import lib.data.modis
Clément Haëck's avatar
Clément Haëck committed
10 11 12 13 14 15 16 17 18 19 20 21
import lib.data.images


logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)

file_dir = path.dirname(path.realpath(__file__))


def main():
    def add_args(parser):
22 23 24 25 26
        parser.add_argument('-nx', type=int, default=200)
        parser.add_argument('-ny', type=int, default=200)
        parser.add_argument('-stepx', type=int, default=10)
        parser.add_argument('-stepy', type=int, default=10)
        parser.add_argument('-cover_threshold', type=float, default=0.70)
Clément Haëck's avatar
Clément Haëck committed
27

28 29
    args = get_args(['region', 'year', 'fixes'], add_args,
                    "Extract images from regridded swaths.")
Clément Haëck's avatar
Clément Haëck committed
30 31 32 33 34 35

    find_images(args)


def find_images(args):
    log.info('finding images')
36 37 38 39 40
    with tempfile.TemporaryDirectory(suffix='find_images_') as tdir:
        filelist = path.join(tdir, 'filelist.txt')
        namelist = path.join(tdir, 'namelist.txt')
        write_filelist(args, filelist)
        write_namelist(args, namelist, filelist)
Clément Haëck's avatar
Clément Haëck committed
41

42 43 44 45 46
        exe_filename = path.join(file_dir, 'find_images.exe')
        if not path.isfile(exe_filename):
            raise RuntimeError("Fortran find_images not compiled. Compile with "
                               "`gfortran -O2 -I... -lnetcff -o find_images.x "
                               "find_images.f90`")
Clément Haëck's avatar
Clément Haëck committed
47

48 49 50
        log.info('launching fortran subprocess')
        clo = [exe_filename, namelist]
        subprocess.call(clo)
Clément Haëck's avatar
Clément Haëck committed
51 52


53
def write_filelist(args, filename):
Clément Haëck's avatar
Clément Haëck committed
54
    log.info('writing filelist')
55
    finder = lib.data.modis.get_finder(args)
Clément Haëck's avatar
Clément Haëck committed
56
    files = finder.get_files()
Clément Haëck's avatar
Clément Haëck committed
57

58 59 60 61 62 63 64 65
    # Create all folders
    images_dir = set()
    for f in files:
        images_dir.add(path.dirname(lib.data.images.nc2ext(f, 'txt')))
    for d in images_dir:
        check_output_dir(d, log)

    with open(filename, 'w') as filelist:
Clément Haëck's avatar
Clément Haëck committed
66 67
        filelist.write("{}\n".format(len(files)))
        for f in files:
68
            date = get_date(finder.get_matches(f, relative=False))
Clément Haëck's avatar
Clément Haëck committed
69 70
            filelist.write("{}\n".format(f))
            filelist.write("{}\n".format(lib.data.images.nc2ext(f, 'txt')))
Clément Haëck's avatar
Clément Haëck committed
71
            filelist.write("{}\n".format(date.strftime('%Y-%m-%d')))
Clément Haëck's avatar
Clément Haëck committed
72 73


74 75
def write_namelist(args, filename, filelist):
    with open(filename, 'w') as f:
Clément Haëck's avatar
Clément Haëck committed
76
        f.write('&input\n')
77
        f.write('    filelist = "{}"\n'.format(filelist))
78 79 80 81 82
        f.write('    nx = {:d}\n'.format(args['nx']))
        f.write('    ny = {:d}\n'.format(args['ny']))
        f.write('    stepx = {:d}\n'.format(args['stepx']))
        f.write('    stepy = {:d}\n'.format(args['stepy']))
        f.write('    cover_threshold = {:f}\n'.format(args['cover_threshold']))
Clément Haëck's avatar
Clément Haëck committed
83 84 85 86 87
        f.write('/\n')


if __name__ == '__main__':
    main()