#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 27 18:32:40 2020

@author: jkh

Basic function to get the number of elements in a netcdf file and print them back to 
the command line. Run as:
python number_nc_lines <gen_ffname>

"""


import netCDF4 as nc4
import sys
import glob

gen_ffname = sys.argv[1] #get input from command line. Could have a wildcard in it


nc_ffnames = glob.glob(gen_ffname)
if len(nc_ffnames) > 1:
  raise ValueError('Got ' + '%d' % len(nc_ffnames) + ' matches for ' + gen_ffname + ' Should be just 1')
elif len(nc_ffnames) == 0:
  raise ValueError('No matches for %r, *.nc file not written' % gen_ffname)
else:
  nc_ffname = nc_ffnames[0] #just the 1 file...

with nc4.Dataset(nc_ffname) as tc_raw:
  year_data = tc_raw['year']
  n_entries = len(year_data)
  print(n_entries)

