Changeset 227 for trunk/src/nc2struct.m


Ignore:
Timestamp:
Mar 31, 2011, 1:42:51 PM (13 years ago)
Author:
sommeria
Message:

add function sub_field_series to apply the sub_field operation to a series of fileds (for instance subtracting a background to an image series)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/nc2struct.m

    r188 r227  
    6565            nc=netcdf.open(nc,'NC_NOWRITE');
    6666            testfile=1;
    67             catch errormsg
    68               Data.Txt=['ERROR opening ' nc ': ' errormsg.identifier];
     67            catch ME
     68              Data.Txt=['ERROR opening ' nc ': ' ME.message];
    6969              return
    7070            end
     
    7777    end
    7878   
    79     %% short reading opion for global attributes only, if the first argument is 'ListGlobalAttribute'
     79    %% short reading option for global attributes only, if the first argument is 'ListGlobalAttribute'
    8080    if isequal(varargin{1},'ListGlobalAttribute')
    8181        for ilist=2:numel(varargin)
     82            valuestr=[];%default
    8283            try
    8384            valuestr = netcdf.getAtt(nc,netcdf.getConstant('NC_GLOBAL'),varargin{ilist});
    84             catch
    85                 valuestr=[];
     85            catch ME
    8686            end
    8787            eval(['Data.' varargin{ilist} '=valuestr;'])
     
    9797    %%  -------- read all global attributes (constants)-----------
    9898    att_key={};%default
    99     iatt_g=0;
     99%     iatt_g=0;
    100100    Data.ListGlobalAttribute={};%default
    101101    for iatt=1:ngatts
    102102        keystr= netcdf.inqAttName(nc,netcdf.getConstant('NC_GLOBAL'),iatt-1);
    103         indstr1=regexp(keystr,'\\','once');%detect '\\'
    104         indstr2=regexp(keystr,'\.','once');%detect '\.'
    105         %indtitle=regexp(keystr,'title','once');%detect 'title'(bad characters)
    106         if isempty(indstr1) && isempty(indstr2)%&&isempty(indtitle)
    107            valuestr = netcdf.getAtt(nc,netcdf.getConstant('NC_GLOBAL'),keystr);
    108            if ischar(valuestr) & length(valuestr)<200 & double(valuestr)<=122 & double(valuestr)>=48 %usual characters
    109                 iatt_g=iatt_g+1;
    110                 indstr1=regexp(keystr,'\\','once');%detect '\\'
    111                 indstr2=regexp(keystr,'\.','once');%detect '\.'
    112                 if isempty(indstr1) && isempty(indstr2)
    113                     eval(['Data.' keystr '=''' valuestr ''';'])
    114                     att_key{iatt_g}=keystr;
    115                 end
    116             elseif isempty(valuestr)
    117                 iatt_g=iatt_g+1;
    118                 eval(['Data.' keystr '=[];'])
    119                 att_key{iatt_g}=keystr;
     103        valuestr = netcdf.getAtt(nc,netcdf.getConstant('NC_GLOBAL'),keystr);
     104        keystr=regexprep(keystr,{'\','/','\.','-',' '},{'','','','',''});%remove  '\','.' or '-' if exists
     105        if strcmp(keystr(1),'_')
     106            keystr(1)=[];
     107        end
     108        try
     109            if ischar(valuestr) & length(valuestr)<200 & double(valuestr)<=122 & double(valuestr)>=48 %usual characters
     110               % valuestr=regexprep(valuestr,{'\\','\/','\.','\-',' '},{'_','_','_','_','_'})%remove  '\','.' or '-' if exists
     111                eval(['Data.' keystr '=''' valuestr ''';'])
     112%             elseif isempty(valuestr)
     113%                 eval(['Data.' keystr '=[];'])
    120114            elseif isnumeric(valuestr)
    121                 iatt_g=iatt_g+1;
    122115                eval(['Data.' keystr '=valuestr;'])
    123                 att_key{iatt_g}=keystr;
    124             end
     116            else
     117                eval(['Data.' keystr '='';'])
     118            end
     119            att_key{iatt}=keystr;
     120        catch ME
     121            att_key{iatt}=['attr_' num2str(iatt)];
     122            eval(['Data.' att_key{iatt} '=[];'])
    125123        end
    126124    end
     
    184182            attname = netcdf.inqAttName(nc,var_index(ivar)-1,iatt-1);
    185183            valuestr= netcdf.getAtt(nc,var_index(ivar)-1,attname);
     184            attname=regexprep(attname,{'\','/','\.','-',' '},{'','','','',''});%remove  '\','.' or '-' if exists
     185            if strcmp(attname(1),'_')
     186                attname(1)=[];
     187            end
     188            try
    186189            if ischar(valuestr)
     190               % valuestr=regexprep(valuestr,{'\\','\/','\.','\-',' '},{'_','_','_','_','_'});%remove  '\','.' or '-' if exists
    187191                eval(['Data.VarAttribute{ivar}.' attname '=''' valuestr ''';'])
    188192            elseif isempty(valuestr)
     
    190194            elseif isnumeric(valuestr)
    191195                eval(['Data.VarAttribute{ivar}.' attname '=valuestr;'])
     196            end
     197            catch ME
     198                display(attname)
     199                display(valuestr)
     200                display(ME.message)         
     201                eval(['Data.VarAttribute{ivar}.atrr_' num2str(iatt) '=''not read'';'])
    192202            end
    193203        end
Note: See TracChangeset for help on using the changeset viewer.