#!/usr/bin/env python

# headless matplotlib (for johansen)
import os
import matplotlib
matplotlib.use('Agg')
import numpy as np

from sadcat import *
from sadcat.tools import AsarImage

import datetime

from nansat import Nansat, Mosaic

# add downloaded MODIS images to catalog
'''
cic = CommonImagesCatalog()
cic.fill_database('asar', '/Data/sat/downloads/ASAR/blacksea/', 'ASA*.N1', AsarImage)
cic = None
'''
# L2-processing of ASAR
#asar2windCatalogStd = L2Catalog('asar_wind')
#asar2windCatalogStd.process(AsarImage)
#asar2windCatalogStd = None

#'''
fileNames = [
##'/Data/sat/downloads/ASAR/blacksea/ASA_WSM_1PNPDE20070626_191849_000000862059_00214_27823_3269.N1',
##'/Data/sat/downloads/ASAR/blacksea/ASA_WSM_1PNIPA20101227_195251_000000923098_00014_46149_7663.N1',
##'/Data/sat/downloads/ASAR/blacksea/ASA_WSM_1PNPDE20120221_080410_000002143112_00021_52190_3880.N1',
#'/Data/sat/downloads/ASAR/blacksea/ASA_WSM_1PNPDK20100507_194428_000001712089_00157_42796_1695.N1',
#'/Home/asumak/data/input/mediterranean/ASA_WSM_1PNIPA20090609_090025_000004282079_00408_38037_8778.N1',
#'/Home/asumak/data/input/mediterranean/ASA_WSM_1PNIPA20090220_204321_000002142076_00358_36484_9209.N1',
#'/Home/asumak/data/input/mediterranean/ASA_WSM_1PNIPA20090317_205548_000001582077_00215_36842_1032.N1',
#
#'/Data/sat/downloads/ASAR/blacksea/ASA_WSM_1PNIPA20080405_073430_000000862067_00264_31881_6413.N1',
#'/Data/sat/downloads/ASAR/blacksea/ASA_WSM_1PNIPA20080407_081158_000000852067_00293_31910_6700.N1',
#'/Data/sat/downloads/ASAR/blacksea/ASA_WSM_1PNIPA20080408_073947_000001292067_00307_31924_6680.N1',
#'/Data/sat/downloads/ASAR/blacksea/ASA_WSM_1PNPDE20080408_074001_000000922067_00307_31924_3377.N1',
#'/Data/sat/downloads/ASAR/blacksea/ASA_WSM_1PNPDE20080413_194154_000000922067_00386_32003_0253.N1',
#'/Data/sat/downloads/ASAR/blacksea/ASA_WSM_1PNPDE20080414_191009_000000852067_00400_32017_1796.N1',
#'/Data/sat/downloads/ASAR/blacksea/ASA_WSM_1PNPDE20080417_075645_000001282067_00436_32053_6567.N1',
#'/Data/sat/downloads/ASAR/blacksea/ASA_WSM_1PNPDE20080420_080246_000001652067_00479_32096_0756.N1',
#'/Data/sat/downloads/ASAR/blacksea/ASA_WSM_1PNIPA20080423_080757_000000672068_00021_32139_9197.N1',
##'/Data/sat/downloads/ASAR/barents/ASA_WSM_1PNPDK20100701_192437_000003182090_00443_43583_0366.N1',
##'/Data/sat/downloads/ASAR/barents/ASA_WSM_1PNPDK20090701_185210_000005512080_00228_38358_4240.N1',
'/Data/sat/downloads/ASAR/kara/ASA_WSM_1PNPDK20090416_102546_000005192078_00137_37265_4902.N1',
'/Data/sat/downloads/ASAR/barents/ASA_WSM_1PNPDK20090605_191149_000004092079_00357_37986_4288.N1',
#'/Data/sat/downloads/ASAR/barents/ASA_WSM_1PNPDK20081231_205624_000002752075_00129_35754_1795.N1',
#'/Data/sat/downloads/ASAR/barents/ASA_WSM_1PNPDK20070318_200655_000004772056_00286_26392_6124.N1',
#'/Data/sat/downloads/ASAR/barents/ASA_WSM_1PNPDK20081223_101042_000001352075_00008_35633_2288.N1',


#'/Data/sat/downloads/ASAR/barents/ASA_WSM_1PNPDK20080426_112643_000003042068_00066_32184_7895.N1',
#'/Data/sat/downloads/ASAR/barents/ASA_WSM_1PNPDK20081121_182912_000005502074_00056_35180_3476.N1',
#'/Data/sat/downloads/ASAR/barents/ASA_WSM_1PNPDK20080815_113843_000001642071_00152_33773_8646.N1',
#'/Data/sat/downloads/ASAR/barents/ASA_WSM_1PNPDK20081026_085528_000000922073_00179_34802_2326.N1',

# no ncep file
#'/Data/sat/downloads/ASAR/blacksea/ASA_WSM_1PNPDE20100201_081038_000001712086_00293_41429_8625.N1',
]


'''
files = os.listdir('/Data/sat/downloads/ASAR/barents/')
for iFileName in files:
    if iFileName.startswith('ASA_WSM_1PNPDK200703') or iFileName.startswith('ASA_WSM_1PNPDK200704') or iFileName.startswith('ASA_WSM_1PNPDK200705'):
        fileNames.append('/Data/sat/downloads/ASAR/barents/' + iFileName)
'''
"""
asarWind = L2Catalog('asar_wind')
asarWind.process(AsarWindImage)


dbRows, dbFields = asarWind.get_sensor_list(asarWind.sensor)
asaWind = None # close connection to the database

dateField = dbFields.index('sensstart')
nameField = dbFields.index('name')

l3pAsarWind = L3PROC('asar_std_BlackSea', 'asar_wind', AsarWindImage, 10)
#l3pAsarWind.l3('hourly')

for day in range(1, 5):
    for hour in [0]:
        uDateTime = datetime.datetime(2011, 4, day, hour)
        l3pAsarWind.binPeriod = 'daily'
        l3pAsarWind.tempBP = l3pAsarWind.tempBinningParams['daily']
        l3pAsarWind.one_date_processing(uDateTime, dbRows, dateField, nameField)
"""

#----------------------#
# perform L2-processing
#----------------------#
"""
opts = {
        'name': 'process_thredds',
        'mapDir': '/Home/asumak/data/output/',
       }

for iFileName in fileNames:
    asarImg = AsarImage(iFileName)
    status = asarImg.process_wind(opts)
"""

#----------------------#
# perform L2-processing
#----------------------#

opts = {    'name': 'process_wind_thredds',
            #'threddsDir': /WebData/maires.nersc.no/public_html/thredds/barents1km_asar_wind/barents1km_asar_wind_%s_%s.nc',
            'threddsDir': '/Home/asumak/data/output/barents/barents1km_asar_wind_%s_%s.nc',
            'maskName' : 'mask',
            'domain': {
                        'srs': '+proj=stere +lat_0=74.5 +lon_0=40 +k_0=1.0 x_0=0 +y_0=0 +elleps=WGS84 +datum=WGS84 +units=m +no_defs',
                        'ext': '-te -550000 -670000 550000 670000 -tr 1000 1000',
                      },
            'bands' : {'U':     {'type' : '>i2',
                                 'scale' : 0.01,
                                 'offset': 0.0,
                                 'standard_name': 'eastward_wind',
                                 'long_name': 'Eastward wind velocity',
                                 'valid_range': [-3500, 3500],
                                 '_FillValue': np.int16(-10000),
                                 },
                       'V':     {'type' : '>i2',
                                 'scale':  0.01,
                                 'offset':0.0,
                                 'standard_name': 'northward_wind',
                                 'long_name': 'Northward wind velocity',
                                 'valid_range': [-3500, 3500],
                                 '_FillValue': np.int16(-10000),
                                 },
                       'mask':  {'type' : '>i1',
                                 'scale': 1.0,
                                 'offset': 0.0,
                                 'standard_name': 'northward_wind status_flag',
                                 'long_name': "Status flag for wind speed retrieval",
                                 'flag_meanings': "no_data clouds land nominal ",
                                 'flag_values' : np.byte([0, 1, 2, 64]),
                                 }
                       },

            'metadata': {
                            'acknowledgment': 'NORMAP NERSC, NORWEGIAN RESEARCH COUNCIL',
                            'creator_name': 'NERSC (Korosov A.)',
                            'comment': 'NORMAP data products from NERSC. References to description of algorithms could be found here: https://github.com/nansencenter/openwind',
                            'contributor_role': 'L2, L3 processing',
                            'creator_email': 'anton.korosov@nersc.no',
                            'Conventions': 'CF-1.5',
                            'contributor_name': 'NERSC (Yamakawa A.)',
                            'creator_url': 'http//www.nersc.no',
                            'cdm_data_type': 'Grid',
                            'date_issued': '2014-09-01',
                            'geospatial_vertical_max': '0',
                            'geospatial_lat_units': 'degrees_north',
                            'geospatial_lon_units': 'degrees_east',
                            'geospatial_lat_resolution': '0.008479111940298507',
                            'geospatial_lon_resolution': '0.05000000090909091',
                            'geospatial_vertical_min': '0',
                            'geospatial_vertical_resolution': '1',
                            'geospatial_vertical_positive': 'down',
                            'geospatial_vertical_units': 'm',
                            'GDAL': 'GDAL 2.0dev, released 2011/12/29',
                            'id': 'barents1km_asar_wind',
                            'institution': 'Nansen Environmental and Remote Sensing Centre',
                            'keywords': 'ATMOSPHERE > ATMOSPHERIC WINDS > SURFACE WINDS > WIND SPEED/WIND DIRECTION, OCEANS > OCEAN WINDS > SURFACE WINDS',
                            'keywords_vocabulary': 'GCMD Science Keywords',
                            'license': 'The data may be used and redistributed for free but is not intended for legal use, since it may contain inaccuracies. The data Contributordoes not make any warranty, express or implied, including warranties of merchantability and fitness for a particular purpose, or assumes any legal liability for the accuracy, completeness, or usefulness, of this information.',
                            'Metadata_Conventions': 'Unidata Dataset Discovery v1.0',
                            'Metadata_Link': 'http://normap.nersc.no/content/documentation-normap-products',
                            'naming_authority': 'nersc.no',
                            'publisher_name': 'NERSC',
                            'publisher_url': 'http//www.nersc.no',
                            'processing_level': '3',
                            'publisher_email': 'stein.sandven@nersc.no',
                            'project': 'NORMAP',
                            'standard_name_vocabulary': 'CF-1.4',
                            'summary': 'Wind Speed retrieved from ASAR backscatter and NCEP wind deirection using CMOD4',
                            'title': 'Barents Sea Surface Winds from ASAR',
                        },
            'rmMetadata' : ['colormap', 'minmax', 'wkv', 'time',
                            'sar_start_date', 'sar_stop_date'],

            'additionalMetadata' : [ 'sar_start_date', 'sar_stop_date',
                                    'date_created', 'date_modified',
                                    'geospatial_lat_min', 'geospatial_lat_max',
                                    'geospatial_lon_min', 'geospatial_lon_max',
                                    'time_coverage_start', 'time_coverage_end'],

            'timeName' : 'sar_start_date',

        }


for iFileName in fileNames:
    asarImage = AsarImage(iFileName)
    status = asarImage.process_wind_thredds(opts)
    print 'status :', status

"""
# perform L3-processing for few days only
l3p = L3PROC('asar_std_BlackSea', 'asar_wind', AsarWindImage, 10)
l3p.tempBP = l3p.tempBinningParams['daily']

# get parameters for mosaicing
maskName = l3p.zone.get('maskName', 'mask')
doReproject = l3p.zone.get('doReproject', True)
threads = l3p.zone.get('threads', 1)
eResampleAlg = l3p.zone.get('eResampleAlg', 0)

# AVERAGE files for this date
for iFileName in fileNames:
    l3aData = Mosaic(domain=l3p.dstDomain, logLevel=l3p.logger.level)
    l3aData.average(files=[iFileName],
                    bands=l3p.zone['products'].keys(),
                    maskName=maskName,
                    doReproject=doReproject,
                    nClass=l3p.l2Class,
                    eResampleAlg=eResampleAlg,
                    threads=threads)

    # add time to Mosaic
    year = int(iFileName.split('/')[-1].split('_')[2][6:10])
    month = int(iFileName.split('/')[-1].split('_')[2][10:12])
    date = int(iFileName.split('/')[-1].split('_')[2][12:14])
    uDate = datetime.date(year, month, date)
    uDate2 = uDate

    l3aData.vrt._set_time(uDate)
    l3aData.set_metadata('start_date', uDate.isoformat())
    l3aData.set_metadata('stop_date', uDate2.isoformat())

    backends = l3p.zone['backends']
    backendNames = backends.keys()

    for backendName in backendNames:
        beFunc = eval('l3p.' + backendName)
        status = beFunc(l3aData, **backends[backendName])
"""
