changeset 11813:2747c3cad27d octave-forge

low-level group support (2)
author abarth93
date Mon, 17 Jun 2013 14:00:33 +0000
parents 35593745a664
children b6f0fc57a9c8
files main/netcdf/src/ncinfo.m main/netcdf/src/netcdf_package.cc main/netcdf/src/test_netcdf_hl.m main/netcdf/src/test_netcdf_package.m
diffstat 4 files changed, 108 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/main/netcdf/src/ncinfo.m	Mon Jun 17 13:05:27 2013 +0000
+++ b/main/netcdf/src/ncinfo.m	Mon Jun 17 14:00:33 2013 +0000
@@ -8,47 +8,27 @@
 % vinfo.Size: the size of the netcdf variable. For vectors the Size field
 %   has only one element.
 %
+% Note: If there are no attributes (or variable or groups), the corresponding 
+% field is an empty matrix and not an empty struct array for compability
+% with matlab.
+% 
+%
 %
 function info = ncinfo(filename,varname)
 
 ncid = netcdf_open(filename,'NC_NOWRITE');
-unlimdimIDs = netcdf_inqUnlimDims(ncid);
 
 if nargin == 1    
-    info.Filename = filename;
-    info.Name = '/';
-    
-    [ndims,nvars,ngatts] = netcdf_inq(ncid);
-    
-    % dimensions
-    info.Dimensions = ncinfo_dim(ncid,0:ndims-1,unlimdimIDs);
-
-    % variables
-    for i=1:nvars
-        info.Variables(i) = ncinfo_var(ncid,filename,i-1,unlimdimIDs);
-    end
-
-    % global attributes
-    info.Attributes = [];
-    gid = netcdf_getConstant('NC_GLOBAL');
-    for i = 0:ngatts-1  
-      tmp = struct();
-      tmp.Name = netcdf_inqAttName(ncid,gid,i);
-      tmp.Value = netcdf_getAtt(ncid,gid,tmp.Name);
-    
-      if isempty(info.Attributes)      
-        info.Attributes = [tmp];
-      else
-        info.Attributes(i+1) = tmp;
-      end
-    end
+    info.Filename = filename;    
+    info = ncinfo_group(ncid,info);
     
     % format
     format = netcdf_inqFormat(ncid);
     info.Format = lower(strrep(format,'FORMAT_',''));    
 elseif nargin == 2
+    unlimdimIDs = netcdf_inqUnlimDims(ncid);
     varid = netcdf_inqVarID(ncid, varname);
-    info = ncinfo_var(ncid,filename,varid,unlimdimIDs);
+    info = ncinfo_var(ncid,varid,unlimdimIDs);
 end
 
 netcdf_close(ncid);
@@ -72,7 +52,7 @@
 end
 
 
-function vinfo = ncinfo_var(ncid,filename,varid,unlimdimIDs)
+function vinfo = ncinfo_var(ncid,varid,unlimdimIDs)
 
 [vinfo.Name,xtype,dimids,natts] = netcdf_inqVar(ncid,varid);
 
@@ -149,6 +129,57 @@
 
 end
 
+
+function info = ncinfo_group(ncid,info)
+if nargin == 1
+  info = struct();
+end
+
+info.Name = netcdf_inqGrpName(ncid);
+unlimdimIDs = netcdf_inqUnlimDims(ncid);
+
+[ndims,nvars,ngatts] = netcdf_inq(ncid);
+
+% dimensions
+
+dimids = netcdf_inqDimIDs(ncid);
+info.Dimensions = ncinfo_dim(ncid,dimids,unlimdimIDs);
+
+% variables
+for i=1:nvars
+  info.Variables(i) = ncinfo_var(ncid,i-1,unlimdimIDs);
+end
+
+% global attributes
+info.Attributes = [];
+gid = netcdf_getConstant('NC_GLOBAL');
+for i = 0:ngatts-1  
+  tmp = struct();
+  tmp.Name = netcdf_inqAttName(ncid,gid,i);
+  tmp.Value = netcdf_getAtt(ncid,gid,tmp.Name);
+  
+  if isempty(info.Attributes)      
+    info.Attributes = [tmp];
+  else
+    info.Attributes(i+1) = tmp;
+  end
+end
+
+info.Groups = [];
+gids = netcdf_inqGrps(ncid);
+for i = 1:length(gids)
+  tmp = ncinfo_group(gids(i));
+  
+  if isempty(info.Groups)      
+    info.Groups = [tmp];
+  else
+    info.Groups(i) = tmp;
+  end
+end
+
+end
+
+
 %% Copyright (C) 2013 Alexander Barth
 %%
 %% This program is free software; you can redistribute it and/or modify
--- a/main/netcdf/src/netcdf_package.cc	Mon Jun 17 13:05:27 2013 +0000
+++ b/main/netcdf/src/netcdf_package.cc	Mon Jun 17 14:00:33 2013 +0000
@@ -1104,6 +1104,29 @@
   return retval;
 }
 
+// int nc_inq_dimids(int ncid, int *ndims, int *dimids, int include_parents);
+DEFUN_DLD(netcdf_inqDimIDs, args,, 
+"")
+{
+  if (args.length() != 1 && args.length() != 2) {
+      print_usage ();
+      return octave_value();
+    }
+
+  int ncid = args(0).scalar_value();
+  int include_parents = 0;
+  if (args.length() == 2) {
+    include_parents = args(0).scalar_value();  
+  }
+
+  int ndims;
+  check_err(nc_inq_ndims(ncid, &ndims));
+  Array<int> dimids = Array<int>(dim_vector(1,ndims));
+  check_err(nc_inq_dimids(ncid, &ndims, dimids.fortran_vec(),include_parents));
+    
+  return octave_value(dimids);
+}
+
 
 DEFUN_DLD(netcdf_inqNVars, args,, 
 "")
@@ -1171,6 +1194,24 @@
   return octave_value(ncids);
 }
 
+//int nc_inq_grpname(int ncid, char *name);
+DEFUN_DLD(netcdf_inqGrpName, args,, 
+"")
+{
+  if (args.length() != 1) {
+      print_usage ();
+      return octave_value();
+    }
+
+  int ncid = args(0).scalar_value();
+  char name[NC_MAX_NAME+1];
+
+  check_err(nc_inq_grpname(ncid, name));    
+  return octave_value(std::string(name));
+}
+
+//int nc_inq_grpname_full(int ncid, size_t *lenp, char *full_name);
+
 
 // int nc_inq_ncid(int ncid, const char *name, int *grp_ncid);
 DEFUN_DLD(netcdf_inqNcid, args,, 
--- a/main/netcdf/src/test_netcdf_hl.m	Mon Jun 17 13:05:27 2013 +0000
+++ b/main/netcdf/src/test_netcdf_hl.m	Mon Jun 17 14:00:33 2013 +0000
@@ -45,11 +45,8 @@
 % netcdf4
 
 nccreate(fname,'temp','Dimensions',{'lon',10,'lat',20},'Format','netcdf4');
-
-% error in octave:
-% Attempting netcdf-4 operation on strict nc3 netcdf-4 file
-%ncwriteatt(fname,'temp','uint_range',uint32([0 10]));
-%assert(isequal(ncreadatt(fname,'temp','uint_range'),[0 10]));
+ncwriteatt(fname,'temp','uint_range',uint32([0 10]));
+assert(isequal(ncreadatt(fname,'temp','uint_range'),[0 10]));
 
 info = ncinfo(fname);
 assert(strcmp(info.Format,'netcdf4'));
--- a/main/netcdf/src/test_netcdf_package.m	Mon Jun 17 13:05:27 2013 +0000
+++ b/main/netcdf/src/test_netcdf_package.m	Mon Jun 17 14:00:33 2013 +0000
@@ -282,6 +282,10 @@
 
 id4 = netcdf.inqNcid(ncid,'group1');
 assert(id1 == id4)
+
+name = netcdf.inqGrpName(id3);
+assert(strcmp(name,'subgroup'))
+
 netcdf.close(ncid);
 %system(['ncdump -h ' fname])
 delete(fname);