=begin
= netcdf_cutter.rb
== $B35MW(B
:ORG_data $B$K3JG<$5$l$k(B NCEP $B%*%j%8%J%k%G!<%?$r(B 1 $B%u7nKh$KJ,3d$9$k%9%/%j%W%H(B.

== USAGE
  $ netcdf_cutter.rb $B%U%!%$%kL>(B $BJQ?t(B

=end
var_attr= {"units"=>"hours since 1-1-1 00:00:00"}

dtype = "NCEP"
phys = "time"

# exchange time units "hours" => "date&time" 
def time2date(jd)
  dates = jd/24
  jd_ext = Date.jd(dates)
  seireki_gannen = Date.valid_date?(1,1,1)
  date = jd_ext + seireki_gannen 
  return date
end

#############  $B0J2<%a%$%s%k!<%A%s(B  ##############
require "numru/netcdf"
require "numru/ggraph"
include NumRu

require "getopts"

#======= ruby 1.6 $B$G(B Date::parse $B$r$D$+$&$?$a$N$*$^$8$J$$(B
class Hash
  alias values_at indices # values_at $B$O(B ruby-1.6 $B$K$OL5$$%a%=%C%I$J$N$G(B
end
#=======

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


if ARGV[ARGV.size-1] =~ /.nc$/ then
  var = NetCDF.open(file[0], "r").var_names[-1]  
else 
  var = ARGV[ARGV.size-1]
end

pname = var.to_s.upcase #=> $BJ*M}NL$r(B get.

#--------------------------------------
# title $B$rF@$k(B. $B$b$7$J$1$l$P(B, longname $B$r$=$l$H$9$k(B.
nc = NetCDF.open(file[0])
p  long_name = nc.var(var).att("long_name").get
p  title = nc.att("title").get
nc.close

if title 
p  $title = title
else 
p  $title = long_name
end

#  gphys $B%*!<%W%s(B
#
gphys = GPhys::NetCDF_IO.open(file[0], var)

start_time = gphys.coord(gphys.axnames.index(phys)).val[0]

date = time2date(start_time)
year = date.year
leap_flag = date.leap?
  # non_leap_year : [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  # leap_year     : [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]  

if leap_flag 
  index = NArray[0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]*4
else
  index = NArray[0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]*4 
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"

require "netcdf_miss-dcchart"


for i in 0..8
  start = gphys.coord(gphys.axnames.index(phys)).val[index[i]]
  endded = gphys.coord(gphys.axnames.index(phys)).val[index[i+1]-1]
  output = "daily_#{pname}_#{year}-0#{i+1}_#{dtype}.nc"
  p "now making "+output

  # title $BB0@-$K%G!<%?$NG/7n$r2C$($k(B.
  newtitle = title.sub(/^4x\ *[D|d]aily/, long_name+',')
  newtitle = newtitle.sub(/(#{year})/, '')
  gphys.cut(phys=>start..endded).save(output, $global_attr, nil, {"title", newtitle+"(#{year}-0#{i+1})"}, $history)
  
  p "done"
end
for i in 9..11
  start = gphys.coord(gphys.axnames.index(phys)).val[index[i]]
  endded = gphys.coord(gphys.axnames.index(phys)).val[index[i+1]-1]
  output = "daily_#{pname}_#{year}-#{i+1}_#{dtype}.nc"
  p "now making "+output

  # title $BB0@-$K%G!<%?$NG/7n$r2C$($k(B.
  newtitle = title.sub(/^4x\ *[D|d]aily/, long_name+',')
  newtitle = newtitle.sub(/(#{year})/, '')
  gphys.cut(phys=>start..endded).save(output, $global_attr, nil, {"title", newtitle+"(#{year}-0#{i+1})"}, $history)

  p "done"
end

