Changeset 55 for trunk/src/nc2struct.m


Ignore:
Timestamp:
Mar 17, 2010, 10:26:41 AM (14 years ago)
Author:
sommeria
Message:

-use of a single parameter file PARAM.xml (instead of PARAM_WIN and PARAM_LINUX).
-correction of a bug for opening an existing projection object
-manual editing of vectors made available with the builtin netcdf tool.
-bug fix for reading netcdf files with the builtin netcdf tool.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/nc2struct.m

    r45 r55  
    22% it reads all the global attributes and all variables, or a selected list.
    33% The corresponding dimensions and variable attributes are then extracted
    4 %%%%%% TODO: add the possibility to read only attributes, see  nc2struct_toolbox %%%
    54%----------------------------------------------------------------------
    65% function [Data,var_detect,ichoice]=nc2struct(nc,ListVarName)
     
    98%  Data: structure containing all the information of the netcdf file (or netcdf object)
    109%           with fields:
    11 %    .ListGlobalAttribute: cell listing the names of the global attributes
    12 %        .Att_1,Att_2... : values of the global attributes
    13 %            .ListDimName: cell listing the names of the array dimensions
    14 %               .DimValue: array dimension values (Matlab vector with the same length as .ListDimName
    15 %            .ListVarName: cell listing the names of the variables
    16 %            .VarDimIndex: cell containing the set of dimension indices (in list .ListDimName) for each variable of .ListVarName
    17 %            .VarDimName: cell containing a cell of dimension names (in list .ListDimName) for each variable of .ListVarName
    18 %           .VarAttribute: cell of structures s containing names and values of variable attributes (s.name=value) for each variable of .ListVarName
    19 %        .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
    20 %  var_detect: vector with same length as ListVarName, with 1 for each detected variable and 0 else.
    21 %  ichoice: = line
    22 %
     10%         (optional) .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%                    .VarDimIndex= list of dimension indices (added information,
     18
    2319%INPUT:
    2420%     nc:      name of a netcdf file (char string) or netcdf object   
     
    4743function [Data,var_detect,ichoice]=nc2struct(nc,varargin)
    4844List=varargin;
    49 if nargin==0
     45if isempty(varargin)
    5046    List{1}='*';
    5147end
     
    122118
    123119    %  -------- read dimensions -----------
    124     dim_name={};
     120    ListDimName={};
    125121    dim_value=[];
    126122    for idim=1:ndims%length(dim_read);
    127         [dim_name{idim},dim_value(idim)] = netcdf.inqDim(nc,idim-1);
    128     end
    129     if ~isempty(dim_name) && ~isempty(dim_value)
    130         Data.ListDimName=dim_name;
    131         Data.DimValue=dim_value;
     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;
    132127%         DimIndices=[1:ndims]; %index of the dimension in the netcdf file
    133         dim_used=zeros(1,ndims);%initialize test of used dimensions
     128        flag_used=zeros(1,ndims);%initialize test of used dimensions
    134129    end
    135130 
     
    198193        end
    199194    end
    200    
    201    
     195       
    202196    %select variable attributes and associate dimensions
    203197%     var_dim_index=[]; %default
     
    211205        end
    212206        var_dim=dimids{var_index(ivar)}+1; %dimension indices used by the variable
    213         dim_used(var_dim)=ones(size(var_dim));
     207        flag_used(var_dim)=ones(size(var_dim));%flag_used =1 for the indices of used dimensions
    214208        VarDimIndex{ivar}=var_dim;
    215209
     
    233227
    234228    %select the used dimensions
    235     if isempty(var_read)
    236         if isfield(Data,'ListDimName') && isfield(Data,'DimValue')
    237         Data=rmfield(Data,'ListDimName');
    238         Data=rmfield(Data,'DimValue');
    239         end
    240     else
     229    if ~isempty(var_read)
     230%         if isfield(Data,'ListDimName') %&& isfield(Data,'DimValue')
     231%         Data=rmfield(Data,'ListDimName');
     232%         %Data=rmfield(Data,'DimValue');
     233%         end
     234%     else
    241235%         list_dim=1:ndims;
    242         dim_index=find(dim_used);
     236        dim_index=find(flag_used);
    243237%         list_dim=list_dim(dim_index);
    244         old2new=cumsum(dim_used);
    245         Data.ListDimName=Data.ListDimName(dim_index);
    246         Data.DimValue=Data.DimValue(dim_index);
     238        old2new=cumsum(flag_used);
     239        ListDimName=ListDimName(dim_index);
     240        dim_value=dim_value(dim_index);
    247241    end
    248242    for ivar=1:length(var_read)
    249         Data.VarDimIndex{ivar}=old2new(VarDimIndex{ivar});% ENLEVER Data.VarDimIndex ulterieurement
    250         Data.VarDimName{ivar}=Data.ListDimName(Data.VarDimIndex{ivar});
    251     end
     243        %Data.VarDimIndex{ivar}=old2new(VarDimIndex{ivar});% ENLEVER Data.VarDimIndex ulterieurement
     244        %Data.VarDimName{ivar}=Data.ListDimName(Data.VarDimIndex{ivar});
     245        Data.VarDimName{ivar}=ListDimName(old2new(VarDimIndex{ivar}));
     246    end
     247    Data.ListDimName=ListDimName;
     248    Data.DimValue=dim_value;
     249    Data.VarDimIndex= VarDimIndex;
    252250    %variable values
    253251    if  ~isempty(ListVarName)
     
    259257            end
    260258            eval(['Data.' VarName '=netcdf.getVar(nc,var_index(ivar)-1);'])%read the variable data
    261             eval(['siz=size(Data.' VarName ');'])
    262             if numel(siz)<=2
    263             eval(['Data.' VarName '=Data.' VarName ''';'])%read the variable data
    264             end
     259            %eval(['siz=size(Data.' VarName ');'])
     260           % if numel(siz)<=2
     261            %eval(['Data.' VarName '=Data.' VarName ''';'])%read the variable data
     262            %end
    265263        end
    266264    end
Note: See TracChangeset for help on using the changeset viewer.