Changeset 140 for trunk


Ignore:
Timestamp:
Nov 29, 2010, 9:55:54 AM (13 years ago)
Author:
sommeria
Message:

bug repair in netcdf file reading, dealing with unavailable variables, + cleaning

Location:
trunk/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/check_field_structure.m

    r89 r140  
    3232    nbfield=numel(Data.ListVarName);
    3333else
    34     errormsg='input field does not contain the list of variables .ListVarNames';
     34    errormsg='input field does not contain the cell array of variable names .ListVarNames';
    3535    return
    3636end
    3737%check dimension names
    38 % definition by VarDimIndex (obsolete)
    39 % if ~isfield(Data,'VarDimName') && isfield(Data,'VarDimIndex') && isfield(Data,'ListDimName')%old convention
    40 %     for ivar=1:nbfield
    41 %         DimCell=Data.VarDimIndex{ivar};
    42 %         if isnumeric(DimCell)
    43 %             if DimCell <= numel(Data.ListDimName)
    44 %                 Data.VarDimName{ivar}=Data.ListDimName(DimCell);
    45 %             else                     
    46 %                 errormsg='dimension names not defined';
    47 %                 return
    48 %             end
    49 %         else
    50 %             errormsg='unrecognized format for .VarDimIndex';
    51 %         end
    52 %     end
    53 % end
    54 if ~(isfield(Data,'VarDimName') && iscell(Data.VarDimName))
    55     errormsg='input field does not contain the list of dimensions .VarDimName';
     38if (isfield(Data,'VarDimName') && iscell(Data.VarDimName))
     39    if  numel(Data.VarDimName)~=nbfield
     40       errormsg=' .ListVarName and .VarDimName have different lengths';
     41        return
     42    end
     43else
     44    errormsg='input field does not contain the  cell array of dimension names .VarDimName';
    5645    return
    5746end
     
    5948    Data=rmfield(Data,'DimValue');
    6049end
    61 if isfield(Data,'VarDimName') && iscell(Data.VarDimName)
    62     nbdim=0;
    63     if numel(Data.VarDimName)==nbfield
    64         for ivar=1:nbfield
    65             VarName=Data.ListVarName{ivar};
    66             if ~isfield(Data,VarName)
    67                 errormsg=['the listed variable ' VarName ' is not found'];
     50nbdim=0;
     51Data.ListDimName={};
     52for ivar=1:nbfield
     53    VarName=Data.ListVarName{ivar};
     54    if ~isfield(Data,VarName)
     55        errormsg=['the listed variable ' VarName ' is not found'];
     56        return
     57    end
     58    eval(['sizvar=size(Data.' VarName ');'])% sizvar = dimension of variable
     59    DimCell=Data.VarDimName{ivar};
     60    if ischar(DimCell)
     61        DimCell={DimCell};%case of a single dimension name, defined by a string
     62    elseif ~iscell(DimCell)
     63        errormsg=['wrong format for .VarDimName{' num2str(ivar) ' (must be the cell of dimension names of the variable ' VarName];
     64        return
     65    end
     66    nbcoord=numel(sizvar);%nbre of coordiantes for variable named VarName
     67    if numel(DimCell)==0
     68        errormsg=['empty declared dimension .VarDimName{' num2str(ivar) '} for ' VarName];
     69        return
     70    elseif numel(DimCell)==1% one dimension declared
     71        if nbcoord==2
     72            if sizvar(1)==1
     73                nbcoord=1;
     74                sizvar(1)=sizvar(2);
     75            elseif sizvar(2)==1
     76                nbcoord=1;
     77            else
     78                errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =2 of the variable ' VarName];
    6879                return
    69             else             
    70                 eval(['sizvar=size(Data.' VarName ');'])% sizvar = dimension of variable
    71                 DimCell=Data.VarDimName{ivar};
    72                 if ischar(DimCell)
    73                     DimCell={DimCell};
    74                 elseif ~iscell(DimCell)
    75                     errormsg=['wrong format for .VarDimName{' num2str(ivar) ' (must be the cell of dimension names of the variable ' VarName];
     80            end
     81        else
     82            errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName];
     83            return
     84        end
     85    else
     86        if numel(DimCell)>nbcoord
     87            DimCell=DimCell(end-nbcoord+1:end);%first singleton diemnsions omitted,
     88        elseif nbcoord > numel(DimCell)
     89            errormsg=['nbre of declared dimensions in .VarDimName{' num2str(ivar) '} smaller than the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName];
     90            return
     91        end
     92    end
     93    DimIndex=[];
     94    for idim=1:nbcoord %loop on the coordinates of variable #ivar
     95        DimName=DimCell{idim};
     96        iprev=find(strcmp(DimName,Data.ListDimName),1);%look for dimension name DimName in the current list
     97        if isempty(iprev)% append the dimension name to the current list
     98            nbdim=nbdim+1;
     99            if sizvar(idim)==2 && strcmp(DimName,VarName)%case of a coordinate defined by the two end values (regular spacing)
     100                RangeTest(nbdim)=1; %to be updated for a later variable 
     101            end
     102            Data.DimValue(nbdim)=sizvar(idim);
     103            Data.ListDimName{nbdim}=DimName;
     104            DimIndex=[DimIndex nbdim];
     105        else % DimName is detected in the current list of dimension names
     106            if ~isequal(Data.DimValue(iprev),sizvar(idim))
     107                if isequal(Data.DimValue(iprev),2)&& RangeTest(iprev)  % the dimension has been already detected as a range [min max]
     108                    Data.DimValue(iprev)=sizvar(idim); %update with actual value
     109                else
     110                    errormsg=['dimension declaration inconsistent with the size =[' num2str(sizvar) '] for ' VarName];
    76111                    return
    77112                end
    78                 nbcoord=numel(sizvar);
    79                 if numel(DimCell)==0
    80                     errormsg=['empty declared dimension .VarDimName{' num2str(ivar) '} for ' VarName];
    81                     return
    82                 elseif numel(DimCell)==1% one dimension declared
    83                     if nbcoord==2
    84                         if sizvar(1)==1
    85                             nbcoord=1;
    86                             sizvar(1)=sizvar(2);
    87                         elseif sizvar(2)==1
    88                             nbcoord=1;
    89                         else
    90                             errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =2 of the variable ' VarName];
    91                             return
    92                         end
    93                     else
    94                           errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName];
    95                           return     
    96                     end 
    97                 else
    98                     if numel(DimCell)>nbcoord
    99                         DimCell=DimCell(end-nbcoord+1:end);%first singleton diemnsions omitted,
    100                     elseif nbcoord > numel(DimCell)
    101                         errormsg=['nbre of declared dimensions in .VarDimName{' num2str(ivar) '} smaller than the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName];
    102                         return
    103                     end
    104                 end
    105                 DimIndex=[];
    106                 for idim=1:nbcoord %loop on the coordinates of variable #ivar   
    107                     DimName=DimCell{idim};
    108                     testprev=0;
    109                     for iprev=1:nbdim %check previously listed dimension names
    110                         if strcmp(Data.ListDimName{iprev},DimName)
    111                            if ~isequal(Data.DimValue(iprev),sizvar(idim))
    112                                if isequal(Data.DimValue(iprev),0)  % the dimension has been already detected as a range [min max]
    113                                    Data.DimValue(idim)=sizvar(idim); %update with actual value
    114                                elseif sizvar(idim)==2 && strcmp(DimName,VarName)
    115                                     %case of a regularly spaced coordinate defined by the first and last values: dimension will be determined later                         
    116                                else
    117                                    errormsg=['dimension declaration inconsistent with the size =[' num2str(sizvar) '] for ' VarName];
    118                                    return
    119                                end
    120                            end
    121                            DimIndex=[DimIndex iprev];
    122                            testprev=1;
    123                            break
    124                         end
    125                     end
    126                     if ~testprev % a new dimension is appended to the list
    127                         nbdim=nbdim+1;
    128                         if sizvar(idim)==2 && strcmp(DimName,VarName)
    129                             Data.DimValue(nbdim)=0; %to be updated for a later variable
    130 %                             Data.VarType{ivar}='range';
    131                         else
    132                             Data.DimValue(nbdim)=sizvar(idim);
    133                         end
    134                         Data.ListDimName{nbdim}=DimName;
    135                         DimIndex=[DimIndex nbdim];
    136                     end
    137                 end
    138                 Data.VarDimIndex{ivar}=DimIndex;
    139113            end
    140         end                               
    141     else
    142         errormsg=' .ListVarNames and .VarDimName have different lengths';
    143         return
     114            DimIndex=[DimIndex iprev];
     115        end
    144116    end
    145 else
    146     errormsg='input field does not contain the cell of dimension names .VarDimName for variables';
    147     return
     117    Data.VarDimIndex{ivar}=DimIndex;
    148118end
    149119DataOut=Data;
  • trunk/src/nc2struct.m

    r127 r140  
    77% OUTPUT:
    88%  Data: structure containing all the information of the netcdf file (or netcdf object)
    9 %           with fields:
    10 %         (optional) .ListGlobalAttribute: cell listing the names of the global attributes
     9%           with (optional)fields:
     10%                   .ListGlobalAttribute: cell listing the names of the global attributes
    1111%                    .Att_1,Att_2... : values of the global attributes
    1212%                    .ListVarName: list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} )
     
    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 %                    .VarDimIndex= list of dimension indices (added information,
    18 %
     17%  var_detect: vector with same length as the cell array ListVarName, = 1 for each detected variable and 0 else.
     18%            var_detect=[] in the absence of input cell array
     19%  ichoice: index of the selected line in the case of multiple choice
     20%        (cell array of varible names with multiple lines) , =[] by default
    1921%INPUT:
    20    nc:      name of a netcdf file (char string) or netcdf object   
    21   additional arguments:
    22 %         -in the absence of other arguments, all the fields are read
    23 %         -a cell array, ListVarName, of  char strings {'VarName1', 'VarName2',...} )
     22nc:  name of a netcdf file (char string) or netcdf object   
     23additional arguments:
     24%       -no additional arguments: all the variables of the netcdf fiel are read.
     25%       -a cell array, ListVarName, made of  char strings {'VarName1', 'VarName2',...} )
    2426%         if ListVarName=[] or {}, no variables is read (only global attributes)
    25 %         if ListVarName is absent, or = '*', ALL the variables are read.
    26 %        if ListVarName is a cell array with n lines, the set of variables
    27 %                        will be sought by order of priority in the list, while output names will be set by the first line
     27%         if ListVarName is absent, or = '*', ALL the variables of the netcdf file are read.
     28%         if ListVarName is a cell array with n lines, the set of variables will be sought by order of priority
     29%                  in the list, while output names will be set by the first line
    2830%        - the string 'ListGlobalAttribute' followed by a list of attribute  names: reads only these attributes (fast reading)
    2931%
     
    5254
    5355if ~isequal(hhh,'')
    54     %default output
    55     Data=[];
    56     var_detect=[];
     56    %% default output
     57    Data=[];%default
     58    var_detect=[];%default
    5759    ichoice=[];%default
    58     %open the netcdf file for reading
     60   
     61    %% open the netcdf file for reading
    5962    if ischar(nc)
    6063        if exist(nc,'file')
     
    6871        testfile=0;
    6972    end
    70     % short reading of global attributes
     73   
     74    %% short reading opion for global attributes only, if the first argument is 'ListGlobalAttribute'
    7175    if isequal(varargin{1},'ListGlobalAttribute')
    7276        for ilist=2:numel(varargin)
     
    8286    end
    8387
    84     % reading of variables, including attributes
    85     ListVarName=varargin{1};  
    86     [ndims,nvars,ngatts]=netcdf.inq(nc);%nbre of dimensions, variables, attributes
    87    
    88     %  -------- read global attributes (constants)-----------
     88    %% full reading: get the nbre of dimensions, variables, global attributes
     89    ListVarName=varargin{1};
     90    [ndims,nvars,ngatts]=netcdf.inq(nc);%nbre of dimensions, variables, global attributes, in the netcdf file
     91   
     92    %%  -------- read all global attributes (constants)-----------
    8993    att_key={};%default
    9094    iatt_g=0;
     
    9296    for iatt=1:ngatts
    9397        keystr= netcdf.inqAttName(nc,netcdf.getConstant('NC_GLOBAL'),iatt-1);
    94         indstr1=regexp(keystr,'\\');%detect '\\'
    95         indstr2=regexp(keystr,'\.');%detect '\.'
     98        indstr1=regexp(keystr,'\\','once');%detect '\\'
     99        indstr2=regexp(keystr,'\.','once');%detect '\.'
    96100        if isempty(indstr1) && isempty(indstr2)
    97101           valuestr = netcdf.getAtt(nc,netcdf.getConstant('NC_GLOBAL'),keystr);
     
    117121    Data.ListGlobalAttribute=att_key;
    118122
    119     %  -------- read dimensions -----------
    120     ListDimName={};
     123    %%  -------- read dimension names-----------
     124    ListDimNameNetcdf={};
    121125    dim_value=[];
    122     for idim=1:ndims%length(dim_read);
    123         [ListDimName{idim},dim_value(idim)] = netcdf.inqDim(nc,idim-1);
    124     end
    125     if ~isempty(ListDimName) %&& ~isempty(dim_value)
    126 %         Data.DimValue=dim_value;
    127 %         DimIndices=[1:ndims]; %index of the dimension in the netcdf file
    128         flag_used=zeros(1,ndims);%initialize test of used dimensions
     126    for idim=1:ndims %loop on the dimensions of the netcdf file
     127        [ListDimNameNetcdf{idim},dim_value(idim)] = netcdf.inqDim(nc,idim-1);%get name and value of each dimension
     128    end
     129    if ~isempty(ListDimNameNetcdf)
     130        flag_used=zeros(1,ndims);%initialize the flag indicating the selected dimensions in the list (0=unused)
    129131    end
    130132 
    131     %  -------- read variables -----------
    132     var_read={}; %default
     133    %%  -------- read names of variables -----------
     134    ListVarNameNetcdf={}; %default
    133135    dimids={};
    134136    nbatt=[];
    135     for ivar=1:nvars
    136         [var_read{ivar},xtype,dimids{ivar},nbatt(ivar)] = netcdf.inqVar(nc,ivar-1);
     137    for ncvar=1:nvars %loop on the variables of the netcdf file
     138        %get name, type, dimensions and attribute numbers of each variable
     139        [ListVarNameNetcdf{ncvar},xtype,dimids{ncvar},nbatt(ncvar)] = netcdf.inqVar(nc,ncvar-1);
    137140    end 
    138     var_index=1:nvars; %default set of variable indices in the netcdf file
    139141    testmulti=0;
    140     OutputList=[];
    141     %select input variables, if requested by the input ListVarName
    142     if ~(isequal(ListVarName,'*')||isempty(ListVarName))
     142    if isequal(ListVarName,'*')||isempty(ListVarName)
     143        var_index=1:nvars; %all the variables are selected in the netcdf file
     144        Data.ListVarName=ListVarNameNetcdf;
     145    else   %select input variables, if requested by the input ListVarName
    143146        sizvar=size(ListVarName);
    144         testmulti=(sizvar(1)>1);
     147        testmulti=(sizvar(1)>1);%test for multiple choice of variable ranked by order of priority
    145148        var_index=zeros(1,sizvar(2));%default
    146         if testmulti
    147             OutputList=ListVarName(1,:);
    148             testend=0;
     149        if testmulti %multiple choice of variable ranked by order of priority
    149150            for iline=1:sizvar(1)
    150                 if testend
    151                     break
     151                search_index=find(strcmp(ListVarName{iline,1},ListVarNameNetcdf),1);%look for the first variable name in the list of netcdf variables
     152                if ~isempty(search_index)
     153                    break % go to the next line
    152154                end
    153           %      var_index=zeros(size(ListVarName));%default
    154                 for ivar=1:sizvar(2)
    155                     if ~isempty(ListVarName{iline,ivar})
    156                          for ilist=1:nvars
    157                             if isequal(var_read{ilist},ListVarName{iline,ivar})
    158                                 var_index(ivar)=ilist;
    159      %                          var_detect(ivar)=1;
    160                             break
    161                             end
    162                          end
    163                          if ivar==1
    164                             if var_index(ivar)==0
    165                                 break%go to next line if the first nc variable is not found
    166                             else
    167                                 testend=1; %this line will be read
    168                                 ichoice=iline-1; %selectedline number in the list of input names of variables
    169                             end
    170                          end
    171                     end
    172                 end
    173             end
    174         else   %single list of input variables
    175             for ivar=1:sizvar(2)
    176                 for ilist=1:nvars
    177                     if isequal(var_read{ilist},ListVarName{ivar})
    178                         var_index(ivar)=ilist;
    179                         var_detect(ivar)=1;
    180                         break
    181                     end
    182                 end
    183             end
    184         end
    185         list_index=find(var_index);
    186         if ~isempty(list_index)
    187             if testmulti
    188                 OutputList=OutputList(list_index);
    189             end
    190             var_index=var_index(list_index);
    191             var_detect=(var_index~=0);
    192             var_read=var_read(var_index);         
    193         end
    194     end
    195        
    196     %select variable attributes and associate dimensions
    197 %     var_dim_index=[]; %default
    198     Data.ListVarName={};%default
    199     VarDimIndex={};%default
    200     for ivar=1:length(var_read)
    201         if testmulti
    202             Data.ListVarName{ivar}=OutputList{ivar};%new name given by ListVarName(1,:)
     155            end
     156            ichoice=iline-1;%selected line number in the list of input names of variables
    203157        else
    204             Data.ListVarName{ivar}=var_read{ivar};%name of the variable
    205         end
    206         var_dim=dimids{var_index(ivar)}+1; %dimension indices used by the variable
    207         flag_used(var_dim)=ones(size(var_dim));%flag_used =1 for the indices of used dimensions
    208         VarDimIndex{ivar}=var_dim;
    209 
    210         %variable attributes
    211         if ivar==1
    212             Data.VarAttribute={};%initialisation of the list of variable attributes
    213         end
    214         %variable attributes
     158            iline=1;
     159        end
     160        for ivar=1:sizvar(2)
     161            search_index=find(strcmp(ListVarName{iline,ivar},ListVarNameNetcdf),1);%look for the variable name in the list of netcdf file
     162            if ~isempty(search_index)
     163                var_index(ivar)=search_index;%index of the netcdf list corresponding to the input list index ivar
     164            end
     165        end
     166        var_detect=(var_index~=0);%=1 for detected variables         
     167        list_index=find(var_index);% indices in the input list corresponding to a detected variable
     168        var_index=var_index(list_index);% netcdf variable indices corresponding to the output list of read variable
     169        Data.ListVarName=ListVarName(1,list_index);%the first line of ListVarName sets the output names of the variables
     170    end
     171     
     172  %% get the dimensions and attributes associated to  variables
     173    for ivar=1:length(var_index)
     174        var_dim{ivar}=dimids{var_index(ivar)}+1; %netcdf dimension indices used by the variable #ivar
     175        Data.VarDimName{ivar}=ListDimNameNetcdf(var_dim{ivar});
     176        flag_used(var_dim{ivar})=ones(size(var_dim{ivar}));%flag_used =1 for the indices of used dimensions
    215177        for iatt=1:nbatt(var_index(ivar))
    216178            attname = netcdf.inqAttName(nc,var_index(ivar)-1,iatt-1);
     
    226188    end
    227189
    228     %select the used dimensions
    229     if ~isempty(var_read)
    230         dim_index=find(flag_used);
    231         old2new=cumsum(flag_used);
    232         ListDimName=ListDimName(dim_index);
    233         dim_value=dim_value(dim_index);
    234     end
    235     for ivar=1:length(var_read)
    236         Data.VarDimName{ivar}=ListDimName(old2new(VarDimIndex{ivar}));
    237     end
    238     Data.ListDimName=ListDimName;
    239     Data.DimValue=dim_value;
    240     Data.VarDimIndex= VarDimIndex;
    241     %variable values
     190    %% select the dimensions used for the set of input variables
     191    if ~isempty(var_index)     
     192        dim_index=find(flag_used);%list of netcdf dimensions indices corresponding to used dimensions
     193        Data.ListDimName=ListDimNameNetcdf(dim_index);
     194        Data.DimValue=dim_value(dim_index);
     195    end
     196   
     197    %% get the values of the input variables
    242198    if  ~isempty(ListVarName)
    243         for ivar=1:length(Data.ListVarName)
     199        for ivar=1:length(var_index)
    244200            VarName=Data.ListVarName{ivar};
    245             indstr=regexp(VarName,'-');%detect '-'
    246             if ~isempty(indstr)
    247                 VarName(indstr)=[];
    248             end
     201            VarName=regexprep(VarName,'-',''); %suppress '-' if it exists in the netcdf variable name
    249202            eval(['Data.' VarName '=double(netcdf.getVar(nc,var_index(ivar)-1));'])%read the variable data
    250203        end
    251204    end
    252     %  -------- close fle-----------
     205   
     206    %%  -------- close fle-----------
    253207    if testfile==1
    254208        netcdf.close(nc)
    255209    end
     210   
     211%% old netcdf library
    256212else
    257213    [Data,var_detect,ichoice]=nc2struct_toolbox(nc,varargin);
  • trunk/src/nc2struct_toolbox.m

    r56 r140  
    11%'nc2struct_toolbox': transform a netcdf file in a corresponding matlab structure, USE OLD NETCDF LIBRARY
     2% it reads all the global attributes and all variables, or a selected list.
     3% The corresponding dimensions and variable attributes are then extracted
    24%----------------------------------------------------------------------
    3 % function [Data,var_detect,ichoice]=nc2struct_toolbox(nc,ListField)
     5% function [Data,var_detect,ichoice]=nc2struct_toolbox(nc,varargin)
    46%
    57% OUTPUT:
    68%  Data: structure containing all the information of the netcdf file (or netcdf object)
    7 %           with fields:
    8 %    .ListGlobalAttribute: cell listing the names of the global attributes
    9 %        .Att_1,Att_2... : values of the global attributes
    10 %            .ListDimName: cell listing the names of the array dimensions
    11 %               .DimValue: array dimension values (Matlab vector with the same length as .ListDimName
    12 %            .ListVarName: cell listing the names of the variables
    13 %            .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName
    14 %            .VarDimName: cell containing a cell of dimension names (in list .ListDimName) for each variable of .ListVarName
    15 %           .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName
    16 %        .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
    17 %  var_detect: vector with same length as ListVarName, with 1 for each detected variable and 0 else.
    18 %  ichoice: = line
    19 %
     9%           with (optional)fields:
     10%                    .ListGlobalAttribute: cell listing the names of the global attributes
     11%                    .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)                         
     14%                    .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
     15%                    .ListDimName=list of dimension (added information, not requested for field description)
     16%                    .DimValue= vlalues of dimensions (added information, not requested for field description)
     17%  var_detect: vector with same length as the cell array ListVarName, = 1 for each detected variable and 0 else.
     18%            var_detect=[] in the absence of input cell array
     19%  ichoice: index of the selected line in the case of multiple choice
     20%        (cell array of varible names with multiple lines) , =[] by default
    2021%INPUT:
    21 %     nc:      name of a netcdf file (char string) or netcdf object   
    22 % ListField: optional list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} )
    23 %         if ListField is absent or ='*', ALL the attributes and variables are read.  %     
    24 %        if  ListField='ListGlobalAttribute', followed by the arguments 'name1', name2'..., only thes global attributes will be read (short option)
    25 %        if  ListField=[] or{}, no variables is read (only global attributes and lists of vdimensions, variables and attriburtes)
    26 %        if ListField is a cell array with n lines, the set of variables
    27 %                        will be sought by order of priority in the list, while output names will be set by the first line
     22%  nc:  name of a netcdf file (char string) or netcdf object   
     23%  additional arguments:
     24%       -no additional arguments: all the variables of the netcdf fiel are read.
     25%       -a cell array, ListVarName, made of  char strings {'VarName1', 'VarName2',...} )
     26%         if ListVarName=[] or {}, no variables is read (only global attributes)
     27%         if ListVarName is absent, or = '*', ALL the variables of the netcdf file are read.
     28%         if ListVarName is a cell array with n lines, the set of variables will be sought by order of priority
     29%                  in the list, while output names will be set by the first line
     30%        - the string 'ListGlobalAttribute' followed by a list of attribute  names: reads only these attributes (fast reading)
    2831%
    2932%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
     
    203206        for idim=1:length(dim_name)% find the index of the current dimension in the list of dimensions
    204207            if isequal(dim_name{idim},var_dim_name)
    205                 Data.VarDimIndex{ivar}(ivardim)=idim;
     208                VarDimIndex{ivar}(ivardim)=idim;
    206209                used(idim)=1;
    207210                break
     
    240243end
    241244for ivar=1:length(var_read)
    242     Data.VarDimIndex{ivar}=(old2new(Data.VarDimIndex{ivar}));
    243     Data.VarDimName{ivar}=(Data.ListDimName(Data.VarDimIndex{ivar}));
     245    VarDimIndex{ivar}=(old2new(VarDimIndex{ivar}));
     246    Data.VarDimName{ivar}=(Data.ListDimName(VarDimIndex{ivar}));
    244247end
    245248%variable values
  • trunk/src/read_civxdata.m

    r127 r140  
    7171    return % error in file reading
    7272end
    73 if isequal(vardetect,0)
    74      Field.Txt=[reshape(FieldNames,1,numel(FieldNames)) ' not accessible in ' filename '/' VelType];
     73if vardetect(1)==0
     74     Field.Txt=[ 'requested field not available in ' filename '/' VelType];
    7575     return
    7676end
     
    137137% rescale to pixel coordiantes
    138138if isfield(Field,'pixcmx')
    139 Field.pixcmx=double(Field.pixcmx);
    140 Field.pixcmy=double(Field.pixcmy);
    141 Field.U=Field.U*Field.pixcmx;
    142 Field.V=Field.V*Field.pixcmy;
    143 Field.X=Field.X*Field.pixcmx;
    144 Field.Y=Field.Y*Field.pixcmy;
     139    Field.pixcmx=double(Field.pixcmx);
     140    Field.pixcmy=double(Field.pixcmy);
     141    Field.U=Field.U*Field.pixcmx;
     142    Field.V=Field.V*Field.pixcmy;
     143    Field.X=Field.X*Field.pixcmx;
     144    Field.Y=Field.Y*Field.pixcmy;
    145145end
    146146if ~isequal(Field.dt,0)
  • trunk/src/struct2nc.m

    r128 r140  
    5151        return
    5252    end
    53     [Data,errormsg]=check_field_structure(Data);%check the validity of the input field structure
     53    [Data,errormsg]=check_field_structure(Data)%check the validity of the input field structure
     54    if ~isempty(errormsg)
     55        errormsg=['invalid input structure:' errormsg];
     56        return
     57    end
    5458    ListVarName=Data.ListVarName;
    5559    nc=netcdf.create(flname,'NC_CLOBBER');%,'clobber'); %create the netcdf file with name flname   
     
    110114            eval(['VarVal=Data.' ListVarName{ivar} ';'])%varval=values of the current variable
    111115            VarDimIndex=Data.VarDimIndex{ivar}; %indices of the variable dimensions in the list of dimensions
    112             siz=size(VarVal);
    113116            VarDimName=Data.VarDimName{ivar};
    114117            if ischar(VarDimName)
    115118                VarDimName={VarDimName};
    116119            end
     120            siz=size(VarVal);
    117121            testrange=(numel(VarDimName)==1 && strcmp(VarDimName{1},ListVarName{ivar}) && numel(VarVal)==2);% case of a coordinate defined on a regular mesh by the first and last values.
    118122            testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), Data.DimValue(VarDimIndex));%matlab vector
Note: See TracChangeset for help on using the changeset viewer.