/* Program xray2hin -- converts xyz atomic data and x-ray data to .HIN format. Written by Joel Polowin. Last mod 19:45 Dec 14, 1996. Copyright 1996 by Hypercube, Inc. */ #include #include #include #include #include void syntax() { fprintf(stderr, "Program xray2hin converts xyz atomic data to .HIN format.\n\n" " Syntax: xray2hin [infile] [> outfile]\n\n" "Data on input lines should be separated by spaces or commas.\n" "If the first line contains six numbers, the file is\n" "interpreted as crystal data beginning with a, b, c,\n" "alpha, beta, gamma values. Otherwise lines contain\n" "x,y,z coordinates with an atom type preceding or\n" "following them. Lines which can't be parsed as above\n" "are converted to comment lines in the output.\n\n" "To assign bonds, read this program's output into HyperChem,\n" "save in Z-matrix format, and read that file back in.\n\n" "Copyright 1996 by Hypercube, Inc.\n"); exit(1); } void main(argc,argv) int argc; char *argv[]; { void syntax(); char line[85],atom[85],atomtype[5],element[4]; FILE *infile; int atomno=0,crystal=0; float a,b,c,alpha,beta,gamma; float sina,cosa,cosb,sing,cosg,pirad,fact1,fact2; float xin,yin,zin,xout,yout,zout; if(argc>2) syntax(); if(argc==2) { if(!(infile=fopen(argv[1],"r"))) { fprintf(stderr,"Can't open file %s.\n",argv[1]); syntax(); } } else infile=stdin; printf("sys 0\n"); /* opening comments for .HIN file */ element[3]=NULL; for(;;) { if(NULL==fgets(line,85,infile)) break; if(84==strlen(line)) fprintf(stderr,"* Warning: truncated line.\n%s\n",line); if((atomno==0) && !crystal && (crystal= ((6==sscanf(line,"%f %f %f %f %f %f",&a,&b,&c,&alpha,&beta,&gamma))|| (6==sscanf(line,"%f,%f,%f,%f,%f,%f",&a,&b,&c,&alpha,&beta,&gamma))) )) /* found a crystal header */ { pirad=180./3.14159265; sina=sin(alpha/pirad); cosa=cos(alpha/pirad); cosb=cos(beta/pirad); sing=sin(gamma/pirad); cosg=cos(gamma/pirad); fact1=(cosb-cosa*cosg)/sing; fact2=sqrt(sina*sing-fact1*fact1); continue; } if((4!=sscanf(line,"%s %f %f %f",atom,&xout,&yout,&zout)) && (4!=sscanf(line,"%f %f %f %s",&xout,&yout,&zout,atom)) && (4!=sscanf(line,"%f,%f,%f,%s",&xout,&yout,&zout,atom)) && (4!=sscanf(line,"%[^,],%f,%f,%f",atom,&xout,&yout,&zout))) { printf(";%s",line); continue; } sscanf(atom,"%4s",atomtype); atomno++; element[0]=toupper(atomtype[0]); element[1]=(isalpha(atomtype[1]))?tolower(atomtype[1]):'\0'; element[2]=(isalpha(atomtype[2]))?tolower(atomtype[2]):'\0'; if(crystal) { xin=a*xout; yin=b*yout; zin=c*zout; xout=xin*sing+zin*fact1; yout=yin+xin*cosg+zin*cosa; zout=zin*fact2; } printf("mol %d\natom 1 %s %s ** - 0 %f %f %f 0\nendmol %d\n", atomno,atomtype,element,xout,yout,zout,atomno); } if(infile!=stdin) fclose(infile); exit(0); }