#!python
#
# Plot NETCDF Data using CARTOPY
#
# Usage: map_plotter [-h] -f FILE -v VAR [--lon LON] [--lat LAT]
#                    [-t TIME] [-d DEPTH] [-c CONF] -o OUT [--dpi DPI]
# 
# Plot NetCDF data in beautiful 2D maps.
# 
# optional arguments:
#   -h, --help               show this help message and exit
#   -f FILE, --file FILE     NetCDF file path
#   -v VAR, --var VAR        Variable to plot
#   --lon LON                Longitude variable name (default: glamt)
#   --lat LAT                Latitude variable name (default: gphit)
#   -t TIME, --time TIME     Time index for NetCDF (default: 0)
#   -d DEPTH, --depth DEPTH  Depth index for NetCDF (default: 0)
#   -c CONF, --conf CONF     Configuration file path
#   -o OUT, --outfile OUT    Output file name
#   --dpi DPI                Output file DPI (default: 300)
#
# Arnau Miro, Elena Terzic (2023)
from __future__ import print_function

import argparse
import MapPlotter as mp


## Define input arguments
argpar = argparse.ArgumentParser(prog="map_plotter", description="Plot NetCDF data in beautiful 2D maps.")
argpar.add_argument('-f','--file',type=str,help='NetCDF file path', required=True, dest='file')
argpar.add_argument('-v','--var',type=str,help='Variable to plot', required=True, dest='var')
argpar.add_argument('--lon',type=str,help='Longitude variable name (default: lon)', dest='lon')
argpar.add_argument('--lat',type=str,help='Latitude variable name (default: lat)', dest='lat')
argpar.add_argument('-t','--time',type=int,help='Time index for NetCDF (default: 0)', dest='time')
argpar.add_argument('-d','--depth',type=int,help='Depth index for NetCDF (default: 0)', dest='depth')
argpar.add_argument('-c','--conf',type=str,help='Configuration file path',dest='conf')
argpar.add_argument('-o','--outfile',type=str,help='Output file name', dest='out')
argpar.add_argument('--dpi',type=int,help='Output file DPI (default: 300)', dest='dpi')


## parse input arguments
args=argpar.parse_args()
if not args.lon:   args.lon   = 'lon'
if not args.lat:   args.lat   = 'lat'
if not args.time:  args.time  = 0
if not args.depth: args.depth = -1
if not args.dpi:   args.dpi   = 300


## Run
plotter = mp.MapPlotter(projection='PlateCarree')
params  = plotter.defaultParams() if not args.conf else plotter.loadParams(args.conf)


## Directly plot from NetCDF
plotter.plot_from_file(args.file,args.var,args.lon,args.lat,iTime=args.time,iDepth=args.depth,params=params)


## Save figure
if not args.out:
	plotter.show()
else:
	plotter.save(args.out,args.dpi)