#!/usr/bin/env python
# coding: utf-8

import glob
import os

from netCDF4 import Dataset
import numpy as np

from smartcarb import map


DATA_PATH = '/input/ESA_CCI/OCO-2'



def read_data(filename):

    with Dataset(filename) as nc:
        time = nc.variables['time'][:]
        lon = nc.variables['longitude'][:]
        lat = nc.variables['latitude'][:]

        xco2 = nc.variables['xco2'][:]
        xco2_std = nc.variables['xco2_uncertainty'][:]

        flag = nc.variables['xco2_quality_flag'][:]


    return time, lon, lat, xco2, xco2_std, flag



def show(date):

    pattern = os.path.join(DATA_PATH, date.strftime('oco2_LtCO2_%y%m%d_*.nc4'))

    try:
        filename = glob.glob(pattern)[0]
    except IndexError:
        return

    time, lon, lat, xco2, xco2_std, flat = read_data(filename)


    mask = (7.5 < lon) & (lon < 19.8) & (49.4 < lat) & (lat < 55.5)

    if mask.sum() >= 10:
        print(filename)
        return
        print('some data')
        map.make_level2_map(lon, lat, xco2, xlim=None, ylim=None)


