# Crystallographic Information File (cif) (for v1.2+)
# Created:	 10/04/2009
# Last modified: 10/02/2011
# ChangeLog:
#	10/02/2011 - Added basic support for data contained in mmCIFs.
#	28/05/2009 - Near total rewrite.
#	21/05/2009 - Major fixes - data in loops was not read correctly.

filter(type="importmodel", name="Crystallographic Information File", nickname="cif", extension="cif", glob="*.cif", id=8)
{
	# CIF files are dictionary-based, so we can recognise keywords as we find them.
	# Loops are a little more complicated. For these we will build up a custom format string
	# and then use it to read in the data once the loop definitions are finished.

	# Variables
	string discard,data1,data2,data3,keywd,line,char,looptype,args[50],keywdstripped;
	int n,arg,cellspec,id_r[3],id_e,id_gen,nread,inloop,nloopdata,fractional = TRUE;
	double length[3],cellangle[3];

	# Recognise quoting
	addreadoption("skipblanks");
	addreadoption("usequotes");

	model m = newmodel("CifModel");
	cellspec = 0;

	inloop = 0;

	while (!eof())
	{
		# Read an entire line
		getline(line);
		# Check the first item as a keyword
		readvar(line, keywd, data1);
		if (keywd == "") continue;
		readvarf(keywd, "%1s", char);

		if ((keywd == "_chemical_name_common") || (keywd == "_chem_comp.name")) { setname(data1); inloop = 0; continue; }
		else if (keywd == "_cell_length_a") { length[1] = atof(data1); cellspec++; inloop = 0; continue; }
		else if (keywd == "_cell_length_b") { length[2] = atof(data1); cellspec++; inloop = 0; continue; }
		else if (keywd == "_cell_length_c") { length[3] = atof(data1); cellspec++; inloop = 0; continue; }
		else if (keywd == "_cell_angle_alpha") { cellangle[1] = atof(data1); cellspec++; inloop = 0; continue; }
		else if (keywd == "_cell_angle_beta") { cellangle[2] = atof(data1); cellspec++; inloop = 0; continue; }
		else if (keywd == "_cell_angle_gamma") { cellangle[3] = atof(data1); cellspec++; inloop = 0; continue; }
		# Spacegroup name - strip spaces which are sometimes be inserted by other programs
		else if (keywd == "_symmetry_space_group_name_H-M") { spacegroup(data1); inloop = 0; continue; }
		# Loops
		else if (keywd == "loop_") { inloop = 1; nloopdata = 0; looptype = "none"; continue; }
		# End of loop data items (if first char is a '_')
		else if (inloop > 0)
		{
			if ((char == "_") && (inloop == 2)) inloop = 0;
			else if ((char != "_") && (inloop == 1)) inloop = 2;
		}

		# Loop handling
		if (inloop == 1)		# Loop data specification
		{
			keywdstripped = replacechars(keywd,"_."," ");
			readvar(keywdstripped, data1, data2, data3);
			if ((data1 == "atom") && (data2 == "site"))
			{
				if (data3 == "aniso") continue;
				looptype = "atom";
				nloopdata++;
				if (keywd == "_atom_site_type_symbol") id_e = nloopdata;
				else if (keywd == "_atom_site_fract_x") id_r[1] = nloopdata;
				else if (keywd == "_atom_site_fract_y") id_r[2] = nloopdata;
				else if (keywd == "_atom_site_fract_z") id_r[3] = nloopdata;
			}
			else if ((data1 == "symmetry") && (data2 == "equiv"))
			{
				if (m.cell.sgid <> 0)
				{
					printf("Generator data ignored - spacegroup is already set.\n");
					inloop = 0;
					continue;
				}
				looptype = "gen";
				nloopdata++;
				if (keywd == "_symmetry_equiv_pos_as_xyz") id_gen = nloopdata;
			}
			else if (((data1 == "space") && (data2 == "group")) || (data3 == "symop"))
			{
				if (m.cell.sgid <> 0)
				{
					printf("Generator data ignored - spacegroup is already set.\n");
					inloop = 0;
					continue;
				}
				looptype = "gen";
				nloopdata++;
				if (keywd == "_space_group_symop_operation_xyz") id_gen = nloopdata;
			}
			else if ((data1 == "chem") && (data2 == "comp") && (data3 == "atom"))
			{
				# PDB-style atom section (as found in mmCIFs)
				if (data3 == "aniso") continue;
				looptype = "atom";
				fractional = FALSE;
				nloopdata++;
				if (keywd == "_chem_comp_atom.type_symbol") id_e = nloopdata;
				else if (keywd == "_chem_comp_atom.model_Cartn_x") id_r[1] = nloopdata;
				else if (keywd == "_chem_comp_atom.model_Cartn_y") id_r[2] = nloopdata;
				else if (keywd == "_chem_comp_atom.model_Cartn_z") id_r[3] = nloopdata;
			}
		}
		else if (inloop == 2)		# Loop data items
		{
			# Read in 'nloopdata' delimited arguments
			for (n = 0; n < nloopdata; n++)
			{
				while (readnext(args[n+1]) == FALSE) getline(line);
				#printf("Arg/data %i = '%s'\n", nread, args[nread+1] );
			}

			# Our next action depends on the loop type
			if (looptype == "atom")
			{
				newatom(args[id_e], atof(args[id_r[1]]), atof(args[id_r[2]]), atof(args[id_r[3]]));
				#printf("Created atom %s with coords %f %f %f\n", args[id_e], atof(args[id_r[1]]), atof(args[id_r[2]]), atof(args[id_r[3]]));
			}
			else if (looptype == "gen") addgenerator(args[id_gen]);
		}
	}

	# Were we given a full cell spec?
	if (cellspec == 6)
	{
		cell(length[1],length[2],length[3],cellangle[1],cellangle[2],cellangle[3]);
		if (fractional) fractoreal();
		# Pack according to spacegroup
		pack();
	}
	else
	{
		printf("Warning: Full cell specification was not found in file.\n");
		if (fractional) printf("Warning: Atom coordinates are still fractional.\n");
	}

	rebond();
	finalisemodel();
}
