Class: GrADS::Gridded::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/grads/binary.rb,
lib/grads/gridded.rb

Instance Method Summary collapse

Constructor Details

#initialize(ctl = nil) ⇒ Writer

Returns a new instance of Writer.



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/grads/binary.rb', line 542

def initialize (ctl = nil)
  @dset  = nil
  @title = nil
  @options = []
  @vectorpairs = nil
  @vars = []
  @nt = 1
  if ctl
    e = ctl.entries
    @undef = e["undef"]
    @pdef  = e["pdef"]
    @xdef  = e["xdef"]
    @ydef  = e["ydef"]
    @zdef  = e["zdef"]
    @tdef  = e["tdef"]
  else
    @undef = nil
    @pdef  = nil
    @xdef  = nil
    @ydef  = nil
    @zdef  = nil
    @tdef  = "1 linear 0z1jan2000"
  end
end

Instance Method Details

#define(&block) ⇒ Object



649
650
651
# File 'lib/grads/binary.rb', line 649

def define (&block)
  instance_eval(&block)
end

#template(ctlfile, &block) ⇒ Object



730
731
732
733
# File 'lib/grads/binary.rb', line 730

def template (ctlfile, &block)
  define(&block)
  write(ctlfile)
end

#write(ctl_file) ⇒ Object



653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'lib/grads/binary.rb', line 653

def write (ctl_file)
  open(ctl_file, "w") { |io|
    if @dset
      io.puts "dset #{@dset}"
    else
      raise "dset is required"
    end
    if @title
      io.puts "title #{@title}"
    end
    io.puts "undef #{@undef}"
    @options.each do |opt|
      io.puts %{options #{opt}}
    end
    if @pdef
      io.puts %{pdef #{@pdef}}
    end
    io.puts %{xdef #{@xdef}}
    io.puts %{ydef #{@ydef}}
    io.puts %{zdef #{@zdef}}
    io.puts %{tdef #{@tdef}}
    if @vectorpairs
      io.puts %{vectorpairs #{@vectorpairs}}
    end

    if @dset =~ /\A\^/
      cwd = File.dirname(File.expand_path(ctl_file))
      dset = File.join(cwd, @dset[1..-1])
    else
      dset = @dset
    end

    io.puts %{vars #{@vars.size}}
    @vars.each do |name, d, obj, desc|
      case d
      when 2
        io.puts %{#{name} 1 99 #{desc}}
      when 3
        case obj.rank 
        when 3
          io.puts %{#{name} #{obj.dim0} 99 #{desc} }
        when 4
          io.puts %{#{name} #{obj.dim1} 99 #{desc} }
        else
          raise "invalid size"
        end
      end
    end
    io.puts "endvars"

    open(dset, "w") { |dat|
      @nt.times do |i|
        @vars.each do |name, d, obj, desc|
          case d
          when 2
            case obj.rank
            when 2
              obj.float.dump_binary(dat)
            when 3
              obj[i,false].float.dump_binary(dat)
            end
          when 3
            case obj.rank
            when 3
              obj.float.dump_binary(dat)
            when 4
              obj[i,false].float.dump_binary(dat)
            end
          end
        end
      end
    }
  }


end