#! /usr/bin/env ruby =begin = title: mkmean-nc.rb -- netCDF データの任意の次元について平均をとったデータを生成するスクリプト. = usage: mkmean-nc-5dim.rb file [file...] (例: $ mkmean-nc-5dim.rb ./*.nc) = description: * 引数にとった nc ファイルそれぞれに対して, 4, 5 次元目それぞれに対して平均をとった nc ファイルを新たに生成する. * 頭に daily という接頭句がついているファイルのみ対象 * 生成されるファイル名は daily を除いたものになる. * 鉛直データから月平均データを生成するのに使用. = history: 2003-12-22 created by daktu32@ep.sci.hokudai.ac.jp =end require "getopts" require "numru/ggraph" require "libgphys-n" include NumRu # オプション解析 ---------------------------------------------------- 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 p i file << i end end if ARGV[ARGV.size-1] =~ /.nc$/ then var = NetCDF.open(file.last, "r").var_names[-1] else var = ARGV[ARGV.size-1] end if file.size == 1 then file = file[0].to_s end # 設定部分 ## 追加メソッド定義ファイル require "libgphys-n.rb" ## 属性設定ファイル require "NCEP-ncattr.conf.rb" filename = File.basename($0) p file p a = Time.now file.each {|f| p gphys = GPhys::IO.open(f, var) p mean = gphys.mean(gphys.axnames.index($OPT_mean)) p newfile = File.basename(f).sub("daily_", "") p newfile = ($OPT_output) if ($OPT_output) ym = f.scan(/(\d+)-(\d+)/) year = ym[0] month = ym[1] global_attr = global_attr(f) mean.save(newfile, global_attr) ## ヘッダを Monthly-Mean 用に書き換え ---------------------- nc = NetCDF.open(newfile, mode="a+") nc.redef # into define mode # 変数属性を書き換え var = nc.var(var) var.each_att do |att| att_val = att.get if att_val.is_a?(String) att.put(att_val.sub(/^4x\ *[D|d]aily/, "Monthly Mean")) end end # 大域属性を書き換え nc.each_att do |att| att_val = att.get if att_val.is_a?(String) att.put(att_val.sub(/^4x\ *[D|d]aily/, "Monthly Mean")) end end # title 属性にデータの年月を加える. title = nc.att('title').get nc.put_att("title", title+"(#{ym})") nc.close p "make #{newfile}" } p Time.now - a