Showing posts with label netcdf. Show all posts
Showing posts with label netcdf. Show all posts

Saturday, February 25, 2012

[Matlab] show the variable information from a netcdf file

function myShowNc(ncFileName)
% show information of a given netcdf file, similar to ncdump
%          using matlab native netcdf functions
% usage:
%           myShowNc(netcdf-filename)
% http://scriptdemo.blogspot.com

if nargin==0
   help myShowNc
   return
end
if ~exist(ncFileName,'file')
   error([ncFileName,' is not found!']);
end

ncfid=netcdf.open(ncFileName,'NC_NOWRITE');

% check dimensions
[ndims,nvars,ngatts,unlimdimID] = netcdf.inq(ncfid);
myncinfo.unLimID=unlimdimID;
myncinfo.dimname=cell(ndims,1);
myncinfo.dimlength=zeros(ndims,1);
myncinfo.varname=cell(nvars,1);
myncinfo.vardims=cell(nvars,1);
myncinfo.varscale=ones(nvars,1);
myncinfo.varoffset=zeros(nvars,1);

disp(['netcdf file: ',ncFileName])
disp(' dimension:')
for Ndim=1:ndims
    [myncinfo.dimname{Ndim},myncinfo.dimlength(Ndim)]=netcdf.inqDim(ncfid,Ndim-1);
    disp([' ',fixStrLen(myncinfo.dimname{Ndim},10),': ',num2str(myncinfo.dimlength(Ndim))])
end

disp(' variables:')
disp([' ',fixStrLen('var-name |',12,0),fixStrLen('var-dims',30,0),fixStrLen('scale',10,1),fixStrLen('offset',10,1)])
% check variables
for nv=1:nvars
    [myncinfo.varname{nv},myncinfo.vardims{nv}, ...
     myncinfo.varscale(nv),myncinfo.varoffset(nv)]=getVarInfo(ncfid,nv-1,myncinfo);
    disp([' ', fixStrLen([myncinfo.varname{nv},' |'],12) , fixStrLen(myncinfo.vardims{nv},30), ...
                fixStrLen(num2str(myncinfo.varscale(nv)),10,1),fixStrLen(num2str(myncinfo.varoffset(nv)),10,1)])
end
netcdf.close(ncfid);

%--------------------------------------------------------------------------------------
function outStr=fixStrLen(inStr,nLen,opt)
% renamed to formatStr in another post.
% if  opt == 0  'right-align' [default case]
%     opt == 1  'center-align'
%     opt == 2  'left-align'
if nargin==1
   nLen=0;
elseif nargin==2
   opt=0;
elseif nargin~=3
   help fixStrLen;
   return
end

oriLen=length(inStr);
if (oriLen>=nLen)
    outStr=inStr;
else
    switch (opt)
        case {0}
            nSpace=nLen-oriLen;
            outStr=[repmat(' ',1,nSpace),inStr];
        case {1}
          nSpace=ceil(0.5*(nLen-oriLen));
          outStr=[repmat(' ',1,nSpace),inStr];
          nSpace=nLen-oriLen-nSpace;
          if (nSpace>0)
             outStr=[outStr,repmat(' ',1,nSpace)];
          end
        case {2}
            nSpace=nLen-oriLen;
            outStr=[inStr,repmat(' ',1,nSpace)];
        otherwise
            disp('Unknown option');
            return
    end
end

function [varname,dimsStr,myScale,myOffSet]=getVarInfo(ncfid,varID,myncinfo)
[varname,vartype,vardimids,numAtts]=netcdf.inqVar(ncfid,varID);
myScale=1; myOffSet=0;
for numA=1:numAtts
    tmpattname=netcdf.inqAttName(ncfid,varID,numA-1);
    switch lower(tmpattname)
        case {'scale_factor','scalefactor'}
           myScale=netcdf.getAtt(ncfid,varID,tmpattname);
        case {'add_offset','off_set','offset','offset_value'}
           myOffSet=netcdf.getAtt(ncfid,varID,tmpattname);
        otherwise
           % do nothing
     end
end
% get dimStr
if ~isempty(vardimids)
   dimsStr=myncinfo.dimname{vardimids(1)+1};
   for nd=2:length(vardimids)
         dimsStr=[myncinfo.dimname{vardimids(nd)+1},' x ',dimsStr];
   end
else
   dimsStr='unlimited';
end 

Monday, August 15, 2011

[Bash] manipulate variables from different netcdf files using nco

#!/bin/bash
# compute the toal precipitation (L+S)
# usage:
#           getTotalPrec.sh RainFile SnowFile
#    assuming the variable name for rain  : pr
#                                                 snow : prsn
#copyright @ http://scriptdemo.blogspot.com

ncVarNameRain="pr"
ncVarNameSnow="prsn"
if [ $# -eq 2 ]; then
   RainFile=$1; SnowFile=$2
else
   sed -n '3,6p' getTotalPrec.sh
   exit
fi

[ ! -e ${RainFile} ] && echo "${RainFile} does not exist!" && exit
[ ! -e ${SnowFile} ] && echo "${SnowFile} does not exist!" && exit
oriP="ori_${RainFile}"
cp ${RainFile} ${oriP}
eval "ncks -A -v ${ncVarNameSnow} ${SnowFile} ${oriP}"
eval "ncap2 -s 'prtot=${ncVarNameRain}+${ncVarNameSnow}' ${oriP} new${oriP}"
ncks -v prtot new${oriP} total_${oriP} && rm -f new${oriP}
eval "ncrename -v prtot,${ncVarNameRain} total_${oriP} && rm -f ${oriP}"

Friday, January 7, 2011

Write netcdf file using matlab native functions

Check the help page from Mathworks for new version Matlab
Please leave your comments to help improve the code in the future. Your help is greatly appreciated!

function SaveNcVarMatlab(ncfile,ncvar,MagicTag,myValue,myDType)
% to save a variable to a netcdf file using native matlab netcdf-functions
% Usage: SaveNcVarMatlab(nc-filename,nc-varname,MagicTag,myValue,[myDType])
% e.g.,
% 1: to save a 4D data
%     SaveNcVarMatlab('mask.nc','tmask',{'t','z','y','x'},4D-data,'float');
%     if the nc-file exist, the code will try to append data to old
%     file as long as the dimensions of input data is consistent
%     with existing one. If necessary, new dimensions will be added
%     automatically.
% 2: to add an attribute to an variable in existing nc file
%     SaveNcVarMatlab('mask.nc','tmask','att','long_name','mask at t-point')
%     Will overwrite existing one automatically. If no value set to
%     the attribute, the code will try to delete that attribute.
% 3: add/delete a global attribute, similar to the second example
%     but won't use ncvar here.
% Note:
%    Overwriting an existing variable is possible but disabled in
%    this version. Also unlimited dimension is not considered
%    here.
% copyright @ http://scriptdemo.blogspot.com
% ref: matlab netcdf help

if nargin<4 help SaveNcVarMatlab; return; end

if iscell(MagicTag)
    if nargin~=5
        help SaveNcVarMatlab
        return
    end
elseif ischar(MagicTag)
    if ischar(myValue)
       tmpstr=myValue; clear myValue
       myValue.name=tmpstr; clear tmpstr
       if nargin==5
          myValue.value=myDType;
       else
           myValue.value='';
       end
    elseif isstruct(myValue)
       if ~isfield(myValue,'name')
           help SaveNcVarMatlab;
           return;
       else
           if ~isfield(myValue,'value')
              if nargin==5
                 myValue.value=myDType;
              else
                 myValue.value='';
              end
           end
       end
    elseif ~isstruct(myValue)
        disp('Not valid value for myValue.')
        help SaveNcVarMatlab
        return
    end
end

% to support my work, please keep the "creator" global attribute
mycreatorstr='SaveNcVarMatlab.m from http://scriptdemo.blogspot.com';

if iscell(MagicTag) % add values
  [ncfid,IsAppend,OldNC,IsOldVar,OldVar]=CheckNCExist(ncfile,ncvar);
  if length(MagicTag)==1
      myValue=reshape(myValue,[],1);
      vardiminput=length(myValue);
  else
     vardiminput=size(myValue);
  end

  if IsAppend
     for nd=1:length(MagicTag)
        if sum(ismember(OldNC.dimname,MagicTag{nd}))==0
           myDim.name{nd}=MagicTag{nd};
           myDim.len(nd)=vardiminput(nd);
           myDim.IsNew(nd)=1;
        else
           dim_loc=find(ismember(OldNC.dimname,MagicTag{nd})==1);
           if OldNC.dimlen(dim_loc)~=vardiminput(nd)
              disp(['The length of input dimension ',MagicTag{nd},' is not the same as the exist one'])
              return
              % should consider unlimite dimension in the future
           else
              myDim.name{nd}=MagicTag{nd};
              myDim.ID(nd)=OldNC.dimid(dim_loc);
              myDim.IsNew(nd)=0;
           end
        end
     end
  else
     for nd=1:length(MagicTag)
         myDim.name{nd}=MagicTag{nd};
         myDim.len(nd)=vardiminput(nd);
         myDim.IsNew(nd)=1;
     end
  end
  if IsOldVar
     % support overwrite???
     netcdf.abort(ncfid); return
  else
     if IsAppend netcdf.reDef(ncfid); end
     % define some new dimension
     for nd=1:length(myDim.name)
         if myDim.IsNew(nd)
            myDim.ID(nd)=netcdf.defDim(ncfid,myDim.name{nd},myDim.len(nd));
         end
     end
     myvarid=netcdf.defVar(ncfid,ncvar,myDType,fliplr(myDim.ID));
     netcdf.putAtt(ncfid,netcdf.getConstant('NC_GLOBAL'),'creator',mycreatorstr);
     netcdf.endDef(ncfid);
     %netcdf.putVar(ncid,varid,start,count,stride,data)
     myValue=permute(myValue,length(size(myValue)):-1:1);
     netcdf.putVar(ncfid,myvarid,myValue);
  end

elseif ischar(MagicTag)
  [ncfid,IsAppend,OldNC,IsOldVar,OldVar]=CheckNCExist(ncfile,ncvar);
  if IsAppend
     switch lower(MagicTag)
       case {'a','attribute','var_attribute','vatt','att'}
         if IsOldVar
            OldVar=GetVAtts(ncfid,OldVar);
            netcdf.reDef(ncfid);
            if isfield(OldVar,'attname')
               if ~isstruct(myValue) %delete attribute
                   if sum(ismember(OldVar.attname,myValue))==1
                      netcdf.delAtt(ncfid,OldVar.varid,myValue);
                   else
                       disp(['attribute: ',myValue,' not exist. Nothing is done'])
                       netcdf.abort(ncfid); return;
                   end
               else
                  if sum(ismember(OldVar.attname,myValue.name))==1
                     %netcdf.delAtt(ncfid,OldVar.varid,myValue.name);
                     if isempty(myValue.value)
                        netcdf.delAtt(ncfid,OldVar.varid,myValue.name);
                     else
                        % overwrite old one automatically
                        netcdf.putAtt(ncfid,OldVar.varid,myValue.name,myValue.value);
                     end
                  else
                     %new attribute
                     if ~isempty(myValue.value)
                       netcdf.putAtt(ncfid,OldVar.varid,myValue.name,myValue.value);
                     end
                  end
               end
            else
              %new attribute
              if ~isempty(myValue.value)
                 netcdf.putAtt(ncfid,OldVar.varid,myValue.name,myValue.value);
              end
            end
         else
            disp(['var: ',ncvar,' isn''t exist in ',ncfile]);
            netcdf.abort(ncfid);
            return
         end
       case {'ga','global_attribute','gatt'}
         OldNC=GetGAtts(ncfid,OldNC);
         netcdf.reDef(ncfid);
         if ~isstruct(myValue)
             if isfield(OldNC,'attname')
                if sum(ismember(OldNC.attname,myValue))==1
                   netcdf.delAtt(ncfid,netcdf.getConstant('GLOBAL'),myValue);
                else
                   disp(['attribute: ',myValue,' not exist. Nothing is done'])
                   netcdf.abort(ncfid); return;
                end
             else
                disp('No global attributes available.')
                netcdf.abort(ncfid); return;
             end
         else
            if isfield(OldNC,'attname')
               if sum(ismember(OldNC.attname,myValue.name))==1
                  netcdf.delAtt(ncfid,netcdf.getConstant('GLOBAL'),myValue.name);
                  if ~isempty(myValue.value)
                     netcdf.putAtt(ncfid,netcdf.getConstant('NC_GLOBAL'),myValue.name,myValue.value);
                  end
               else
                  %new global attribute
                  if ~isempty(myValue.value)
                     netcdf.putAtt(ncfid,netcdf.getConstant('NC_GLOBAL'),myValue.name,myValue.value);
                  end
               end
            else
               % also new global attribute
               if ~isempty(myValue.value)
                  netcdf.putAtt(ncfid,netcdf.getConstant('NC_GLOBAL'),myValue.name,myValue.value);
               else
                  netcdf.abort(ncfid); return;
               end
            end
         end
       otherwise
           disp(['Not defined attribute type: MagicTag: ',MagicTag])
           netcdf.abort(ncfid); return;
     end
  else
     netcdf.abort(ncfid);
     disp([ncfile,' dos not exist, global attribute can''t be added.'])
     return
  end
  netcdf.putAtt(ncfid,netcdf.getConstant('NC_GLOBAL'),'creator',mycreatorstr);
  netcdf.endDef(ncfid);
else
  disp('Not valid value for MagicTag')
  return;
end

%close the file
netcdf.close(ncfid);
end

function [ncfid,IsAppend,OldNC,IsOldVar,OldVar]=CheckNCExist(ncfile,ncvar)
  IsAppend=0; IsOldVar=0;
  if exist(ncfile,'file')
     disp([ncfile,' already exist, try the append mode']);
     IsAppend=1;
  end

  % Open the nc file
  if IsAppend
     ncfid=netcdf.open(ncfile,'NC_WRITE');
     [OldNC.numdims, OldNC.numvars, OldNC.numGAtt, unlimdimID] = netcdf.inq(ncfid);
     for NDim=1:OldNC.numdims
        [OldNC.dimname{NDim}, OldNC.dimlen(NDim)] = netcdf.inqDim(ncfid,NDim-1);
        OldNC.dimid(NDim)=NDim-1;
     end
     for NVar=1:OldNC.numvars
        [tmpvarname, tmpvarxtype,tmpvardims, tmpvarnumatts] = netcdf.inqVar(ncfid,NVar-1);
        if strcmp(tmpvarname,ncvar)
            IsOldVar=1;
            OldVar.varid=NVar-1;
            OldVar.varname=tmpvarname;
            OldVar.varxtype=tmpvarxtype;
            OldVar.vardimids=tmpvardims;
            OldVar.numatts=tmpvarnumatts;
            break;
        end
     end
     if IsOldVar==0 OldVar=nan; end
  else
     ncfid=netcdf.create(ncfile,'NC_NOCLOBBER');
     OldNC=nan; OldVar=nan;
  end
end

function OldVar=GetVAtts(ncfid,OldVar)
  for numA=1:OldVar.numatts
      OldVar.attname{numA}=netcdf.inqAttName(ncfid,OldVar.varid,numA-1);
      % OldVar.attid(numA)=netcdf.inqAttID(ncfid,varid,OldVar.attname{numA});
  end
end
function OldNC=GetGAtts(ncfid,OldNC)
  for numA=1:OldNC.numGAtt
      OldNC.attname{numA}= netcdf.inqAttName(ncfid,netcdf.getConstant('NC_GLOBAL'),numA-1);
  end
end

Monday, January 3, 2011

[UPDATED] read netcdf file using native matlab functions

check the ncread for new version matlab from Mathworks. 
 
The attributes of scale factor, offset value and missing value are considered in this version.

function ncvardata=GetNcVarMatlab(ncfile,ncvar,RangeStr)
% to read a variable from the netcdf file using native matlab netcdf-functions
% Usage: vardata=GetNcVarMatlab(nc-filename,nc-varname,Dimension-Range)
% e.g.,
%        mydata=GetNcVarMatlab('mask.nc','tmask','1:1,1:46,1:400,1:568');
%                      RangeStr here is similar to the output of ncdump
% note: by default, variable will be converted into the class of double,
%                      and the missing value will be converted into nan.
% copyright @ http://scriptdemo.blogspot.com


IsDebug=0;
IsMissingNaN=1; % convert missing values into NaN
if ~exist(ncfile,'file')
   error([ncfile,' can not been found']);
   return
end

ncfid=netcdf.open(ncfile,'NC_NOWRITE');
% get dimension length
[numdims, numvars, numglobalatts, unlimdimID] = netcdf.inq(ncfid);
for NDim=1:numdims
   [dimname{NDim}, dimlen(NDim)] = netcdf.inqDim(ncfid,NDim-1);
end

%var infor
varid=netcdf.inqVarID(ncfid,ncvar);
[varname,varxtype,vardimids,NumAtts]=netcdf.inqVar(ncfid,varid);
numdims=length(vardimids); % some variables may have less dimensions

%check scale_factor & off_set
IsRescale=0; IsMissing=0;
myScale=1; myOffSet=0.0;
for numA=1:NumAtts
    tmpattname=netcdf.inqAttName(ncfid,varid,numA-1);
    switch lower(tmpattname)
       case {'scale_factor','scalefactor'}
            IsRescale=1;
            myscale=netcdf.getAtt(ncfid,varid,tmpattname);
       case {'add_offset','off_set','offset'}
            IsRescale=1;
            myoffset=netcdf.getAtt(ncfid,varid,tmpattname);
       case {'missing_value','missingvalue','missing'}
            IsMissing=1;
            mymissing=netcdf.getAtt(ncfid,varid,tmpattname);
       otherwise
            % do nothing
    end
end
if IsRescale==1
   if (myscale==1 & myoffset==0)
      IsRescale=0;
   end
else
   if ~exist('myscale','var') myscale=1; end
   if ~exist('myoffset','var') myoffset=0; end
end

if ~exist('RangeStr','var')
   RangeStr=':';
   for nd=2:numdims
       RangeStr=[RangeStr,',:'];
   end
end

IndC=strfind(RangeStr,',');% index of commas
if length(IndC)~=numdims-1
   disp(['Input dimension range is not correct, should be: ',num2str(numdims),'-d']);
   return
end

startstr=''; countstr=''; stridestr='';
for nn=1:length(IndC)+1
    if nn==1
       tmpstr=RangeStr(1:IndC(1)-1);
    elseif nn<numdims
       tmpstr=RangeStr(IndC(nn-1)+1:IndC(nn)-1);
    else
       tmpstr=RangeStr(IndC(nn-1)+1:end);
    end

    [tmpstart,tmpcount,tmpstride]=GetIndFromStr(tmpstr,':',dimlen(vardimids(numdims-nn+1)+1));
    if nn==1
        startstr=[num2str(tmpstart),'],'];
        countstr=[num2str(tmpcount),'],'];
        stridestr=[num2str(tmpstride),']'];
    elseif nn==numdims
        startstr=[',[',num2str(tmpstart),',',startstr];
        countstr=['[',num2str(tmpcount),',',countstr];
        stridestr=['[',num2str(tmpstride),',',stridestr];
    else
        startstr=[num2str(tmpstart),',',startstr];
        countstr=[num2str(tmpcount),',',countstr];
        stridestr=[num2str(tmpstride),',',stridestr];
    end
end

if IsDebug
   disp(['ncvardata=netcdf.getVar(ncfid,varid',startstr,countstr,stridestr,');']);
end
eval(['ncvardata=netcdf.getVar(ncfid,varid',startstr,countstr,stridestr,');']);
netcdf.close(ncfid);
ncvardata=permute(ncvardata,length(size(ncvardata)):-1:1);

if IsRescale==1
   %convert all data type into double
   if ~strcmp(class(ncvardata),'double')
      ncvardata=double(ncvardata);
   end
   if IsMissing
      if IsMissingNaN==1; % convert missing values into NaN
         ncvardata(ncvardata==mymissing)=nan;
         ncvardata=ncvardata*double(myscale)+double(myoffset);
      else
         disp(['missing value for ',ncvar,' is ',num2str(mymissing)]);
         %IndValid=find(ncvardata~=mymissing); length(IndValid)
         %ncvardata(IndValid)=ncvardata(IndValid)*double(myscale)+double(myoffset);
         ncvardata(ncvardata~=mymissing)=ncvardata(ncvardata~=mymissing)*double(myscale)+double(myoffset);
      end
   else
      ncvardata=ncvardata*double(myscale)+double(myoffset);
   end
else
  ncvardata=double(ncvardata);
end

function [numstart,numcount,numstride]=GetIndFromStr(tmpstr,ctag,Ndim)
% Read the numbers from the special string
IsDebug=0;
IndC=strfind(tmpstr,ctag);
if isempty(IndC)
   numstart=str2double(tmpstr)-1;
   numcount=1;
   numstride=1;
else
  switch length(IndC)
      case {1}
          switch IndC
            case {1} % : and :num
                if strcmp(tmpstr,':')
                   numstart=0; numcount=Ndim; numstride=1;
                else
                   if strcmp(tmpstr(2:end),'end')
                    numstart=0; numcount=Ndim; numstride=1;
                   else
                    numstart=0; numcount=str2double(tmpstr(2:end))-1; numstride=1;
                   end
                end
            case {length(tmpstr)}
                if strcmp(tmpstr,':')
                   numstart=0; numcount=Ndim; numstride=1;
                   return
                else
                   numstart=str2double(tmpstr(1:IndC-1))-1;
                   numcount=Ndim-numstart;
                   numstride=1;
                end
            otherwise
               numstart=str2double(tmpstr(1:IndC-1))-1;
               if strcmp(tmpstr(IndC+1:end),'end')
                  numend=Ndim;
               else
                  numend=str2double(tmpstr(IndC+1:end));
               end
               numcount=numend-numstart;
               numstride=1;
          end
      case {2}
          % num0:stride:num1
          if IndC(1)==1
             numstart=0;
          else
             numstart=str2double(tmpstr(1:IndC(1)-1))-1;
          end
          if IndC(2)==IndC(1)+1
             numstride=1;
          else
             numstride=str2double(tmpstr(IndC(1)+1:IndC(2)-1));
          end
          if IndC(2)==length(tmpstr)
                 numend=Ndim-1;
          else
            if strcmp(tmpstr(IndC(2)+1:end),'end')
               numend=Ndim-1;
            else
               numend=str2double(tmpstr(IndC(2)+1:end))-1;
            end
          end
          numcount=floor((numend-numstart)/numstride)+1;
          numend=numstart+(numcount-1)*(numStride);
      otherwise
          error('Not defined range type')
  end
end
if IsDebug
   disp(['tmpstr=',tmpstr])
   disp(['start:',num2str(numstart), ' stride:',num2str(numstride),' count:',num2str(numcount)])
end

Wednesday, December 22, 2010

read netcdf file using native matlab functions

UPDATED VERSION:  Here

function ncvardata=GetNcVarMatlab(ncfile,ncvar,RangeStr)
% to read data from the netcdf file using native matlab netcdf-functions
% Usage: vardata=GetNcVarMatlab(nc-filename,nc-varname,Dimension-Range)
% e.g.,
% mydata=GetNcVarMatlab('mask.nc','tmask','1:1,1:46,1:400,1:568');
% RangeStr here is similar to the output of ncdump
% copyright by http://scriptdemo.blogspot.com

IsDebug=0;
if ~exist(ncfile,'file')
   error([ncfile,' can not been found']);
   return
end

ncfid=netcdf.open(ncfile,'NC_NOWRITE');
% get dimension length
[numdims, numvars, numglobalatts, unlimdimID] = netcdf.inq(ncfid);
for NDim=1:numdims
      [dimname{NDim}, dimlen(NDim)] = netcdf.inqDim(ncfid,NDim-1);
end

%var infor
varid=netcdf.inqVarID(ncfid,ncvar);
[varname,varxtype,vardimids,varnumatts]=netcdf.inqVar(ncfid,varid);
numdims=length(vardimids); % some variables may have less dimensions

if ~exist('RangeStr','var')
   RangeStr=':';
   for nd=2:numdims
         RangeStr=[RangeStr,',:'];
   end
end

IndC=strfind(RangeStr,',');% index of commas
if length(IndC)~=numdims-1
   disp(['Input dimension range is not correct, should be: ',num2str(numdims),'-d']);
   return
end

startstr=''; countstr=''; stridestr='';
for nn=1:length(IndC)+1
      if nn==1
         tmpstr=RangeStr(1:IndC(1)-1);
      elseif nn<numdims
         tmpstr=RangeStr(IndC(nn-1)+1:IndC(nn)-1);
      else
         tmpstr=RangeStr(IndC(nn-1)+1:end);
      end

      [tmpstart,tmpcount,tmpstride]=GetIndFromStr(tmpstr,':',dimlen(vardimids(numdims-nn+1)+1));
     if nn==1
        startstr=[num2str(tmpstart),'],'];
        countstr=[num2str(tmpcount),'],'];
        stridestr=[num2str(tmpstride),']'];
     elseif nn==numdims
        startstr=[',[',num2str(tmpstart),',',startstr];
        countstr=['[',num2str(tmpcount),',',countstr];
        stridestr=['[',num2str(tmpstride),',',stridestr];
     else
        startstr=[num2str(tmpstart),',',startstr];
        countstr=[num2str(tmpcount),',',countstr];
        stridestr=[num2str(tmpstride),',',stridestr];
     end
end

if IsDebug
   disp(['ncvardata=netcdf.getVar(ncfid,varid',startstr,countstr,stridestr,');']);
end
eval(['ncvardata=netcdf.getVar(ncfid,varid',startstr,countstr,stridestr,');']);
netcdf.close(ncfid);
ncvardata=permute(ncvardata,length(size(ncvardata)):-1:1);

function [numstart,numcount,numstride]=GetIndFromStr(tmpstr,ctag,Ndim)
% Read the numbers from the special string
IsDebug=0;
IndC=strfind(tmpstr,ctag);
if isempty(IndC)
   numstart=str2double(tmpstr)-1;
   numcount=1;
   numstride=1;
else
  switch length(IndC)
    case {1}
        switch IndC
            case {1} % : and :num
                if strcmp(tmpstr,':')
                   numstart=0; numcount=Ndim; numstride=1;
                else
                   if strcmp(tmpstr(2:end),'end')
                      numstart=0; numcount=Ndim; numstride=1;
                   else
                      numstart=0; numcount=str2double(tmpstr(2:end))-1; numstride=1;
                   end
                end
            case {length(tmpstr)}
                if strcmp(tmpstr,':')
                    numstart=0; numcount=Ndim; numstride=1;
                    return
                else
                    numstart=str2double(tmpstr(1:IndC-1))-1;
                    numcount=Ndim-numstart;
                    numstride=1;
                end
           otherwise
                numstart=str2double(tmpstr(1:IndC-1))-1;
                if strcmp(tmpstr(IndC+1:end),'end')
                    numend=Ndim;
                else
                    numend=str2double(tmpstr(IndC+1:end));
                end
                numcount=numend-numstart;
                numstride=1;
            end
   case {2}
      % num0:stride:num1
      if IndC(1)==1
         numstart=0;
      else
         numstart=str2double(tmpstr(1:IndC(1)-1))-1;
      end
      if IndC(2)==IndC(1)+1
         numstride=1;
      else
         numstride=str2double(tmpstr(IndC(1)+1:IndC(2)-1));
      end
      if IndC(2)==length(tmpstr)
             numend=Ndim;
      else
         if strcmp(tmpstr(IndC(2)+1:end),'end')
            numend=Ndim;
         else
            numend=str2double(tmpstr(IndC(2)+1:end));
         end
      end
      numcount=numend-numstart;
   otherwise
       error('Not defined range type')
   end
end
if IsDebug
   disp(['tmpstr=',tmpstr])
   disp(['start:',num2str(numstart), ' stride:',num2str(numstride),' count:',num2str(numcount)])
end

ShowCalendar