[hdf-compass] 06/295: finished implementing ASCFile class, an Array subclass of Node

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Sun May 8 10:35:19 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/master
in repository hdf-compass.

commit a85b3e2e6ae2e6c6f4fc2cd84855c13505366d5b
Author: Matt Comerford <matthew.comerford at colorado.edu>
Date:   Thu Jun 19 16:20:37 2014 -0600

    finished implementing ASCFile class, an Array subclass of Node
---
 asc_model/__init__.py | 110 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 71 insertions(+), 39 deletions(-)

diff --git a/asc_model/__init__.py b/asc_model/__init__.py
index 5dc7d20..fdfd20f 100644
--- a/asc_model/__init__.py
+++ b/asc_model/__init__.py
@@ -1,47 +1,16 @@
+"""
+HDF Compass "pilot" plugin for viewing ASCII Grid Data.
+
+Subclasses consist of a Container and an Array, representing
+directories and the ASCII grid data respectively.
+"""
+
 import os
 import os.path as op
 import numpy as np
 
 import compass_model
 
-'''
-That sounds like a great place to start.  Here are some more explicit
-requirements for your "pilot" plugin:
-
-1. The user should be able to browse the filesystem (you can borrow
-code from the "filesystem" plugin for this).  You'll need to implement
-two classes: a subclass of Container (to handle directories), and a
-subclass of Array (to handle the csv files).
-
-2. The user should be able to load array data from files with the
-".csv" extension (hint: use out the canhandle() static method so your
-class is only associated with ".csv" files).
-
-3. Values in the text file must be read in and displayed with the
-array control.  You can assume comma-delimited data with
-newline-separated rows; for example, this 2 x 3 array:
-
-1,2,3
-4,5,6
-
-You should implement the plugin as a new top-level package, let's call
-it "csv_model", similar in structure to "hdf5_model" and
-"filesystem_model".  Some hints on testing:
-
-1. in analogy to the filesystem and array plugins (file://localhost
-and array://localhost), you can have your Store subclass respond to
-"csv://localhost" URLS.
-2. To make the viewer aware of your plugin (so the "csv://localhost"
-URL can actually be opened), add "import csv_model" to the import
-statements in the run() function of compass_viewer/__init__.py.
-
-Also may be useful for the actual reading:
-
-http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html
-
-Give it a shot and let me know if you run into problems.
-'''
-
 class Filesystem(compass_model.Store):
 	
 	"""
@@ -89,6 +58,7 @@ class Filesystem(compass_model.Store):
 			return None
 		return self[op.dirname(key)]
 
+
 class Directory(compass_model.Container):
 
 	"""
@@ -118,6 +88,13 @@ class Directory(compass_model.Container):
 		return self._store
 
 	@property
+	def displayname(self):
+		bn = op.basename(self.key)
+		if len(bn) == 0:
+			return "/"
+		return bn
+
+	@property
 	def description(self):
 		return 'folder "%s" (%d members)' % (self.displayname, len(self))
 
@@ -127,4 +104,59 @@ class Directory(compass_model.Container):
 	def __iter__(self):
 		for name in self._names:
 			key = op.join(self.key, name)
-			yield self._store[key]
\ No newline at end of file
+			yield self._store[key]
+
+	def __getitem__(self, idx):
+		key = op.join(self.key, self._names[idx])
+		return self._store[key]
+
+
+class ASCFile(compass_model.Array):
+
+	"""
+		Represents a .asc grid file.
+	"""
+
+	classkind = "ASCII Grid File"
+
+	@staticmethod
+	def canhandle(store, key):
+		return op.isfile(key) and key.endswith('.asc')
+
+	def __init__(self, store, key):
+		self._store = store
+		self._key = key
+		self.data = np.loadtxt(self._key, skiprows=5).transpose()
+
+	@property
+	def key(self):
+		return self._key
+
+	@property
+	def store(self):
+		return self._store
+
+	@property
+	def displayname(self):
+		return op.basename(self.key)
+
+	@property
+	def description(self):
+		return 'File "%s", size %d bytes' % (self.displayname, op.getsize(self.key))
+
+	@property
+	def shape(self):
+		return self.data.shape
+
+	@property
+	def dtype(self):
+		return self.data.dtype
+
+	def __getitem__(self, args):
+		return self.data[args]
+
+
+Filesystem.push(ASCFile)
+Filesystem.push(Directory)
+
+compass_model.push(Filesystem)
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/hdf-compass.git



More information about the debian-science-commits mailing list