from mpl_toolkits.basemap import Basemap, shiftgrid, addcyclic
import datetime as dt 
import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt
import os
import pandas as pd
import xarray as xr

os.chdir('/home/csjc/Documents/test/data')
nc_f = 'zos_AVISO_L4_199210-201012.nc'
nc_fid = Dataset(nc_f, 'r')



def ncdump(nc_fid, verb=True):
    '''
    ncdump outputs dimensions, variables and their attribute information.
    The information is similar to that of NCAR's ncdump utility.
    ncdump requires a valid instance of Dataset.

    Parameters
    ----------
    nc_fid : netCDF4.Dataset
        A netCDF4 dateset object
    verb : Boolean
        whether or not nc_attrs, nc_dims, and nc_vars are printed

    Returns
    -------
    nc_attrs : list
        A Python list of the NetCDF file global attributes
    nc_dims : list
        A Python list of the NetCDF file dimensions
    nc_vars : list
        A Python list of the NetCDF file variables
    '''
    def print_ncattr(key):
        """
        Prints the NetCDF file attributes for a given key

        Parameters
        ----------
        key : unicode
            a valid netCDF4.Dataset.variables key
        """
        try:
            print("\t\ttype:", repr(nc_fid.variables[key].dtype))
            for ncattr in nc_fid.variables[key].ncattrs():
                print('\t\t%s:' % ncattr,\
                      repr(nc_fid.variables[key].getncattr(ncattr)))
        except KeyError:
            print("\t\tWARNING: %s does not contain variable attributes" % key)

    # NetCDF global attributes
    nc_attrs = nc_fid.ncattrs()
    if verb:
        print("NetCDF Global Attributes:")
        for nc_attr in nc_attrs:
            print('\t%s:' % nc_attr, repr(nc_fid.getncattr(nc_attr)))
    nc_dims = [dim for dim in nc_fid.dimensions]  # list of nc dimensions
    # Dimension shape information.
    if verb:
        print("NetCDF dimension information:")
        for dim in nc_dims:
            print("\tName:", dim )
            print("\t\tsize:", len(nc_fid.dimensions[dim]))
            print_ncattr(dim)
    # Variable information.
    nc_vars = [var for var in nc_fid.variables]  # list of nc variables
    if verb:
        print("NetCDF variable information:")
        for var in nc_vars:
            if var not in nc_dims:
                print('\tName:', var)
                print("\t\tdimensions:", nc_fid.variables[var].dimensions)
                print("\t\tsize:", nc_fid.variables[var].size)
                print_ncattr(var)
    return nc_attrs, nc_dims, nc_vars

nc_attrs, nc_dims, nc_vars = ncdump(nc_fid)
#lats = nc_fid.variables['latitude'][:]
# lons = nc_fid.variables['longitude'][:]
# time = nc_fid.variables['time'][:]
# seaheight = nc_fid.variables['sea_surface_height_above_sea_level'][:]


print(nc_fid.variables['sea_surface_height_above_sea_level'])
#mydf = pd.DataFrame({"Latidute": lats, "Longitude" : lons, "Sea Height" : seaheight}, index = pd.to_datetime('2000-01-01 00:00:00.0') + pd.to_timedelta(time, unit = 'seconds'))
# print(mydf)
# print(mydf.describe())

# print(nc_fid.variables.keys())

# print(lats)
# print("Longitude")
# print(lons)
# print("Time")
# print(time)
# print("Sea height")
# print(seaheight)

ds = xr.open_mfdataset(['osu_cioss_weekly_msla_geovel_2012_v1.nc'])
print(ds)