source: trunk/src/check_field_structure.m @ 404

Last change on this file since 404 was 404, checked in by sommeria, 12 years ago

various bugs corrected. nc2struct_toolbox suppressed (correspond to obsolete versions of Matlab)

File size: 5.3 KB
Line 
1%'check_field_structure': check the validity of the field struture representation consistant with the netcdf format
2%------------------------------------------------------------------------
3% function [DataOut,errormsg]=check_field_structure(Data)
4%
5% OUTPUT:
6% DataOut: structure reproducing the input structure Data (TODO: suppress this output)
7% errormsg: error message which is not empty when the input structure does not have the right form
8%
9% INPUT:
10% Data:   structure containing
11%         (optional) .ListGlobalAttribute: cell listing the names of the global attributes
12%                    .Att_1,Att_2... : values of the global attributes
13%         (requested)  .ListVarName: list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} )
14%         (requested)  .VarDimName: list of dimension names for each element of .ListVarName (cell array of string cells)                         
15%         (requested) .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
16
17
18function [errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data)
19DataOut=[]; %default
20errormsg=[];
21if ~isstruct(Data)
22    errormsg='input field is not a structure';
23    return
24end
25if isfield(Data,'ListVarName') && iscell(Data.ListVarName)
26    nbfield=numel(Data.ListVarName);
27else
28    errormsg='input field does not contain the cell array of variable names .ListVarNames';
29    return
30end
31%check dimension names
32if (isfield(Data,'VarDimName') && iscell(Data.VarDimName))
33    if  numel(Data.VarDimName)~=nbfield
34       errormsg=' .ListVarName and .VarDimName have different lengths';
35        return
36    end
37else
38    errormsg='input field does not contain the  cell array of dimension names .VarDimName';
39    return
40end
41% if isfield(Data,'DimValue')
42%     Data=rmfield(Data,'DimValue');
43% end
44nbdim=0;
45ListDimName={};
46
47%% main loop on the list of variables
48for ivar=1:nbfield
49    VarName=Data.ListVarName{ivar};
50    if ~isfield(Data,VarName)
51        errormsg=['the listed variable ' VarName ' is not found'];
52        return
53    end
54    sizvar=size(Data.(VarName));% sizvar = dimension of variable
55    DimCell=Data.VarDimName{ivar};
56    if ischar(DimCell)
57        DimCell={DimCell};%case of a single dimension name, defined by a string
58    elseif ~iscell(DimCell)
59        errormsg=['wrong format for .VarDimName{' num2str(ivar) ' (must be the cell of dimension names of the variable ' VarName];
60        return
61       
62    end
63    nbcoord=numel(sizvar);%nbre of coordinates for variable named VarName
64    testrange=0;
65    if numel(DimCell)==0
66        errormsg=['empty declared dimension .VarDimName{' num2str(ivar) '} for ' VarName];
67        return
68    elseif numel(DimCell)==1% one dimension declared
69        if nbcoord==2
70            if sizvar(1)==1
71                nbcoord=1;
72                sizvar(1)=sizvar(2);
73            elseif sizvar(2)==1
74                nbcoord=1;
75            else
76                errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =2 of the variable ' VarName];
77                return
78            end
79            if sizvar(1)==2 && isequal(VarName,DimCell{1})
80                testrange=1;% test for a dimension variable representing a range
81            end
82        else
83            errormsg=['1 dimension declared in .VarDimName{' num2str(ivar) '} inconsistent with the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName];
84            return
85        end
86    else
87        if numel(DimCell)>nbcoord
88            sizvar(nbcoord+1:numel(DimCell))=1;% case of singleton dimensions (not seen by the function size)
89           % DimCell=DimCell(end-nbcoord+1:end)%first singleton diemensions omitted,
90        elseif nbcoord > numel(DimCell)
91            errormsg=['nbre of declared dimensions in .VarDimName{' num2str(ivar) '} smaller than the nbre of dimensions =' num2str(nbcoord) ' of the variable ' VarName];
92            return
93        end
94    end
95    DimIndex=[];
96    %for idim=1:nbcoord
97    for idim=1:numel(DimCell) %loop on the coordinates of variable #ivar
98        DimName=DimCell{idim};
99        iprev=find(strcmp(DimName,ListDimName),1);%look for dimension name DimName in the current list
100        if isempty(iprev)% append the dimension name to the current list
101            nbdim=nbdim+1;
102            RangeTest(nbdim)=0; %default
103            if sizvar(idim)==2 && strcmp(DimName,VarName)%case of a coordinate defined by the two end values (regular spacing)
104                RangeTest(nbdim)=1; %to be updated for a later variable 
105            end
106            DimValue(nbdim)=sizvar(idim);
107            ListDimName{nbdim}=DimName;
108            DimIndex=[DimIndex nbdim];
109        else % DimName is detected in the current list of dimension names
110            if ~isequal(DimValue(iprev),sizvar(idim))
111                if isequal(DimValue(iprev),2)&& RangeTest(iprev)  % the dimension has been already detected as a range [min max]
112                    DimValue(iprev)=sizvar(idim); %update with actual value
113                elseif ~testrange               
114                    errormsg=['dimension declaration inconsistent with the size =[' num2str(sizvar) '] for ' VarName];
115                    return
116                end
117            end
118            DimIndex=[DimIndex iprev];
119        end
120    end
121    VarDimIndex{ivar}=DimIndex;
122end
123DataOut=Data;
124   
125   
Note: See TracBrowser for help on using the repository browser.