Class: GrADS::Gridded::Writer

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

Instance Method Summary collapse

Constructor Details

#initialize(ctl = nil) ⇒ Writer

Returns a new instance of Writer.



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/grads/gridded.rb', line 516

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



623
624
625
# File 'lib/grads/gridded.rb', line 623

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

#template(ctlfile, &block) ⇒ Object



704
705
706
707
# File 'lib/grads/gridded.rb', line 704

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

#write(ctl_file) ⇒ Object



627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
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
# File 'lib/grads/gridded.rb', line 627

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