Home > . > nc2struct_toolbox.m

nc2struct_toolbox

PURPOSE ^

'nc2struct_toolbox': transform a netcdf file in a corresponding matlab structure, USE OLD NETCDF LIBRARY

SYNOPSIS ^

function [Data,var_detect,ichoice]=nc2struct_toolbox(nc,ListVarName)

DESCRIPTION ^

'nc2struct_toolbox': transform a netcdf file in a corresponding matlab structure, USE OLD NETCDF LIBRARY
----------------------------------------------------------------------
 function [Data,var_detect,ichoice]=nc2struct_toolbox(nc,ListVarName)

 OUTPUT:
  Data: structure containing all the information of the netcdf file (or netcdf object)
           with fields:
    .ListGlobalAttribute: cell listing the names of the global attributes
        .Att_1,Att_2... : values of the global attributes
            .ListDimName: cell listing the names of the array dimensions
               .DimValue: array dimension values (Matlab vector with the same length as .ListDimName
            .ListVarName: cell listing the names of the variables
            .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName
            .VarDimName: cell containing a cell of dimension names (in list .ListDimName) for each variable of .ListVarName
           .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName
        .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
  var_detect: vector with same length as ListVarName, with 1 for each detected variable and 0 else.
  ichoice: = line 

INPUT:
     nc:      name of a netcdf file (char string) or netcdf object   
 ListVarName: optional list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} ) 
         if ListVarName=[] or{}, no variables is read (only global attributes and lists of vdimensions, variables and attriburtes)
         if ListVarName is absent or ='*', ALL the variables are read. 
        if ListVarName is a cell array with n lines, the set of variables
                        will be sought by order of priority in the list, while output names will be set by the first line
 
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
     This file is part of the toolbox UVMAT.
 
     UVMAT 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.
 
     UVMAT 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 (file UVMAT/COPYING.txt) for more details.
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 %'nc2struct_toolbox': transform a netcdf file in a corresponding matlab structure, USE OLD NETCDF LIBRARY
0002 %----------------------------------------------------------------------
0003 % function [Data,var_detect,ichoice]=nc2struct_toolbox(nc,ListVarName)
0004 %
0005 % OUTPUT:
0006 %  Data: structure containing all the information of the netcdf file (or netcdf object)
0007 %           with fields:
0008 %    .ListGlobalAttribute: cell listing the names of the global attributes
0009 %        .Att_1,Att_2... : values of the global attributes
0010 %            .ListDimName: cell listing the names of the array dimensions
0011 %               .DimValue: array dimension values (Matlab vector with the same length as .ListDimName
0012 %            .ListVarName: cell listing the names of the variables
0013 %            .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName
0014 %            .VarDimName: cell containing a cell of dimension names (in list .ListDimName) for each variable of .ListVarName
0015 %           .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName
0016 %        .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
0017 %  var_detect: vector with same length as ListVarName, with 1 for each detected variable and 0 else.
0018 %  ichoice: = line
0019 %
0020 %INPUT:
0021 %     nc:      name of a netcdf file (char string) or netcdf object
0022 % ListVarName: optional list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} )
0023 %         if ListVarName=[] or{}, no variables is read (only global attributes and lists of vdimensions, variables and attriburtes)
0024 %         if ListVarName is absent or ='*', ALL the variables are read.
0025 %        if ListVarName is a cell array with n lines, the set of variables
0026 %                        will be sought by order of priority in the list, while output names will be set by the first line
0027 %
0028 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
0029 %  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
0030 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
0031 %     This file is part of the toolbox UVMAT.
0032 %
0033 %     UVMAT is free software; you can redistribute it and/or modify
0034 %     it under the terms of the GNU General Public License as published by
0035 %     the Free Software Foundation; either version 2 of the License, or
0036 %     (at your option) any later version.
0037 %
0038 %     UVMAT is distributed in the hope that it will be useful,
0039 %     but WITHOUT ANY WARRANTY; without even the implied warranty of
0040 %     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0041 %     GNU General Public License (file UVMAT/COPYING.txt) for more details.
0042 %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
0043 
0044 function [Data,var_detect,ichoice]=nc2struct_toolbox(nc,ListVarName)
0045 
0046 %default output
0047 Data=[];
0048 % ListIndex=[];
0049 var_detect=[];
0050 ichoice=[];%default
0051 %open the netcdf file for reading
0052 if ischar(nc)
0053     if exist(nc,'file')
0054         nc=netcdf(nc,'nowrite');
0055         testfile=1;
0056     else
0057        Data.Txt=['ERROR:file ' nc ' does not exist'];
0058        return
0059     end
0060 else
0061     testfile=0;
0062 end
0063     
0064 %  -------- read global attributes -----------
0065 att_read=att(nc);%cell of 'global attributes' (nc objects)
0066 att_key={};%default
0067 iatt_g=0;
0068 for iatt=1:length(att_read)
0069     aa=att_read{iatt};
0070     keystr=name(aa);
0071     indstr1=regexp(keystr,'\\');%replace dots'
0072     indstr2=regexp(keystr,'\.');%replace dots'
0073     if ~isequal(keystr,'title') % PROBLEM WITH civx files do not read 'title'
0074     if  isempty(indstr1) && isempty(indstr2) %
0075        eval(['valuestr=nc.' keystr '(:);'])
0076         if ischar(valuestr) && length(valuestr)<200
0077             iatt_g=iatt_g+1;
0078             indstr1=regexp(keystr,'\\');%replace dots'
0079             indstr2=regexp(keystr,'\.');%replace dots'
0080             if isempty(indstr1) && isempty(indstr2)
0081                 eval(['Data.' keystr '=''' valuestr ''';'])
0082                 att_key{iatt_g}=keystr;
0083             end
0084         elseif isempty(valuestr)
0085             iatt_g=iatt_g+1;
0086             eval(['Data.' keystr '=[];'])
0087             att_key{iatt_g}=keystr;
0088         elseif isnumeric(valuestr)
0089             iatt_g=iatt_g+1;
0090             eval(['Data.' keystr '=valuestr;'])
0091             att_key{iatt_g}=keystr;
0092         end
0093     end
0094     end
0095 end
0096 Data.ListGlobalAttribute=att_key;
0097 nbattr=length(att_key);
0098 neworder=[nbattr+1 [1:nbattr]];
0099 Data=orderfields(Data,neworder);
0100 
0101 %  -------- read dimensions -----------
0102 dim_read=dim(nc);%cell of variable dimension names (nc objects)
0103 dim_name={};
0104 dim_value=[];
0105 for idim=1:length(dim_read);
0106     aa=dim_read{idim};
0107     if ~isempty(aa)
0108     dim_name{idim}=name(aa);
0109     dim_value(idim)=length(aa);
0110     end
0111 end
0112 if ~isempty(dim_name) && ~isempty(dim_value)
0113     Data.ListDimName=dim_name;
0114     Data.DimValue=dim_value;
0115     used=zeros(1,length(dim_value));%initialize test of used dimensions
0116 end
0117 
0118 %  -------- read variables -----------
0119 var_read={}; %default
0120 testmulti=0;
0121 OutputList=[];
0122 if isequal(ListVarName,'*')|| isempty(ListVarName)
0123      var_read=var(nc);%cell of all variables
0124 elseif ~isempty(ListVarName)
0125     sizvar=size(ListVarName);
0126     testmulti=(sizvar(1)>1);
0127     if testmulti
0128         OutputList=ListVarName(1,:);
0129         testend=0;
0130         for iline=1:sizvar(1)
0131             if testend
0132                 break
0133             end
0134             for ivar=1:sizvar(2)
0135                 var_read{ivar}=[];%default
0136                 var_detect(ivar)=0;%default
0137                 VarName=ListVarName{iline,ivar};
0138                 if ~isempty(VarName)
0139                      var_read{ivar}=nc{VarName};%select the input variable names
0140                      if ivar==1
0141                         if isempty (var_read{ivar})
0142                             break%go to next line if the first nc variable is not found
0143                         else
0144                             testend=1; %this line will be read
0145                             ichoice=iline-1; %selectedline number in the list of input names of variables
0146                             var_detect(ivar)=1;
0147                         end
0148                      else
0149                           var_detect(ivar)=~isempty (var_read{ivar});
0150                      end
0151                 end
0152             end
0153         end
0154         if ~isempty(find(var_detect))
0155             OutputList=OutputList(find(var_detect));  
0156         end
0157     else   %single list of input variables
0158         var_detect=ones(size(ListVarName));
0159         for ivar=1:sizvar(2)
0160             var_read{ivar}=nc{ListVarName{ivar}};%select the input variable names
0161             var_detect(ivar)=~isempty(var_read{ivar});
0162         end
0163     end
0164     var_read=var_read(find(var_detect));
0165 end
0166 % var_dim_index=[]; %default
0167 Data.ListVarName={};%default
0168 for ivar=1:length(var_read)
0169     vv=var_read{ivar};
0170     Data.ListVarName{ivar}=name(vv);%name of the variable
0171     if testmulti
0172         Data.ListVarName{ivar}=OutputList{ivar};
0173     else
0174         Data.ListVarName{ivar}=name(vv);%name of the variable
0175     end
0176     var_dim=dim(vv);%dimension netcdf object of the variable
0177     for ivardim=1:length(var_dim)
0178         var_dim_name=name(var_dim{ivardim});%name of the dimension
0179         for idim=1:length(dim_name)% find the index of the current dimension in the list of dimensions
0180             if isequal(dim_name{idim},var_dim_name)
0181                 Data.VarDimIndex{ivar}(ivardim)=idim;
0182                 used(idim)=1;
0183                 break
0184             end
0185         end
0186     end 
0187     Data.VarDimName{ivar}={};
0188     %variable attributes
0189 %     if ivar==1
0190     Data.VarAttribute{ivar}=[];%initialisation of the list of variable attributes
0191 %     end
0192     %variable attributes
0193     att_read=att(vv);
0194     for iatt=1:length(att_read)
0195         aa=att_read{iatt};
0196         eval(['valuestr=vv.' name(aa) '(:);'])
0197         if ischar(valuestr)
0198             eval(['Data.VarAttribute{ivar}.' name(aa) '=''' valuestr ''';'])
0199         elseif isempty(valuestr)
0200             eval(['Data.VarAttribute{ivar}.' name(aa) '=[];'])
0201         elseif isnumeric(valuestr)
0202             eval(['Data.VarAttribute{ivar}.' name(aa) '=valuestr;'])
0203         end
0204     end
0205 end
0206 
0207 %select the used dimensions
0208 if isempty(var_read) 
0209     if isfield(Data,'ListDimName') && isfield(Data,'DimValue')
0210     Data=rmfield(Data,'ListDimName');
0211     Data=rmfield(Data,'DimValue');
0212     end
0213 else
0214     old_dim_index=find(used); %dimension indices which are used by the selected variables
0215     old2new=cumsum(used); 
0216     Data.ListDimName=Data.ListDimName(old_dim_index);
0217     Data.DimValue=Data.DimValue(old_dim_index);
0218 end
0219 for ivar=1:length(var_read)
0220     Data.VarDimIndex{ivar}=(old2new(Data.VarDimIndex{ivar}));
0221     Data.VarDimName{ivar}=(Data.ListDimName(Data.VarDimIndex{ivar}));
0222 end
0223 %variable values
0224 
0225 if  ~isempty(ListVarName)
0226     for ivar=1:length(Data.ListVarName)
0227         vv=var_read{ivar};
0228         vdata=vv(:);%data array of the field variable
0229         eval(['Data.' Data.ListVarName{ivar} '=vdata;'])%read the variable data
0230     end
0231 end
0232 %  -------- close fle-----------
0233 if testfile==1
0234     close(nc) 
0235 end
0236

Generated on Fri 13-Nov-2009 11:17:03 by m2html © 2003