images.py 2.36 KB
Newer Older
Clément Haëck's avatar
Clément Haëck committed
1
"""Clean MODIS images."""
Clément Haëck's avatar
Clément Haëck committed
2

Clément Haëck's avatar
Clément Haëck committed
3
from datetime import datetime
Clément Haëck's avatar
Clément Haëck committed
4 5
from os import path

6
from filefinder import Finder
7
from filefinder.library import get_date
8 9 10

import lib
import lib.data
Clément Haëck's avatar
Clément Haëck committed
11
from lib.box import Image
12

Clément Haëck's avatar
Clément Haëck committed
13
ARGS = {'region', 'days'}
Clément Haëck's avatar
Clément Haëck committed
14 15


16 17
def get_images_list(args=None, **kwargs):
    files = get_images_text_files(args, **kwargs)
Clément Haëck's avatar
Clément Haëck committed
18 19 20 21 22 23 24 25 26 27 28 29 30
    images = parse_text_files(files)
    return images


def regroup_by_file(images):
    images_grp = {}
    for image in images:
        if image.filename not in images_grp:
            images_grp[image.filename] = []
        images_grp[image.filename].append(image)
    return images_grp


Clément Haëck's avatar
Clément Haëck committed
31 32 33 34 35 36 37 38 39
def regroup_by_date(images):
    images_grp = {}
    for image in images:
        if image.date not in images_grp:
            images_grp[image.date] = []
        images_grp[image.date].append(image)
    return images_grp


Clément Haëck's avatar
Clément Haëck committed
40 41 42 43 44 45
def nc2ext(file, ext='txt'):
    ofile = path.join(path.dirname(file), 'images',
                      path.splitext(path.basename(file))[0] + '.{}'.format(ext))
    return ofile


46
def get_images_text_files(args=None, **kwargs):
Clément Haëck's avatar
Clément Haëck committed
47
    args = lib.data.process_args(ARGS | {'fixes'}, args, **kwargs)
Clément Haëck's avatar
Clément Haëck committed
48
    pregex = 'images/A_%(Y)%(m)%(d).txt'
49
    if args['days'] == 1:
Clément Haëck's avatar
Clément Haëck committed
50
        pregex = '%(Y)/%(m)/' + pregex
51
    root = get_root(args)
52
    finder = Finder(root, pregex)
53
    finder.fix_matchers(args['fixes'])
Clément Haëck's avatar
Clément Haëck committed
54 55 56
    return finder.get_files()


57
def get_root(args=None, **kwargs):
Clément Haëck's avatar
Clément Haëck committed
58 59
    args = lib.data.process_args(ARGS, args, **kwargs)
    return lib.data.get_default_directory(args, 'MODIS')
60 61


Clément Haëck's avatar
Clément Haëck committed
62 63 64 65 66 67 68 69 70 71
def parse_text_files(files):
    images = []
    for f_image in files:
        with open(f_image, 'r') as f:
            f.readline()  # Header
            nx, ny = [int(i.strip()) for i in f.readline().split()]
            f.readline()  # Header
            lines = f.readlines()
        for line in lines:
            line = line.split()
Clément Haëck's avatar
Clément Haëck committed
72 73 74 75 76 77
            date = datetime.strptime(line[0].strip(), '%Y-%m-%d')
            f_nc = line[1].strip()
            ix, iy = [int(line[i].strip()) for i in [2, 3]]
            lon_min, lat_min, lon_max, lat_max = [float(line[i].strip())
                                                  for i in range(4, 8)]

Clément Haëck's avatar
Clément Haëck committed
78 79
            images.append(Image(lon_min, lon_max, lat_min, lat_max,
                                ix, iy, nx, ny, f_nc, date))
Clément Haëck's avatar
Clément Haëck committed
80
    return images
81 82 83 84


def get_files(finder):
    return {get_date(m): path.join(finder.root, f) for f, m in finder.files}