! Ferret virtual world generator
!
! Al Hermann and Chris Moore 10/22/98
!
! this script writes out a wrl file for four variables 
! variables are assumed to be on a regular grid with evenly spaced x 
! and evenly spaced y (need not be the same spacing in x and y)
!
! z is positive downwards so bathymetry values (increasing with depth)
! will appear in the usual oceanographic way
!
! you may want to scale the variables to get a nice surface;
! try making the range in height about the same as the range of the x-axis 
! for a pleasing result
!
! usage: go fer_vrml wrlname var1 var1col var2 var2col 
!
! NOTE: Be sure to explicitly set a region before you invoke this script
!
! wrlname = name of your world - i.e. you get a file called "wrlname.wrl"
! for subsequent viewing by a browser with the appropriate plug-in
! such as the Cosmo Viewer on a PC
!
! var1 - first variable to render as a surface height
! var1col - a variable to color the first surface 
!           (if blank then you get a red surface)
! var2 - second variable to render as a surface height
! var2col - a variable to color the second surface 
!           (if blank then you get a red surface)
! 
let/quiet ii=i
let/quiet jj=j
let/quiet xx=x
let/quiet yy=y
let/quiet imax = ii[i=@max]
let/quiet imin = ii[i=@min]
let/quiet jmax = jj[j=@max]
let/quiet jmin = jj[j=@min]
let/quiet xmax = xx[i=@max]
let/quiet xmin = xx[i=@min]
let/quiet ymax = yy[j=@max]
let/quiet ymin = yy[j=@min]

let/quiet xd = imax-imin+1
let/quiet zd = jmax-jmin+1
let/quiet dx = (xmax-xmin)/(imax-imin)
let/quiet dz = (ymax-ymin)/(jmax-jmin)
let/quiet xsize= (xd-1)*dx
let/quiet zsize= (zd-1)*dz
let/quiet xmid = xsize/2.
let/quiet zmid = -1.*zsize/2.

sp rm $1.wrl
sp echo "#VRML V2.0 utf8" | cat >> $1.wrl
sp echo "# END_HEADER" | cat >> $1.wrl

sp echo "Viewpoint {" | cat >> $1.wrl
sp echo "  position `xmid` 0 `zsize` " | cat >> $1.wrl 
sp echo "  orientation 1.0 0.0 0.0 -0. # rotating -.0 radians around x axis" | cat >> $1.wrl
sp echo '  description "look north"' | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl

sp echo "Viewpoint {" | cat >> $1.wrl
sp echo "  position `-1.*xsize` 0 `zmid` " | cat >> $1.wrl
sp echo "  orientation 0 1.0 0. -1.541 # rotating pi/2 radians around y axis" | cat >> $1.wrl
sp echo '  description "look east"' | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl

sp echo "Viewpoint {" | cat >> $1.wrl
sp echo "  position `xmid` 0 `-2.*zsize`" | cat >> $1.wrl
sp echo "  orientation 0 1.0 0. -3.1416 # rotating pi/2 radians around y axis" | cat >> $1.wrl
sp echo '  description "look south"' | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl

sp echo "Viewpoint {" | cat >> $1.wrl
sp echo "  position `2.*xsize` 0 `zmid`" | cat >> $1.wrl
sp echo "  orientation 0 1.0 0. -4.682 # rotating 3/2 pi radians around y axis" | cat >> $1.wrl
sp echo '  description "look west"' | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl

sp echo "Viewpoint {" | cat >> $1.wrl
sp echo "  position `xmid` `3.*xsize` `zmid`" | cat >> $1.wrl
sp echo "  orientation 1.0 0. 0. -1.541 # rotating pi/4 radians around x axis" | cat >> $1.wrl
sp echo '  description "look down"' | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl

sp echo "        DirectionalLight {" | cat >> $1.wrl
sp echo "                on              TRUE" | cat >> $1.wrl
sp echo "                intensity       0.50" | cat >> $1.wrl
sp echo "                color           1 1 1" | cat >> $1.wrl         
sp echo "                direction       -1. -1. -0." | cat >> $1.wrl
sp echo "        } #Directional Light  " | cat >> $1.wrl  

sp echo "Transform {" | cat >> $1.wrl
sp echo "translation `xmid` 0 0" | cat >> $1.wrl 
sp echo "children [" | cat >> $1.wrl
sp echo "Shape {" | cat >> $1.wrl
sp echo " appearance Appearance { material Material {" | cat >> $1.wrl
sp echo "	diffuseColor 1.0 0.0 0.0}}" | cat >> $1.wrl
sp echo " geometry Box { size `xsize` `xsize*.01` `xsize*.01` }" | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl
sp echo "]" | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl

sp echo "Transform {" | cat >> $1.wrl
sp echo "translation 0 `-1.*xmid` 0" | cat >> $1.wrl
sp echo "children [" | cat >> $1.wrl
sp echo "Shape {" | cat >> $1.wrl
sp echo " appearance Appearance { material Material {" | cat >> $1.wrl
sp echo "	diffuseColor 0.0 0.0 1.0}}" | cat >> $1.wrl
sp echo " geometry Box { size `xsize*.01` `xsize` `xsize*.01` }" | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl
sp echo "]" | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl

sp echo "Transform {" | cat >> $1.wrl
sp echo "translation 0 0 `zmid`" | cat >> $1.wrl
sp echo "children [" | cat >> $1.wrl
sp echo "Shape {" | cat >> $1.wrl
sp echo " appearance Appearance { material Material {" | cat >> $1.wrl
sp echo "	diffuseColor 0.0 1.0 0.0}}" | cat >> $1.wrl
sp echo " geometry Box { size `zsize*.01` `zsize*.01` `zsize` } " | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl
sp echo "]" | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl

sp echo "Transform {" | cat >> $1.wrl
sp echo "translation 0 0 0" | cat >> $1.wrl
sp echo "rotation 1. 0. 0. 3.14159" | cat >> $1.wrl
sp echo "children [" | cat >> $1.wrl
sp echo "Shape {" | cat >> $1.wrl
sp echo "appearance Appearance {" | cat >> $1.wrl
sp echo "        material Material {" | cat >> $1.wrl
sp echo "          diffuseColor 0.8 0.8 0.8" | cat >> $1.wrl
sp echo "          specularColor 0.2 0.2 0.2" | cat >> $1.wrl
sp echo "		transparency 0." | cat >> $1.wrl
sp echo "        }" | cat >> $1.wrl
sp echo "      }" | cat >> $1.wrl
sp echo "  geometry ElevationGrid {" | cat >> $1.wrl
sp echo "         solid FALSE" | cat >> $1.wrl
sp echo "	 xDimension `xd`" | cat >> $1.wrl
sp echo "	 zDimension `zd`" | cat >> $1.wrl
sp echo "	 xSpacing `dx`" | cat >> $1.wrl
sp echo "	 zSpacing `dz`" | cat >> $1.wrl
sp echo "	 creaseAngle 0.785" | cat >> $1.wrl

sp echo "	 height  [" | cat >> $1.wrl

list/nohead/append/file=$1.wrl/format=(e13.6,",") $2

sp echo "]" | cat >> $1.wrl

sp echo "	colorPerVertex TRUE" | cat >> $1.wrl
sp echo "	color Color {" | cat >> $1.wrl
sp echo "	color[" | cat >> $1.wrl
let colvar= $3"0.*$2"
let colvarmin=colvar[i=`imin`:`imax`@min,j=`jmin`:`jmax`@min]
let colvarmax=colvar[i=`imin`:`imax`@max,j=`jmin`:`jmax`@max]
let colsc =(colvar-colvarmin+1.e-10)/(colvarmax-colvarmin+1.e-10)
let ramp = 1.
let bamp = ramp
let gamp = ramp
let blue = if (colsc lt .5) then bamp*(1.) else bamp*(1.-2.*(colsc-.5))
let green= gamp*(1.-2.*abs(colsc-0.5))
let red= if (colsc gt .5) then ramp*(1.) else ramp*(2.*(colsc-0.))
list/nohead/append/file=$1.wrl/format=(3f5.2,",") missing(red,0.),missing(green,0.),missing(blue,0.)
sp echo "]" | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl

sp echo "}" | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl
sp echo "]" | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl

sp echo "Transform {" | cat >> $1.wrl
sp echo "translation 0 0 0" | cat >> $1.wrl
sp echo "rotation 1. 0. 0. 3.14159" | cat >> $1.wrl
sp echo "children [" | cat >> $1.wrl
sp echo "Shape {" | cat >> $1.wrl
sp echo "appearance Appearance {" | cat >> $1.wrl
sp echo "        material Material {" | cat >> $1.wrl
sp echo "          diffuseColor .8 .8 .8" | cat >> $1.wrl
sp echo "          specularColor 0.2 0.2 0.2" | cat >> $1.wrl
sp echo "          transparency 0."  | cat >> $1.wrl
sp echo "        }" | cat >> $1.wrl
sp echo "      }" | cat >> $1.wrl
sp echo "  geometry ElevationGrid {" | cat >> $1.wrl
sp echo "         solid FALSE" | cat >> $1.wrl
sp echo "	 xDimension `xd`" | cat >> $1.wrl
sp echo "	 zDimension `zd`" | cat >> $1.wrl
sp echo "	 xSpacing `dx`" | cat >> $1.wrl
sp echo "	 zSpacing `dz`" | cat >> $1.wrl
sp echo "	 creaseAngle 0.785" | cat >> $1.wrl
sp echo "	 height  [" | cat >> $1.wrl

list/nohead/append/file=$1.wrl/format=(e13.6,",") $4

sp echo "]" | cat >> $1.wrl

sp echo "	colorPerVertex TRUE" | cat >> $1.wrl
sp echo "	color Color {" | cat >> $1.wrl
sp echo "	color[" | cat >> $1.wrl
let colvar2= $5"0.*$4"
let colvarmin2=colvar2[i=`imin`:`imax`@min,j=`jmin`:`jmax`@min]
let colvarmax2=colvar2[i=`imin`:`imax`@max,j=`jmin`:`jmax`@max]
let colsc2 =(colvar2-colvarmin2+1.e-10)/(colvarmax2-colvarmin2+1.e-10)
let ramp = 1.
let bamp = ramp
let gamp = ramp
let blue2 = if (colsc2 lt .5) then bamp*(1.) else bamp*(1.-2.*(colsc2-.5))
let green2= gamp*(1.-2.*abs(colsc2-0.5))
let red2= if (colsc2 gt .5) then ramp*(1.) else ramp*(2.*(colsc2-0.))
list/nohead/append/file=$1.wrl/format=(3f5.2,",") missing(red2,0.),missing(green2,0.),missing(blue2,0.)
sp echo "]" | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl

sp echo "}" | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl
sp echo "]" | cat >> $1.wrl
sp echo "}" | cat >> $1.wrl

exit