From 411385fc4776f3b7ede4fb7b71bbde7c77a01ec1 Mon Sep 17 00:00:00 2001
From: Wouter Peters <wouter.peters@wur.nl>
Date: Wed, 4 Aug 2010 12:24:33 +0000
Subject: [PATCH] inquiring the unlimited length is made possible by adding a
 variable with the same name

---
 da/tools/io.py | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)

diff --git a/da/tools/io.py b/da/tools/io.py
index b358c897..6cce56be 100755
--- a/da/tools/io.py
+++ b/da/tools/io.py
@@ -192,16 +192,17 @@ class CT_CDF(object):
             return var
 
     def inquire_unlimited(self):
-        """ Get the index of the unlimited dimension"""
+        """ Get the index and name of the unlimited dimension"""
 
         try:
             index     = self.file.dimensions.values().index(None)
             unlimname = self.file.dimensions.keys()[index]
-            unlimlen  = ncf.file.variables[unlimname].shape[0]
+            unlimlen  = self.file.variables[unlimname].shape[0]
         except:
             unlimlen = -1
+            unlimname = 'None'
 
-        return unlimlen
+        return (unlimname,unlimlen,)
 
     def AddData(self,datadict,silent=True):
         """ 
@@ -225,7 +226,7 @@ class CT_CDF(object):
         we don't want the unlimited counter to increase everytime. To get the value of the counter (needed when appending data for instance)
         use
 
-        unlim_count = ncf.inquire_unlimited()
+        unlimname,unlim_count = ncf.inquire_unlimited()
 
         where an integer value is returned for the length, and -1 indicates there is no unlimited dimension
 
@@ -296,18 +297,12 @@ class CT_CDF(object):
 
         # Now that the variable instance is returned, start to add the data. First figure out if we are dealing with an unlimited dimension
 
-        dimlens  =  self.file.dimensions.values()
-        dimnames =  self.file.dimensions.keys()
-        try:
-            index = self.file.dimensions.values().index(None)
-        except:
-            index = -1
-
-        has_unlimited_dim = (index > 0)
+        unlimname,unlimlen = self.inquire_unlimited()
+        has_unlimited_dim = (unlimname != 'None')
 
         # If so, check if the first dimension of the passed dataset corresponds to the unlimited dimension
 
-        if has_unlimited_dim and var.dimensions[0] == dimnames[index]:
+        if has_unlimited_dim and var.dimensions[0] == unlimname:
 
             # Add the data to the unlimited dim, note the special occassion for passing only one value (a record)
 
@@ -361,7 +356,7 @@ if __name__ == '__main__':
     #dimlag=ncf.AddLagDim(unlimited=True)
     #dimidate=ncf.AddDateDimFormat()
 
-    unlimlen = ncf.inquire_unlimited()
+    unlimname,unlimlen = ncf.inquire_unlimited()
 
     if unlimlen < 0: unlimlen=0
 
-- 
GitLab