Module: NumRu::GPhys::GrADS_IO

Defined in:
lib/numru/gphys/gphys_grads_io.rb

Class Method Summary collapse

Class Method Details

.is_a_GrADS?(filename) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
# File 'lib/numru/gphys/gphys_grads_io.rb', line 65

def is_a_GrADS?(filename)
  file = nil
  begin
    file = File.open(filename)
    str = file.read(4)
  ensure
    file.close if file
  end
  return str=="DSET"
end

.open(file, varname) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/numru/gphys/gphys_grads_io.rb', line 76

def open(file, varname)
  if file.is_a?(String)
    file = GrADS_Gridded.open(file)
  elsif ! file.is_a?(GrADS_Gridded)
    raise ArgumentError, "1st arg must be a GrADS_Gridded or a file name"
  end

  grvar = GrADSVar.new(file,varname,true,false)
  data = VArrayGrADS.new(grvar)

  shape_current = grvar.shape_current
  dimnames = grvar.dim_names
  rank = dimnames.length
  bare_index = [ false ] * rank # will be true if coord var is not found

  axes = Array.new
  for i in 0...rank
    grvar = GrADSVar.new(file,dimnames[i],false,true)
    axpos = VArrayGrADS.new(grvar)[0...shape_current[i]]

    cell_center = true
    cell = false

    axis = Axis.new(cell,bare_index[i])
    if !cell
      axis.set_pos( axpos )
    else
      if cell_center
        if varray_cell_bounds
          axis.set_cell(axpos, varray_cell_bounds).set_pos_to_center
        else
          p "cell bounds are guessed"
          axis.set_cell_guess_bounds(axpos).set_pos_to_center
        end
      else  # then it is cell_bounds
        if varray_cell_center
          axis.set_cell(varray_cell_center, axpos).set_pos_to_bounds
        else
          p "cell center is guessed"
          axis.set_cell_guess_center(axpos).set_pos_to_bounds
        end
      end
    end
    
    #p "yet-to-be-defined: method to define aux coord vars"
    
    axes[i] = axis
  end

  grid = Grid.new( *axes )

  GPhys.new(grid,data)
end

.var_names(file) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/numru/gphys/gphys_grads_io.rb', line 147

def var_names(file)
  opened = false
  case file
  when String
    file = GrADS_Gridded.open(file,"r")
  when GrADS_Gridded
    opened = true
  else
    raise ArgumentError, "arg must be a GrADS_Gridded or a file name"
  end
  var_names = file.var_names
  file.close unless opened
  return var_names
end

.var_names_except_coordinates(file) ⇒ Object



161
162
163
# File 'lib/numru/gphys/gphys_grads_io.rb', line 161

def var_names_except_coordinates(file)
  var_names(file)
end

.write(file, gphys, name = nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/numru/gphys/gphys_grads_io.rb', line 130

def write(file, gphys, name=nil)
  if file.is_a?(String)
    file = GrADS_Gridded.open(file,"w")
  elsif ! file.is_a?(GrADS_Gridded)
    raise ArgumentError, "1st arg must be a GrADS_Gridded or a file name"
  end
  if( ! file.dset )
    bfname = file.path.gsub(/\.ctl/,"")+".dat"
    while( File.exist?(bfname) )
      bfname = bfname + "_"
    end
    file.dset = bfname
  end
  VArrayGrADS.write_control(file, gphys)
  VArrayGrADS.write_binary(file, gphys.data)
end