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

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

File:
1 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;
Note: See TracChangeset for help on using the changeset viewer.