#!/usr/bin/env python3
from __future__ import print_function
# import numpy as np
from pygt3file import GT3File
import argparse
import sys
import os
import matplotlib.pyplot as plt
# import mpl_toolkits.basemap as basemap
# from cartopy import config
import cartopy.crs as ccrs


class A:
    """ base class for argparse,"""
    pass


def find_axfile(name, search_path=[u".", u"$GT3AXISDIR", u"$GTOOLDIR/gt3"]):
    """
    Find gtool3 axis file with given axis name `name` from path listed as `search_path`.

    Return path of the found axis file or `None` unless found.
    """
    axis_path = map(os.path.expandvars, search_path)
    axis_path = [a for a in axis_path if a.find('$') < 0]
    axis_path = [a for a in axis_path if os.path.exists(a)]

    if (opt_debug):
        print('*** axis_path:')
        print(axis_path)

    axfile = None
    for axdir in axis_path:
        # print(axdir)
        axfile = os.path.join(axdir, 'GTAXLOC.'+name)
        if (os.path.exists(axfile)):
            break

    # print("dbg:find_axfile:",axfile)
    return axfile


parser = argparse.ArgumentParser(description='Plot one data in gt3file')

parser.add_argument(
    '-d', '--debug', help='debug output',
    action='store_true')
parser.add_argument(
    '-v', '--verbose', help='verbose output',
    action='store_true')
parser.add_argument(
    '-H', '--header', help='Show header only.',
    action='store_true')
parser.add_argument(
    '-T', '--table', help='Show data table only.',
    action='store_true')
parser.add_argument(
    'file', help='file name.')

parser.add_argument(
    '-n', '--number', help='data number to plot.',
    type=int, nargs=1, default=[0])


a = A()
parser.parse_args(namespace=a)

file = a.file

opt_number = a.number[0]
opt_header_only = a.header
opt_show_table = a.table
opt_debug = a.debug
opt_verbose = a.verbose

if (opt_debug):
    opt_verbose = True

if (opt_debug):
    print("dbg:opt_header_only:", opt_header_only)
    print("dbg:opt_show_table:", opt_show_table)
    print("dbg:opt_number:", opt_number)
    print("dbg:file:", file)

f = GT3File(file)
f.opt_debug = opt_debug
f.opt_verbose = opt_verbose

f.scan()

if (opt_show_table):
    f.show_table()
    sys.exit(0)


################################################################################
# Extract target data
################################################################################

target_header = None
target_data = None

while True:
    f.read_one_header()
    if (opt_debug):
        print('dbg: current data:', f.current_header.number)
    if (f.is_eof):
        break
    if (opt_number == f.current_header.number):
        if (opt_debug):
            print("dbg: data #%d found." % opt_number)
        f.read_one_data()
        break
    else:
        f.skip_one_data()

target_header = f.current_header
target_data = f.current_data
if (opt_verbose):
    target_header.dump()

if (opt_debug):
    print(target_data.flags)
    print(target_data.dtype)
    print(target_data.size)
    print(target_data.itemsize)
    print(target_data.ndim)
    print(target_data.shape)
    print(target_data.size)


################################################################################
# Prepare axis data
################################################################################
xax = {}
yax = {}

xax["name"] = target_header.aitm1
xax["indices"] = (target_header.astr1, target_header.aend1)
yax["name"] = target_header.aitm2
yax["indices"] = (target_header.astr2, target_header.aend2)


if (opt_debug):
    print("dbg xax:", xax)
xf = GT3File(find_axfile(xax["name"]))
xf.scan()
xf.read_one_header()
xf.read_one_data()
if (opt_debug):
    xf.dump_current_header()
    xf.dump_current_data()
xaxdata = xf.current_data.flatten()
if (xf.current_header.cyclic):
    xaxdata = xaxdata[:-1]
# if (xf.current_header.cyclic):
#     target_data,xaxdata = basemap.addcyclic(target_data,xaxdata[:-1])
xf.close()

if (opt_debug):
    print("dbg yax:", yax)
yf = GT3File(find_axfile(yax["name"]))
yf.scan()
yf.read_one_header()
yf.read_one_data()
if (opt_verbose):
    yf.dump_current_header()
    yf.dump_current_data()
yaxdata = yf.current_data.flatten()
if (yf.current_header.cyclic):
    yaxdata = xaxdata[:-1]
yf.close()


################################################################################
# Start plotting
################################################################################
if (opt_debug):
    print("dbg: target_data.shape:", target_data.shape)
    print("dbg: xaxdata.shape:", xaxdata.shape)
    print("dbg: yaxdata.shape:", yaxdata.shape)

# ax = plt.axes(projection=ccrs.Mollweide())
# ax = plt.axes(projection=ccrs.NorthPolarStereo())
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180.0))
img = ax.contourf(xaxdata, yaxdata, target_data[0, :, :], 60,
                  transform=ccrs.PlateCarree())

ax.axis("image")
title = "%s[%s]" % (target_header.titl, target_header.unit)
ax.set(title=title)
ax.coastlines()

plt.colorbar(img, ax=ax, orientation="horizontal")
plt.show()

sys.exit(0)

648009e24b8e12d9ae67861fd3bcc60d167bfbf0
<mask token>

<mask token>


class A:
    """ base class for argparse,"""
    pass


def find_axfile(name, search_path=[u'.', u'$GT3AXISDIR', u'$GTOOLDIR/gt3']):
    """
    Find gtool3 axis file with given axis name `name` from path listed as `search_path`.

    Return path of the found axis file or `None` unless found.
    """
    axis_path = map(os.path.expandvars, search_path)
    axis_path = [a for a in axis_path if a.find('$') < 0]
    axis_path = [a for a in axis_path if os.path.exists(a)]
    if opt_debug:
        print('*** axis_path:')
        print(axis_path)
    axfile = None
    for axdir in axis_path:
        axfile = os.path.join(axdir, 'GTAXLOC.' + name)
        if os.path.exists(axfile):
            break
    return axfile


<mask token>

<mask token>


class A:
    """ base class for argparse,"""
    pass


def find_axfile(name, search_path=[u'.', u'$GT3AXISDIR', u'$GTOOLDIR/gt3']):
    """
    Find gtool3 axis file with given axis name `name` from path listed as `search_path`.

    Return path of the found axis file or `None` unless found.
    """
    axis_path = map(os.path.expandvars, search_path)
    axis_path = [a for a in axis_path if a.find('$') < 0]
    axis_path = [a for a in axis_path if os.path.exists(a)]
    if opt_debug:
        print('*** axis_path:')
        print(axis_path)
    axfile = None
    for axdir in axis_path:
        axfile = os.path.join(axdir, 'GTAXLOC.' + name)
        if os.path.exists(axfile):
            break
    return axfile


<mask token>
parser.add_argument('-d', '--debug', help='debug output', action='store_true')
parser.add_argument('-v', '--verbose', help='verbose output', action=
    'store_true')
parser.add_argument('-H', '--header', help='Show header only.', action=
    'store_true')
parser.add_argument('-T', '--table', help='Show data table only.', action=
    'store_true')
parser.add_argument('file', help='file name.')
parser.add_argument('-n', '--number', help='data number to plot.', type=int,
    nargs=1, default=[0])
<mask token>
parser.parse_args(namespace=a)
<mask token>
if opt_debug:
    opt_verbose = True
if opt_debug:
    print('dbg:opt_header_only:', opt_header_only)
    print('dbg:opt_show_table:', opt_show_table)
    print('dbg:opt_number:', opt_number)
    print('dbg:file:', file)
<mask token>
f.scan()
if opt_show_table:
    f.show_table()
    sys.exit(0)
<mask token>
while True:
    f.read_one_header()
    if opt_debug:
        print('dbg: current data:', f.current_header.number)
    if f.is_eof:
        break
    if opt_number == f.current_header.number:
        if opt_debug:
            print('dbg: data #%d found.' % opt_number)
        f.read_one_data()
        break
    else:
        f.skip_one_data()
<mask token>
if opt_verbose:
    target_header.dump()
if opt_debug:
    print(target_data.flags)
    print(target_data.dtype)
    print(target_data.size)
    print(target_data.itemsize)
    print(target_data.ndim)
    print(target_data.shape)
    print(target_data.size)
<mask token>
if opt_debug:
    print('dbg xax:', xax)
<mask token>
xf.scan()
xf.read_one_header()
xf.read_one_data()
if opt_debug:
    xf.dump_current_header()
    xf.dump_current_data()
<mask token>
if xf.current_header.cyclic:
    xaxdata = xaxdata[:-1]
xf.close()
if opt_debug:
    print('dbg yax:', yax)
<mask token>
yf.scan()
yf.read_one_header()
yf.read_one_data()
if opt_verbose:
    yf.dump_current_header()
    yf.dump_current_data()
<mask token>
if yf.current_header.cyclic:
    yaxdata = xaxdata[:-1]
yf.close()
if opt_debug:
    print('dbg: target_data.shape:', target_data.shape)
    print('dbg: xaxdata.shape:', xaxdata.shape)
    print('dbg: yaxdata.shape:', yaxdata.shape)
<mask token>
ax.axis('image')
<mask token>
ax.set(title=title)
ax.coastlines()
plt.colorbar(img, ax=ax, orientation='horizontal')
plt.show()
sys.exit(0)

<mask token>


class A:
    """ base class for argparse,"""
    pass


def find_axfile(name, search_path=[u'.', u'$GT3AXISDIR', u'$GTOOLDIR/gt3']):
    """
    Find gtool3 axis file with given axis name `name` from path listed as `search_path`.

    Return path of the found axis file or `None` unless found.
    """
    axis_path = map(os.path.expandvars, search_path)
    axis_path = [a for a in axis_path if a.find('$') < 0]
    axis_path = [a for a in axis_path if os.path.exists(a)]
    if opt_debug:
        print('*** axis_path:')
        print(axis_path)
    axfile = None
    for axdir in axis_path:
        axfile = os.path.join(axdir, 'GTAXLOC.' + name)
        if os.path.exists(axfile):
            break
    return axfile


parser = argparse.ArgumentParser(description='Plot one data in gt3file')
parser.add_argument('-d', '--debug', help='debug output', action='store_true')
parser.add_argument('-v', '--verbose', help='verbose output', action=
    'store_true')
parser.add_argument('-H', '--header', help='Show header only.', action=
    'store_true')
parser.add_argument('-T', '--table', help='Show data table only.', action=
    'store_true')
parser.add_argument('file', help='file name.')
parser.add_argument('-n', '--number', help='data number to plot.', type=int,
    nargs=1, default=[0])
a = A()
parser.parse_args(namespace=a)
file = a.file
opt_number = a.number[0]
opt_header_only = a.header
opt_show_table = a.table
opt_debug = a.debug
opt_verbose = a.verbose
if opt_debug:
    opt_verbose = True
if opt_debug:
    print('dbg:opt_header_only:', opt_header_only)
    print('dbg:opt_show_table:', opt_show_table)
    print('dbg:opt_number:', opt_number)
    print('dbg:file:', file)
f = GT3File(file)
f.opt_debug = opt_debug
f.opt_verbose = opt_verbose
f.scan()
if opt_show_table:
    f.show_table()
    sys.exit(0)
target_header = None
target_data = None
while True:
    f.read_one_header()
    if opt_debug:
        print('dbg: current data:', f.current_header.number)
    if f.is_eof:
        break
    if opt_number == f.current_header.number:
        if opt_debug:
            print('dbg: data #%d found.' % opt_number)
        f.read_one_data()
        break
    else:
        f.skip_one_data()
target_header = f.current_header
target_data = f.current_data
if opt_verbose:
    target_header.dump()
if opt_debug:
    print(target_data.flags)
    print(target_data.dtype)
    print(target_data.size)
    print(target_data.itemsize)
    print(target_data.ndim)
    print(target_data.shape)
    print(target_data.size)
xax = {}
yax = {}
xax['name'] = target_header.aitm1
xax['indices'] = target_header.astr1, target_header.aend1
yax['name'] = target_header.aitm2
yax['indices'] = target_header.astr2, target_header.aend2
if opt_debug:
    print('dbg xax:', xax)
xf = GT3File(find_axfile(xax['name']))
xf.scan()
xf.read_one_header()
xf.read_one_data()
if opt_debug:
    xf.dump_current_header()
    xf.dump_current_data()
xaxdata = xf.current_data.flatten()
if xf.current_header.cyclic:
    xaxdata = xaxdata[:-1]
xf.close()
if opt_debug:
    print('dbg yax:', yax)
yf = GT3File(find_axfile(yax['name']))
yf.scan()
yf.read_one_header()
yf.read_one_data()
if opt_verbose:
    yf.dump_current_header()
    yf.dump_current_data()
yaxdata = yf.current_data.flatten()
if yf.current_header.cyclic:
    yaxdata = xaxdata[:-1]
yf.close()
if opt_debug:
    print('dbg: target_data.shape:', target_data.shape)
    print('dbg: xaxdata.shape:', xaxdata.shape)
    print('dbg: yaxdata.shape:', yaxdata.shape)
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180.0))
img = ax.contourf(xaxdata, yaxdata, target_data[0, :, :], 60, transform=
    ccrs.PlateCarree())
ax.axis('image')
title = '%s[%s]' % (target_header.titl, target_header.unit)
ax.set(title=title)
ax.coastlines()
plt.colorbar(img, ax=ax, orientation='horizontal')
plt.show()
sys.exit(0)

#!/usr/bin/env python3
from __future__ import print_function
# import numpy as np
from pygt3file import GT3File
import argparse
import sys
import os
import matplotlib.pyplot as plt
# import mpl_toolkits.basemap as basemap
# from cartopy import config
import cartopy.crs as ccrs


class A:
    """ base class for argparse,"""
    pass


def find_axfile(name, search_path=[u".", u"$GT3AXISDIR", u"$GTOOLDIR/gt3"]):
    """
    Find gtool3 axis file with given axis name `name` from path listed as `search_path`.

    Return path of the found axis file or `None` unless found.
    """
    axis_path = map(os.path.expandvars, search_path)
    axis_path = [a for a in axis_path if a.find('$') < 0]
    axis_path = [a for a in axis_path if os.path.exists(a)]

    if (opt_debug):
        print('*** axis_path:')
        print(axis_path)

    axfile = None
    for axdir in axis_path:
        # print(axdir)
        axfile = os.path.join(axdir, 'GTAXLOC.'+name)
        if (os.path.exists(axfile)):
            break

    # print("dbg:find_axfile:",axfile)
    return axfile


parser = argparse.ArgumentParser(description='Plot one data in gt3file')

parser.add_argument(
    '-d', '--debug', help='debug output',
    action='store_true')
parser.add_argument(
    '-v', '--verbose', help='verbose output',
    action='store_true')
parser.add_argument(
    '-H', '--header', help='Show header only.',
    action='store_true')
parser.add_argument(
    '-T', '--table', help='Show data table only.',
    action='store_true')
parser.add_argument(
    'file', help='file name.')

parser.add_argument(
    '-n', '--number', help='data number to plot.',
    type=int, nargs=1, default=[0])


a = A()
parser.parse_args(namespace=a)

file = a.file

opt_number = a.number[0]
opt_header_only = a.header
opt_show_table = a.table
opt_debug = a.debug
opt_verbose = a.verbose

if (opt_debug):
    opt_verbose = True

if (opt_debug):
    print("dbg:opt_header_only:", opt_header_only)
    print("dbg:opt_show_table:", opt_show_table)
    print("dbg:opt_number:", opt_number)
    print("dbg:file:", file)

f = GT3File(file)
f.opt_debug = opt_debug
f.opt_verbose = opt_verbose

f.scan()

if (opt_show_table):
    f.show_table()
    sys.exit(0)


################################################################################
# Extract target data
################################################################################

target_header = None
target_data = None

while True:
    f.read_one_header()
    if (opt_debug):
        print('dbg: current data:', f.current_header.number)
    if (f.is_eof):
        break
    if (opt_number == f.current_header.number):
        if (opt_debug):
            print("dbg: data #%d found." % opt_number)
        f.read_one_data()
        break
    else:
        f.skip_one_data()

target_header = f.current_header
target_data = f.current_data
if (opt_verbose):
    target_header.dump()

if (opt_debug):
    print(target_data.flags)
    print(target_data.dtype)
    print(target_data.size)
    print(target_data.itemsize)
    print(target_data.ndim)
    print(target_data.shape)
    print(target_data.size)


################################################################################
# Prepare axis data
################################################################################
xax = {}
yax = {}

xax["name"] = target_header.aitm1
xax["indices"] = (target_header.astr1, target_header.aend1)
yax["name"] = target_header.aitm2
yax["indices"] = (target_header.astr2, target_header.aend2)


if (opt_debug):
    print("dbg xax:", xax)
xf = GT3File(find_axfile(xax["name"]))
xf.scan()
xf.read_one_header()
xf.read_one_data()
if (opt_debug):
    xf.dump_current_header()
    xf.dump_current_data()
xaxdata = xf.current_data.flatten()
if (xf.current_header.cyclic):
    xaxdata = xaxdata[:-1]
# if (xf.current_header.cyclic):
#     target_data,xaxdata = basemap.addcyclic(target_data,xaxdata[:-1])
xf.close()

if (opt_debug):
    print("dbg yax:", yax)
yf = GT3File(find_axfile(yax["name"]))
yf.scan()
yf.read_one_header()
yf.read_one_data()
if (opt_verbose):
    yf.dump_current_header()
    yf.dump_current_data()
yaxdata = yf.current_data.flatten()
if (yf.current_header.cyclic):
    yaxdata = xaxdata[:-1]
yf.close()


################################################################################
# Start plotting
################################################################################
if (opt_debug):
    print("dbg: target_data.shape:", target_data.shape)
    print("dbg: xaxdata.shape:", xaxdata.shape)
    print("dbg: yaxdata.shape:", yaxdata.shape)

# ax = plt.axes(projection=ccrs.Mollweide())
# ax = plt.axes(projection=ccrs.NorthPolarStereo())
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180.0))
img = ax.contourf(xaxdata, yaxdata, target_data[0, :, :], 60,
                  transform=ccrs.PlateCarree())

ax.axis("image")
title = "%s[%s]" % (target_header.titl, target_header.unit)
ax.set(title=title)
ax.coastlines()

plt.colorbar(img, ax=ax, orientation="horizontal")
plt.show()

sys.exit(0)
