Mercurial > forge
changeset 11796:472856b658d9 octave-forge
initial import
author | abarth93 |
---|---|
date | Sat, 15 Jun 2013 12:52:22 +0000 |
parents | aafb80484fd0 |
children | e921fd6e2d64 |
files | main/netcdf/src/Makefile main/netcdf/src/PKG_ADD.sh main/netcdf/src/nc_constants.h main/netcdf/src/nccreate.m main/netcdf/src/ncinfo.m main/netcdf/src/ncread.m main/netcdf/src/ncreadatt.m main/netcdf/src/ncwrite.m main/netcdf/src/ncwriteatt.m main/netcdf/src/ncwriteschema.m main/netcdf/src/netcdf_package.cc main/netcdf/src/test_netcdf_package.m main/netcdf/src/test_netcdf_package_type.m |
diffstat | 13 files changed, 1739 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/netcdf/src/Makefile Sat Jun 15 12:52:22 2013 +0000 @@ -0,0 +1,27 @@ +LIBS=-lnetcdf +LIBS=$(shell nc-config --libs) + +NCHEADER=/home/abarth/Downloads/netcdf-4.2.1.1/include/netcdf.h +NCHEADER=/usr/include/netcdf.h + +all: netcdf_package.oct + +constants: + awk '/\#define[ \t]+NC_[0-9a-zA-Z_]*[ \t]+/ { printf " constants[\"%s\"] = %s;\n",$$2,$$2 }' $(NCHEADER) | grep -v NC_TURN_OFF_LOGGING | fgrep -v NC_FILL_STRING > nc_constants.h + +clean: + rm netcdf_package.oct + +netcdf_package.oct: netcdf_package.cc nc_constants.h + mkoctfile netcdf_package.cc $(LIBS) + +PKG_ADD: netcdf_package.cc + sh PKG_ADD.sh + +check: PKG_ADD netcdf_package.oct + (mkdir tmp; cd tmp; octave --eval "pkg unload octcdf; addpath $$PWD/..; test_netcdf_package") +# (cd ..; octave --eval "pkg unload octcdf; addpath $$PWD/netcdf; test_netcdf_package") + + +dynamiclibrary: dynamiclibrary.cc + g++ -o dynamiclibrary dynamiclibrary.cc -ldl \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/netcdf/src/PKG_ADD.sh Sat Jun 15 12:52:22 2013 +0000 @@ -0,0 +1,18 @@ +#!/bin/sh + +netcdf_functions=$(awk -F'[(,]' '/DEFUN_DLD/ { print $2 } ' netcdf_package.cc) + +outfile=PKG_ADD + +rm -f $outfile + +for i in $netcdf_functions; do + echo ${i#netcdf_} + cat >> $outfile <<EOF +autoload ("$i", fullfile (fileparts (mfilename ("fullpath")), "netcdf_package.oct")); +EOF + cat >> import_netcdf.m <<EOF +netcdf.${i#netcdf_} = @$i; +EOF + +done
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/netcdf/src/nc_constants.h Sat Jun 15 12:52:22 2013 +0000 @@ -0,0 +1,162 @@ + constants["NC_NAT"] = NC_NAT; + constants["NC_BYTE"] = NC_BYTE; + constants["NC_CHAR"] = NC_CHAR; + constants["NC_SHORT"] = NC_SHORT; + constants["NC_INT"] = NC_INT; + constants["NC_LONG"] = NC_LONG; + constants["NC_FLOAT"] = NC_FLOAT; + constants["NC_DOUBLE"] = NC_DOUBLE; + constants["NC_UBYTE"] = NC_UBYTE; + constants["NC_USHORT"] = NC_USHORT; + constants["NC_UINT"] = NC_UINT; + constants["NC_INT64"] = NC_INT64; + constants["NC_UINT64"] = NC_UINT64; + constants["NC_STRING"] = NC_STRING; + constants["NC_VLEN"] = NC_VLEN; + constants["NC_OPAQUE"] = NC_OPAQUE; + constants["NC_ENUM"] = NC_ENUM; + constants["NC_COMPOUND"] = NC_COMPOUND; + constants["NC_FILL_BYTE"] = NC_FILL_BYTE; + constants["NC_FILL_CHAR"] = NC_FILL_CHAR; + constants["NC_FILL_SHORT"] = NC_FILL_SHORT; + constants["NC_FILL_INT"] = NC_FILL_INT; + constants["NC_FILL_FLOAT"] = NC_FILL_FLOAT; + constants["NC_FILL_DOUBLE"] = NC_FILL_DOUBLE; + constants["NC_FILL_UBYTE"] = NC_FILL_UBYTE; + constants["NC_FILL_USHORT"] = NC_FILL_USHORT; + constants["NC_FILL_UINT"] = NC_FILL_UINT; + constants["NC_FILL_INT64"] = NC_FILL_INT64; + constants["NC_FILL_UINT64"] = NC_FILL_UINT64; + constants["NC_MAX_BYTE"] = NC_MAX_BYTE; + constants["NC_MIN_BYTE"] = NC_MIN_BYTE; + constants["NC_MAX_CHAR"] = NC_MAX_CHAR; + constants["NC_MAX_SHORT"] = NC_MAX_SHORT; + constants["NC_MIN_SHORT"] = NC_MIN_SHORT; + constants["NC_MAX_INT"] = NC_MAX_INT; + constants["NC_MIN_INT"] = NC_MIN_INT; + constants["NC_MAX_FLOAT"] = NC_MAX_FLOAT; + constants["NC_MIN_FLOAT"] = NC_MIN_FLOAT; + constants["NC_MAX_DOUBLE"] = NC_MAX_DOUBLE; + constants["NC_MIN_DOUBLE"] = NC_MIN_DOUBLE; + constants["NC_MAX_UBYTE"] = NC_MAX_UBYTE; + constants["NC_MAX_USHORT"] = NC_MAX_USHORT; + constants["NC_MAX_UINT"] = NC_MAX_UINT; + constants["NC_MAX_INT64"] = NC_MAX_INT64; + constants["NC_MIN_INT64"] = NC_MIN_INT64; + constants["NC_MAX_UINT64"] = NC_MAX_UINT64; + constants["NC_FILL"] = NC_FILL; + constants["NC_NOFILL"] = NC_NOFILL; + constants["NC_NOWRITE"] = NC_NOWRITE; + constants["NC_WRITE"] = NC_WRITE; + constants["NC_CLOBBER"] = NC_CLOBBER; + constants["NC_NOCLOBBER"] = NC_NOCLOBBER; + constants["NC_64BIT_OFFSET"] = NC_64BIT_OFFSET; + constants["NC_NETCDF4"] = NC_NETCDF4; + constants["NC_CLASSIC_MODEL"] = NC_CLASSIC_MODEL; + constants["NC_SHARE"] = NC_SHARE; + constants["NC_MPIIO"] = NC_MPIIO; + constants["NC_MPIPOSIX"] = NC_MPIPOSIX; + constants["NC_PNETCDF"] = NC_PNETCDF; + constants["NC_LOCK"] = NC_LOCK; + constants["NC_FORMAT_CLASSIC"] = NC_FORMAT_CLASSIC; + constants["NC_FORMAT_64BIT"] = NC_FORMAT_64BIT; + constants["NC_FORMAT_NETCDF4"] = NC_FORMAT_NETCDF4; + constants["NC_FORMAT_NETCDF4_CLASSIC"] = NC_FORMAT_NETCDF4_CLASSIC; + constants["NC_SIZEHINT_DEFAULT"] = NC_SIZEHINT_DEFAULT; + constants["NC_ALIGN_CHUNK"] = NC_ALIGN_CHUNK; + constants["NC_UNLIMITED"] = NC_UNLIMITED; + constants["NC_GLOBAL"] = NC_GLOBAL; + constants["NC_MAX_DIMS"] = NC_MAX_DIMS; + constants["NC_MAX_ATTRS"] = NC_MAX_ATTRS; + constants["NC_MAX_VARS"] = NC_MAX_VARS; + constants["NC_MAX_NAME"] = NC_MAX_NAME; + constants["NC_MAX_VAR_DIMS"] = NC_MAX_VAR_DIMS; + constants["NC_ENDIAN_NATIVE"] = NC_ENDIAN_NATIVE; + constants["NC_ENDIAN_LITTLE"] = NC_ENDIAN_LITTLE; + constants["NC_ENDIAN_BIG"] = NC_ENDIAN_BIG; + constants["NC_CHUNKED"] = NC_CHUNKED; + constants["NC_CONTIGUOUS"] = NC_CONTIGUOUS; + constants["NC_NOCHECKSUM"] = NC_NOCHECKSUM; + constants["NC_FLETCHER32"] = NC_FLETCHER32; + constants["NC_NOSHUFFLE"] = NC_NOSHUFFLE; + constants["NC_SHUFFLE"] = NC_SHUFFLE; + constants["NC_NOERR"] = NC_NOERR; + constants["NC_EBADID"] = NC_EBADID; + constants["NC_ENFILE"] = NC_ENFILE; + constants["NC_EEXIST"] = NC_EEXIST; + constants["NC_EINVAL"] = NC_EINVAL; + constants["NC_EPERM"] = NC_EPERM; + constants["NC_ENOTINDEFINE"] = NC_ENOTINDEFINE; + constants["NC_EINDEFINE"] = NC_EINDEFINE; + constants["NC_EINVALCOORDS"] = NC_EINVALCOORDS; + constants["NC_EMAXDIMS"] = NC_EMAXDIMS; + constants["NC_ENAMEINUSE"] = NC_ENAMEINUSE; + constants["NC_ENOTATT"] = NC_ENOTATT; + constants["NC_EMAXATTS"] = NC_EMAXATTS; + constants["NC_EBADTYPE"] = NC_EBADTYPE; + constants["NC_EBADDIM"] = NC_EBADDIM; + constants["NC_EUNLIMPOS"] = NC_EUNLIMPOS; + constants["NC_EMAXVARS"] = NC_EMAXVARS; + constants["NC_ENOTVAR"] = NC_ENOTVAR; + constants["NC_EGLOBAL"] = NC_EGLOBAL; + constants["NC_ENOTNC"] = NC_ENOTNC; + constants["NC_ESTS"] = NC_ESTS; + constants["NC_EMAXNAME"] = NC_EMAXNAME; + constants["NC_EUNLIMIT"] = NC_EUNLIMIT; + constants["NC_ENORECVARS"] = NC_ENORECVARS; + constants["NC_ECHAR"] = NC_ECHAR; + constants["NC_EEDGE"] = NC_EEDGE; + constants["NC_ESTRIDE"] = NC_ESTRIDE; + constants["NC_EBADNAME"] = NC_EBADNAME; + constants["NC_ERANGE"] = NC_ERANGE; + constants["NC_ENOMEM"] = NC_ENOMEM; + constants["NC_EVARSIZE"] = NC_EVARSIZE; + constants["NC_EDIMSIZE"] = NC_EDIMSIZE; + constants["NC_ETRUNC"] = NC_ETRUNC; + constants["NC_EAXISTYPE"] = NC_EAXISTYPE; + constants["NC_EDAP"] = NC_EDAP; + constants["NC_ECURL"] = NC_ECURL; + constants["NC_EIO"] = NC_EIO; + constants["NC_ENODATA"] = NC_ENODATA; + constants["NC_EDAPSVC"] = NC_EDAPSVC; + constants["NC_EDAS"] = NC_EDAS; + constants["NC_EDDS"] = NC_EDDS; + constants["NC_EDATADDS"] = NC_EDATADDS; + constants["NC_EDAPURL"] = NC_EDAPURL; + constants["NC_EDAPCONSTRAINT"] = NC_EDAPCONSTRAINT; + constants["NC_EHDFERR"] = NC_EHDFERR; + constants["NC_ECANTREAD"] = NC_ECANTREAD; + constants["NC_ECANTWRITE"] = NC_ECANTWRITE; + constants["NC_ECANTCREATE"] = NC_ECANTCREATE; + constants["NC_EFILEMETA"] = NC_EFILEMETA; + constants["NC_EDIMMETA"] = NC_EDIMMETA; + constants["NC_EATTMETA"] = NC_EATTMETA; + constants["NC_EVARMETA"] = NC_EVARMETA; + constants["NC_ENOCOMPOUND"] = NC_ENOCOMPOUND; + constants["NC_EATTEXISTS"] = NC_EATTEXISTS; + constants["NC_ENOTNC4"] = NC_ENOTNC4; + constants["NC_ESTRICTNC3"] = NC_ESTRICTNC3; + constants["NC_ENOTNC3"] = NC_ENOTNC3; + constants["NC_ENOPAR"] = NC_ENOPAR; + constants["NC_EPARINIT"] = NC_EPARINIT; + constants["NC_EBADGRPID"] = NC_EBADGRPID; + constants["NC_EBADTYPID"] = NC_EBADTYPID; + constants["NC_ETYPDEFINED"] = NC_ETYPDEFINED; + constants["NC_EBADFIELD"] = NC_EBADFIELD; + constants["NC_EBADCLASS"] = NC_EBADCLASS; + constants["NC_EMAPTYPE"] = NC_EMAPTYPE; + constants["NC_ELATEFILL"] = NC_ELATEFILL; + constants["NC_ELATEDEF"] = NC_ELATEDEF; + constants["NC_EDIMSCALE"] = NC_EDIMSCALE; + constants["NC_ENOGRP"] = NC_ENOGRP; + constants["NC_ESTORAGE"] = NC_ESTORAGE; + constants["NC_EBADCHUNK"] = NC_EBADCHUNK; + constants["NC_ENOTBUILT"] = NC_ENOTBUILT; + constants["NC_HAVE_NEW_CHUNKING_API"] = NC_HAVE_NEW_CHUNKING_API; + constants["NC_INDEPENDENT"] = NC_INDEPENDENT; + constants["NC_COLLECTIVE"] = NC_COLLECTIVE; + constants["NC_ENTOOL"] = NC_ENTOOL; + constants["NC_EXDR"] = NC_EXDR; + constants["NC_SYSERR"] = NC_SYSERR; + constants["NC_FATAL"] = NC_FATAL; + constants["NC_VERBOSE"] = NC_VERBOSE;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/netcdf/src/nccreate.m Sat Jun 15 12:52:22 2013 +0000 @@ -0,0 +1,71 @@ +function nccreate(filename,varname,varargin) + +dimensions = {}; +datatype = 'double'; +format = 'netcdf4_classic'; +FillValue = []; +ChunkSize = []; +DeflateLevel = 'disable'; +Shuffle = false; + + +for i = 1:2:length(varargin) + if strcmp(varargin{i},'Dimensions') + dimensions = varargin{i+1}; + elseif strcmp(varargin{i},'Datatype') + datatype = varargin{i+1}; + elseif strcmp(varargin{i},'Format') + format = varargin{i+1}; + elseif strcmp(varargin{i},'FillValue') + FillValue = varargin{i+1}; + elseif strcmp(varargin{i},'ChunkSize') + ChunkSize = varargin{i+1}; + elseif strcmp(varargin{i},'Shuffle') + Shuffle = varargin{i+1}; + else + error(['unknown keyword ' varargin{i} '.']); + end +end + +if exist(filename,'file') + ncid = netcdf_open(filename,'NC_WRITE'); + netcdf_reDef(ncid); +else + mode = format2mode(format); + ncid = netcdf_create(filename,mode); +end + +% +%varid = netcdf_inqVarID(ncid, varname); + +% create dimensions + +dimids = []; +i = 1; + +while i <= length(dimensions) + + if i == length(dimensions) + dimids(end+1) = netcdf_inqDimID(ncid,dimensions{i}); + i = i+1; + elseif ischar(dimensions{i+1}) + dimids(end+1) = netcdf_inqDimID(ncid,dimensions{i}); + i = i+1; + else + try + dimids(end+1) = netcdf_defDim(ncid,dimensions{i},dimensions{i+1}); + catch + dimids(end+1) = netcdf_inqDimID(ncid,dimensions{i}); + end + i = i+2; + end +end + + +varid = netcdf_defVar(ncid,varname,datatype,dimids); + +% TODO use netcdf4 stuff + + + +netcdf_close(ncid); \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/netcdf/src/ncinfo.m Sat Jun 15 12:52:22 2013 +0000 @@ -0,0 +1,164 @@ +% Information about complete NetCDF file or variable. +% +% finfo = ncinfo(filename) +% vinfo = ncinfo(filename,varname) +% return information about complete NetCDF file (filename) or about +% the specific variable varname. +% +% vinfo.Size: the size of the netcdf variable. For vectors the Size field +% has only one element. +% +% +function info = ncinfo(filename,varname) + +ncid = netcdf_open(filename,'NC_NOWRITE'); + +if nargin == 1 + info.Filename = filename; + + % format + format = netcdf_inqFormat(ncid); + info.Format = lower(strrep(format,'FORMAT_','')); + + % dimensions + [ndims,nvars,ngatts,unlimdimid] = netcdf_inq(ncid); + % need to check if this is necessary? + info.Dimensions = struct('Name',{},'Length',{}); + for i=0:ndims-1 + tmp = struct(); + [tmp.Name,tmp.Length] = netcdf_inqDim(ncid,i); + info.Dimensions(i+1) = tmp; + 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) + vinfo.Attributes = [tmp]; + else + vinfo.Attributes(i+1) = tmp; + end + end + + nvars = netcdf_inqNVars(ncid); + + for i=1:nvars + info.Variables(i) = ncinfo_var(ncid,filename,i-1); + end +elseif nargin == 2 + varid = netcdf_inqVarID(ncid, varname); + info = ncinfo_var(ncid,filename,varid); +end + +netcdf_close(ncid); +end + +function vinfo = ncinfo_var(ncid,filename,varid) + + +%varid = netcdf_inqVarID(ncid, varname) +[vinfo.Name,xtype,dimids,natts] = netcdf_inqVar(ncid,varid); + +%vinfo.Filename = filename; + +% number of dimenions +nd = length(dimids); + + +if nd == 0 + vinfo.Size = 1; +else + vinfo.Size = zeros(1,nd); +end + +vinfo.Dimensions = []; +vinfo.FillValue = []; + +% Data type + +if xtype == netcdf_getConstant('NC_CHAR') + vinfo.Datatype = 'char'; +elseif xtype == netcdf_getConstant('NC_FLOAT') + vinfo.Datatype = 'float'; +elseif xtype == netcdf_getConstant('NC_DOUBLE') + vinfo.Datatype = 'double'; +elseif xtype == netcdf_getConstant('NC_BYTE') + vinfo.Datatype = 'int8'; +elseif xtype == netcdf_getConstant('NC_SHORT') + vinfo.Datatype = 'int16'; +elseif xtype == netcdf_getConstant('NC_INT') + vinfo.Datatype = 'int32'; +elseif xtype == netcdf_getConstant('NC_INT64') + vinfo.Datatype = 'int64'; +elseif xtype == netcdf_getConstant('NC_UBYTE') + vinfo.Datatype = 'uint8'; +elseif xtype == netcdf_getConstant('NC_USHORT') + vinfo.Datatype = 'uint16'; +elseif xtype == netcdf_getConstant('NC_UINT') + vinfo.Datatype = 'uint32'; +elseif xtype == netcdf_getConstant('NC_UINT64') + vinfo.Datatype = 'uint64'; +else + error('netcdf:unknownDataType','unknown data type %d',xtype) +end + +% Information about dimension + +for i=1:length(dimids) + tmp = struct(); + + [tmp.Name, tmp.Length] = netcdf_inqDim(ncid,dimids(i)); + vinfo.Size(i) = tmp.Length; + + %tmp.Unlimited = ?? + + if isempty(vinfo.Dimensions) + vinfo.Dimensions = [tmp]; + else + vinfo.Dimensions(i) = tmp; + end +end + +% Attributes + +vinfo.Attributes = []; + +for i = 0:natts-1 + tmp = struct(); + tmp.Name = netcdf_inqAttName(ncid,varid,i); + tmp.Value = netcdf_getAtt(ncid,varid,tmp.Name); + + if isempty(vinfo.Attributes) + vinfo.Attributes = [tmp]; + else + vinfo.Attributes(i+1) = tmp; + end + + if strcmp(tmp.Name,'_FillValue') + vinfo.FillValue = tmp.Value; + end +end + + +end + + +%% Copyright (C) 2013 Alexander Barth +%% +%% This program is free software; you can redistribute it and/or modify +%% it under the terms of the GNU General Public License as published by +%% the Free Software Foundation; either version 3 of the License, or +%% (at your option) any later version. +%% +%% This program is distributed in the hope that it will be useful, +%% but WITHOUT ANY WARRANTY; without even the implied warranty of +%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +%% GNU General Public License for more details. +%% +%% You should have received a copy of the GNU General Public License +%% along with this program; If not, see <http://www.gnu.org/licenses/>.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/netcdf/src/ncread.m Sat Jun 15 12:52:22 2013 +0000 @@ -0,0 +1,87 @@ +% Read a NetCDF variable. +% +% x = ncread(filename,varname) +% x = ncread(filename,varname,start,count,stride) +% read the variable varname from file filename. +% The parameter start contains the starting indices, count +% is the number of elements and stride the increment between +% two successive elements (default 1). + +function x = ncread(filename,varname,start,count,stride) + +ncid = netcdf_open(filename,'NC_NOWRITE'); +varid = netcdf_inqVarID(ncid, varname); +[varname_,xtype,dimids,natts] = netcdf_inqVar(ncid,varid); + +% number of dimenions +nd = length(dimids); + +sz = zeros(1,nd); +for i=1:length(dimids) + [dimname, sz(i)] = netcdf_inqDim(ncid,dimids(i)); +end + +if nargin < 3 + start = ones(1,nd); +end + +if nargin < 4 + count = inf*ones(1,nd); +end + +if nargin < 5 + stride = ones(1,nd); +end + +% replace inf in count +i = count == inf; +count(i) = (sz(i)-start(i))./stride(i) + 1; + +x = netcdf_getVar(ncid,varid,start-1,count,stride); + +% apply attributes + +factor = []; +offset = []; +fv = []; + +for i = 0:natts-1 + attname = netcdf_inqAttName(ncid,varid,i); +% attname + if strcmp(attname,'scale_factor') + factor = netcdf_getAtt(ncid,varid,'scale_factor'); + elseif strcmp(attname,'add_offset') + offset = netcdf_getAtt(ncid,varid,'add_offset'); + elseif strcmp(attname,'_FillValue') + fv = netcdf_getAtt(ncid,varid,'_FillValue'); + end +end + +if ~isempty(fv) + x(x == fv) = NaN; +end + +if ~isempty(factor) + x = x * factor; +end + +if ~isempty(offset) + x = x + offset; +end + +netcdf_close(ncid); + +%% Copyright (C) 2013 Alexander Barth +%% +%% This program is free software; you can redistribute it and/or modify +%% it under the terms of the GNU General Public License as published by +%% the Free Software Foundation; either version 2 of the License, or +%% (at your option) any later version. +%% +%% This program is distributed in the hope that it will be useful, +%% but WITHOUT ANY WARRANTY; without even the implied warranty of +%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +%% GNU General Public License for more details. +%% +%% You should have received a copy of the GNU General Public License +%% along with this program; If not, see <http://www.gnu.org/licenses/>.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/netcdf/src/ncreadatt.m Sat Jun 15 12:52:22 2013 +0000 @@ -0,0 +1,23 @@ + +function val = ncreadatt(filename,varname,attname) + +ncid = netcdf_open(filename,'NC_NOWRITE'); +varid = netcdf_inqVarID(ncid, varname); +[varname_,xtype,dimids,natts] = netcdf_inqVar(ncid,varid); +val = []; +found = 0; + +for i = 0:natts-1 + if strcmp(netcdf_inqAttName(ncid,varid,i),attname) + val = netcdf_getAtt(ncid,varid,attname); + found = 1; + break + end +end + +netcdf_close(ncid); + +if ~found + error('netcdf:readAttrib','attribute not found'); +end +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/netcdf/src/ncwrite.m Sat Jun 15 12:52:22 2013 +0000 @@ -0,0 +1,90 @@ +% Write a NetCDF variable. +% +% ncwrite(filename,varname,x) +% ncwrite(filename,varname,x,start,stride) +% write the variable varname to file filename. +% The parameter start contains the starting indices +% and stride the increment between +% two successive elements (default 1). + +function ncwrite(filename,varname,x,start,stride) + +ncid = netcdf_open(filename,'NC_WRITE'); +varid = netcdf_inqVarID(ncid, varname); +[varname_,xtype,dimids,natts] = netcdf_inqVar(ncid,varid); + +% number of dimenions +nd = length(dimids); + +sz = zeros(1,nd); +count = zeros(1,nd); + +for i=1:length(dimids) + [dimname, sz(i)] = netcdf_inqDim(ncid,dimids(i)); + count(i) = size(x,i); +end + +if nargin < 4 + start = ones(1,nd); +end + +if nargin < 5 + stride = ones(1,nd); +end + + +% apply attributes + +factor = []; +offset = []; +fv = []; + +for i = 0:natts-1 + attname = netcdf_inqAttName(ncid,varid,i); + attname + if strcmp(attname,'scale_factor') + factor = netcdf_getAtt(ncid,varid,'scale_factor'); + elseif strcmp(attname,'add_offset') + offset = netcdf_getAtt(ncid,varid,'add_offset'); + elseif strcmp(attname,'_FillValue') + fv = netcdf_getAtt(ncid,varid,'_FillValue'); + end +end + +if ~isempty(fv) + x(isnan(x)) = fv; +end + +if ~isempty(offset) + x = x - offset; +end + +if ~isempty(factor) + x = x / factor; +end +start +stride +rg(x) +filename +varid +ncid +netcdf_putVar(ncid,varid,start-1,count,stride,x); +%netcdf_putVar(ncid,varid,x); + +netcdf_close(ncid); + + +%% Copyright (C) 2012-2013 Alexander Barth +%% +%% This program is free software; you can redistribute it and/or modify +%% it under the terms of the GNU General Public License as published by +%% the Free Software Foundation; either version 2 of the License, or +%% (at your option) any later version. +%% +%% This program is distributed in the hope that it will be useful, +%% but WITHOUT ANY WARRANTY; without even the implied warranty of +%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +%% GNU General Public License for more details. +%% +%% You should have received a copy of the GNU General Public License +%% along with this program; If not, see <http://www.gnu.org/licenses/>.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/netcdf/src/ncwriteatt.m Sat Jun 15 12:52:22 2013 +0000 @@ -0,0 +1,14 @@ +function ncwriteatt(filename,varname,attname,val) + +ncid = netcdf_open(filename,'NC_WRITE'); +netcdf_reDef(ncid); + +if strcmp(varname,'/') + varid = 0; +else + varid = netcdf_inqVarID(ncid, varname); +end + +netcdf_putAtt(ncid,varid,attname,val); + +netcdf_close(ncid);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/netcdf/src/ncwriteschema.m Sat Jun 15 12:52:22 2013 +0000 @@ -0,0 +1,66 @@ +function ncwriteschema(filename,s) + +% normalize schema + +if ~isfield(s,'Attributes') + s.Attributes = struct('Name',{},'Value',{}); +end + +if ~isfield(s,'Variables') + s.Variables = struct('Name',{},'Dimensions',{},'Datatype',{});; +end + + +mode = format2mode(s.Format); +ncid = netcdf_create(filename,mode); + +% dimension +for i = 1:length(s.Dimensions) + s.Dimensions(i).id = netcdf_defDim(ncid,s.Dimensions(i).Name,... + s.Dimensions(i).Length); +end + +% global attributes +gid = netcdf_getConstant('NC_GLOBAL'); +for j = 1:length(s.Attributes) + netcdf_putAtt(ncid,gid,s.Attributes(j).Name,s.Attributes(j).Value); +end + +% variables +for i = 1:length(s.Variables) + v = s.Variables(i); + v.Name + % get dimension id + dimids = zeros(length(v.Dimensions),1); + for j = 1:length(v.Dimensions) + ind = find(strcmp({s.Dimensions.Name},v.Dimensions(j).Name)); + if isempty(ind) + error('netcdf:unknownDim','Unkown dimension %s',Dimensions(j).Name); + end + + dimids(j) = s.Dimensions(ind).id; + end + + % define variable + switch lower(v.Datatype) + case 'int32' + dtype = 'int'; + case 'single' + dtype = 'float'; + case 'double' + dtype = 'double'; + case 'char' + dtype = 'char'; + otherwise + error('netcdf:unkownType','unknown type %s',v.Datatype); + end + + varid = netcdf_defVar(ncid,v.Name,dtype,dimids); + + % define attributes + for j = 1:length(v.Attributes) + netcdf_putAtt(ncid,varid,v.Attributes(j).Name,v.Attributes(j).Value); + end +end + +netcdf_close(ncid); \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/netcdf/src/netcdf_package.cc Sat Jun 15 12:52:22 2013 +0000 @@ -0,0 +1,848 @@ +#include <octave/oct.h> +#include <octave/ov-cell.h> + +#include <netcdf.h> + +#include <string> +#include <map> +#include <iostream> +#include <algorithm> +#include <vector> + +using namespace std; + +typedef std::map<std::string, long long>::const_iterator map_const_iterator; +std::map<std::string, long long> constants; + +void init() { + #include "nc_constants.h" +} + +void check_err(int status) +{ + if (status != NC_NOERR) error("%s",nc_strerror(status)); +} + + +int _get_constant(std::string name) +{ + if (constants.empty()) + { + init(); + } + + map_const_iterator cst = constants.find(name); + + if (cst != constants.end ()) + return cst->second; + else + error("unknown netcdf constant: %s",name.c_str()); +} + +int _get_type(std::string name) +{ + std::transform(name.begin(), name.end(),name.begin(), ::toupper); + return _get_constant("NC_" + name); +} + + +int netcdf_constants(octave_value ov) +{ + if (ov.is_scalar_type()) + { + return ov.scalar_value(); + } + else + { + return _get_constant(ov.string_value()); + } +} + +void start_count_stride(int ncid, int varid, octave_value_list args,int len,size_t* start,size_t* count,ptrdiff_t* stride) +{ + int ndims, dimids[NC_MAX_VAR_DIMS]; + + check_err(nc_inq_varndims (ncid, varid, &ndims)); + check_err(nc_inq_vardimid (ncid, varid, dimids)); + + // default values for start, count and stride + // i.e. all variable is loaded + + for (int i=0; i<ndims; i++) { + check_err(nc_inq_dimlen(ncid,dimids[i],&(count[i]))); + start[i] = 0; + //cout << "count def " << count[i] << " " << i << endl; + stride[i] = 1; + } + + // start argument + + if (len > 2) + { + uint64NDArray tmp = args(2).uint64_array_value(); + + if (tmp.dims().numel() != ndims) + { + error("number of elements of argument %s should match the number " + "of dimension of the netCDF variable", + "start"); + } + + for (int i=0; i<ndims; i++) + { + start[i] = (size_t)tmp(ndims-i-1); + + // if start is specified, the default for count is 1 (how odd!) + count[i] = 1; + //cout << "start " << start[i] << " " << i << endl; + } + } + + // count argument + + if (len > 3) + { + uint64NDArray tmp = args(3).uint64_array_value(); + + if (tmp.dims().numel() != ndims) + { + error("number of elements of argument %s should match the number " + "of dimension of the netCDF variable", + "count"); + } + + for (int i=0; i<ndims; i++) + { + count[i] = (size_t)tmp(ndims-i-1); + //count[i] = (size_t)tmp(i); + //cout << "count " << count[i] << " " << i << endl; + } + } + + // stride argument + + if (len > 4) + { + int64NDArray tmp = args(4).int64_array_value(); + + if (tmp.dims().numel() != ndims) + { + error("number of elements of argument %s should match the number " + "of dimension of the netCDF variable", + "stride"); + } + + for (int i=0; i<ndims; i++) + { + stride[i] = (ptrdiff_t)tmp(ndims-i-1); + } + } + +} + + +DEFUN_DLD(netcdf_getConstant, args,, +"") +{ + if (args.length() != 1) { + print_usage (); + return octave_value(); + } + + return octave_value(netcdf_constants(args(0))); +} + + +DEFUN_DLD(netcdf_getConstantNames, args,, +"") +{ + + + if (args.length() != 0) { + print_usage (); + return octave_value(); + } + + Cell c = Cell (dim_vector(1,constants.size())); + //Cell c = Cell (); + int i = 0; + for (map_const_iterator p = constants.begin (); p != constants.end (); p++) { + //std::cout << "val " << p->first << p->second << std::endl; + c(i++) = octave_value(p->first); + } + + + + return octave_value(c); + +} + + +DEFUN_DLD(netcdf_inqLibVers, args,, +"") +{ + if (args.length() != 0) { + print_usage (); + return octave_value(); + } + + return octave_value(string(nc_inq_libvers())); +} + +DEFUN_DLD(netcdf_setDefaultFormat, args,, +"") +{ + if (args.length() != 0) { + print_usage (); + return octave_value(); + } + int format = netcdf_constants(args(0)); + int old_format; + + check_err(nc_set_default_format(format, &old_format)); + + return octave_value(old_format); +} + + +DEFUN_DLD(netcdf_create, args,, +"") +{ + + if (args.length() != 2) { + print_usage (); + return octave_value(); + } + + std::string filename = args(0).string_value(); + int mode = netcdf_constants(args(1)); + int ncid; + + check_err(nc_create(filename.c_str(), mode, &ncid)); + + return octave_value(ncid); +} + +DEFUN_DLD(netcdf_open, args,, +"") +{ + + if (args.length() != 2) { + print_usage (); + return octave_value(); + } + + std::string filename = args(0).string_value(); + int mode = netcdf_constants(args(1)); + int ncid; + + check_err(nc_open(filename.c_str(), mode, &ncid)); + + return octave_value(ncid); +} + + + + +//int nc_inq (int ncid, int *ndimsp, int *nvarsp, int *ngattsp, +// int *unlimdimidp); +DEFUN_DLD(netcdf_inq, args,, +"") +{ + if (args.length() != 1) { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + int ndims, nvars, ngatts, unlimdimid; + octave_value_list retval; + + check_err(nc_inq(ncid,&ndims,&nvars,&ngatts,&unlimdimid)); + + retval(0) = octave_value(ndims); + retval(1) = octave_value(nvars); + retval(2) = octave_value(ngatts); + retval(3) = octave_value(unlimdimid); + return retval; +} + + +// int nc_inq_format (int ncid, int *formatp); +DEFUN_DLD(netcdf_inqFormat, args,, +"") +{ + + if (args.length() != 1) { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + int format; + check_err(nc_inq_format(ncid, &format)); + + if (format == NC_FORMAT_CLASSIC) { + return octave_value("FORMAT_CLASSIC"); + } + if (format == NC_FORMAT_64BIT) { + return octave_value("FORMAT_64BIT"); + } + if (format == NC_FORMAT_NETCDF4) { + return octave_value("FORMAT_NETCDF4"); + } + + return octave_value("FORMAT_NETCDF4_CLASSIC"); +} + +// int nc_def_dim (int ncid, const char *name, size_t len, int *dimidp); + +DEFUN_DLD(netcdf_defDim, args,, +"") +{ + + if (args.length() != 3) { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + std::string name = args(1).string_value(); + size_t len = args(2).scalar_value(); + int dimid; + + check_err(nc_def_dim (ncid, name.c_str(), len, &dimid)); + + return octave_value(dimid); +} + +// int nc_def_var (int ncid, const char *name, nc_type xtype, +// int ndims, const int dimids[], int *varidp); + +DEFUN_DLD(netcdf_defVar, args,, +"") +{ + + if (args.length() != 4) { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + std::string name = args(1).string_value (); + std::string xtype = args(2).string_value (); + + Array<double> tmp = args(3).vector_value (); + OCTAVE_LOCAL_BUFFER (int, dimids, tmp.numel()); + + for (int i = 0; i < tmp.numel(); i++) + { + dimids[i] = tmp(tmp.numel()-i-1); + } + + int varid; + + //cout << "nc " << _get_type(xtype) << endl; + check_err(nc_def_var (ncid, name.c_str(), _get_type(xtype), tmp.numel(), dimids, &varid)); + + return octave_value(varid); +} + + +DEFUN_DLD(netcdf_endDef, args,, +"") +{ + if (args.length() != 1) + { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + check_err(nc_enddef (ncid)); + + return octave_value(); +} + +DEFUN_DLD(netcdf_reDef, args,, +"") +{ + if (args.length() != 1) + { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + check_err(nc_redef (ncid)); + + return octave_value(); +} + +// http://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c/nc_005fput_005fvar_005f-type.html#nc_005fput_005fvar_005f-type + +DEFUN_DLD(netcdf_putVar, args,, +"") +{ + if (args.length() < 3 || args.length() > 6) + { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + int varid = args(1).scalar_value (); + octave_value data = args(args.length()-1); + size_t start[NC_MAX_VAR_DIMS]; + size_t count[NC_MAX_VAR_DIMS]; + ptrdiff_t stride[NC_MAX_VAR_DIMS]; + nc_type xtype; + + check_err(nc_inq_vartype (ncid, varid, &xtype)); + //int sliced_numel = tmp.numel(); + + start_count_stride(ncid, varid, args, args.length()-1, start, count, stride); + + // check if count matched size(data) + + switch (xtype) + { +#define OV_NETCDF_PUT_VAR(netcdf_type,c_type,method) \ + case netcdf_type: \ + { \ + check_err(nc_put_vars (ncid, varid, start, count, stride, (c_type*)data.method().fortran_vec())); \ + break; \ + } + + OV_NETCDF_PUT_VAR(NC_BYTE, signed char, int8_array_value) + OV_NETCDF_PUT_VAR(NC_UBYTE, unsigned char, uint8_array_value) + OV_NETCDF_PUT_VAR(NC_SHORT, short, int16_array_value) + OV_NETCDF_PUT_VAR(NC_USHORT, unsigned short, uint16_array_value) + OV_NETCDF_PUT_VAR(NC_INT, int, int32_array_value) + OV_NETCDF_PUT_VAR(NC_UINT, unsigned int, uint32_array_value) + OV_NETCDF_PUT_VAR(NC_INT64, long long, int64_array_value) + OV_NETCDF_PUT_VAR(NC_UINT64, unsigned long long, uint64_array_value) + + OV_NETCDF_PUT_VAR(NC_FLOAT, float, float_array_value) + OV_NETCDF_PUT_VAR(NC_DOUBLE,double,array_value) + + OV_NETCDF_PUT_VAR(NC_CHAR, char, char_array_value) + default: + { + error("unknown type %d" ,xtype); + } + } + return octave_value(); +} + + + + +// http://www.mathworks.com/help/techdoc/ref/netcdf.getvar.html + +DEFUN_DLD(netcdf_getVar, args,, +"data = netcdf_getVar(ncid,varid,start,count,stride) \n\ +start: 0-based indexes \n\ +") +{ + if (args.length() < 2 || args.length() > 5) + { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + int varid = args(1).scalar_value (); + std::list<Range> ranges; + int ndims; + size_t start[NC_MAX_VAR_DIMS]; + size_t count[NC_MAX_VAR_DIMS]; + ptrdiff_t stride[NC_MAX_VAR_DIMS]; + + octave_value data; + nc_type xtype; + + check_err(nc_inq_vartype (ncid, varid, &xtype)); + check_err(nc_inq_varndims (ncid, varid, &ndims)); + int sz = 1; + + dim_vector sliced_dim_vector; + + if (ndims < 2) + { + sliced_dim_vector.resize(2); + sliced_dim_vector(0) = 1; + sliced_dim_vector(1) = 1; + } + else + { + sliced_dim_vector.resize(ndims); + } + + start_count_stride(ncid, varid, args, args.length(), start, count, stride); + + if (error_state) + { + print_usage (); + return octave_value(); + } + + + // total size sz + for (int i=0; i<ndims; i++) { + sz = sz * count[i]; + sliced_dim_vector(i) = count[ndims-i-1]; + //sliced_dim_vector(i) = count[i]; + } + + + //cout << "start " << start[0] << endl; + // need to take count and stride + + + + //cout << "sz " << sz << endl; + //cout << "sliced_dim_vector " << sliced_dim_vector(0) << " x " << sliced_dim_vector(1) << endl; + + switch (xtype) + { +#define OV_NETCDF_GET_VAR_CASE(netcdf_type,c_type) \ + case netcdf_type: \ + { \ + Array < c_type > arr = Array < c_type >(sliced_dim_vector); \ + check_err(nc_get_vars(ncid, varid, start, count, stride, arr.fortran_vec())); \ + data = octave_value(arr); \ + break; \ + } + + OV_NETCDF_GET_VAR_CASE(NC_BYTE,octave_int8) + OV_NETCDF_GET_VAR_CASE(NC_UBYTE,octave_uint8) + OV_NETCDF_GET_VAR_CASE(NC_SHORT,octave_int16) + OV_NETCDF_GET_VAR_CASE(NC_USHORT,octave_uint16) + OV_NETCDF_GET_VAR_CASE(NC_INT,octave_int32) + OV_NETCDF_GET_VAR_CASE(NC_UINT,octave_uint32) + OV_NETCDF_GET_VAR_CASE(NC_INT64,octave_int64) + OV_NETCDF_GET_VAR_CASE(NC_UINT64,octave_uint64) + + OV_NETCDF_GET_VAR_CASE(NC_FLOAT,float) + OV_NETCDF_GET_VAR_CASE(NC_DOUBLE,double) + + OV_NETCDF_GET_VAR_CASE(NC_CHAR, char) + + default: + { + error("unknown type %d" ,xtype); + } + + } + + /* + if (xtype == NC_DOUBLE) + { + Array < double > arr = Array < double >(sliced_dim_vector); + check_err(nc_get_vars_double(ncid, varid, start, count, stride, arr.fortran_vec())); + data = octave_value(arr); + } + else if (xtype == NC_FLOAT) + { + Array < float > arr = Array < float >(sliced_dim_vector); + check_err(nc_get_vars_float(ncid, varid, start, count, stride, arr.fortran_vec())); + data = octave_value(arr); + } + */ + return data; +} + +DEFUN_DLD(netcdf_close, args,, +"") +{ + + if (args.length() != 1) { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + check_err(nc_close(ncid)); + return octave_value(ncid); +} + +// int nc_inq_attname(int ncid, int varid, int attnum, char *name); + +DEFUN_DLD(netcdf_inqAttName, args,, +"") +{ + if (args.length() != 3) { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + int varid = args(1).scalar_value(); + int attnum = args(2).scalar_value(); + char name[NC_MAX_NAME+1]; + + check_err(nc_inq_attname(ncid, varid, attnum, name)); + + return octave_value(std::string(name)); +} + + +DEFUN_DLD(netcdf_getAtt, args,, +"") +{ + if (args.length() != 3) { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + int varid = args(1).scalar_value(); + std::string attname = args(2).string_value(); + nc_type xtype; + size_t len; + octave_value data; + + check_err(nc_inq_att(ncid, varid, attname.c_str(), &xtype, &len)); + +#define OV_NETCDF_GET_ATT_CASE(netcdf_type,c_type) \ + if (xtype == netcdf_type) \ + { \ + Array< c_type > arr = Array< c_type >(dim_vector(1,len)); \ + check_err(nc_get_att(ncid, varid, attname.c_str(), arr.fortran_vec())); \ + data = octave_value(arr); \ + } + OV_NETCDF_GET_ATT_CASE(NC_BYTE,octave_int8) + OV_NETCDF_GET_ATT_CASE(NC_UBYTE,octave_uint8) + OV_NETCDF_GET_ATT_CASE(NC_SHORT,octave_int16) + OV_NETCDF_GET_ATT_CASE(NC_USHORT,octave_uint16) + OV_NETCDF_GET_ATT_CASE(NC_INT,octave_int32) + OV_NETCDF_GET_ATT_CASE(NC_UINT,octave_uint32) + OV_NETCDF_GET_ATT_CASE(NC_INT64,octave_int64) + OV_NETCDF_GET_ATT_CASE(NC_UINT64,octave_uint64) + + OV_NETCDF_GET_ATT_CASE(NC_FLOAT,float) + OV_NETCDF_GET_ATT_CASE(NC_DOUBLE,double) + + OV_NETCDF_GET_ATT_CASE(NC_CHAR, char) + + + return data; +} + + +DEFUN_DLD(netcdf_putAtt, args,, +"") +{ + if (args.length() != 4) { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + int varid = args(1).scalar_value(); + std::string attname = args(2).string_value(); + octave_value data = args(3); + + nc_type xtype; + xtype = NC_BYTE; + + // get matching netcdf type + + if (data.is_string()) + xtype = NC_CHAR; + else if (data.is_int8_type()) + xtype = NC_BYTE; + else if (data.is_uint8_type()) + xtype = NC_UBYTE; + else if (data.is_int16_type()) + xtype = NC_SHORT; + else if (data.is_uint16_type()) + xtype = NC_USHORT; + else if (data.is_int32_type()) + xtype = NC_INT; + else if (data.is_uint32_type()) + xtype = NC_UINT; + else if (data.is_int64_type()) + xtype = NC_INT64; + else if (data.is_uint64_type()) + xtype = NC_UINT64; + else if (data.is_single_type()) + xtype = NC_FLOAT; + else + xtype = NC_DOUBLE; + + //cout << "xtype " << xtype << endl; + size_t len = data.numel(); + + switch (xtype) + { +#define OV_NETCDF_PUT_ATT(netcdf_type,c_type,method) \ + case netcdf_type: \ + { \ + check_err(nc_put_att (ncid, varid, attname.c_str(), xtype, len, data.method().fortran_vec())); \ + break; \ + } + + OV_NETCDF_PUT_ATT(NC_BYTE, signed char, int8_array_value) + OV_NETCDF_PUT_ATT(NC_UBYTE, unsigned char, uint8_array_value) + OV_NETCDF_PUT_ATT(NC_SHORT, short, int16_array_value) + OV_NETCDF_PUT_ATT(NC_USHORT, unsigned short, uint16_array_value) + OV_NETCDF_PUT_ATT(NC_INT, int, int32_array_value) + OV_NETCDF_PUT_ATT(NC_UINT, unsigned int, uint32_array_value) + OV_NETCDF_PUT_ATT(NC_INT64, long long, int64_array_value) + OV_NETCDF_PUT_ATT(NC_UINT64, unsigned long long, uint64_array_value) + + OV_NETCDF_PUT_ATT(NC_FLOAT, float, float_array_value) + OV_NETCDF_PUT_ATT(NC_DOUBLE,double,array_value) + + OV_NETCDF_PUT_ATT(NC_CHAR, char, char_array_value) + } + + /* check_err(nc_put_att (int ncid, int varid, const char *name, nc_type xtype, + size_t len, const void *op));*/ + + return octave_value(); + +} + + +DEFUN_DLD(netcdf_inqVarID, args,, +"") +{ + + if (args.length() != 2) { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + std::string varname = args(1).string_value(); + int varid; + + check_err(nc_inq_varid(ncid,varname.c_str(), &varid)); + + return octave_value(varid); +} + + + +DEFUN_DLD(netcdf_inqVar, args,, +"") +{ + + if (args.length() != 2) { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + int varid = args(1).scalar_value(); + char name[NC_MAX_NAME+1]; + int ndims, dimids[NC_MAX_VAR_DIMS], natts; + nc_type xtype; + octave_value_list retval; + + if (! error_state) { + + check_err(nc_inq_varndims(ncid, varid, &ndims)); + + check_err(nc_inq_var(ncid, varid, name, &xtype, + &ndims, dimids, &natts)); + + retval(0) = octave_value(std::string(name)); + retval(1) = octave_value(xtype); + + // copy output arguments + Array<double> dimids_ = Array<double>(dim_vector(1,ndims)); + for (int i = 0; i < ndims; i++) { + dimids_(i) = dimids[ndims-i-1]; + } + + retval(2) = octave_value(dimids_); + retval(3) = octave_value(natts); + } + + return retval; +} + + + +DEFUN_DLD(netcdf_inqDim, args,, +"") +{ + + if (args.length() != 2) { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + int dimid = args(1).scalar_value(); + octave_value_list retval; + + if (! error_state) { + char name[NC_MAX_NAME+1]; + size_t length; + check_err(nc_inq_dim(ncid, dimid, name, &length)); + + retval(0) = octave_value(std::string(name)); + retval(1) = octave_value(length); + } + + return retval; +} + + +DEFUN_DLD(netcdf_inqDimID, args,, +"") +{ + + if (args.length() != 2) { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + std::string dimname = args(1).string_value(); + int id; + octave_value_list retval; + + if (! error_state) { + check_err(nc_inq_dimid(ncid, dimname.c_str(), &id)); + + retval(0) = octave_value(id); + } + + return retval; +} + + +DEFUN_DLD(netcdf_inqNVars, args,, +"") +{ + + if (args.length() != 1) { + print_usage (); + return octave_value(); + } + + int ncid = args(0).scalar_value(); + octave_value_list retval; + + if (! error_state) { + char name[NC_MAX_NAME+1]; + size_t length; + int nvars; + + check_err(nc_inq_nvars(ncid, &nvars)); + retval(0) = octave_value(nvars); + } + + return retval; +} + + + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/netcdf/src/test_netcdf_package.m Sat Jun 15 12:52:22 2013 +0000 @@ -0,0 +1,127 @@ +import_netcdf + +disp(netcdf.inqLibVers()); + +ncid = netcdf.create('foo.nc','NC_CLOBBER'); +assert(strcmp(netcdf.inqFormat(ncid),'FORMAT_CLASSIC')); + +assert(netcdf.getConstant('NC_NOWRITE') == 0) +assert(netcdf.getConstant('NC_WRITE') == 1) + +netcdf.getConstantNames(); + +n = 10; +m = 5; + +dimid_lon = netcdf.defDim(ncid,'lon',m) +dimid = netcdf.defDim(ncid,'time',n) + +varidd = netcdf.defVar(ncid,'double_var','double',[dimid_lon,dimid]); + +varid = netcdf.defVar(ncid,'byte_var','byte',[dimid]); + +varidf = netcdf.defVar(ncid,'float_var','float',[dimid]); + +varidi = netcdf.defVar(ncid,'int_var','int',[dimid]); + +varids = netcdf.defVar(ncid,'short_var','short',[dimid]); + +assert(varidd == netcdf.inqVarID(ncid,'double_var')) + + +assert(netcdf.inqNVars(ncid) == 5) + +[varname,xtype,dimids,natts] = netcdf.inqVar(ncid,varidd); +assert(strcmp(varname,'double_var')); + +[dimname,len] = netcdf.inqDim(ncid,dimid); +assert(len == n); +assert(strcmp(dimname,'time')); + + +types = {'double','float','byte','short','int'}; + + +for i=1:length(types) + vid{i} = netcdf.defVar(ncid,[types{i} '_variable'],types{i},[dimid_lon,dimid]); +end + +netcdf.putAtt(ncid,varidd,'toto',int8(123)); +netcdf.putAtt(ncid,varidd,'toto32',int32(123)); +%netcdf.putAtt(ncid,varidd,'toto64',int64(123)); % does not work in octave and matlab +netcdf.putAtt(ncid,varidd,'add_offset',single(123123.123)); +netcdf.putAtt(ncid,varidd,'_FillValue',double(123123.123)); +netcdf.putAtt(ncid,varidd,'name','this is a name'); + +assert(isequal(netcdf.getAtt(ncid,varidd,'add_offset'),single(123123.123))); + +netcdf.endDef(ncid) + + +z = randn(m,n); +netcdf.putVar(ncid,varidd,z); + +varf = randn(n,1); +netcdf.putVar(ncid,varidf,varf); + +vari = floor(randn(n,1)); +netcdf.putVar(ncid,varidi,vari); + +netcdf.putVar(ncid,varids,[1:n]) + +z2 = netcdf.getVar(ncid,varidd); +assert(all(all(abs(z2 - z) < 1e-5))) + +z2 = netcdf.getVar(ncid,varidd,[0 0]); +assert(z2 == z(1,1)) + +z2 = netcdf.getVar(ncid,varidd,[2 2],[3 5]); +assert(isequal(z2,z(3:5,3:7))) + +z2 = netcdf.getVar(ncid,varidd,[2 2],[3 4],[1 2]); +assert(isequal(z2,z(3:5,3:2:9))) + + +netcdf.putVar(ncid,varidd,[0 0],123.); +z(1,1) = 123; +z2 = netcdf.getVar(ncid,varidd); +assert(isequal(z,z2)) + +netcdf.putVar(ncid,varidd,[2 2],[3 3],ones(3,3)); +z(3:5,3:5) = 1; +z2 = netcdf.getVar(ncid,varidd); +assert(isequal(z,z2)) + + +netcdf.putVar(ncid,varidd,[0 0],[3 5],[2 2],zeros(3,5)); +z(1:2:5,1:2:9) = 0; +z2 = netcdf.getVar(ncid,varidd); +assert(isequal(z,z2)) + + +z2 = netcdf.getVar(ncid,varidf); +assert(all(z2 - varf < 1e-5)) + + +vari2 = netcdf.getVar(ncid,varidi); +assert(all(vari2 == vari)) + + + +test_netcdf_package_type('byte','int8'); +test_netcdf_package_type('ubyte','uint8'); +test_netcdf_package_type('short','int16'); +test_netcdf_package_type('ushort','uint16'); +test_netcdf_package_type('int','int32'); +test_netcdf_package_type('uint','uint32'); +test_netcdf_package_type('int64','int64'); +test_netcdf_package_type('uint64','uint64'); + +test_netcdf_package_type('double','double'); +test_netcdf_package_type('float','single'); + +test_netcdf_package_type('char','char'); + +%test_netcdf_hl + +netcdf.close(ncid);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/netcdf/src/test_netcdf_package_type.m Sat Jun 15 12:52:22 2013 +0000 @@ -0,0 +1,42 @@ +function test_netcdf_package_type(nctype,octtype); + +import_netcdf + +fprintf('\n%s\n',nctype) + +m = 5; +n = 10; + +fname = sprintf('foo-%s.nc',nctype); +delete(fname); +mode = bitor(netcdf.getConstant('NC_CLOBBER'),netcdf.getConstant('NC_NETCDF4')); +ncid = netcdf.create(fname,mode); + +dimid_lon = netcdf.defDim(ncid,'lon',m); +dimid = netcdf.defDim(ncid,'time',n); + + +varid = netcdf.defVar(ncid,'variable',nctype,[dimid_lon,dimid]); +netcdf.endDef(ncid) + +if strcmp(octtype,'char') + z = char(floor(26*rand(m,n)) + 65); + + netcdf.putVar(ncid,varid,z); + z2 = netcdf.getVar(ncid,varid); + + assert(isequal(z,z2)) +else + + z = zeros(m,n,octtype); + z(:) = randn(m,n); + + netcdf.putVar(ncid,varid,z); + z2 = netcdf.getVar(ncid,varid); + + assert(all(all(abs(z2 - z) < 1e-5))) +end + + + +netcdf.close(ncid);