#!/usr/bin/env cctbx.python

#
#  Copyright (C) (2013) STFC Rutherford Appleton Laboratory, UK.
#
#  Author: David Waterman.
#
#  This code is distributed under the BSD license, a copy of which is
#  included in the root directory of this package.
#

"""
Test command line program dials.refine by running a job with saved data and
comparing with expected output.

This serves as a high level test that not only checks whether refinement works,
but also that the command line program is functioning and that the output models
have not changed format and so on.

"""

# python imports
from __future__ import division
import os
import libtbx.load_env # required for libtbx.env.find_in_repositories
from libtbx import easy_run
from libtbx.test_utils import approx_equal
from libtbx.test_utils import open_tmp_directory
from dials.model.experiment.experiment_list import ExperimentListFactory

def test1():

  dials_regression = libtbx.env.find_in_repositories(
    relative_path="dials_regression",
    test=os.path.isdir)

  # use the i04_weak_data for this test
  data_dir = os.path.join(dials_regression, "refinement_test_data", "i04_weak_data")
  experiments_path = os.path.join(data_dir, "experiments.json")
  pickle_path = os.path.join(data_dir, "indexed_strong.pickle")

  for pth in ():
    assert os.path.exists(pth)

  cmd = "dials.refine " + experiments_path + " " + pickle_path

  # work in a temporary directory
  cwd = os.path.abspath(os.curdir)
  tmp_dir = open_tmp_directory(suffix="test_dials_refine")
  os.chdir(tmp_dir)
  result = easy_run.fully_buffered(command=cmd).raise_if_errors()
  os.chdir(cwd)

  # load results
  reg_exp = ExperimentListFactory.from_json_file(
              os.path.join(data_dir, "regression_experiments.json"),
              check_format=False)[0]
  ref_exp = ExperimentListFactory.from_json_file(
              os.path.join(tmp_dir, "refined_experiments.json"),
              check_format=False)[0]

  # test refined models against expected
  assert reg_exp.crystal == ref_exp.crystal
  assert reg_exp.detector == ref_exp.detector
  assert reg_exp.beam == ref_exp.beam

  print "OK"
  return

def run():
  if not libtbx.env.has_module("dials_regression"):
    print "Skipping tests in " + __file__ + " as dials_regression not present"
    return

  test1()

if __name__ == '__main__':
  import sys
  from libtbx.utils import show_times_at_exit
  show_times_at_exit()
  run()

@@ -618,16 +618,30 @@
 port os%0A
+import shutil%0A
 import l
@@ -1542,16 +1542,25 @@
 mp_dir)%0A
+  try:%0A  
   result
@@ -1623,23 +1623,8 @@
 )%0A  
-os.chdir(cwd)%0A%0A
   # 
@@ -1636,16 +1636,18 @@
 results%0A
+  
   reg_ex
@@ -1680,32 +1680,34 @@
 from_json_file(%0A
+  
               os
@@ -1765,32 +1765,34 @@
 ,%0A              
+ 
+ 
 check_format=Fal
@@ -1798,16 +1798,18 @@
 lse)%5B0%5D%0A
+  
   ref_ex
@@ -1853,45 +1853,8 @@
 ile(
-%0A              os.path.join(tmp_dir, 
 %22ref
@@ -1867,35 +1867,36 @@
 xperiments.json%22
-)
 ,%0A
+  
               ch
@@ -1915,16 +1915,95 @@
 alse)%5B0%5D
+%0A  finally:%0A    os.chdir(cwd)%0A    # clean up tmp dir%0A    shutil.rmtree(tmp_dir)
 %0A%0A  # te

remove temporary directory after completing (or failing) test