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;

[Fortran] dump the byte-binary ice concentration data from NSIDC

! @ http://scriptdemo.blogspot.com
! tested on  Linux 2.6.32.26-175.fc12.i686
! convert the byte-binary ice-concentration data file from NSIDC to ascii text format
! data description:
!         http://nsidc.org/data/docs/daac/nsidc0051_gsfc_seaice.gd.html
! data download:
!         ftp://sidads.colorado.edu/pub/DATASETS/nsidc0051_gsfc_nasateam_seaice/final-gsfc/

program dumpGSFCIce
integer, parameter                    :: lenHeader=300
character(len=40)                     :: fname, myfmt, txtfname
character(len=lenHeader)          :: header
integer                                    :: numLen, j,fid,npos, nrow, ncol, getnum, myscale,nlen
character(len=1),allocatable,dimension(:)    :: dblock
integer, allocatable,dimension(:)                 :: myice
logical                                                      :: isDebug=.false.

write(*,*) 'input the bin file name to open:'
read(*,'(a)') fname
write(*,*) 'input the txt file name to save:'
read(*,'(a)') txtfname

fid=7
open(fid,file=trim(fname),form='unformatted',access='direct',recl=4*lenHeader)
read(fid,rec=1) header
close(fid)

ncol=getnum(header(7:12))
nrow=getnum(header(13:18))
myscale=getnum(header(121:126))
if (isDebug) then
   write(*,*) '------------------ begin file header info ------------------------'
   write(*,'(a20,a)') ' file name: ', header(127:150)
   write(*,'(a20,a)') ' image title: ', header(151:230)
   write(*,'(a20,a)') ' year: ', header(103:108)
   write(*,'(a20,a)') ' julian day: ', header(109:114)
   write(*,'(a20,a)') ' scale factor: ', header(121:126)
   write(*,'(a20,a)') ' instrument: ', header(55:60)
   write(*,'(a20,a)') ' other info: ', header(231:300)
   write(*,'(a20,i0)')' num of col: ', ncol
   write(*,'(a20,i0)')' num of row: ', nrow
   write(*,'(a20,i0)')' scale factor: ', myscale
   write(*,*) '------------------ end file header info ------------------------'
endif

! declaration
nlen=ncol*nrow
numLen=lenHeader+nlen
allocate(dblock(numLen))
allocate(myice(nlen))

open(fid,file=trim(fname),form='unformatted',access='direct',recl=4*numLen)
read(fid,rec=1) dblock(:)
close(fid)

! convert the 1-byte char to integer
npos=lenHeader
Do j=1,nlen
     npos=npos+1
     myice(j)=ichar(dblock(npos))
Enddo
deallocate(dblock)

! write the file
write(myfmt,'(a,i0,a)') '(',ncol,'i4)'
open(fid,file=trim(txtfname),form='formatted')
write(fid,trim(myfmt)) myice
close(fid)
deallocate(myice)
end program dumpGSFCIce

function getnum(instr)
character(len=6), intent(in) :: instr
character(len=6) :: newstr
integer :: getnum
integer :: i, n
logical :: isnum
n=0
Do i=1,6
     if (isnum(instr(i:i))) then
        n=n+1
        newstr(n:n)=instr(i:i)
     endif
Enddo

if (n==0) then
   getnum=0
else
   read(newstr(1:n),*) getnum
endif
end function getnum

function isnum(myc)
character(len=1), intent(in) :: myc
logical :: isnum
isnum=.false.
if ( (ichar(myc).ge.ichar('0')) .and. (ichar(myc).le.ichar('9'))) isnum=.true.
end function isnum

Wednesday, June 19, 2013

[Bash] text to html using vim built-in function TOhtml

#!/bin/bash
# convert a text file to html using vim built-in function TOhtml
# usage:
#            toHTML text-files
# http://scriptdemo.blogspot.com

if [ $# -eq 0 ]; then
   sed -n '3,4p' `which toHTML`
   exit
else
   for mytxt in $*
   do
      if [ -e ${mytxt} ]; then
         vim -c 'syntax on|TOhtml' -c 'wq|q!' ${mytxt}
      else
         echo "${mytxt} is not found"
      fi
   done
fi

Friday, May 3, 2013

[Matlab] remove the water bodies in a m_map figure

function m_nolakes(lakeColor)
% delete the water bodies patches in figures created by m_map
%           default lakeColor is white ([1 1 1])
% usage:
%           m_nolakes(lakeColor)
%           http://scriptdemo.blogspot.com

if nargin==0
   lakeColor=[1 1 1];
elseif nargin~=1
   help m_nolakes
   return
end

gshhstypes={'c','l','i','h','f'};
for nt=1:numel(gshhstypes)
    eval(['hw=findobj(gcf,''type'',''patch'',''tag'',''m_gshhs_',gshhstypes{nt},''',''facecolor'',lakeColor);']);
    if ~isempty(hw)
       delete(hw);
    end
end
hw=findobj(gcf,'type','patch','tag','m_coast','facecolor',lakeColor);
if ~isempty(hw)
   delete(hw);
end

Sunday, April 7, 2013

[Bash] extractBib: extract bib items from a source bibtex database for a given tex file

#!/bin/bash
# extract bib items from a source bibtex database for a given tex file
# usage:
#          extractBib tex-file bibtex-database output-bibtex-file
# e.g.,
#          extractBib mypaper.tex mybibtexdatabase.bib mypaper.bib
# http://scriptdemo.blogspot.com

function getkeys()
{
   texfile=$1
   mykeys=`grep "cite.*{.*}" ${texfile} | sed -e 's/\ //g' | awk '{for (i=1; i<=NF;i++) {if ($i ~ /cite/) {print $i}}}' | sed -e 's/\}/\n/g' | awk -F\{ '{print $2}' | sed -e 's/\,/\n/g' | sed '/^[\ ]*[\ ]*$/d' | sort | uniq | sed '/^\\\\/d' | sed '/:/d'` 
   echo ${mykeys}
}

if [ $# -eq 3 ]; then
   texfile=$1; bibfile=$2; outfile=$3
elif [ $# -eq 2 ]; then
   texfile=$1; bibfile=$2; outfile="extractbib_"$1
else
   echo "usage:"
   echo "          extractBib tex-file bibtex-database output-bibtex-file"
   exit
fi

# make sure no overwrite
[ -e ${outfile} ] && echo "${outfile} exists alreay! exit" && exit

allkeys=`getkeys ${texfile}`
if [ ${#allkeys[*]} -ne 0 ]; then
   touch ${outfile}
   for eachkey in ${allkeys[*]}
   do
       #echo ${eachkey}
       sed -ne "/${eachkey}/,/^[\ ]*\}$/p" ${bibfile} >> ${outfile}
   done
fi

Thursday, February 14, 2013

[Matlab] show a normal random heart

% Just for fun, not really useful....
% @ http://scriptdemo.blogspot.com 
clear; close all;
r=0.618; n=10000; re=sqrt(1-r*r);
x=normrnd(0,1,n,1);
y=x*r+normrnd(0,1,n,1)*re;y(x<0)=-y(x<0);
plot(x,y,'o','markerEdgeColor',[1 0 1],'markerFaceColor','none');
axis equal tight off;
set(gcf,'MenuBar','none','color','w')

ShowCalendar