Thursday, September 5, 2013

[MATLAB] read the byte-binary ice concentration data from NSIDC

function myice=readgsfcice(fname,fnamemask)
% read the binary ice concentration data from gsfc
% similar to the fortran version here
% data description:
%     http://nsidc.org/data/docs/daac/nsidc0051_gsfc_seaice.gd.html
% data :
%     ftp://sidads.colorado.edu/pub/DATASETS/nsidc0051_gsfc_nasateam_seaice/final-gsfc/north/
% mask :
%     ftp://sidads.colorado.edu/pub/DATASETS/brightness-temperatures/polar-stereo/tools/masks/
% usage:
%     myice=readgsfcice(ice_filename, mask_filename)
%     myice: ranging 0 to 1, land value is nan if the mask file is specified
%     http://scriptdemo.blogspot.com
 

isMask=1;
if nargin<1
   help readgsfcice
   return
elseif nargin==2
   if ~exist(fnamemask,'file')
      disp(['mask file not found: ',fnamemask])
      isMask=0;
   end
end
if nargin==1
   isMask=0;
end
if ~exist(fname,'file')
   error(['file not found: ',fname])
end

% read the data
fid=fopen(fname,'r');
% read the header
myheader=(char(fread(fid,300,'uchar')))';
nrow=str2num(myheader(13:18));
ncol=str2num(myheader(7:12));
myscale=str2num(myheader(121:126));
%read data block
myice=fread(fid,'uint8');
fclose(fid);

% read the mask file
if isMask==1
   fid=fopen(fnamemask,'r');
   mymask=fread(fid,'uint8');
   fclose(fid);
   myice(mymask==1)=nan;
end
myice=reshape(myice,ncol,nrow)/myscale;

No comments:

ShowCalendar