source: trunk/src/check_field_structure.m @ 405

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

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

File size: 5.3 KB
RevLine 
[38]1%'check_field_structure': check the validity of the field struture representation consistant with the netcdf format
[89]2%------------------------------------------------------------------------
[8]3% function [DataOut,errormsg]=check_field_structure(Data)
4%
5% OUTPUT:
[404]6% DataOut: structure reproducing the input structure Data (TODO: suppress this output)
[8]7% errormsg: error message which is not empty when the input structure does not have the right form
8%
[89]9% INPUT:
[8]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
[404]18function [errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data)
[8]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
[140]28    errormsg='input field does not contain the cell array of variable names .ListVarNames';
[8]29    return
30end
31%check dimension names
[140]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';
[8]39    return
40end
[404]41% if isfield(Data,'DimValue')
42%     Data=rmfield(Data,'DimValue');
43% end
[140]44nbdim=0;
[404]45ListDimName={};
[158]46
47%% main loop on the list of variables
[140]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
[382]54    sizvar=size(Data.(VarName));% sizvar = dimension of variable
[140]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
[382]61       
[140]62    end
[158]63    nbcoord=numel(sizvar);%nbre of coordinates for variable named VarName
64    testrange=0;
[140]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];
[8]77                return
[140]78            end
[158]79            if sizvar(1)==2 && isequal(VarName,DimCell{1})
80                testrange=1;% test for a dimension variable representing a range
81            end
[140]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
[382]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,
[140]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=[];
[382]96    %for idim=1:nbcoord
97    for idim=1:numel(DimCell) %loop on the coordinates of variable #ivar
[140]98        DimName=DimCell{idim};
[404]99        iprev=find(strcmp(DimName,ListDimName),1);%look for dimension name DimName in the current list
[140]100        if isempty(iprev)% append the dimension name to the current list
101            nbdim=nbdim+1;
[158]102            RangeTest(nbdim)=0; %default
[140]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
[404]106            DimValue(nbdim)=sizvar(idim);
107            ListDimName{nbdim}=DimName;
[140]108            DimIndex=[DimIndex nbdim];
109        else % DimName is detected in the current list of dimension names
[404]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
[158]113                elseif ~testrange               
[140]114                    errormsg=['dimension declaration inconsistent with the size =[' num2str(sizvar) '] for ' VarName];
[8]115                    return
116                end
117            end
[140]118            DimIndex=[DimIndex iprev];
119        end
[8]120    end
[404]121    VarDimIndex{ivar}=DimIndex;
[8]122end
123DataOut=Data;
124   
125   
Note: See TracBrowser for help on using the repository browser.