Class: NumRu::OPeNDAPDir

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

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ OPeNDAPDir

Returns a new instance of OPeNDAPDir.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/opendapdir.rb', line 7

def initialize(url)
  @path = url.sub(/\/$/,'')
  @child_dirs = Array.new
  @child_gphys_files = Array.new   # files that can have gphysiable data
  @child_plain_files = Array.new     # external files (non-opendap access)
  open(url) {|f|
	if f.content_type != 'text/html'
     raise(ArgumentError,"#{url}: Not text/html. Cannot be an OPeNDAP Dir")
	end
	opendapdir = false  # to initialize
	while(line = f.gets)
	  if /Parent Directory/i =~ line
 opendapdir = true
 break
	  end
	end
	if !opendapdir
	  raise(ArgumentError,"#{url}: is not likely an OPeNDAP directory.")
	end
	while(line = f.gets)
	  case line
	  when /<A HREF="(.*)">(.*)<\/a>\s*(.*)$/
        child = { :url => $1 }
 label = $2
        time_size_descr = $3
        if /^(\S+\s+\S+)\s+(\S+)/ =~ time_size_descr
          size = $2
          time = $1
          t = DateTime.parse(time)
          child[:mtime] = Time.local(t.year,t.mon,t.day,t.hour,t.min,t.sec)
                              #^ local works with mysql (how about others?)
          if /^([\d\.]+)([KMG])?$/ =~ size
            if $2.nil?
              child[:size] = $1.to_i
            else
              size = $1.to_f
              size *= 1000 if $2=='K'
              size *= 1000000 if $2=='M'
              size *= 10000000 if $2=='G'
              child[:size] = size.to_i
            end
          elsif '-' == size
            child[:size] = nil
          else
            raise "(BUG) new pattern to support : #{size}"
          end
        end
 /^(.*)\/([^\/]+)\/?$/ =~ child[:url]
 dir = $1
 child[:name] = $2
 if /\/$/ =~ child[:url]
   @child_dirs.push(child)
 elsif /\.html$/ !~ label and /\.html$/ =~ child[:name]
   child[:url].sub!(/\.html$/,'')
   child[:name].sub!(/\.html$/,'')
   if /\.nc$/ =~ label  # we don't need to limit to .nc files, but..
		@child_gphys_files.push(child)
   end
 elsif dir != @path
   @child_plain_files.push(child)
 else
   raise "(BUG) Unclassified kind of a child : #{child[:url]}"
 end
#	    p child
#	    print "#{File.basename(child[:url])}  #{child[:mtime]}  ",child[:size],"\n"
#	    print "@@\n"
	  end
	end
  }
end

Instance Method Details

#each_dirObject



99
100
101
102
103
104
105
# File 'lib/opendapdir.rb', line 99

def each_dir
  @child_dirs.each{|child|
	dir = OPeNDAPDir.new(child[:url])
	yield(dir, child[:mtime])
  }
  nil
end

#each_file(name_pattern = nil) ⇒ Object

name_pattern : regexp to limit by name



123
124
125
126
127
128
129
# File 'lib/opendapdir.rb', line 123

def each_file(name_pattern = nil)
  @child_plain_files.each{|child|
    if name_pattern.nil? or name_pattern =~ child[:name]
      yield(child[:url], child[:mtime], child[:size])
    end
  }
end

#each_gphys_fileObject

Open dods url as NetCDF files and iterate. It is up to the users to close the files.



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/opendapdir.rb', line 109

def each_gphys_file
  @child_gphys_files.each{|child|
    begin
      file = NetCDF.open(child[:url])
      yield(file.path, child[:mtime], child[:size], file.class)
    rescue NetcdfSyserr
      warn "#{__FILE__}:#{__LINE__}: **Error neglected** Unable to open #{child[:url]}"
    end
  }
  nil
end

#entriesObject



90
91
92
93
94
95
96
97
# File 'lib/opendapdir.rb', line 90

def entries
  if @enttries.nil?
    @enttries = @child_dirs.collect{|c| c[:name]} +
      @child_gphys_files.collect{|c| c[:name]} +
      @child_plain_files.collect{|c| c[:name]}
  end
  @enttries
end

#nameObject



86
87
88
# File 'lib/opendapdir.rb', line 86

def name
  File.basename(path)
end

#pathObject



82
83
84
# File 'lib/opendapdir.rb', line 82

def path
  @path
end