#! /usr/bin/env ruby
=begin

= title:

  chtitle.rb  -- netCDF $B%G!<%?$N(B title $BB0@-CM$rJQ99$9$k(B.

= usage:

  chtitle.rb file [file...] 

= description:
  dcchart $B%W%m%8%'%/%H$GDs6!$9$k(B netCDF $B%G!<%?$N(B title $BB0@-$r0J2<$N%U%)!<%^%C%H$K=q$-49$($k(B.

    1. $B7nJ?6Q%G!<%?$N>l9g(B

       Monthly Mean {$BJ*M}NL(B}, NMC Reanalysys({$BG/(B}-{$B7n(B})

    2. daily $B%G!<%?$N>l9g(B

       4x /(D|d)/aily {$BJ*M}NL(B}, NMC Reanalysys({$BG/(B}-{$B7n(B})

= history:

  2003-12-22 created by daktu32@ep.sci.hokudai.ac.jp

=end
require "getopts"

require "netcdf_miss-dcchart"
require "ggraph-dcchart"
include NumRu

# $B%*%W%7%g%s2r@O(B ----------------------------------------------------
unless getopts("hHo", "help", "output:", "mean:")
  print "#{$0}:illegal options. please exec this program with -h/--help. \n"
  exit 1
end



file = Array.new
ARGV.each do |i| 
  if i =~ /.nc$/ then
    file << i
  end
end

if file.size == 1 then
  file = file[0].to_s
end


# $B@_DjItJ,(B
## $BDI2C%a%=%C%IDj5A%U%!%$%k(B
require "libgphys-n.rb"
## $BB0@-@_Dj%U%!%$%k(B
require "NCEP-ncattr.conf.rb"


filename = File.basename($0)


file.each {|f|

  ## $B%U%!%$%kL>$+$iG/7n<hF@(B
  ym = File.basename(f).scan(/(\d+)-(\d+)/)
  y = ym[0][0]
  m = ym[0][1]
  ym = y+'-'+m

  ## $B%X%C%@$r=q$-49$((B ----------------------
  nc = NetCDF.open(f, mode="a+")
  nc.redef # into define mode

  # title $BB0@-$K%G!<%?$NG/7n$r2C$($k(B.
  title     = nc.att('title').get
  var = nc.var_names[-1]
  long_name = nc.var(var).att('long_name').get

  title = title.sub(/\(\d\d\d\d\)/, "")
  title = title.sub(/^4x\ *[D|d]aily/, long_name+',')
  title = title.sub(/Monthly Mean/, long_name+',')
  nc.put_att("title", title+"(#{ym})")
  nc.close

  print "*\n"
}
