Changeset 943 for trunk/src/struct2nc.m


Ignore:
Timestamp:
May 2, 2016, 9:49:22 AM (8 years ago)
Author:
sommeria
Message:

merge_proj_polar corrected

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/struct2nc.m

    r938 r943  
    11% 'struct2nc': create a netcdf file from a Matlab structure
    22%---------------------------------------------------------------------
    3 % errormsg=struct2nc(flname,Data)
     3% errormsg=struct2nc(flname,Data,action)
    44%
    55% OUTPUT:
     
    1616%       (optional) .VarAttribute: cell array of structures of the form .VarAttribute{ivar}.key=value, defining an attribute key name and value for the variable #ivar
    1717%      (requested) .Var1, .Var2....: variables (Matlab arrays) with names listed in .ListVarName
     18% action: if ='keep_open', don't close the file after creation, keep it open for further data input
    1819
    1920%=======================================================================
     
    3536%=======================================================================
    3637
    37 function [errormsg,nc]=struct2nc(flname,Data,action)
     38function [errormsg,nc]=struct2nc(flname,Data,action,ListDimName,DimValue,VarDimIndex)
    3839nc=[];
    39 % if ~ischar(flname)
    40 %     errormsg='invalid input for the netcf file name';
    41 %     return
    42 % end
     40if ~ischar(flname)
     41    errormsg='invalid input for the netcf file name';
     42    return
     43end
    4344if ~exist('Data','var')
    4445     errormsg='no data  input for the netcdf file';
    4546    return
    4647end
    47 if ~exist('action','var')
    48     action='one_input'; %fill the file with data and close it
    49 end
    5048
    5149
    5250%% check the validity of the input field structure
     51if ~ (exist('action','var') && strcmp(action,'keep_open'))
    5352[errormsg,ListDimName,DimValue,VarDimIndex]=check_field_structure(Data);
    5453if ~isempty(errormsg)
     
    5655    return
    5756end
     57end
    5858ListVarName=Data.ListVarName;
    5959
    6060%% create the netcdf file with name flname in format NETCDF4
    61 if ischar(flname)
     61% if ischar(flname)
    6262    FilePath=fileparts(flname);
    6363    if ~strcmp(FilePath,'') && ~exist(FilePath,'dir')
     
    6969    cmode = bitor(cmode, netcdf.getConstant('CLOBBER'));
    7070    nc = netcdf.create(flname, cmode);
    71 else
    72     nc=flname;
    73 end
     71% else
     72%     nc=flname;
     73% end
    7474
    7575%% write global constants
     
    113113for ivar=1:length(ListVarName)
    114114    if isfield(Data,ListVarName{ivar})
    115         VarClass=class(Data.(ListVarName{ivar}));
     115        VarClass{ivar}=class(Data.(ListVarName{ivar}));
    116116        VarType='';
    117         switch VarClass
     117        switch VarClass{ivar}
    118118            case {'single','double'}
    119119                VarType='nc_float'; % store all floating reals as single
     
    146146
    147147%% fill the variables with input data
    148 for ivar=1:length(ListVarName)
    149     if ~isnan(varid(ivar))
    150         VarVal=Data.(ListVarName{ivar});
    151         %varval=values of the current variable
    152         VarDimName=Data.VarDimName{ivar};
    153         if ischar(VarDimName)
    154             VarDimName={VarDimName};
    155         end
    156         siz=size(VarVal);
    157         testrange=(numel(VarDimName)==1 && strcmp(VarDimName{1},ListVarName{ivar}) && numel(VarVal)==2);% case of a coordinate defined on a regular mesh by the first and last values.
    158         testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), DimValue(VarDimIndex{ivar}));%matlab vector
    159         %testcolumn=isequal(length(siz),2) && isequal(siz(1), DimValue(VarDimIndex{ivar}))&& isequal(siz(2),1);%matlab column vector
    160         if testline || testrange
    161             if testrange
    162                 VarVal=linspace(VarVal(1),VarVal(2),DimValue(VarDimIndex{ivar}));% restitute the whole array of coordinate values
    163             end
    164             netcdf.putVar(nc,varid(ivar), double(VarVal'));
    165         else
    166             netcdf.putVar(nc,varid(ivar), double(VarVal));
    167         end     
    168     end
    169 end
    170 if strcmp(action,'one_input')
    171 netcdf.close(nc)
     148if ~(exist('action','var') && strcmp(action,'keep_open'))
     149    for ivar=1:length(ListVarName)
     150        if ~isnan(varid(ivar))
     151            VarVal=Data.(ListVarName{ivar});
     152            if strcmp(VarClass{ivar},'single')
     153                VarVal=single(VarVal);
     154            end
     155            %varval=values of the current variable
     156            VarDimName=Data.VarDimName{ivar};
     157            if ischar(VarDimName)
     158                VarDimName={VarDimName};
     159            end
     160            siz=size(VarVal);
     161            testrange=(numel(VarDimName)==1 && strcmp(VarDimName{1},ListVarName{ivar}) && numel(VarVal)==2);% case of a coordinate defined on a regular mesh by the first and last values.
     162            testline=isequal(length(siz),2) && isequal(siz(1),1)&& isequal(siz(2), DimValue(VarDimIndex{ivar}));%matlab vector
     163            %testcolumn=isequal(length(siz),2) && isequal(siz(1), DimValue(VarDimIndex{ivar}))&& isequal(siz(2),1);%matlab column vector
     164            if testline || testrange
     165                if testrange
     166                    VarVal=linspace(VarVal(1),VarVal(2),DimValue(VarDimIndex{ivar}));% restitute the whole array of coordinate values from the first and last values
     167                end
     168                netcdf.putVar(nc,varid(ivar), VarVal');
     169            else
     170                netcdf.putVar(nc,varid(ivar), VarVal);
     171            end
     172        end
     173    end
     174end
     175 netcdf.close(nc)
    172176[success,errormsg] = fileattrib(flname ,'+w');% allow writing access for the group of users
    173 end
    174 
    175177%'check_field_structure': check the validity of the field struture representation consistant with the netcdf format
    176178%------------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.