source: trunk/src/check_field_structure.m @ 150

Last change on this file since 150 was 140, checked in by sommeria, 13 years ago

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

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, with the additional elements:
7%           with fields:
8%
9%            .ListDimName: cell listing the names of the array dimensions
10%             .DimValue: array dimension values (Matlab vector with the same length as .ListDimName
11%            .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName
12%            .VarDimName: cell containing a cell of dimension names (in list .ListDimName) for each variable of .ListVarName
13% errormsg: error message which is not empty when the input structure does not have the right form
14%
15% INPUT:
16% Data:   structure containing
17%         (optional) .ListGlobalAttribute: cell listing the names of the global attributes
18%                    .Att_1,Att_2... : values of the global attributes
19%         (requested)  .ListVarName: list of variable names to select (cell array of  char strings {'VarName1', 'VarName2',...} )
20%         (requested)  .VarDimName: list of dimension names for each element of .ListVarName (cell array of string cells)                         
21%         (requested) .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
22
23
24function [DataOut,errormsg]=check_field_structure(Data)
25DataOut=[]; %default
26errormsg=[];
27if ~isstruct(Data)
28    errormsg='input field is not a structure';
29    return
30end
31if isfield(Data,'ListVarName') && iscell(Data.ListVarName)
32    nbfield=numel(Data.ListVarName);
33else
34    errormsg='input field does not contain the cell array of variable names .ListVarNames';
35    return
36end
37%check dimension names
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';
45    return
46end
47if isfield(Data,'DimValue')
48    Data=rmfield(Data,'DimValue');
49end
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];
79                return
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];
111                    return
112                end
113            end
114            DimIndex=[DimIndex iprev];
115        end
116    end
117    Data.VarDimIndex{ivar}=DimIndex;
118end
119DataOut=Data;
120   
121   
Note: See TracBrowser for help on using the repository browser.