Home > utilities > read_netcdf_vars_xy.m

read_netcdf_vars_xy

PURPOSE ^

SYNOPSIS ^

function M = read_netcdf_vars_xy(varargin)

DESCRIPTION ^

 A function to extract netcdf variables for a number of (x, y) locations
 read_netcdf_vars.m is called repeatidly

 help read_netcdf_vars for detailed usage info

 INPUT
   Pass the variable names that you want to extract
   [optional pair] filename, the netCDF filename
   [optional triple] dimrange, the dimension name, the dimension range

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function M = read_netcdf_vars_xy(varargin)
0002 %
0003 % A function to extract netcdf variables for a number of (x, y) locations
0004 % read_netcdf_vars.m is called repeatidly
0005 %
0006 % help read_netcdf_vars for detailed usage info
0007 %
0008 % INPUT
0009 %   Pass the variable names that you want to extract
0010 %   [optional pair] filename, the netCDF filename
0011 %   [optional triple] dimrange, the dimension name, the dimension range
0012 
0013 % Author(s)
0014 %   Rory O'Hara Murray, Marine Scotland Science
0015 %
0016 % Revision history
0017 %   v0 May 2014
0018 %==========================================================================
0019 
0020 % defaults
0021 nodeflag = false;
0022 cellflag = false;
0023 
0024 % look for some keywords with some setting after them and remember which
0025 % index of varargin are 'taken' in freeI.
0026 freeI = true(1,size(varargin,2));
0027 subsample_num = 0;
0028 for ii=1:1:length(varargin)
0029     keyword  = lower(varargin{ii});
0030     vartype = class(keyword);
0031     if vartype(1)~='c' | length(keyword)<10, continue; end
0032     switch(keyword(1:10))
0033         case 'nodevalues'
0034             nodeflag = true;
0035             nodevalues = varargin{ii+1};
0036             freeI([ii ii+1]) = false;
0037         case 'cellvalues'
0038             cellflag = true;
0039             cellvalues = varargin{ii+1};
0040             freeI([ii ii+1]) = false;
0041     end
0042 end
0043 
0044 % error checking
0045 if cellflag & nodeflag
0046     if length(cellvalues) ~= length(nodevalues)
0047         error('The number of elements and nodes are not the same. This functions only works if they are the same :-(');
0048         return
0049     end
0050 end
0051 
0052 % Call read_netcdf_vars.m with all the same arguments, appart from those
0053 % used here to define xy_nodes and cells
0054 
0055 if nodeflag && cellflag
0056     for count=1:length(nodevalues);
0057         
0058         nodes=nodevalues(count);
0059         cells=cellvalues(count);
0060         M1 = read_netcdf_vars(varargin{freeI}, 'dimrange', 'node', nodes+[0 1], 'dimrange', 'nele', cells+[0 1]);
0061         
0062         if count==1
0063             M = M1;
0064         elseif count>0
0065             % save all the variables
0066             varnames = fieldnames(M1);
0067             for ii=4:2:length(varnames)
0068                 dimids = M1.(varnames{ii-1}).dimids;
0069                 if size(dimids,1)>0 && dimids(1)<=1;
0070                     M.(varnames{ii})(count,:,:) = M1.(varnames{ii});
0071                 end
0072             end
0073         end
0074         
0075     end
0076 elseif nodeflag
0077     for count=1:length(nodevalues);
0078         
0079         nodes=nodevalues(count);
0080         M1 = read_netcdf_vars(varargin{freeI}, 'dimrange', 'node', nodes+[0 1]);
0081         
0082         if count==1
0083             M = M1;
0084         elseif count>0
0085             % save all the variables
0086             varnames = fieldnames(M1);
0087             for ii=4:2:length(varnames)
0088                 dimids = M1.(varnames{ii-1}).dimids;
0089                 if dimids(1)==1;
0090                     M.(varnames{ii})(count,:,:) = M1.(varnames{ii});
0091                 end
0092             end
0093         end
0094         
0095     end
0096 elseif cellflag
0097     for count=1:length(cellvalues);
0098         
0099         cells=cellvalues(count);
0100         M1 = read_netcdf_vars(varargin{freeI}, 'dimrange', 'nele', cells+[0 1]);
0101         
0102         if count==1
0103             M = M1;
0104         elseif count>0
0105             % save all the variables
0106             varnames = fieldnames(M1);
0107             for ii=4:2:length(varnames)
0108                 dimids = M1.(varnames{ii-1}).dimids;
0109                 if dimids(1)==0;
0110                     M.(varnames{ii})(count,:,:) = M1.(varnames{ii});
0111                 end
0112             end
0113         end
0114         
0115     end
0116 end

Generated on Wed 20-Feb-2019 16:06:01 by m2html © 2005