#!/usr/bin/env ruby =begin = gtadd-n.rb : 引数の netCDF データの和を値としてもつ netCDF データを作成. = USAGE : $ gtadd-n.rb -[hHo] <--output=[outputname]> [ncfile1] [ncfile2] ... = note * 同じ格子構造をもっているファイルでなくてはならない. * 変数は一致しなくても演算は可能. =end require "getopts" require "numru/ggraph" require "libgphys-n" #require "numru/netcdf_miss" require "NCEP-ncattr.conf" # 属性設定ファイル include NumRu def gtadd(file) gphys_file = Array.new file.each {|f| var = NetCDF.open(f, "r").var_names[-1] temp = GPhys::IO.open(f, var) temp.data.val.count_invalid gphys_file << temp } sum = gphys_file[0] 1.upto(gphys_file.size-1) do |i| sum += gphys_file[i] end gphys = sum return gphys end def print_help print < [ncfile [ncfile ..] ] [var] * 引数解説 [ncfile] : 対象とする netCDF ファイル名を指定. (ex. mean-V_2001_01.nc) 複数指定可能. その場合, 変数値を各格子毎に平均 したものを出力する. [var] : 足したい変数名. * オプション -h, -H, --help : このメッセージを表示. -o, --output : 出力する netCDF ファイル名を指定. デフォルトは, "gtadd-n.nc" HISTORY: 2003/12/06 created by daktu32@ep.sci.hokudai.ac.jp ==================================================== HELP end # オプション解析 ---------------------------------------------------- unless getopts("hH", "help", "o:", "output:") print "#{$0}:illegal options. please exec this program with -h/--help. \n" exit 1 end if ($OPT_h) || ($OPT_H) || ($OPT_help) || ARGV.size == 0 then print_help exit 1 end # 保存ファイル名が与えられたら, その名前で保存. デフォルトは "gtadd.nc". if ($OPT_o) then output = ($OPT_o).to_s elsif ($OPT_output) then output = ($OPT_output).to_s else output = "gtadd-n.nc" end # オプション解析 終了---------------------------------------------------- # メインルーチン files = ARGV#.delete_if{|x| x !=~ /^(.*)\.nc$/} # 引数最初のデータの SF, AO を取得 var = NetCDF.open(ARGV[0], "r").var_names[-1] gp1 = GPhys::IO.open(ARGV[0], var) sf = gp1.data.get_att('scale_factor') if gp1.data.get_att('scale_factor') ao = gp1.data.get_att('add_offset') if gp1.data.get_att('add_offset') gphys = gtadd(files) gphys.data.set_att('scale_factor', sf) gphys.data.set_att('add_offset', ao) vname = NetCDF.open(files[0], "r").var_names[-1] globalattr = global_attr(files[0]) gphys.save(output, globalattr, vname) ## ヘッダを Monthly-Mean 用に書き換え ---------------------- nc = NetCDF.open(output, 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 nc.close exit