#!/usr/bin/env python
import argparse,sys,os,glob,datetime
from proc import Processor
from getoptions import get_args, get_options
from ncfiles import expand_ncfiles
from netCDF4 import Dataset # http://code.google.com/p/netcdf4-python/
from subprocess import check_output,call
#-----------------------------------------------------------------------
#
# Main:
#
args = get_args() # in getoptions.py
#
# Make a Processor instance:
#
proc = Processor()
#
# Sort out history files from ncfiles positional argument.
# At least one file is required.
#
proc.ncfiles = expand_ncfiles(args.ncfiles,proc)
proc.res = proc.getres(proc.ncfiles) # check model resolution
#print 'proc.res=',proc.res
#
# Get namelist params from command-line args:
#
proc.executable = get_options('executable',args.executable,proc)
proc.singleut   = get_options('singleut' ,args.singleut,proc)
proc.multiut    = get_options('multiut'  ,args.multiut,proc)
if proc.singleut=='0' and proc.multiut=='0':
  print 'Note tgcmproc: Neither -singleut nor -multiut option is set.'
  print '               I will default to a single ut run.'
  proc.singleut = '1'
proc.nlfile   = get_options('nlfile'   ,args.nlfile,proc)

proc.mtimes   = get_options('mtimes'   ,args.mtimes,proc)
proc.flnm_ps  = get_options('flnm_ps'  ,args.flnm_ps,proc)
proc.stdout   = get_options('stdout'   ,args.stdout,proc)
proc.submit   = get_options('submit'   ,args.submit,proc)
proc.cfields  = get_options('cfields'  ,args.cfields,proc)
#
# Options for single-ut run (-singleut=1):
#
proc.pltmaps  = get_options('pltmaps'  ,args.pltmaps,proc)
proc.fmap_zpht= get_options('fmap_zpht',args.fmap_zpht,proc)
proc.pltlon   = get_options('pltlon'   ,args.pltlon,proc)
proc.flons    = get_options('flons'    ,args.flons,proc) # flons='zm'
proc.fslts    = get_options('fslts'    ,args.fslts,proc) # 0.,12.
proc.pltlat   = get_options('pltlat'   ,args.pltlat,proc)
proc.flats    = get_options('flats'    ,args.flats,proc)
proc.pltxyloc = get_options('pltxyloc' ,args.pltxyloc,proc)
proc.xylocs   = get_options('xylocs'   ,args.xylocs,proc)
proc.xyloc_zprange = get_options('xyloc_zprange',args.xyloc_zprange,proc)
#
# Options for multi-ut run (-multiut=1):
#
proc.pltutvert      = get_options('pltutvert'     ,args.pltutvert,proc)
proc.utvert_locs    = get_options('utvert_locs'   ,args.utvert_locs,proc)
proc.utvert_zprange = get_options('utvert_zprange',args.utvert_zprange,proc)
proc.pltutlat       = get_options('pltutlat'      ,args.pltutlat,proc)
proc.utlat_zphtlon  = get_options('utlat_zphtlon' ,args.utlat_zphtlon,proc)
#
# Make single string w/ list of ncfiles for namelist:
#
proc.ncfiles_str = ''
for path in proc.ncfiles: 
  if '*' in path: continue
  proc.ncfiles_str = proc.ncfiles_str+"'"+path+"',"
#
# Make namelist file for single-ut and/or multi-ut run:
# (Each of these should have reasonable defaults for plotting options)
#
if proc.singleut=='1': proc.make_namelist_singleut()
if proc.multiut =='1': proc.make_namelist_multiut()

#
# Get executable for model resolution.
# For now (11/15), assume user has an executable in their 
#   path, so just get it from the shell. Later it could 
#   be an option (maybe separate option for each res).
# Or it could be an env var e.g., $TGCMROOT/bin/tgcmproc_f90.
#
#if proc.res == '5.0':
#  tgcmproc = check_output(['which','tgcmproc_f90'])
#else:
#  tgcmproc = check_output(['which','tgcmproc_dres_f90'])
#tgcmproc = tgcmproc.rstrip('\n')

#
# tgcmproc execute command and args for subprocess:
# (not sure how to make subprocess run in background and return)
#
command = proc.executable + ' < ' + proc.nlfile  
if proc.stdout: command = command + ' > ' + proc.stdout
#
# Inform user about current setup:
#
#print '\nSetup for run of processor executable: ',proc.executable
print 'Namelist input file:   ',proc.nlfile
print 'Model resolution:      ',proc.res
#if proc.stdout:
#  print 'Processor stdout file: ',proc.stdout
#else:
#  print 'Processor stdout file: ','[to the terminal]'
#print 'Output postscript file:',proc.flnm_ps
print 'Model times:           ',proc.mtimes
#print 'Fields to plot:        ',proc.cfields

#print 'Single-ut run:         ',proc.singleut
#if proc.singleut=='1':
#  print '  ipltmaps:            ',proc.pltmaps
#  if proc.pltmaps=='1':
#    print '  fmap_zpht:           ',proc.fmap_zpht
#  print '  ipltlon:             ',proc.pltlon
#  if proc.pltlon=='1':
#    print '  flons:               ',proc.flons
#    print '  fslts:               ',proc.fslts
#  print '  ipltlat:             ',proc.pltlat
#  if proc.pltlat=='1':
#    print '  flats:               ',proc.flats
#  print '  ipltxyloc:           ',proc.pltxyloc
#  if proc.pltxyloc=='1':
#    print '  xylocs:              ',proc.xylocs
#    print '  xyloc_zprange:       ',proc.xyloc_zprange
#print 'Multi-ut run:          ',proc.multiut
#if proc.multiut=='1':
#  print '  ipltutvert:          ',proc.pltutvert
#  if proc.pltutvert=='1':
#    print '  utvert_locs:         ',proc.utvert_locs
#    print '  utvert_zprange:      ',proc.utvert_zprange
#  print '  pltutlat:            ',proc.pltutvert
#  if proc.pltutlat=='1':
#    print '  utlat_zphtlon:       ',proc.utlat_zphtlon
    
#print 'History files:'
#if proc.singleut=='1':
#  for file in proc.ncfiles: print '  ',file
#if proc.multiut=='1':
#  print 'First: ',proc.ncfiles[0]
#  print 'Last:  ',proc.ncfiles[len(proc.ncfiles)-1]
#
# Prompt user for processor execution unless submit==no:
#
if proc.submit:
  if proc.submit == 'y' or proc.submit == 'yes':
    print 'Tgcmproc is executing (please wait)...'
    call(command, shell=True)
else:
  answer = raw_input('\nRun the processor with the above setup? (y/n/q default y) ')
  if answer == 'q' or answer == 'n': sys.exit()
  if answer == 'y' or answer == '' : 
    call(command, shell=True)
    print 'Tgcmproc is executing (please wait)...'

date = datetime.datetime.now()
#print 'Tgcmproc completed at ',date,':'
print '  Output file (stdout): ',proc.stdout 
print '  PS file (postscript): ',proc.flnm_ps 
