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 |
Saturday, February 25, 2012
[Matlab] show the variable information from a netcdf file
Labels:
dimension,
header,
Matlab,
nc,
ncdump,
netcdf,
offset,
scale factor,
variable name
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment