Changeset 648 for trunk/src/nc2struct.m


Ignore:
Timestamp:
Jun 9, 2013, 10:31:58 PM (7 years ago)
Author:
sommeria
Message:

get_field updated, several bugs corrected,open_uvmat suppressd

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/nc2struct.m

    r534 r648  
    2323%  nc:  name of a netcdf file (char string) or netcdf object   
    2424%  additional arguments:
    25 %       -no additional arguments: all the variables of the netcdf fiel are read.
     25%       -no additional arguments: all the variables of the netcdf file are read.
    2626%       -a cell array, ListVarName, made of  char strings {'VarName1', 'VarName2',...} )
    27 %         if ListVarName=[] or {}, no variables is read (only global attributes)
     27%         if ListVarName=[] or {}, no variable value is read (only global attributes and list of variables and dimensions)
    2828%         if ListVarName is absent, or = '*', ALL the variables of the netcdf file are read.
    2929%         if ListVarName is a cell array with n lines, the set of variables will be sought by order of priority
    3030%                  in the list, while output names will be set by the first line
    3131%        - the string 'ListGlobalAttribute' followed by a list of attribute  names: reads only these attributes (fast reading)
    32 %
     32%        - the string 'TimeVarName', a string (the variable considered as
     33%        time), an integer (the selected time index), the cell of other
     34%        input variables limited to the input time index (considered as the last index of arrays)
     35%        - the string 'TimeDimName', a string (the dimension considered as time), an integer (the selected time index), the cell of other
     36%        input variables limited to the input time index (considered as the last index of arrays)
     37
    3338%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    3439%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
     
    9196       return
    9297    end
     98   
     99    %% time variable or dimension
     100    input_index=1;
     101    if isequal(varargin{1},'TimeVarName')
     102        TimeVarName=varargin{2};
     103        TimeVarIndex=varargin{3};
     104        input_index=4;
     105    elseif isequal(varargin{1},'TimeDimName')
     106        TimeDimName=varargin{2};
     107        TimeVarIndex=varargin{3};
     108        input_index=4;
     109    end
    93110
    94111    %% full reading: get the nbre of dimensions, variables, global attributes
    95     ListVarName=varargin{1};
     112    ListVarName=varargin{input_index};
    96113    [ndims,nvars,ngatts]=netcdf.inq(nc);%nbre of dimensions, variables, global attributes, in the netcdf file
    97114   
     
    130147    if ~isempty(ListDimNameNetcdf)
    131148        flag_used=zeros(1,ndims);%initialize the flag indicating the selected dimensions in the list (0=unused)
     149    end
     150    if isequal(varargin{1},'TimeDimName')% time dimension introduced
     151        TimeDimIndex=find(strcmp(varargin{2},ListDimNameNetcdf));
     152        if isempty(TimeDimIndex)
     153            Data.Txt=['requested time dimension ' varargin{2} ' not found'];
     154            return
     155        end
     156        if dim_value(TimeDimIndex)<varargin{3}
     157            Data.Txt=['requested time index ' num2str(varargin{3}) ' exceeds matrix dimension'];
     158            return
     159        end
    132160    end
    133161 
     
    180208     
    181209  %% get the dimensions and attributes associated to  variables
     210  var_dim=cell(size(var_index));% initiate list of dimensions for variables
    182211    for ivar=1:length(var_index)
    183212        var_dim{ivar}=dimids{var_index(ivar)}+1; %netcdf dimension indices used by the variable #ivar
     
    208237        end
    209238    end
     239   
    210240
    211241    %% select the dimensions used for the set of input variables
     
    221251            VarName=Data.ListVarName{ivar};
    222252            VarName=regexprep(VarName,'-',''); %suppress '-' if it exists in the netcdf variable name
    223             eval(['Data.' VarName '=double(netcdf.getVar(nc,var_index(ivar)-1));'])%read the variable data
     253            CheckSub=0;
     254            if input_index==4% if a dimension is selected as time
     255                if var_dim{ivar}(end)==TimeDimIndex% if the last dim of the variable is the time
     256                    slice_length=prod(var_dim{ivar}(1:end-1));
     257                    Data.(VarName)=double(netcdf.getVar(nc,var_index(ivar)-1),TimeIndex*slice_length,slice_length); %read the variable data
     258                    CheckSub=1;
     259                end
     260            end
     261            if ~CheckSub
     262                Data.(VarName)=double(netcdf.getVar(nc,var_index(ivar)-1)); %read the whole variable data
     263            end
    224264        end
    225265    end
Note: See TracChangeset for help on using the changeset viewer.