Changeset 816


Ignore:
Timestamp:
Aug 13, 2014, 10:33:12 PM (10 years ago)
Author:
g7moreau
Message:
  • Cosmetic change
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/nc2struct.m

    r809 r816  
    1 %'nc2struct': transform a netcdf file in a corresponding matlab structure
     1%'nc2struct': transform a NetCDF file in a corresponding matlab structure
    22% it reads all the global attributes and all variables, or a selected list.
    33% The corresponding dimensions and variable attributes are then extracted
     
    66%
    77% OUTPUT:
    8 %  Data: structure containing all the information of the netcdf file (or netcdf object)
     8%  Data: structure containing all the information of the NetCDF file (or NetCDF object)
    99%           with (optional)fields:
    1010%                    .ListGlobalAttribute: cell listing the names of the global attributes
    1111%                    .Att_1,Att_2... : values of the global attributes
    12 %                    .ListVarName: list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} ) 
    13 %                    .VarDimName: list of dimension names for each element of .ListVarName (cell array of string cells)                         
     12%                    .ListVarName: list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} )
     13%                    .VarDimName: list of dimension names for each element of .ListVarName (cell array of string cells)
    1414%                    .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
    1515%                  .ListDimName=list of dimension (added information, not requested for field description)
    1616%                  .DimValue= vlalues of dimensions (added information, not requested for field description)
    17 %                  .VarType= integers giving the type of variable as coded by netcdf= 2 for char, =4 for single,=( for double
     17%                  .VarType= integers giving the type of variable as coded by netcdf =2 for char, =4 for single,=( for double
    1818%  var_detect: vector with same length as the cell array ListVarName, = 1 for each detected variable and 0 else.
    19 %            var_detect=[] in the absence of input cell array
    20 %  ichoice: index of the selected line in the case of multiple choice
    21 %        (cell array of varible names with multiple lines) , =[] by default
    22 %INPUT:
    23 %  nc:  name of a netcdf file (char string) or netcdf object   
     19%            var_detect=[] in the absence of input cell array
     20%  ichoice: index of the selected line in the case of multiple choice
     21%        (cell array of varible names with multiple lines) , =[] by default
     22%
     23% INPUT:
     24%  nc:  name of a NetCDF file (char string) or NetCDF object
    2425%  additional arguments:
    25 %       -no additional arguments: all the variables of the netcdf file are read.
    26 %       -a cell array, ListVarName, made of  char strings {'VarName1', 'VarName2',...} ) 
     26%       -no additional arguments: all the variables of the NetCDF file are read.
     27%       -a cell array, ListVarName, made of  char strings {'VarName1', 'VarName2',...} )
    2728%         if ListVarName=[] or {}, no variable value is read (only global attributes and list of variables and dimensions)
    28 %         if ListVarName is absent, or = '*', ALL the variables of the netcdf file are read.
     29%         if ListVarName is absent, or = '*', ALL the variables of the NetCDF file are read.
    2930%         if ListVarName is a cell array with n lines, the set of variables will be sought by order of priority in the list,
    3031%            while output names will be set by the first line
    3132%       - the string 'ListGlobalAttribute' followed by a list of attribute  names: reads only these attributes (fast reading)
    32 %       - the string 'TimeVarName', a string (the name of the variable considered as time), an integer or vector with integer values 
     33%       - the string 'TimeVarName', a string (the name of the variable considered as time), an integer or vector with integer values
    3334%            representing time indices to select for each variable, the cell of other input variable names.
    34 %       - the string 'TimeDimName', a string (the name of the dimension considered as time), an integer or vector with integer values 
     35%       - the string 'TimeDimName', a string (the name of the dimension considered as time), an integer or vector with integer values
    3536%            representing time indices to select for each variable, the cell of other input variable names.
    3637
     
    5253%     GNU General Public License (see LICENSE.txt) for more details.
    5354%=======================================================================
    54    
     55
    5556function [Data,var_detect,ichoice,errormsg]=nc2struct(nc,varargin)
    5657errormsg='';%default error message
     
    5859    varargin{1}='*';
    5960end
    60 hhh=which('netcdf.open');% look for built-in matlab netcdf library
     61hhh=which('netcdf.open');% look for built-in matlab NetCDF library
    6162
    6263if ~isequal(hhh,'')
     
    6566    var_detect=[];%default
    6667    ichoice=[];%default
    67    
    68     %% open the netcdf file for reading
     68
     69    %% open the NetCDF file for reading
    6970    if ischar(nc)
    7071        if exist(nc,'file')
     
    8384        testfile=0;
    8485    end
    85    
     86
    8687    %% short reading option for global attributes only, if the first argument is 'ListGlobalAttribute'
    8788    if isequal(varargin{1},'ListGlobalAttribute')
     
    9798        return
    9899    end
    99    
     100
    100101    %% time variable or dimension
    101102    input_index=1;
     
    112113        input_index=4;
    113114    end
    114    
     115
    115116    %% full reading: get the nbre of dimensions, variables, global attributes
    116117    ListVarName=varargin{input_index};
    117     [ndims,nvars,ngatts]=netcdf.inq(nc);%nbre of dimensions, variables, global attributes, in the netcdf file
    118    
     118    [ndims,nvars,ngatts]=netcdf.inq(nc);%nbre of dimensions, variables, global attributes, in the NetCDF file
     119
    119120    %%  -------- read all global attributes (constants)-----------
    120121    Data.ListGlobalAttribute={};%default
     
    142143    end
    143144    Data.ListGlobalAttribute=att_key;
    144    
     145
    145146    %%  -------- read dimension names-----------
    146147    ListDimNameNetcdf=cell(1,ndims);
    147148    dim_value=zeros(1,ndims);
    148     for idim=1:ndims %loop on the dimensions of the netcdf file
     149    for idim=1:ndims %loop on the dimensions of the NetCDF file
    149150        [ListDimNameNetcdf{idim},dim_value(idim)] = netcdf.inqDim(nc,idim-1);%get name and value of each dimension
    150151    end
     
    162163            return
    163164        end
    164     end 
    165    
     165    end
     166
    166167    %%  -------- read names of variables -----------
    167168    ListVarNameNetcdf=cell(1,nvars); %default
    168169    dimids=cell(1,nvars);
    169170    nbatt=zeros(1,nvars);
    170     for ncvar=1:nvars %loop on the variables of the netcdf file
     171    for ncvar=1:nvars %loop on the variables of the NetCDF file
    171172        %get name, type, dimensions and attribute numbers of each variable
    172173        [ListVarNameNetcdf{ncvar},xtype(ncvar),dimids{ncvar},nbatt(ncvar)] = netcdf.inqVar(nc,ncvar-1);
     
    174175%     testmulti=0;
    175176    if isequal(ListVarName,'*')||isempty(ListVarName)
    176         var_index=1:nvars; %all the variables are selected in the netcdf file
     177        var_index=1:nvars; %all the variables are selected in the NetCDF file
    177178        Data.ListVarName=ListVarNameNetcdf;
    178179    else   %select input variables, if requested by the input ListVarName
     
    186187        if size(ListVarName,1)>1 %multiple choice of variable ranked by order of priority
    187188            for iline=1:size(ListVarName,1)
    188                 search_index=find(strcmp(ListVarName{iline,1},ListVarNameNetcdf),1);%look for the first variable name in the list of netcdf variables
     189                search_index=find(strcmp(ListVarName{iline,1},ListVarNameNetcdf),1);%look for the first variable name in the list of NetCDF variables
    189190                if ~isempty(search_index)
    190191                    break % go to the next line
     
    207208        var_index=zeros(1,size(ListVarName,2));%default list of variable indices
    208209        for ivar=1:size(ListVarName,2)
    209             search_index=find(strcmp(ListVarName{iline,ivar},ListVarNameNetcdf),1);%look for the variable name in the list of netcdf file
     210            search_index=find(strcmp(ListVarName{iline,ivar},ListVarNameNetcdf),1);%look for the variable name in the list of NetCDF file
    210211            if ~isempty(search_index)
    211212                var_index(ivar)=search_index;%index of the netcdf list corresponding to the input list index ivar
     
    214215        var_detect=(var_index~=0);%=1 for detected variables
    215216        list_index=find(var_index);% indices in the input list corresponding to a detected variable
    216         var_index=var_index(list_index);% netcdf variable indices corresponding to the output list of read variable
     217        var_index=var_index(list_index);% NetCDF variable indices corresponding to the output list of read variable
    217218        Data.ListVarName=ListVarName(1,list_index);%the first line of ListVarName sets the output names of the variables
    218219    end
    219    
     220
    220221    %% get the dimensions and attributes associated to  variables
    221222    var_dim=cell(size(var_index));% initiate list of dimensions for variables
     
    243244        end
    244245    end
    245    
     246
    246247    %% select the dimensions used for the set of input variables
    247248    if ~isempty(var_index)
     
    253254        end
    254255    end
    255    
     256
    256257    %% get the values of the input variables
    257258    if  ~isempty(ListVarName)
    258259        for ivar=1:length(var_index)
    259260            VarName=Data.ListVarName{ivar};
    260             VarName=regexprep(VarName,'-','_'); %suppress '-' if it exists in the netcdf variable name (leads to errors in matlab)
     261            VarName=regexprep(VarName,'-','_'); %suppress '-' if it exists in the NetCDF variable name (leads to errors in matlab)
    261262%             CheckSub=0;
    262263            if input_index==4% if a dimension is selected as time
     
    272273                    ind_size(index_time)=numel(TimeIndex);%length of the selected set of time indices
    273274                    if numel(TimeIndex)==1 && ~strcmp(VarName,TimeVarName)
    274                         Data.VarDimName{ivar}(index_time)=[];% for a single selected time remove the time in the list of dimensions (except for tTime itself) 
     275                        Data.VarDimName{ivar}(index_time)=[];% for a single selected time remove the time in the list of dimensions (except for tTime itself)
    275276                    end
    276                 end                   
     277                end
    277278                Data.(VarName)=double(netcdf.getVar(nc,var_index(ivar)-1,ind_vec,ind_size)); %read the variable data
    278279                Data.(VarName)=squeeze(Data.(VarName));%remove singeton dimension
     
    286287    end
    287288    Data.VarType=xtype(var_index);
    288    
     289
    289290    %%  -------- close fle-----------
    290291    if testfile==1
    291292        netcdf.close(nc)
    292293    end
    293    
    294     %% old netcdf library
     294
     295    %% old NetCDF library
    295296else
    296297    [Data,var_detect,ichoice]=nc2struct_toolbox(nc,varargin);
Note: See TracChangeset for help on using the changeset viewer.