# -*- coding: utf-8 -*-

"""
####################################################################
#                                                                  #
#                           AWI Basemap                            #
#       PROCESS GENERAL BATHYMETRIC CHART OF THE OCEANS DATA       #
#                                                                  #
####################################################################

This script is processing the General Bathymetric Chart of the
Oceans (GEBCO) input for the AWI Basemap. The global map is
downsampled to the target resolution (RESOLUTION_DEG) set in the
configuration. Then the global grid is clipped to the Arctic and
Antarctic extent, reprojected (warped) to the Arctic and Antarctic
projections (EPSG_ARCTIC and EPSG_ANTARCTIC), and downsampled to the
target resolution (RESOLUTION_M).

"""


#=============================================================================
#    SCRIPT INFO
#=============================================================================

__author__ = 'Simon Dreutter'
__version__ = '0.1'
__date__ = '2020-12-17'
__email__ = 'simon.dreutter@awi.de'
__status__ = 'Developement'


#=============================================================================
#    IMPORT
#=============================================================================

# import AWI Basemap configuration
from config import *


#=============================================================================
#    PROCESS
#=============================================================================

# heading
print()
print('============================================================')
print('      GENERAL BATHYMETRIC CHART OF THE OCEANS / GEBCO       ')

msg = 'Resampling GEBCO...'
cmd = f'gdalwarp -s_srs {EPSG_WORLD} -t_srs {EPSG_WORLD} -tr {RESOLUTION_DEG} {RESOLUTION_DEG} -r cubic -of GTiff {WARP_OPTIONS} {GDAL_CREATE_OPTIONS} {GEBCO_IN} {GEBCO_WORLD}'
run(msg,cmd)

msg = 'Clipping GEBCO for the Arctic...'
cmd = f'gdal_translate -projwin -180.0 90.0 180.0 {ARCTIC_EXTENT_LAT} -a_srs {EPSG_WORLD} -of VRT {GEBCO_IN} {GEBCO_ARCTIC_CLIP_VRT}'
run(msg,cmd)

msg = 'Reprojecting GEBCO for the Arctic...'
cmd = f'gdalwarp -s_srs {EPSG_WORLD} -t_srs {EPSG_ARCTIC} -r near -of VRT {WARP_OPTIONS} {GEBCO_ARCTIC_CLIP_VRT} {GEBCO_ARCTIC_FULL_VRT}'
run(msg,cmd)

msg = 'Resampling Arctic to target resolution...'
cmd = f'gdalwarp -s_srs {EPSG_ARCTIC} -t_srs {EPSG_ARCTIC} -tr {RESOLUTION_M} {RESOLUTION_M} -r cubic -of GTiff {WARP_OPTIONS} {GDAL_CREATE_OPTIONS} {GEBCO_ARCTIC_FULL_VRT} {GEBCO_ARCTIC}'
run(msg,cmd)

msg = 'Clipping GEBCO for the Antarctic...'
cmd = f'gdal_translate -projwin -180.0 {ANTARCTIC_EXTENT_LAT} 180.0 -90.0 -a_srs {EPSG_WORLD} -of VRT {GEBCO_IN} {GEBCO_ANTARCTIC_CLIP_VRT}'
run(msg,cmd)

msg = 'Reprojecting GEBCO for the Antarctic...'
cmd = f'gdalwarp -s_srs {EPSG_WORLD} -t_srs {EPSG_ANTARCTIC} -r near -of VRT {WARP_OPTIONS} {GEBCO_ANTARCTIC_CLIP_VRT} {GEBCO_ANTARCTIC_FULL_VRT}'
run(msg,cmd)

msg = 'Resampling Antarctic to target resolution...'
cmd = f'gdalwarp -s_srs {EPSG_ANTARCTIC} -t_srs {EPSG_ANTARCTIC} -tr {RESOLUTION_M} {RESOLUTION_M} -r cubic -of GTiff {WARP_OPTIONS} {GDAL_CREATE_OPTIONS} {GEBCO_ANTARCTIC_FULL_VRT} {GEBCO_ANTARCTIC}'
run(msg,cmd)

cleanup(GEBCO_ARCTIC_CLIP_VRT)
cleanup(GEBCO_ARCTIC_FULL_VRT)
cleanup(GEBCO_ANTARCTIC_CLIP_VRT)
cleanup(GEBCO_ANTARCTIC_FULL_VRT)