Friday, August 19, 2011

[Processing] spiral demo using Processing

//@ http://scriptdemo.blogspot.com
float myAngle, myScale, myAlpha0, myAlpha;
float shiftX, shiftY;
int rr,gg,bb;
// to control the shape of spiral
float minScale, dAngle, coefScale;
boolean isLine=false;
boolean isColor=true;

void setup()
{
  size(500,500);
  shiftX=width/2;
  shiftY=height/2;
  myAngle=0;
  myScale=1.0;
  minScale=0.01;
  coefScale=0.99;
  myAlpha0=8;
  dAngle=PI/50;

  noStroke();
  background(255);
  frameRate(30);
  smooth();
}

void draw()
{
   while(myScale>minScale)
   {
      pushMatrix();
      myAngle=myAngle+dAngle;
      myScale=myScale*coefScale;
      translate(shiftX,shiftY);
      rotate(myAngle);
      scale(myScale);
      if (isColor)
      {
         rr=ceil(255*sin(myAngle));
         gg=ceil(255*cos(myAngle));
         if (rr<=0) rr=rr+255;
         if (gg<=0) gg+=255;
         bb=(rr+gg)%256;
      } else {
        rr=10; gg=10; bb=10;
        if (isLine) myAlpha0=max(myAlpha0,20);
      }
      myAlpha=min(myAlpha0*(ceil(myAngle/TWO_PI)),255);
      if (isLine)
      {
        stroke(rr,gg,bb,myAlpha);
        noFill();
        line(-1*shiftX,shiftY,shiftX,shiftY);
      } else {
        noStroke();
        fill(bb,gg,rr,myAlpha);
        rect(-1*shiftX,shiftY,width,height);
      }
      popMatrix();
   }
}

void mouseClicked()
{
  isLine=!isLine;
  myAlpha=myAlpha0;
  myScale=1;
  myAngle=random(-PI,PI);
  background(255);
}
void mouseDragged()
{
   isColor=!isColor;
   myAlpha=myAlpha0;
   myScale=1;
   myAngle=random(-PI,PI);
   background(255);
}

[Processing] Transformation example in Processing

2D Transformations example:Online Demo
//@ http://scriptdemo.blogspot.com/
int numP;
myrose[] roses;
float rotAngle, myscale;
float shiftX, shiftY;

void setup()
{
  size(500,500);
  numP=36;
  smooth();
  roses=new myrose[numP];
  myscale=1;
  rotAngle=0;
  shiftX=width/2;
  shiftY=height/2;
  frameRate(30);
  for (int n=0; n<numP; n++)
  {
    roses[n]=new myrose();
  }
  noStroke();
}
void draw()
{
  background(255);
  translate(shiftX,shiftY);
  if (!mousePressed)
  {
    rotAngle=rotAngle+PI/100;
  }
  rotate(rotAngle);
  scale(myscale);
  pushMatrix();
  for (int n=0; n<numP; n++)
  {
   roses[n].myrose();
   roses[n].setangle(n*TWO_PI/numP);
   roses[n].showit();
  }
  popMatrix();
}

class myrose
{
  float myangle;
  int nres=20;
  float[] xx=new float[nres*2+1];
  float[] yy=new float[nres*2+1];
  float rr=0.4*width;
  float rr05=rr/2;
  float rry=0.1*rr;

  void myrose()
  {
     for (int n=-nres; n<=nres; n++)
     {
        xx[n+nres]=rr05+n*rr05/nres;
        yy[n+nres]=rry*sin(n*TWO_PI/nres);
     }
  }
  void showit()
  {
    pushMatrix();
    rotate(myangle);
    for (int n=0; n<xx.length; n++)
    {
      fill(n*255/xx.length,127);
      ellipse(xx[n],yy[n],5,5);
    }
    popMatrix();
  }
  void setangle(float ang)
  {
    myangle=ang;
  }
}

void mouseDragged()
{
  if (mouseButton==CENTER)
  {
    shiftX+=(mouseX-pmouseX);
    shiftY+=(mouseY-pmouseY);
  } else if (mouseButton==LEFT) {
    float d0=(pmouseX-width/2)*(pmouseX-width/2)+(pmouseY-height/2)*(pmouseY-height/2);
    float dc=(mouseX-width/2)*(mouseX-width/2)+(mouseY-height/2)*(mouseY-height/2);
    myscale=(sqrt(dc)-sqrt(d0));
  }
}

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}"

ShowCalendar