    //name: polarPattern
    // filename: polarPattern.sci 
    // Release 0.1 
    // status: beta 
    // author: Christian Leray
    // license: CeCILL-C
    // date: August 2013
    // platform: scilab 5.4.1 (1) (written & tested on)
    // os: winXP-64 (written & tested on)
    // keywords: 2D, 3D pattern
    // description: 
    //      This file contains 2 mains functions that are useful to draw 
    //      radiated patterns.
    //         -Polar3D that draws spherical data (see description here after),
    //         -Polar2D that draws a cut plane(see description here after).

function [clmn]=CutPhi(dat,Phi)
//retreive two columns in matrix corresponding to a Phi cut values and 
//return result in a line
    i_phi1=find(round(Ph)==Phi);     //column  index corresponding to Phi=0°
    i_phi2=find(round(Ph)==Phi+180); //column  index corresponding to Phi=180°
    clmn=dat(1:$-1,i_phi2);          //column containing data from 0 to -180° 
    clmn=clmn($:-1:1);               //reverse data order from -180 to 0°
    clmn=[dat(:,i_phi1);clmn]';//then add column containing data from 0 to 180° 
endfunction

function [x,y,z]=spheric(ph,th,dat)
// Computes spherical coordinates [x,y,z]
// Ph:    Phi angle {0, 360°}, 0 on X axis size=[1,n] 
// Thet:  Theta angle {0, 180°}, 0 on Z axis ,size=[m,1] 
// dat:   matrix [m,n] of modulus according to Phi Thet
    x=dat.*(sind(th)*cosd(ph));
    y=dat.*(sind(th)*sind(ph));
    z=dat.*(cosd(th)*ones(ph));
endfunction;
//---------------------------------------------------------------
// polar3D(Ph,Thet,gdb,vmin,vmax,ttl)
// Ph:    Phi angle, size=[1,y] 
// Thet:  Theta angle,size=[x,1] 
// gdb:   matrix [x,y] of values of modules to display, according to Phi Thet
// vmin:  minimum value of data to display, 
// vmax:  maximum value of data to display
// titl:  title of the plot
function polar3D(Ph,Thet,gdb,scal,vmax,ttl)
    vmin=vmax-scal
    dat=((gdb-vmin).*(gdb > vmin))/(vmax-vmin);//Set values < Vmin to 0 and normalize 
    [x,y,z]=spheric(Ph,Thet,dat);              //Spherical coordinates
    drawlater();
    title(ttl,'fontname',7,'fontsize',2);
    colorbar(vmin,vmax);
    // color are previously defined by jetcolormap(128)
    // draw new axes
    ax=1;
    kx=.1;
    // axe X
    xsegs([kx*ax,ax],[0,0],[0,0]);
    e=gce(); e.thickness=1.2; e.arrow_size = 1; e.segs_color = 63;
    // axe Y
    xsegs([0,0],[kx*ax,ax],[0,0]); 
    e=gce(); e.thickness=1.2;  e.arrow_size = 1;  e.segs_color = 12; 
    // axe Z
    xsegs([0,0],[0,0],[kx*ax,ax]); 
    e=gce(); e.thickness=1.2; e.arrow_size = 1;  e.segs_color = 115; 
    
    xstring(ax,0,'x'); e=gce(); e.font_size=2; e.font_foreground=64; 
    xstring(0,ax,'y'); e=gce(); e.font_size=2; e.font_foreground=12;
    xstring(0,0,'z');  e=gce(); e.font_size=2; e.font_foreground=115; 
    move(e,[0,0,ax]);
    // circle plan xy and XZ
    t=[0:0.1:2*%pi]';
    param3d(cos(t),sin(t),zeros(t))
    e=gce(); e.foreground=12;e.line_style=7;e.closed="on";
    param3d(cos(t),zeros(t),sin(t))
    e=gce();e.foreground=115;e.closed="on";e.line_style=7;
    // draw arrows
    t=[0.3,.4]';
    param3d(cos(t),sin(t),zeros(t))
    e=gce(); e.foreground=12;
    e.arrow_size_factor=3;e.polyline_style=4;
    t=[%pi/2-.3,%pi/2-.4]';
    param3d(cos(t),zeros(t),sin(t))
    e=gce();e.foreground=115;
    e.arrow_size_factor=3;e.polyline_style=4;
    xstring(1.05,.2,["$\phi$"])
    e=gce();e.font_foreground=12;
    xstring(0,0,["$\theta$"])
    e=gce(); move(e,[0.2,0,ax]);e.font_foreground=115;
    // 3D patterns
    a=gca()
    surf(x,y,z,dat*size(a.Parent.color_map,1));    //color given by dat
    a.children(1).color_flag=3;                    // 3 interpolated mode 
    a.children(1).color_mode=-1;                   // delete mesh
    a.children(1).cdata_mapping='direct';
    a.title.font_foreground=3;
    a.title.font_size=3;
    a.box="off";
    a.cube_scaling="on";
    //a.isoview="on";
    a.tight_limits="on";
    a.data_bounds=[-1,-1,-1; 1,1,1];
    a.rotation_angles=[45,-45];
    a.axes_visible="off";                      // delete first axes and labels
    a.x_label.visible="off";
    a.y_label.visible="off";
    a.z_label.visible="off";
    drawnow();
endfunction
//-------------------------------------------------------------------------
// polar2D(alpha,gdb,vmin,vmax,titl,lgd,ax)
// alpha: value of angle,  size=[1,x] x= nb of values
// gdb:   matix of values of modules to display, size=[x,n], 
//        n = nb of curves to display
// vmin:  minimum value of data to display, 
// vmax:  maximum value of data to display
// titl:  title of the plot
// lgd:   legend  of the curves, size=[1,n]
// ax:    axis configuraton value in {'xy', 'xz', 'yz'}
//        'xy': representation in xy plane
//        'xz': representation in xz plane
//        'yz': representation in yz plane
//        the axis are drawn on the upper right side of the graphic
function polar2D(alpha,gdb,scal,vmax,titl,lgd,ax)
    if size(gdb,1)>4 then
        disp('number of curves >4');
    end
    vmin=vmax-scal;
    dat =((gdb-vmin).*(gdb>vmin))/scal ;//Set values < Vmin to 0 and normalize 
    div=scal/5;
    drawlater();
    col=["red","blue","green","yellow"]    // four color max
    //configuration of axis and graduations according to ax value
    ax=convstr(ax,'l');
    select ax
        case "xy" then
            cx=["blue","green","r."];   // axis colors
            XL= cos(0:%pi/6:11*%pi/6);  //angle graduations position
            YL= sin(0:%pi/6:11*%pi/6);
            grad= string(0:30:330);     // graduation 0 360° 
        case "xz" then
            cx=["red","green","bx"];
            alpha=(90-alpha);          // 0° is on Z axis 
            XL= sin(0:%pi/6:11*%pi/6); // pi/2 rotation of graduations
            YL= cos(0:%pi/6:11*%pi/6);
            grad= string(0:30:180);    // graduation 0 +-180°
            grad=[grad  string(-150:30:-30)]
        case "yz" then
            cx=["red","blue","g."];
            alpha=(90-alpha);          // 0° is on Z axis 
            XL= sin(0:%pi/6:11*%pi/6); // pi/2  rotation of graduation
            YL= cos(0:%pi/6:11*%pi/6);
            grad= string(0:30:180);     // graduation 0 +-180°
            grad=[grad  string(-150:30:-30)]
        else
            //disp('other')
            cx=["green","blue","r."];
            grad= string(0:30:330);
    end
    // draw patterns
    alpha=repmat(alpha,size(dat,1),1) // set the data size to alpha
    plot2d((dat.*cosd(alpha))',(dat.* sind(alpha))',rect=[-1,-1,1,1]);
    e=gce();
    e.children.thickness=2;
    for i=1:size(dat,1)
        e.children(i).foreground=color(col(i));
    end

    //legend
    hl=legend(lgd,boxed = %f);
    hl.legend_location= "in_lower_right";//"out_upper_left" 
    //"in_lower_left" ; "out_lower_left";
    hl.fill_mode="off";
    //gain circles
    arcs=[];
    for i=1:div
      arcs=[arcs, [i*[-1;1;2;2]/div;0;360*64]]
    end
    xarcs(arcs);
    e=gce();
      e.children.line_style=7;
      e.children.thickness=.5;
      e.children.foreground=color("grey");
    x1= sin(0:%pi/6:2*%pi);      y1= cos(0:%pi/6:2*%pi);
    x2= sin(0:%pi/6:2*%pi)/div;  y2= cos(0:%pi/6:2*%pi)/div;
    xpolys([x1;x2],[y1;y2]);
    e=gce();
      e.children.line_style=7;
      e.children.thickness=.5;
      e.children.foreground=color("grey");
    // graduations angle
    XL=-.15*(XL<0)+XL;            // display correction for better readability
    YL=-.1*(YL<0)+YL;
    xstring(XL',YL',grad');
    a=gca();
    txt=a.children(1).children;
    txt.font_foreground=color("darkslategrey");
    txt.fractional_font="on";
    txt.font_size=1
    // graduations in dB
    s=[];  YL=[];
    for i=0:div
        divx=vmax-i*scal/div;    
        if divx==floor(divx) then     // integer 
            fm='%4.0f';
        else
            fm='%4.1f';
        end
        s=[s sprintf(fm, divx)]  
        YL=[YL (div-i)/div];
    end  
    XL=zeros(YL);
    XXL=[YL;XL];
    XXL=rotate(XXL,%pi/12);
    YL=XXL(1,:)
    XL=XXL(2,:)
    xstring(XL,YL,s)
    txt=a.children(1).children;
    txt.font_foreground=color("blue");
    txt.fractional_font="on";
    txt.font_size=1
    // draws reference with  3 axis
    //  X axis
    xpoly([.8;.8],[.8;1.12]);
    e=gce()
    e.line_style=1;
    e.polyline_style=4;
    e.thickness=1;
    e.arrow_size_factor=2.5;
    e.foreground=color(cx(1));
    // Y axis
    xpoly([.8;1.12],[.8;.8]);
    e=gce();
    e.line_style=1;
    e.polyline_style=4;
    e.thickness=1;
    e.arrow_size_factor=2.5;
    e.foreground=color(cx(2)); 
    // Z axis
    plot(.8,.8,cx(3)); //draws "x" or "o"o point 
    
    a=gca();
    a.data_bounds=[-1.15,-1.15;1.15,1.15];
    a.margins=[0.125 0.125 0.125 0.125]
    a.tight_limits="on"
    a.title.text=titl;
    a.title.font_foreground=color("blue");
    a.title.fractional_font="on";
    a.title.font_size=2;
    a.box="off";
    a.axes_visible="off";
    a.isoview="on";   //doesn't work with 5.40
    drawnow();
 endfunction;
 //--------------------------------------------------------------------
function triax(xp,yp)
    // draw a 3axes reference in current figure at coordinates Xp,Yp
    //xp: x_left, yp: y_up, numbers in {0 1} 
    a=newaxes();
    a.axes_bounds= [xp,yp,.18,.12];//[x_left,y_up, ,height]
    a.margins=[0.125 0.125 0.125 0.125]
    a.box="off";
    a.data_bounds=[0,0;1,1];
    a.axes_visible="off";
    ttl= a.title;
    ttl.text='Axes';
    ttl.font_foreground =color("blue");
    ttl.font_size = 2;
    //ttl.font_style = 7;
    //tri-axe
    // axe z
    xpoly([.3;.3],[.3;.7]);
    e=gce()
    e.line_style=1;
    e.polyline_style=4;
    e.thickness=2;
    e.arrow_size_factor=2.5;
    e.foreground=color("red");
    xstring(0.3,.8,'z');
    e=gce(); 
    e.font_foreground =color("red");
    e.font_size = 2;
    
    // axe y
    xpoly([.3;.6],[.3;.1]);
    e=gce();
    e.line_style=1;
    e.polyline_style=4;
    e.thickness=2;
    e.arrow_size_factor=2.5;
    e.foreground=color("blue"); 
    xstring(.6,.1,'y');    
    e=gce(); 
    e.font_foreground =color("blue");
    e.font_size = 2;
    // axe x
    xpoly([.3;.1],[.3;.1]);
    e=gce();
    e.line_style=1;
    e.polyline_style=4;
    e.thickness=2;
    e.arrow_size_factor=2.5;
    e.foreground=color("green"); 
    xstring(0,.12,'x');
    e=gce(); 
    e.font_foreground =color("green");
    e.font_size = 2; 
endfunction
