changeset 12682:e97980ace11d octave-forge

add example of using ncArray
author abarth93
date Mon, 24 Aug 2015 08:51:53 +0000
parents 0c936bf960f6
children d36d7d7c4b68
files extra/ncArray/inst/ncarray_example.m extra/ncArray/inst/ncarray_example_file.m extra/ncArray/inst/private/ncarray_example_file.m
diffstat 3 files changed, 98 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extra/ncArray/inst/ncarray_example.m	Mon Aug 24 08:51:53 2015 +0000
@@ -0,0 +1,52 @@
+
+% Tutorial for using ncArray
+
+% It is advised to run this script in an empty directory.
+% It will delete and overwrite files named file1.nc, file2.nc and file3.nc.
+
+% size of the example data (2x3)
+
+n = 3;
+m = 2;
+
+% create 3 files (file1.nc, file2.nc,...) with a 2x3 variable called SST
+data = zeros(n,m);
+
+disp('create example files: file1.nc, file2.nc, file3.nc')
+
+for i = 1:3  
+    data(:) = i;
+    files{i} = sprintf('file%d.nc',i);
+    delete(files{i});
+    ncarray_example_file(files{i},data);
+end
+
+
+% Using ncArray
+
+SST = ncArray('file1.nc','SST');
+
+disp('load the entire file')
+data = SST(:,:,:);
+
+disp('get the attribute units')
+units = SST.units;
+
+
+disp('load a particular value');
+data = SST(3,2,1);
+
+% Using ncCatArray
+
+disp('concatenate the files over the 3rd dimension (here time)')
+
+SST = ncCatArray(3,{'file1.nc','file2.nc','file3.nc'},'SST');
+
+% or just 
+% SST = ncCatArray(3,'file*.nc','SST');
+
+disp('load all 3 files');
+data = SST(:,:,:);
+
+disp('load a particular value (1,2,1) of the 3rd file');
+data = SST(1,2,3);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extra/ncArray/inst/ncarray_example_file.m	Mon Aug 24 08:51:53 2015 +0000
@@ -0,0 +1,46 @@
+% create an example NetCDF file with the name filename and given data
+
+function ncarray_example_file(filename,data)
+
+  dtype = 'double';
+  sz = size(data);
+
+  % Variables
+  nccreate(filename,'lon','Format','classic','Datatype',dtype,...
+           'Dimensions',{'x',sz(1), 'y',sz(2)});
+  ncwriteatt(filename,'lon','long_name','Longitude')
+  ncwriteatt(filename,'lon','units','degrees_east')
+  
+  nccreate(filename,'lat','Datatype',dtype,'Dimensions',{'x',sz(1), 'y',sz(2)});
+  ncwriteatt(filename,'lat','long_name','Latitude')
+  ncwriteatt(filename,'lat','units','degrees_north')
+  
+  nccreate(filename,'time','Datatype',dtype,'Dimensions',{'time',1});
+  ncwriteatt(filename,'time','long_name','Time')
+  ncwriteatt(filename,'time','units','days since 1858-11-17 00:00:00 GMT')
+  
+  nccreate(filename,'SST','Datatype',dtype,'Dimensions',...
+           {'x',sz(1), 'y',sz(2), 'time',1});
+  ncwriteatt(filename,'SST','missing_value',single(9999))
+  ncwriteatt(filename,'SST','_FillValue',single(9999))
+  ncwriteatt(filename,'SST','units','degC')
+  ncwriteatt(filename,'SST','long_name','Sea Surface Temperature')
+  ncwriteatt(filename,'SST','coordinates','lat lon')
+
+  ncwrite(filename,'SST',data);  
+
+  % Copyright (C) 2012,2013,2015 Alexander Barth <barth.alexander@gmail.com>
+  %
+  % 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/>.
+
--- a/extra/ncArray/inst/private/ncarray_example_file.m	Mon Aug 24 08:49:34 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,85 +0,0 @@
-function ncarray_example_file(filename,data)
-
-if ~isempty(which('nccreate')) && ~isempty(which('ncwriteatt')) && ...
-      ~isempty(which('ncwrite'))
-  % use matlab netcdf high level interface
-
-  %dtype = 'single';
-  dtype = 'double';
-
-  % Variables
-  nccreate(filename,'lon','Format','classic','Datatype',dtype,...
-           'Dimensions',{'x',220, 'y',144});
-  ncwriteatt(filename,'lon','long_name','Longitude')
-  ncwriteatt(filename,'lon','units','degrees_east')
-  
-  nccreate(filename,'lat','Datatype',dtype,'Dimensions',{'x',220, 'y',144});
-  ncwriteatt(filename,'lat','long_name','Latitude')
-  ncwriteatt(filename,'lat','units','degrees_north')
-  
-  nccreate(filename,'time','Datatype',dtype,'Dimensions',{'time',1});
-  ncwriteatt(filename,'time','long_name','Time')
-  ncwriteatt(filename,'time','units','days since 1858-11-17 00:00:00 GMT')
-  
-  nccreate(filename,'SST','Datatype',dtype,'Dimensions',...
-           {'x',220, 'y',144, 'time',1});
-  ncwriteatt(filename,'SST','missing_value',single(9999))
-  ncwriteatt(filename,'SST','_FillValue',single(9999))
-  ncwriteatt(filename,'SST','units','degC')
-  ncwriteatt(filename,'SST','long_name','Sea Surface Temperature')
-  ncwriteatt(filename,'SST','coordinates','lat lon')
-
-  ncwrite(filename,'SST',data);  
-else
-  % use octcdf interface
-
-  nc = netcdf(filename,'c');
-
-  % dimensions
-  
-  nc('x') = size(data,1);
-  nc('y') = size(data,2);
-  nc('time') = size(data,3);
-
-  % variables
-
-  nc{'lon'} = ncdouble('y','x');  % 31680 elements 
-  nc{'lon'}.long_name = ncchar('Longitude');
-  nc{'lon'}.units = ncchar('degrees_east');
-  
-  nc{'lat'} = ncdouble('y','x');  % 31680 elements 
-  nc{'lat'}.long_name = ncchar('Latitude');
-  nc{'lat'}.units = ncchar('degrees_north');
-
-  nc{'time'} = ncdouble('time');  % 1 elements 
-  nc{'time'}.long_name = ncchar('Time');
-  nc{'time'}.units = ncchar('days since 1858-11-17 00:00:00 GMT');
-  
-  nc{'SST'} = ncdouble('time','y','x');  % 31680 elements 
-  nc{'SST'}.missing_value = ncdouble(9999);
-  nc{'SST'}.FillValue_ = ncdouble(9999);
-  nc{'SST'}.units = ncchar('degC');
-  nc{'SST'}.long_name = ncchar('Sea Surface Temperature');
-  nc{'SST'}.coordinates = ncchar('lat lon');
-
-  % global attributes
-  
-  nc{'SST'}(:) = permute(data,[3 2 1]);
-  close(nc)
-end
-
-% Copyright (C) 2012,2013 Alexander Barth <barth.alexander@gmail.com>
-%
-% 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/>.
-