
#---------------------------------------------------------------
#**************************************************************-
#-*                   Photon Design                           *-
#-*          YZ and XZ field extract tool for Fimmprop        *-
#-*                Written by Klearchos Chaloulos             *-
#-*                        2007                               *-
#-*************************************************************-
#---------------------------------------------------------------

from PDCMatrix import *

def GetYZProfile(NodeName,Zmin,Zmax,Zstep,Xmin,Xmax,Xpoint,cmpt):

    #Output matrix rows are z, cols are y    
    f=pdApp()
    f.ConnectToApp()
    f.Exec("Ref& a=app.findnode("+NodeName+")")

    #----------Test Run To Find Xdim, Ydim---------
    f.Exec("a.calcfieldprofile("+repr(Zmin)+",0,0)")
    f.Exec("Set arr = a.fieldprofile.getfieldarray("+repr(cmpt)+",0)")
    Xdim=int(f.Exec("arr.xdim"))
    Ydim=int(f.Exec("arr.ydim"))
    #----------End of Test Run To Find Xdim, Ydim---------

    Xstep=(Xmax-Xmin)/Xdim
    nXpoint=int((Xpoint-Xmin)/Xstep)
    
    rows=int((Zmax-Zmin)/Zstep)
    matTemp=pdcmatrix('d',rows,Ydim+1)
    for i in range (0,rows):
        z=Zmin+i*Zstep
        f.Exec("a.calcfieldprofile("+repr(z)+",0,0)")
        f.Exec("Set arr = a.fieldprofile.getfieldarray("+repr(cmpt)+",0)")
        for j in range(0,Ydim+1):
            field=f.Exec("arr.fieldarray["+repr(nXpoint)+"]["+repr(j)+"]")
            matTemp.setRI(i,j,field.real,field.imag)
        continue    
    return matTemp

def GetXZProfile(NodeName,Zmin,Zmax,Zstep,Ymin,Ymax,Ypoint,cmpt):

    #Output matrix rows are z, cols are x     
    f=pdApp()
    f.ConnectToApp()
    f.Exec("Ref& a=app.findnode("+NodeName+")")

    #----------Test Run To Find Xdim, Ydim---------
    f.Exec("a.calcfieldprofile("+repr(Zmin)+",0,0)")
    f.Exec("Set arr = a.fieldprofile.getfieldarray("+repr(cmpt)+",0)")
    Xdim=int(f.Exec("arr.xdim"))
    Ydim=int(f.Exec("arr.ydim"))
    #----------End of Test Run To Find Xdim, Ydim---------

    Ystep=(Ymax-Ymin)/Ydim
    nYpoint=int((Ypoint-Ymin)/Ystep)#was ymin
    
    rows=int((Zmax-Zmin)/Zstep)
    matTemp=pdcmatrix('d', rows, Xdim+1)
    for i in range (0,rows):
        z=Zmin+i*Zstep
        f.Exec("a.calcfieldprofile("+repr(z)+",0,0)")
        f.Exec("Set arr = a.fieldprofile.getfieldarray("+repr(cmpt)+",0)")
        for j in range(0,Xdim+1):
            field=f.Exec("arr.fieldarray["+repr(j)+"]["+repr(nYpoint)+"]")
            matTemp.setRI(i,j,field.real,field.imag)
        continue    
    return matTemp

def WriteAMFFile(strFileName,Ex,Ey,Ez,Hx,Hy,Hz,Xmin,Xmax,Ymin,Ymax,lamda,IsComplex):
    f=file(strFileName,'w+')
    nx=Ex.rows() #nx
    ny=Ex.cols() #ny
    
    #Write File Header     
    strlist=[]
    strlist.append("begin <amf(1.0)>"+'\n')
    strlist.append("  "+repr(nx-1)+" "+repr(ny-1)+" //nxseg nyseg"+'\n')
    strlist.append("    "+repr(Xmin)+"   "+repr(Xmax)+"   "+repr(Ymin)+"   "+repr(Ymax)+"   //xmin xmax ymin ymax"+'\n')
    strlist.append("  1 1 1 1 1 1 //hasEX hasEY hasEZ hasHX hasHY hasHZ"+'\n')
    strlist.append("    0.000000       0.000000  //beta"+'\n')
    strlist.append("    0.500000  //lambda"+'\n')
    strlist.append("  "+repr(IsComplex)+" //iscomplex"+'\n')
    strlist.append("  0 //isEmode"+'\n')
    strlist.append("  //components follow as nyseg by nxseg matrices (real imag real imag ....)"+'\n')
    strlist.append("  //----------------------------------------------------------"+'\n')
    f.writelines(strlist)
    

    #Ex
    f.write("  //Ex components"+'\n')
    for i in range(0,nx):
        f.write("    ")
        Ex.WriteLineToTextFileRI(f,i,"      ")
        continue
        
    #Ey
    f.write("  //Ey components"+'\n')
    for i in range(0,nx):
        f.write("    ")
        Ey.WriteLineToTextFileRI(f,i,"      ")
        continue

    #Ez
    f.write("  //Ez components"+'\n')
    for i in range(0,nx):
        f.write("    ")
        Ez.WriteLineToTextFileRI(f,i,"      ")
        continue

    #Hx
    f.write("  //Hx components"+'\n')
    for i in range(0,nx):
        f.write("    ")
        Hx.WriteLineToTextFileRI(f,i,"      ")
        continue
     
    #Hy
    f.write("  //Hy components"+'\n')
    for i in range(0,nx):
        f.write("    ")
        Hy.WriteLineToTextFileRI(f,i,"      ")
        continue
    
    #Hz
    f.write("  //Hz components"+'\n')
    for i in range(0,nx):
        f.write("    ")
        Hz.WriteLineToTextFileRI(f,i,"      ")
        continue

    f.write("end")
    return

#----------------End of WriteANFFile-----------------------


#----------------Script begin-------------------------


#--------Define parameters-----------


print ""
print "*****************************************************************"
print "*             XZ and ZY Field Profile                           *"
print "*          -----------------------------------                  *"
print "*        by Klearchos Chaloulos (c) Photon Design 2007          *"
print "*                                                               *"
print "*****************************************************************"
print ""

    
#---------------Prompt User for parameters-------------
print "Enter the node name of the component."
print "For example if I have a component called comp1"
print "which is stored in the root node under a project called 'project'"
print "then I would enter 'project/comp1':   "
NodeName=raw_input(">>>>")

print "Enter Zmin Zmax nz in this order, separated by spaces"
strTemp=raw_input(">>>>")
arrTemp=stringToArray(strTemp)
Zmin=arrTemp[0]
Zmax=arrTemp[1]
nz=arrTemp[2]
Zstep=(Zmax-Zmin)/nz

print "Enter Xmin Xmax XPoint in this order, separated by spaces"
strTemp=raw_input(">>>>")
arrTemp=stringToArray(strTemp)
Xmin=arrTemp[0]
Xmax=arrTemp[1]
Xpoint=arrTemp[2]

print "Enter Ymin Ymax YPoint in this order, separated by spaces"
strTemp=raw_input(">>>>")
arrTemp=stringToArray(strTemp)
Ymin=arrTemp[0]
Ymax=arrTemp[1]
Ypoint=arrTemp[2]

print "Enter lambda"
raw_input(">>>>>")
lamda=0.5
rows=int((Zmax-Zmin)/Zstep)

print "Enter Output filename, including extension .amf"
strFileName=raw_input(">>>>")

print "Type XZ for an XZ Profile or YZ for a YZ Profile"
strXZYZ=raw_input(">>>>")
IsXZ=0
if (strXZYZ=="XZ" or strXZYZ=="xz" or strXZYZ=="Xz" or strXZYZ=="xZ" or strXZYZ=="XZ"):
    IsXZ=1
    



#-------End of parameters definition

Ex=pdcmatrix('d',4,4)
Ey=pdcmatrix('d',4,4)
Ez=pdcmatrix('d',4,4)
Hx=pdcmatrix('d',4,4)
Hy=pdcmatrix('d',4,4)
Hz=pdcmatrix('d',4,4)

if (IsXZ==0):
    Ex=GetYZProfile(NodeName,Zmin,Zmax,Zstep,Xmin,Xmax,Xpoint,1)
    Ey=GetYZProfile(NodeName,Zmin,Zmax,Zstep,Xmin,Xmax,Xpoint,2)
    Ez=GetYZProfile(NodeName,Zmin,Zmax,Zstep,Xmin,Xmax,Xpoint,3)
    Hx=GetYZProfile(NodeName,Zmin,Zmax,Zstep,Xmin,Xmax,Xpoint,4)
    Hy=GetYZProfile(NodeName,Zmin,Zmax,Zstep,Xmin,Xmax,Xpoint,5)
    Hz=GetYZProfile(NodeName,Zmin,Zmax,Zstep,Xmin,Xmax,Xpoint,6)
    WriteAMFFile(strFileName,Ez,Ey,Ex,Hz,Hy,Hx,Zmin,Zmax,Ymin,Ymax,lamda,1)
elif (IsXZ==1):
    Ex=GetXZProfile(NodeName,Zmin,Zmax,Zstep,Xmin,Xmax,Xpoint,1)
    Ey=GetXZProfile(NodeName,Zmin,Zmax,Zstep,Xmin,Xmax,Xpoint,2)
    Ez=GetXZProfile(NodeName,Zmin,Zmax,Zstep,Xmin,Xmax,Xpoint,3)
    Hx=GetXZProfile(NodeName,Zmin,Zmax,Zstep,Xmin,Xmax,Xpoint,4)
    Hy=GetXZProfile(NodeName,Zmin,Zmax,Zstep,Xmin,Xmax,Xpoint,5)
    Hz=GetXZProfile(NodeName,Zmin,Zmax,Zstep,Xmin,Xmax,Xpoint,6)
    WriteAMFFile(strFileName,Ez,Ey,Ex,Hz,Hy,Hx,Zmin,Zmax,Xmin,Xmax,lamda,1)

del Ex
del Ey
del Ez
del Hx
del Hy
del Hz
del arrTemp
del strTemp