Sort images by day/night

Viewed 129

I have a program that sorts images according to its hour (that it's on it's name). I order them looking if at 6 and at 18 it's day or night (because the are taken in an interval of three hours, so I don't mind the previous or after hours).

I'm using a personal criteria to do this. I'm focusing on what happens in Spain.

As you can see, I'm getting the hour, day or month from the name of the photo because names are like: IR39_MSG2-SEVI-MSG15-0100-NA-20080701151239

import os , time, shutil

directorio_origen = "D:/TR/Eumetsat_IR_photos/"
directorio_destino_dia = "D:/TR/IR_Photos/Daytime"
directorio_destino_noche = "D:/TR/IR_Photos/Nighttime"

def get_hour(file_name):
    return file_name[37:39]

def get_day(file_name):
    return file_name[35:37]

def get_month(file_name):
    return file_name[33:35]


for root, dirs, files in os.walk(directorio_origen, topdown=False):

    for fn in files:
        path_to_file = os.path.join(root, fn)
    
        hora = int(get_hour(fn))
        dia = int(get_day(fn))
        mes = int(get_month(fn))
    
        if ((mes == 1 or mes == 2 or mes == 11 or mes == 12 and 6 < hora < 18) or
        (mes == 3 and dia < 17 and 6 < hora < 18) or (mes == 3 and dia == 17 and 6 <= hora < 18) or(mes == 3 and dia > 17 and 6 <= hora <= 18) or
        (4 <= mes <= 8 and 6 <= hora <= 18) or
        (mes == 9 and dia <= 15 and 6 <= hora <= 18) or (mes == 9 and dia >= 16 and 6 <= hora < 18) or
        (mes == 10 and dia <= 13 and 6 <= hora < 18) or (mes == 10 and dia >= 14  and 6 < hora < 18)): 
            new_directorio = directorio_destino_dia
        else:
            new_directorio = directorio_destino_noche
        try:
            if not os.path.exists(new_directorio):
                os.mkdir(new_directorio)
            shutil.copy(path_to_file, new_directorio)

        except OSError:
            print("El archivo no ha podido ser ordenado" % fn)

What I would want to solve is that images as IR39_MSG2-SEVI-MSG15-0100-NA-20100110**21**1241, IR39_MSG2-SEVI-MSG15-0100-NA-20100111**00**1240 or IR39_MSG2-SEVI-MSG15-0100-NA-20100104**00**1241 are sorted as daytime, and I don't know why (hour in highlited).

The thing is that images with hous such as 00, 03 or 21 will always be nighttime and 09 or 12 will always be daytime.

EDITED

So you can understand why I'm using this criteria, I will attach a photo os a dataframe where you can see if at 6 o 18 is daytime or nighttime depending on whether or not the sun rises:

Dataframe

It's not important to see the data clearly, it's only to give you an idea of why I do things like that.

PD: I'm not considering that my images are taken in different minutes.

Thanks for your help.

1 Answers

The problem seems in first condition (mes == 1 or mes == 2 or mes == 11 or mes == 12 and 6 < hora < 18). Change it to (mes in (1, 2, 11, 12) and 6 < hora < 18) and it should work:

def is_day_time(mes, dia, hora):

    if (
        (mes in (1, 2, 11, 12) and 6 < hora < 18) or
        (mes == 3 and dia < 17 and 6 < hora < 18) or 
        (mes == 3 and dia == 17 and 6 <= hora < 18) or
        (mes == 3 and dia > 17 and 6 <= hora <= 18) or
        (4 <= mes <= 8 and 6 <= hora <= 18) or
        (mes == 9 and dia <= 15 and 6 <= hora <= 18) or 
        (mes == 9 and dia >= 16 and 6 <= hora < 18) or
        (mes == 10 and dia <= 13 and 6 <= hora < 18) or 
        (mes == 10 and dia >= 14  and 6 < hora < 18)
        ):
        return True
    return False

print( is_day_time(1, 10, 21) )
print( is_day_time(1, 11, 0) )
print( is_day_time(1, 4, 0) )

Prints:

False
False
False
Related