quicklook_generator.py 2.63 KB
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import os.path as op
import find_directory_names


def create_jpg(in_jp2s, out_jpg):
    ''' 
    Create empty shapefiles based on the SRS of the in_tif
    '''
    bands_text = ''    
    for band in in_jp2s:
        bands_text = bands_text + ' ' + band    

    # Create the VRT
    tempVRT = str('tmp/temp_vrt.vrt')
    build_vrt = 'gdalbuildvrt -separate {} {} '.format(tempVRT, bands_text)
    os.system(build_vrt)

    # Create the JPG
    translate = 'gdal_translate -of JPEG -outsize 800 0 -scale 0 2500 -ot byte '+tempVRT + ' '+  out_jpg

    os.system(translate)

    return


def create_all_quicklook(location):
    '''
    Create all the quicklooks for a given location
    '''
    out_dir = op.join('/mnt/data/home/baetensl/clouds_detection_git/Tools/tmp/Quicks', location)
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)
    
    all_dates = find_directory_names.get_all_dates(location)
    print(all_dates)
    nb_total_dates = len(all_dates)
    k = 0
    for current_date in all_dates:
        print('{}/{} dates done'.format(k, nb_total_dates))
        try:
            L1C_dir, band_prefix, date = find_directory_names.get_L1C_dir(location, current_date, display = False)
        except Exception as e: 
            print(e)
            k += 1
            pass
            
        R = op.join(L1C_dir, (band_prefix + '04.jp2'))
        G = op.join(L1C_dir, (band_prefix + '03.jp2'))
        B = op.join(L1C_dir, (band_prefix + '02.jp2'))
        in_jp2s = [R,G,B]

        out_jpg = op.join(out_dir, (location+'_'+date+'.jpg'))
        create_jpg(in_jp2s, out_jpg)
        
        k += 1

    

def main():
    locations = ['Mongu', 'Gobabeb', 'RailroadValley', 'Arles', 'Marrakech']
    locations = ['Italie']
    for location in locations:
    #~ location = 'Pretoria'
    
        create_all_quicklook(location)
    
    return
    R = '/mnt/data/SENTINEL2/L1C_PDGS/Mongu/S2B_MSIL1C_20171222T082329_N0206_R121_T34LGJ_20171222T105215.SAFE/GRANULE/L1C_T34LGJ_A004151_20171222T083834/IMG_DATA/T34LGJ_20171222T082329_B04.jp2'
    G = '/mnt/data/SENTINEL2/L1C_PDGS/Mongu/S2B_MSIL1C_20171222T082329_N0206_R121_T34LGJ_20171222T105215.SAFE/GRANULE/L1C_T34LGJ_A004151_20171222T083834/IMG_DATA/T34LGJ_20171222T082329_B03.jp2'
    B = '/mnt/data/SENTINEL2/L1C_PDGS/Mongu/S2B_MSIL1C_20171222T082329_N0206_R121_T34LGJ_20171222T105215.SAFE/GRANULE/L1C_T34LGJ_A004151_20171222T083834/IMG_DATA/T34LGJ_20171222T082329_B02.jp2'
    in_jp2s = [R,G,B]
    
    out_jpg = '/mnt/data/home/baetensl/classification_clouds/Codes/test.jpg'
    
    create_jpg(in_jp2s, out_jpg)
    
if __name__=='__main__':
    main()