#!/usr/bin/env ruby
=begin
=gpcut

Extract, slicing and thinning a GPhys variable.

==USAGE 

   % gpcut [options] path@varname[,dimname=pos1[:pos2[:thinning_intv]][,dimname=...]]

==OPTIONS

   -h,      --help         : print this message. 
   -m dim,  --mean dim     : average along dim axis (optional).
   -o file, --output file  : output filename (optional). 
                           : Default output filename is 'gphys.nc'.

==HISTORY

  2005/05/17  S Takehiro (created)
  2005/07/15  S Takehiro (open_gturl method is used for opening gphys variable)
  2005/08/10  S Takehiro (utilize internal function for printing help message)

=end

require "numru/gphys"
include NumRu

require "getoptlong"

#------------------------ print help message ------------------------
def help
  file = File.open(__FILE__)
  after_begin = false
  after_end = false
  while (line = file.gets)
    after_end = true if /^=end/ =~ line
    print line if after_begin && !after_end
    after_begin = true if /^=begin/ =~ line
  end
  file.close
end

#------------------------ Default Settings ------------------------
Output_default = 'gphys.nc'
URLfmt = "path@varname[,dimname=pos1[:pos2[:thinning_intv]][,dimname=...]]"

#---------------------- Option Configuration ----------------------
parser = GetoptLong.new(
  ["--mean",     "-m", GetoptLong::REQUIRED_ARGUMENT],
  ["--output",   "-o", GetoptLong::REQUIRED_ARGUMENT],
  ["--help",     "-h", GetoptLong::NO_ARGUMENT      ])
begin
  parser.each{|opt, arg|
    case opt
    when "--mean" then eval "$OPT_mean='#{arg}'"
    when "--output" then eval "$OPT_output='#{arg}'"
    when "--help" then eval "$OPT_help=true"
    else
      raise "must not happen"
    end
  }
  rescue GetoptLong::AmbigousOption, GetoptLong::InvalidOption,
          GetoptLong::MissingArgument, 
          GetoptLong::NeedlessArgument => err
    help
    $srderr.puts err.message
    exit 1
end

#------------------------ Help message ------------------------
if $OPT_help then
  help
  exit(0)
end  

#------------------------ Option check ------------------------
$OPT_output = Output_default unless $OPT_output

#------------------------ Open gphys variable ------------------------
gturl = ARGV[0]
gphys = GPhys::IO.open_gturl(gturl)

#----------------------- mean along any axis -------------------------
if ($OPT_mean)
  dims = ($OPT_mean).split(/\s*,\s*/)
  dims.each{|dim|
    dim = dim.to_i if dim.to_i.to_s == dim
    gphys = gphys.mean(dim)
  }
end

#------------------------ Output file check  --------------------------
raise "#{$OPT_output} already exists." if FileTest.exist?($OPT_output) 

#---------------------- Output GPhys variable  ------------------------
GPhys::IO.write( f=NetCDF.create($OPT_output), gphys )
NetCDF_Conventions.add_history(f, File.basename($0)+" "+ARGV[0])
f.close

print File.basename($0) +": "+ARGV[0]+ " is written to #{$OPT_output}.\n"
