[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[dennou-ruby:001871] yaml



堀之内です。

GPhys 利用者の方。
たまたま某MLでしって、ほおっと思ったトリビアです。

標準で付いてくるのは Ruby 1.8 からになりますが、yaml というのあっ
て(Markup、オブジェクトの中身を構造化してテキストにしてくれるそうです。
使い方は、obj というオブジェクトがあったとして、

require "yaml"
ymstr = obj.to_yaml
print ymstr

です。GPhys みたいに階層構造を持ったオブジェクトを見てみるのには
いいかもしれません。実際にやってみるとこんな感じ。GPhys の例は
ちょっと長すぎて見にくいですが。

## 例0

require "yaml"
ary = [0,1,['aa',3,['c','d']]]
print ary.to_yaml# --> 下記の表示結0

<<表示結果0>>
- 0
- 1
- 
  - aa
  - 3
  - 
    - c
    - d=> nil

## 例1

require "yaml"
require "numru/gphys"
include NumRu
gphys_nc = GPhys::IO.open('VWND_NCEP.nc','vwnd')
print gphys_nc.to_yaml                    # --> 下記の表示結果1
gphys_on_memory = gphys_nc[false,0].copy  # 最初(0)の時刻分をメモリ上にコピー
print gphys_on_memory.to_yaml             # --> 下記の表示結果2

<<表示結果1>>
--- !ruby/object:NumRu::GPhys 
data: !ruby/object:NumRu::VArrayNetCDF 
  ary: &id001 !ruby/object:NumRu::NetCDFVar 
    missval: !ruby/object:NArray {}
    vmax: !ruby/object:NArray {}
    vmin: !ruby/object:NArray {}
  attr: !ruby/object:NumRu::AttributeNetCDF 
    nv: *id001
  convention: &id003 !ruby/object:Module {}
  mapping: 
  name: vwnd
  varray: 
grid: !ruby/object:NumRu::Grid 
  axes: 
    - !ruby/object:NumRu::Axis 
      aux: 
      bare_index: false
      cell: false
      cell_bounds: 
      cell_center: 
      init_fin: true
      name: lon
      pos: !ruby/object:NumRu::VArrayNetCDF 
        ary: &id002 !ruby/object:NumRu::NetCDFVar 
          missval: 
          vmax: 
          vmin: 
        attr: !ruby/object:NumRu::AttributeNetCDF 
          nv: *id002
        convention: *id003
        mapping: 
        name: lon
        varray: 
    - !ruby/object:NumRu::Axis 
      aux: 
      bare_index: false
      cell: false
      cell_bounds: 
      cell_center: 
      init_fin: true
      name: lat
      pos: !ruby/object:NumRu::VArrayNetCDF 
        ary: &id004 !ruby/object:NumRu::NetCDFVar 
          missval: 
          vmax: 
          vmin: 
        attr: !ruby/object:NumRu::AttributeNetCDF 
          nv: *id004
        convention: *id003
        mapping: 
        name: lat
        varray: 
    - !ruby/object:NumRu::Axis 
      aux: 
      bare_index: false
      cell: false
      cell_bounds: 
      cell_center: 
      init_fin: true
      name: level
      pos: !ruby/object:NumRu::VArrayNetCDF 
        ary: &id005 !ruby/object:NumRu::NetCDFVar 
          missval: 
          vmax: 
          vmin: 
        attr: !ruby/object:NumRu::AttributeNetCDF 
          nv: *id005
        convention: *id003
        mapping: 
        name: level
        varray: 
    - !ruby/object:NumRu::Axis 
      aux: 
      bare_index: false
      cell: false
      cell_bounds: 
      cell_center: 
      init_fin: true
      name: time
      pos: !ruby/object:NumRu::VArrayNetCDF 
        ary: &id006 !ruby/object:NumRu::NetCDFVar 
          missval: 
          vmax: 
          vmin: 
        attr: !ruby/object:NumRu::AttributeNetCDF 
          nv: *id006
        convention: *id003
        mapping: 
        name: time
        varray: 
  axnames: 
    - lon
    - lat
    - level
    - time
  lost_axes: []
  rank: 4=> nil


<<表示結果2>>
--- !ruby/object:NumRu::GPhys 
data: !ruby/object:NumRu::VArray 
  ary: !ruby/object:NArrayMiss 
    array: !ruby/object:NArray {}
    mask: !ruby/object:NArray {}
  attr: !ruby/hash:NumRu::Attribute 
    GRIB_id: !ruby/object:NArray {}
    title: "4xDaily V wind, NMC reanalysis (2001-04)"
    parent_stat: >-
      Other

      -

    var_desc: >-
      v-wind

      V

    missing_value: !ruby/object:NArray {}
    long_name: 4xDaily V wind
    least_significant_digit: !ruby/object:NArray {}
    scale_factor: !ruby/object:NArray {}
    units: m/s
    valid_range: !ruby/object:NArray {}
    level_desc: >-
      Multiple levels

      F

    actual_range: !ruby/object:NArray {}
    dataset: >-
      NMC Reanalysis

      L

    add_offset: !ruby/object:NArray {}
    precision: !ruby/object:NArray {}
    statistic: >-
      Individual Obs

      I

    GRIB_name: VGRD
  mapping: 
  name: vwnd
  varray: 
grid: !ruby/object:NumRu::Grid 
  axes: 
    - !ruby/object:NumRu::Axis 
      aux: 
      bare_index: false
      cell: false
      cell_bounds: 
      cell_center: 
      init_fin: true
      name: lon
      pos: !ruby/object:NumRu::VArray 
        ary: !ruby/object:NArray {}
        attr: !ruby/hash:NumRu::Attribute 
          long_name: Longitude
          units: degrees_east
          actual_range: !ruby/object:NArray {}
        mapping: 
        name: lon
        varray: 
    - !ruby/object:NumRu::Axis 
      aux: 
      bare_index: false
      cell: false
      cell_bounds: 
      cell_center: 
      init_fin: true
      name: lat
      pos: !ruby/object:NumRu::VArray 
        ary: !ruby/object:NArray {}
        attr: !ruby/hash:NumRu::Attribute 
          long_name: Latitude
          units: degrees_north
          actual_range: !ruby/object:NArray {}
        mapping: 
        name: lat
        varray: 
    - !ruby/object:NumRu::Axis 
      aux: 
      bare_index: false
      cell: false
      cell_bounds: 
      cell_center: 
      init_fin: true
      name: level
      pos: !ruby/object:NumRu::VArray 
        ary: !ruby/object:NArray {}
        attr: !ruby/hash:NumRu::Attribute 
          GRIB_id: !ruby/object:NArray {}
          positive: down
          long_name: Level
          units: mb
          actual_range: !ruby/object:NArray {}
          GRIB_name: hPa
        mapping: 
        name: level
        varray: 
  axnames: 
    - lon
    - lat
    - level
  lost_axes: 
    - time=1.75338e+07 hours since 1-1-1 00:00:0.0
  rank: 3=> nil