Perhaps there's is a better, more official way of filling an array, but
I do it in the way as shown in the two attached scripts.
The first (FillArray) to fill a 1-dimensional array, and the second
(FillArray2D) to fill a 2-dimensional array. These are dx scripts,
so you can only run then from the command line with
dx -script FillArray.net
or
dx -script FillArray2D.net
The last script uses a macro which I created to plot fields in one call
(show_plane). This macro can only be processed in script mode, and should
be put in your current directory, or somewhere in a path defined by
the DXMACROS environment variable. It is not necessary for filling arrays!
In your case you should of coarse read your value from the current file
in the Fill loop where I calculate a value from the current index.
Hopefully this helps.
If anybody knows a better way to fill arrays: please let me know, because
I agree that is doesn't look very efficient.
Cheers
Eelco
On Fri, 31 May 2002, Minh Do-Quang wrote:
> Hi all,
>
> I am a new DX-users. I have a 100 data files. From each of them I need
> to pick up a value at a fixed position. I did it already by using the
> map component. But I don't now how to capture all of them in to a array
> in order to plot or export into file.
>
> Thanks for your advance.
> Minh
>
>
>
>
// a script how to fill an 2D array using two nested loops
include "show_plane.net"
macro FillNy(array,nx,ny,i) -> (out)
{
// loop over the 'number' items of 'array'
j = ForEachN(0,ny-1,1);
// make a position array
position = Mark(array,"positions");
// retrieve the current globally stored output array
out,link = GetGlobal(array);
// calulate the current value to set
value = Compute("10.*$0 + 100*$1",i,j);
// if current index , fill value, else copy old value
out = Compute("$0.x==$1 && $0.y==$2 ?
$3:$4",position,i,j,value,out);
// set the current output array
SetGlobal(out,link);
}
macro Fill(array,nx,ny) -> (out)
{
// loop over the 'number' items of 'array'
i = ForEachN(0,nx-1,1);
out = FillNy(array,nx,ny,i);
out = Remove(out,"saved data");
}
// create the array
nx = 4;
ny = 6;
counts = Compute("[$0,$1]",nx,ny);
data = Construct([0 0] ,[1 1],counts,0.0);
// fill it
data = Fill(data,nx,ny);
data = Post(data,"connections");
// show it
image = show_plane(data,showbar=1,barshape=[200
10],showgrid=1,showglyph=1,
glyphtype="text",glyphscale=.5,aspect=1,up=[0 1 0]);
Display(image);
KeyIn();
// a script how to fill an array
macro Fill(array,number=10) -> (out)
{
// loop over the 'number' items of 'array'
current = ForEachN(0,number,1);
// make a position array
position = Mark(array,"positions");
// retrieve the current locally stored output array
out,link = GetLocal(array);
// calulate the current value to set
value = Compute("[$0*$0, 2*$0*$0]",current);
// if current index , fill value, else copy old value
out = Compute("$1.x==$0 ? $3 : $2 ",current,position,out,value);
// set the current output array
SetLocal(out,link);
}
// create the array
number = 10;
data = Construct(0 ,1,number,[0.0 0.0]);
// fill it
data = Fill(data,number);
Print(data,"rd");
macro show_plane( data, // the
data field to show (2d or 3d)
selectplanes=[2,0,0], //
[direction, thickness, positions1,
// postion2, position3]
datarange,
// min, max, or [min,max]
caption,
// caption and color{"cap","color"}
captionposition,
// the position of the caption
captionscale,
// the size of the caption
showcolors=1,
// 0=none, 1=color, 2=greyscale
coloropacity=1,
// the color opacity
colorrange,
// [start, range]
showbox=0,
// a color string or 1 to show box
showboxplane=0,
// same as showbox, but only for slab
showgrid=0,
// a color string or 1 to show grid
isolines,
// if 1 int: number of line
// if list of floats: the line values
isolinecolor="black", // the
isoline color
isosurface,
// idem as isolines,
isosurfacecolor,
isosurfaceopacity,
showzeros=[0 0 0],
// show the coordinate axes
zerocolors,
// sting (list) of the axes colors
origin,
// the origin of the coordinate axes
showaxes=0,
axesscale=1,
rubber=0,
showbar=0,
// if 1 or color string, show bar
barposition,
barshape,
showglyph=0,
glyphtype=0,
glyphscale=1,
addglyph,
background="grey",
direction,
width,
aspect=.75,
resolution=300,
from,
to,
up=[0 0 1],
camera,
showlight=[1 1],
// turn on the default spotlights/ambient
lightcolor,
lightposition,
lightcamera,
ambientlightcolor,
image_title="") ->
(image,camera,collect)
{
// set the axes labels
axlab={"x","y","z"};
// check if 2D or 3d data field
is3d = Inquire(data,"is 3D positions");
// pick slice from the data
// first, see how many planes are given in the array
shape = Inquire(selectplanes,"shape");
number = Compute("$0-2",shape);
planedirect = Compute("$0.x",selectplanes);
thickness = Compute("$0.y",selectplanes);
planenumber1 = Compute("select($0,2)",selectplanes);
planenumber2 = Compute("$1 > 1 ? select($0,3):
-1",selectplanes,number);
planenumber3 = Compute("$1 > 2 ? select($0,4):
-1",selectplanes,number);
planenumber4 = Compute("$1 > 3 ? select($0,5):
-1",selectplanes,number);
planenumber5 = Compute("$1 > 4 ? select($0,6):
-1",selectplanes,number);
showplane2 = Compute("$0>=0 ? 1 : 2",planenumber2);
showplane3 = Compute("$0>=0 ? 1 : 2",planenumber3);
showplane4 = Compute("$0>=0 ? 1 : 2",planenumber4);
showplane5 = Compute("$0>=0 ? 1 : 2",planenumber5);
planenumber2 = Switch(showplane2,planenumber2,NULL);
planenumber3 = Switch(showplane3,planenumber3,NULL);
planenumber4 = Switch(showplane4,planenumber4,NULL);
planenumber5 = Switch(showplane5,planenumber5,NULL);
planenumber =
List(planenumber1,planenumber2,planenumber3,planenumber4,
planenumber5);
slice = Slab(data,planedirect,planenumber,thickness=thickness);
temp = ShowBoundary(slice);
taketemp = Compute("$0>0 ? 1 : 2",thickness);
slice = Switch(taketemp,temp,slice);
// make volume
volume = ShowBoundary(data);
showvolume = Compute("$0<0 ? 1 : 2",planenumber1);
// choise between volume and slice
slice = Switch(showvolume,volume,slice);
// if 2d data field, take original data
slice = Switch(is3d+1,data,slice);
// make grid
isstring = Inquire(showgrid,"is string");
isint = Inquire(showgrid,"is int");
showcolor = Compute("$0==1 ? $1 :
$2",isstring,showgrid,"black");
showit = Compute("$0==1 ? $1 : 0",isint,showgrid);
showit = Compute("$0==1 || $1==1 ? 1 :
0",showit,isstring);
grid = ShowConnections(slice);
grid = Color(grid,showcolor);
grid = Route(showit,grid);
// make bounding boxes
isstring = Inquire(showbox,"is string");
isint = Inquire(showbox,"is int");
showcolor = Compute("$0==1 ? $1 :
$2",isstring,showbox,"black");
showit = Compute("$0==1 ? $1 : 0",isint,showbox);
showit = Compute("$0==1 || $1==1 ? 1 :
0",showit,isstring);
box = ShowBox(data);
box = Color(box,showcolor);
box = Route(showit,box);
// make bounding boxes
isstring = Inquire(showboxplane,"is string");
isint = Inquire(showboxplane,"is int");
showcolor = Compute("$0==1 ? $1 :
$2",isstring,showboxplane,"black");
showit = Compute("$0==1 ? $1 : 0",isint,showboxplane);
showit = Compute("$0==1 || $1==1 ? 1 :
0",showit,isstring);
boxplane = ShowBox(slice);
boxplane = Color(boxplane,showcolor);
boxplane = Route(showit,boxplane);
// make iso contours
isnull = Inquire(isolines,"is null");
isint = Inquire(isolines,"is int");
// is the isolines is an integer, take them to be the number of lines
num = Switch(isint+1,NULL,isolines);
testnum = Switch(isint+1,1,isolines);
tmp = Compute("$0==0 && $1==1 ? 0 :
1",testnum,isint);
linedata = Route(tmp,slice);
tmp = Route(isint,tmp);
// else use them as values
val = Switch(isint+1,isolines,NULL);
linedata = Route(isnull+1,linedata);
lines =
Isosurface(linedata,value=val,number=num);
lines = Color(lines,isolinecolor);
// add colors to the data
isvector = Inquire(colorrange,"is
vector");
colorrange = Route(isvector,colorrange);
startcolor = Compute("$0.x",colorrange);
rangecolor = Compute("$0.y",colorrange);
startcolor = Switch(isvector+1,NULL,startcolor);
rangecolor = Switch(isvector+1,NULL,rangecolor);
isvector = Inquire(datarange,"is
vector");
datarange = Route(isvector,datarange);
min = Compute("$0.x",datarange);
max = Compute("$0.y",datarange);
min = Switch(isvector+1,NULL,min);
max = Switch(isvector+1,NULL,max);
sliceu,colormapu = Color(slice,"white",0);
slicec,colormapc = AutoColor(slice,min=min,max=max,start=startcolor,
range=rangecolor,opacity=coloropacity);
sliceg,colormapg = AutoGrayScale(slice,min=min,max=max,start=startcolor,
range=rangecolor,opacity=coloropacity);
slice =
Switch(showcolors+1,sliceu,slicec,sliceg);
colormap =
Switch(showcolors+1,NULL,colormapc,colormapg);
// create relief
rubbersheet = RubberSheet(slice);
rubbersheet = Normals(rubbersheet,"connections");
rubber = Compute("$0==1 ? 1 : 2",rubber);
slice = Switch(rubber,rubbersheet,slice);
// make the isosurfaces ////////////////////////////////////////
isnull = Inquire(isosurface,"is null");
isint = Inquire(isosurface,"is int");
// set number if 'isosurface' is integer, else set NULL
num = Switch(isint+1,NULL,isosurface);
// for test use in compute, is no integer, set tmp to zero
tmp = Switch(isint+1,0,isosurface);
// if 'isosurface' was no integer, take it as value list
val = Switch(isint+1,isosurface,NULL);
// display only if 'isosurface' was integer greater then zero or a
value list
tmp = Compute("($0==1 && $1>0) || ($0==0 &&
$2==0) ? 1 :0 ",
isint,tmp,isnull);
surfacedata = Route(tmp,data);
surface = Isosurface(surfacedata,value=val,number=num);
surface =
Color(surface,isosurfacecolor,isosurfaceopacity);
// make x and y zeroline ////////////////////////////////////////
// first extract the bounding box and the corners
corners = Extract(data,"box");
cnr1 = Select(corners,0);
dimdata = Inquire(data,"is 3d positions");
select = Compute("$0==1 ? 7 : 3",dimdata);
cnr2 = Select(corners,select);
cnr1 = Compute("$0==1 ? $1 : [$1.x, $1.y,
0]",is3d,cnr1);
cnr2 = Compute("$0==1 ? $1 : [$1.x, $1.y,
0]",is3d,cnr2);
center = Compute("($0+$1)/2.0",cnr1,cnr2);
// choose between default or passed origin
isnull = Inquire(origin,"is null");
origin = Switch(isnull+1,origin,center);
// in case of a 2d origin, transform to a 2d vector
is2vector = Inquire(origin,"is 2vector");
origin = Compute("$0==1 ? [$1.x, $1.y, 0]: $1 ",
is2vector,origin);
// check the axis colors
isarray = Inquire(zerocolors,"is array");
items = Inquire(zerocolors,"items");
items = Switch(isarray+1,0,items);
select0 = Select(zerocolors,0);
select1 = Select(zerocolors,1);
select2 = Select(zerocolors,2);
isstring = Inquire(zerocolors,"is null");
select = zerocolors;
select = Switch(isstring+1,select,"black");
item = Compute("$0>=1 ? 2 : 1 ",items);
item = Compute("$0>=2 ? 2 : 1 ",items);
item = Compute("$0>=3 ? 2 : 1 ",items);
xzerocolor = Switch(item,select,select0);
yzerocolor = Switch(item,select,select1);
zzerocolor = Switch(item,select,select2);
// calculate of each zeroline its centrum and half length
linex_o =
Compute("[($1.x+$2.x)/2.,$0.y,$0.z]",origin,cnr1,cnr2);
liney_o =
Compute("[$0.x,($1.y+$2.y)/2.,$0.z]",origin,cnr1,cnr2);
linez_o =
Compute("[$0.x,$0.y,($1.z+$2.z)/2.]",origin,cnr1,cnr2);
linex_n = Compute("[($1.x - $0.x)/2., 0, 0]",cnr1,cnr2);
liney_n = Compute("[0, ($1.y - $0.y)/2., 0]",cnr1,cnr2);
linez_n = Compute("[0, 0, ($1.z - $0.z)/2.]",cnr1,cnr2);
// check if it excists
rank = Inquire(showzeros,"rank");
dim = Inquire(showzeros,"shape");
dim = Switch(rank+1,1,dim);
showxzero = Compute("$0==0 ? $2 : \
$0==1
&& $1>=1 ? $2.x : 0",rank,shape,showzeros);
showyzero = Compute("$0==1 && $1>=2 ? $2.y :
0",rank,shape,showzeros);
showzzero = Compute("$0==1 && $1>=3 ? $2.z :
0",rank,shape,showzeros);
xzero = Grid(linex_o,"line",linex_n,2);
xzero = ShowConnections(xzero);
xzero = Color(xzero,xzerocolor);
xzero = Route(showxzero,xzero);
yzero = Grid(liney_o,"line",liney_n,2);
yzero = ShowConnections(yzero);
yzero = Color(yzero,yzerocolor);
yzero = Route(showyzero,yzero);
zzero = Grid(linez_o,"line",linez_n,2);
zzero = ShowConnections(zzero);
zzero = Color(zzero,zzerocolor);
zzero = Route(showzzero,zzero);
// create captions
isnull = Inquire(caption,"is null");
isnull = Compute("$0==1 ? 0 : 1",isnull);
caption = Route(isnull,caption);
// see if the caption input is given as an array
isarray = Inquire(caption,"is array");
isarray = isarray+1;
// only get the number of items if caption was an array, else take 0
items = Inquire(caption,"items");
items = Switch(isarray,0,items);
items = Compute("$0==2 ? 1 : 0",items);
// now select the elements from the array
captionselect0 = Select(caption,0);
captionselect1 = Select(caption,items);
// if no color was given: take the default col
defaultcolor = Compute("$0==2 && $1==2 ? 2 : 1",isarray,items);
captioncolor = Switch(defaultcolor,"black",captionselect1);
captiontext = Switch(isarray,caption,captionselect0);
captioncolor = Compute("$0==0 ? $2 : $1
",items,captionselect1,"black");
imagecaption = Caption(captiontext,position=captionposition,font="fixed",
height=captionscale,alignment=0.0);
imagecaption = Color(imagecaption,captioncolor);
// create colorbars
isstring = Inquire(showbar,"is string");
isint = Inquire(showbar,"is int");
showcolor = Compute("$0==1 ? $1 :
$2",isstring,showbar,"black");
showit = Compute("$0==1 ? $1 : 0",isint,showbar);
showit = Compute("$0==1 || $1==1 ? 1 :
0",showit,isstring);
bar = ColorBar(colormap,shape=barshape,position=barposition,
colors=showcolor,min=min,max=max);
bar = Route(showit,bar);
// create glyph
isstring = Inquire(showglyph,"is string");
isint = Inquire(showglyph,"is int");
showcolor = Compute("$0==1 ? $1 :
$2",isstring,showglyph,"black");
showit = Compute("$0==1 ? $1 : 0",isint,showglyph);
showit = Compute("$0==1 || $1==1 ? 1 :
0",showit,isstring);
glyphs =
AutoGlyph(slice,type=glyphtype,scale=glyphscale);
glyphs = Color(glyphs,showcolor);
glyphs = Route(showit,glyphs);
// create lights
showspotlight = Compute("$0.x",showlight);
showambientlight = Compute("$0.y",showlight);
spotlight =
Light(lightposition,lightcolor,lightcamera);
ambientlight = AmbientLight(ambientlightcolor);
light =
Route(showambientlight,ambientlight);
spotlight = Route(showspotlight,spotlight);
lights = Collect(spotlight,ambientlight);
// set the camera
// first get the default viewing directions
view
= Collect(slice,box);
camera_auto =
AutoCamera(view,direction);
to_auto,from_auto,up_auto,width_auto =
GetCameraSettings(camera_auto);
// see if the camera settings are given, if so: overule defaults
isto = Inquire(to,"is null");
isfrom = Inquire(from,"is null");
iswidth = Inquire(width,"is null");
isup = Inquire(up,"is null");
iscamera = Inquire(camera,"is null");
to =
Switch(isto+1,to,to_auto);
from =
Switch(isfrom+1,from,from_auto);
width =
Switch(iswidth+1,width,width_auto);
up =
Switch(isup+1,up,up_auto);
// now set the camera
camera_user =
Camera(to,from,width,resolution,aspect,up,
background=background);
// if a whole camera was give, urule everything
camera = Switch(iscamera+1,camera,camera_user);
//
isstring = Inquire(showaxes,"is string");
isint = Inquire(showaxes,"is int");
showcolor = Compute("$0==1 ? $1 :
$2",isstring,showaxes,"black");
showit = Compute("$0==1 ? $1 : 0",isint,showaxes);
showit = Compute("$0==1 || $1==1 ? 1 :
0",showit,isstring);
ax =
AutoAxes(slice,camera,axlab,grid=0,
labelscale=axesscale,colors=showcolor);
ax = Route(showit,ax);
// if there are no colors, don't show it
showcolors = Compute("$0==0? 0 : 1",showcolors);
slice = Route(showcolors,slice);
// collect all items of each individual image
collect = Collect(slice,imagecaption,bar,ax,addglyph,lines,box,grid,glyphs,
xzero,yzero,zzero,lights,surface,boxplane);
// render the images
image = Render(collect,camera)[cache:0];
// finally: show it if title is set
doshow = Compute("strlen($0)==0 ? 0 : 1",image_title);
string = Format("X,,%s",image_title);
imshow = Route(doshow,image);
Display(imshow,where=string);
}