image_output_11.png image_output_17.png image_output_14.png image_output_13.png image_output_5.png image_output_18.png image_output_7.png image_output_20.png image_output_4.png image_output_8.png image_output_16.png image_output_6.png image_output_12.png image_output_3.png image_output_2.png image_output_1.png image_output_10.png image_output_15.png image_output_9.png image_output_19.png
from cv2 import resize

import glob

import matplotlib.pyplot as plt

import numpy as np

import os

import tqdm.auto as tqdm

olci_root = '/kaggle/input/medisar-olci'
available_olci = os.listdir(olci_root)
def plot_olci(key):
    with open(f'{olci_root}/{key}/metadata.txt', 'r') as file:
        lines = [line.replace('\n', '') for line in file.readlines()]
    min_value = eval(lines[1][4:])
    max_value = eval(lines[2][4:])
    delta = eval(lines[3][6:])
    im = np.array(PIL.Image.open(f'{olci_root}/{key}/{key}_S3.png')).astype(float)
    im[im == 0] = np.nan
    im = min_value + delta * (im - 1)
    title = ' '.join(lines[0].split(' ')[:3])
    plt.colorbar(orientation='horizontal', fraction=0.046)
    plt.axis(False)

i = 0
max_slicks = 0
folders = glob.glob('/kaggle/input/medisar20??/*/*/*')
pbar = tqdm.tqdm(folders, smoothing=0.001)
for folder in pbar:
    content = os.listdir(folder)
    if 'era5_wind_speed_256.png' not in content:
        continue
    wind = np.array(PIL.Image.open(folder + '/era5_wind_speed_256.png')) / 10
    if wind[wind > 0].mean() > 5:
        continue
    mask = np.array(PIL.Image.open(folder + '/mask.png')).astype('float')
    if mask.mean() > 0.5:
        continue
    slicks = np.array(PIL.Image.open(folder + '/biological_slicks.png')) / 255
    slicks[slicks < 0.75] = 0
    max_slicks = max(max_slicks, slicks.mean())
    pbar.set_description(f'{max_slicks}')
    if slicks.mean() < 0.05:
        continue
    key = os.path.split(folder)[1].lower()
    if not key in available_olci:
        continue
    sar = np.array(PIL.Image.open(glob.glob(folder + '/*vv*')[0]))
    slicks[resize(mask, slicks.shape[::-1]) > 0.5] = np.nan
    plt.figure(figsize=(20, 6))
    plt.suptitle(os.path.split(folder)[1], fontweight='bold')
    plt.subplot(131)
    plt.imshow(sar, cmap='gray', vmin=0, vmax=2 ** 12)
    plt.colorbar(orientation='horizontal', fraction=0.046)
    plt.axis(False)
    plt.subplot(132)
    plt.imshow(slicks, cmap='gray', vmin=0, vmax=1)
    plt.colorbar(orientation='horizontal', fraction=0.046)
    plt.axis(False)
    plt.subplot(133)
    plot_olci(key)
    plt.tight_layout()
    plt.show()
    plt.close()
    i += 1
    if i == 20:
        break