#!/bin/csh
# modified PDB contains charge information
# Please note: it can handle only 6 atom types
function electronscatteringfactor0(aa,bb,Ntype)
{
# Ntype = 6
# H & C (it is C and H)
aa[1,1]=0.1361;aa[1,2]=0.5482;aa[1,3]= 1.2266;aa[1,4]= 0.5971;aa[1,5]=0;
bb[1,1]=0.3731;bb[1,2]=3.2814;bb[1,3]=13.0456;bb[1,4]=41.0202;bb[1,5]=0; 
aa[2,1]=0.0367;aa[2,2]=0.1269;aa[2,3]= 0.2360;aa[2,4]= 0.1290;aa[2,5]=0;
bb[2,1]=0.5608;bb[2,2]=3.7913;bb[2,3]=13.5557;bb[2,4]=37.7229;bb[2,5]=0; 
# N & O
aa[3,1]=0.1372;aa[3,2]=0.5344;aa[3,3]= 1.0862;aa[3,4]= 0.4547;aa[3,5]=0;
bb[3,1]=0.3287;bb[3,2]=2.6733;bb[3,3]=10.3165;bb[3,4]=32.7631;bb[3,5]=0; 
aa[4,1]=0.1432;aa[4,2]=0.5103;aa[4,3]= 0.9370;aa[4,4]= 0.3923;aa[4,5]=0;
bb[4,1]=0.3055;bb[4,2]=2.2683;bb[4,3]= 8.2625;bb[4,4]=25.6645;bb[4,5]=0; 
# P & S
aa[5,1]=0.3540;aa[5,2]=0.9397;aa[5,3]= 2.6203;aa[5,4]= 1.5707;aa[5,5]=0;
bb[5,1]=0.3941;bb[5,2]=3.1810;bb[5,3]=15.6579;bb[5,4]=49.5249;bb[5,5]=0; 
aa[6,1]=0.3478;aa[6,2]=0.9158;aa[6,3]= 2.5066;aa[6,4]= 1.3884;aa[6,5]=0;
bb[6,1]=0.3652;bb[6,2]=2.8915;bb[6,3]=13.0522;bb[6,4]=40.1848;bb[6,5]=0; 
# Na+1
aa[7,1]=0.0256;aa[7,2]=0.0919;aa[7,3]=0.2970;aa[7,4]=0.5140;aa[7,5]=0.1990;
bb[7,1]=0.0397;bb[7,2]=0.2870;bb[7,3]=1.1800;bb[7,4]=3.7500;bb[7,5]=10.800;
# Cl-1 
aa[8,1]=0.2650;aa[8,2]=0.5960;aa[8,3]=1.6000;aa[8,4]=2.6900;aa[8,5]=1.2300;
bb[8,1]=0.2520;bb[8,2]=1.5600;bb[8,3]=6.2100;bb[8,4]=17.800;bb[8,5]=47.800;
# O-1
aa[9,1]=0.2050;aa[9,2]=0.6280;aa[9,3]=1.1780;aa[9,4]=1.0300;aa[9,5]=0.2900;
bb[9,1]=0.3970;bb[9,2]=2.6400;bb[9,3]=8.8000;bb[9,4]=27.100;bb[9,5]=91.800;
# O-2
aa[10,1]=0.0421;aa[10,2]=0.2100;aa[10,3]=0.8520;aa[10,4]=1.8200;aa[10,5]=1.1700;
bb[10,1]=0.0609;bb[10,2]=0.5590;bb[10,3]=2.9600;bb[10,4]=11.500;bb[10,5]=37.700;
# Mg+2
aa[11,1]=0.0210;aa[11,2]=0.0672;aa[11,3]=0.1980;aa[11,4]=0.3680;aa[11,5]=0.1740;
bb[11,1]=0.0331;bb[11,2]=0.2220;bb[11,3]=0.8380;bb[11,4]=2.4800;bb[11,5]=6.7500;
# K+1
aa[12,1]=0.1990;aa[12,2]=0.3960;aa[12,3]=0.9280;aa[12,4]=1.4500;aa[12,5]=0.4500;
bb[12,1]=0.1920;bb[12,2]=1.1000;bb[12,3]=3.9100;bb[12,4]=9.7500;bb[12,5]=23.400;
}
####
function electronscatteringfactor(aaa,bbb,bfact,sHalf)
{Easf=0; for(j1=1; j1<=5; j1+=1) {Easf=Easf+aaa[j1]*exp(-bbb[j1]*sHalf*sHalf)}; 
Easf=Easf*exp(-bfact*sHalf*sHalf); return Easf}
###
function ccpvolr(cx,ac,v) {dtor=0.017453;
#dtor=atan(1.0)/45.0;
a1=cx[1]; b1=cx[2]; c1=cx[3];
alph=dtor*cx[4]; beta=dtor*cx[5];gamm=dtor*cx[6];sum=0.5*(alph+beta+gamm);
v=sin(sum-alph)*sin(sum-beta)*sin(sum-gamm);
v=2.0*a1*b1*c1*sqrt(sin(sum)*v);
astar=b1*c1*sin(alph)/v; bstar=c1*a1*sin(beta)/v; cstar=a1*b1*sin(gamm)/v;
cosast=(cos(beta)*cos(gamm)-cos(alph))/(sin(beta)*sin(gamm));
cosbst=(cos(gamm)*cos(alph)-cos(beta))/(sin(gamm)*sin(alph));
cosgst=(cos(alph)*cos(beta)-cos(gamm))/(sin(alph)*sin(beta));
ac[1]=astar*astar; ac[2]=bstar*bstar; ac[3]=cstar*cstar;
ac[4]=2.0*bstar*cstar*cosast; ac[5]=2.0*cstar*astar*cosbst; ac[6]=2.0*astar*bstar*cosgst;
return}
function orth(cell,a,isw) {c=0.017453; 
for(i=1; i<=3; i+=1) {cabg[i]=cos(cell[i+3]*c); sabg[i]=sin(cell[i+3]*c)};
cabgs[1]=(cabg[2]*cabg[3]-cabg[1])/(sabg[2]*sabg[3]);
cabgs[2]=(cabg[3]*cabg[1]-cabg[2])/(sabg[3]*sabg[1]);
cabgs[3]=(cabg[1]*cabg[2]-cabg[3])/(sabg[1]*sabg[2]);
vol=cell[1]*cell[2]*cell[3]*sqrt(1.0+2.0*cabg[1]*cabg[2]*cabg[3]-cabg[1]**2-cabg[2]**2-cabg[3]**2);
sabgs1=sqrt(1.0-cabgs[1]**2);
#abcs[1]=cell[2]*cell[3]*sabg[1]/vol;
#abcs[2]=cell[3]*cell[1]*sabg[2]/vol;
#abcs[3]=cell[1]*cell[2]*sabg[3]/vol;
#### fractional to cartesian;
for(i=1; i<=3; i+=1) {for(j=1; j<=3; j+=1) {a[i,j]=0;}};
if(isw=="0") {
a[1,1]=cell[1];
a[1,2]=cell[2]*cabg[3];
a[1,3]=cell[3]*cabg[2];
a[2,2]=cell[2]*sabg[3];
a[2,3]=-cell[3]*sabg[2]*cabgs[1];
a[3,3]=cell[3]*sabg[2]*sabgs1
};
if(isw=="1") {
a[1,1]=1.0/cell[1];
a[1,2]=-cabg[3]/(sabg[3]*cell[1]);
a[1,3]=-(cabg[3]*sabg[2]*cabgs[1]+cabg[2]*sabg[3])/(sabg[2]*sabgs1*sabg[3]*cell[1]);
a[2,2]=1.0/(sabg[3]*cell[2]);
a[2,3]=cabgs[1]/(sabgs1*sabg[3]*cell[2]);
a[3,3]=1.0/(sabg[2]*sabgs1*cell[3]);
}
return }
function product(y,r,x) {
y[1]=x[1]*r[1,1]+x[2]*r[1,2]+x[3]*r[1,3];
y[2]=x[1]*r[2,1]+x[2]*r[2,2]+x[3]*r[2,3];
y[3]=x[1]*r[3,1]+x[2]*r[3,2]+x[3]*r[3,3];
return}
BEGIN{cx[1]=25.0; cx[2]=25.0; cx[3]=25.0; cx[4]=90.; cx[5]=90.0; cx[6]=90.0;
# H, C, N, O, and P, S, Na+, Cl-, O-1, O-2, Mg+2, K+; 12 types;
Ntype=12; electronscatteringfactor0(aa,bb,Ntype);
# resolution limits
resl=100.; resh=0.75; pi=3.14159; twopi=2.0*pi; r2d=180./pi;
minh=0; maxh=int(cx[1]/resh)+1; maxk=int(cx[2]/resh)+1; maxl=int(cx[3]/resh)+1;
constant4ion=+0.023934;
NA=0;
}
#### always ==> atom id stored in 78 with length of 4!!!
###
{if($1=="CRYST1") {cx[1]=$2; cx[2]=$3; cx[3]=$4; cx[4]=$5; cx[5]=$6; cx[6]=$7; 
ccpvolr(cx,ac,v); isw=1; orth(cx,rotman,isw);
minh=0; maxh=int(cx[1]/resh)+1; maxk=int(cx[2]/resh)+1; maxl=int(cx[3]/resh)+1;};
if($1=="ATOM"||$1=="HETATM") {NA=NA+1;
xxx[1]=0.+substr($0,31,8); xxx[2]=0.+substr($0,39,8); xxx[3]=0.+substr($0,47,8);
product(yyy,rotman,xxx); x[NA]=yyy[1]; y[NA]=yyy[2]; z[NA]=yyy[3];
# x[NA]=xxx[1]/cx[1]; y[NA]=xxx[2]/cx[2]; z[NA]=xxx[3]/cx[3];
 b[NA]=0.+substr($0,61,5); atom[NA]=substr($0,78,4); qatom[NA]=0.+substr($0,68,5)};
#print cx[1],cx[2],cx[3],cx[4],cx[5],cx[6], xxx[1], xxx[2],xxx[3],yyy[1],yyy[2],yyy[3];
}
END{
for(h=minh; h<=maxh; h+=1) { for(k=-maxk; k<=maxk; k+=1) { for(l=-maxl; l<=maxl; l+=1) {
ss=h*h*ac[1]+k*k*ac[2]+l*l*ac[3]+k*l*ac[4]+h*l*ac[5]+h*k*ac[6];
s=sqrt(ss); sHalf=s/2.0; 
#print ac[1],ac[2],ac[3],ac[4],ac[5],ac[6],s,resh,resl, maxh,
if(s<=1.0/resh&&s>=1.0/resl) {Apart1=0; Bpart1=0; Apart2=0; Bpart2=0; 
# loop over all atoms
for(i=1; i<=NA; i+=1) {
x0=x[i]; y0=y[i]; z0=z[i]; hx=(h*x0+k*y0+l*z0)*twopi; bfact=b[i]; 
if(atom[i]=="H   ") {Natom=2}; if(atom[i]=="C   ") {Natom=1}; 
if(atom[i]=="  H ") {Natom=2}; if(atom[i]=="  C ") {Natom=1}; 
if(atom[i]=="N   ") {Natom=3}; if(atom[i]=="O   ") {Natom=4}; 
if(atom[i]=="  N ") {Natom=3}; if(atom[i]=="  O ") {Natom=4}; 
if(atom[i]=="P   ") {Natom=5}; if(atom[i]=="S   ") {Natom=6};
if(atom[i]=="  P ") {Natom=5}; if(atom[i]=="  S ") {Natom=6};
if(atom[i]=="NA+1") {Natom=7}; if(atom[i]=="CL-1") {Natom=8};
if(atom[i]=="MG+2") {Natom=11}; if(atom[i]=="K+1 ") {Natom=12};
for(j1=1; j1<=5; j1+=1) {aaa[j1]=aa[Natom,j1]; bbb[j1]=bb[Natom,j1]};
Easf=electronscatteringfactor(aaa,bbb,bfact,sHalf);
Apart1=Apart1+Easf*cos(hx); Bpart1=Bpart1+Easf*sin(hx); 
q=qatom[i];
Easfq=constant4ion*q/sHalf**2*exp(-bfact*sHalf*sHalf);
Apart2=Apart2+Easfq*cos(hx); Bpart2=Bpart2+Easfq*sin(hx);}
############
Apart3=Apart1+Apart2; Bpart3=Bpart1+Bpart2;
F1=sqrt(Apart1**2+Bpart1**2); sF1=1.0; Phi1=atan2(Bpart1,Apart1)*r2d;
F2=sqrt(Apart2**2+Bpart2**2); sF2=1.0; Phi2=atan2(Bpart2,Apart2)*r2d;
F3=sqrt(Apart3**2+Bpart3**2); sF3=1.0; Phi3=atan2(Bpart3,Apart3)*r2d;
printf("%4d%4d%4d %9.3e%5.2f%9.3f %9.3e%5.2f%9.3f %9.3e%5.2f%9.3f\n",h,k,l,F1,sF1,Phi1,F2,sF2,Phi2,F3,sF3,Phi3)}}}}}
#printf("%4d%4d%4d%9.3f%5.2f%9.3f\n",h,k,l,F3,sF3,Phi3)}}}}}
