from __future__ import absolute_import, division, print_function

import procrunner


def test_export_best(dials_data, tmpdir):
    result = procrunner.run(
        [
            "dials.import",
            "template="
            + dials_data("centroid_test_data").join("centroid_####.cbf").strpath,
        ],
        working_directory=tmpdir.strpath,
    )
    assert not result.returncode and not result.stderr
    result = procrunner.run(
        ["dials.find_spots", "imported.expt"], working_directory=tmpdir.strpath
    )
    assert not result.returncode and not result.stderr
    result = procrunner.run(
        ["dials.index", "imported.expt", "strong.refl", "space_group=P422"],
        working_directory=tmpdir.strpath,
    )
    assert not result.returncode and not result.stderr
    result = procrunner.run(
        [
            "dials.integrate",
            "indexed.expt",
            "indexed.refl",
            "prediction.padding=0",
            "sigma_m_algorithm=basic",
        ],
        working_directory=tmpdir.strpath,
    )
    assert not result.returncode and not result.stderr
    result = procrunner.run(
        ["dials.export_best", "integrated.expt", "integrated.refl"],
        working_directory=tmpdir.strpath,
    )
    assert not result.returncode and not result.stderr

    assert tmpdir.join("best.dat").check()
    assert tmpdir.join("best.hkl").check()
    assert tmpdir.join("best.par").check()

    with tmpdir.join("best.dat").open("r") as f:
        lines = "".join(f.readlines()[:10])
    assert (
        lines.replace("\r", "")
        == """\
  181.8877       0.77       1.60
   63.1895       1.59       1.81
   38.2372       1.87       1.71
   27.4131       1.84       1.55
   21.3655       1.89       1.51
   17.5043       1.88       1.49
   14.8254       1.89       1.45
   12.8580       1.91       1.45
   11.3518       1.89       1.42
   10.1617       1.87       1.41
"""
    )

    with tmpdir.join("best.hkl").open("r") as f:
        lines = "".join(f.readlines()[:10])
    assert (
        lines.replace("\r", "")
        == """\
 -20   27   -8      20.17      20.00
 -20   27   -7      74.13      21.59
 -20   27   -6      22.34      19.57
 -20   27   -5       6.33      19.72
 -20   28  -10      19.77      18.77
 -20   28   -9      50.37      20.28
 -20   28   -7      69.23      21.42
 -20   28   -6      24.42      19.56
 -20   28   -4     -10.35      19.51
 -20   28   -2      47.53      20.49
"""
    )

    lines = tmpdir.join("best.par").read()
    assert (
        lines.replace("\r", "")
        == """\
# parameter file for BEST
TITLE          From DIALS
DETECTOR       PILA
SITE           Not set
DIAMETER       434.64
PIXEL          0.172
ROTAXIS        0.01 0.00 1.00 FAST
POLAXIS        0.00 1.00 0.00
GAIN               1.00
CMOSAIC            0.54
PHISTART           0.00
PHIWIDTH           0.20
DISTANCE         191.09
WAVELENGTH      0.97950
POLARISATION    0.99900
SYMMETRY       P422
UB             -0.012248 -0.020067  0.003152
               -0.005029 -0.000351 -0.024623
                0.019651 -0.012597 -0.004336
CELL              42.20    42.20    39.68  90.00  90.00  90.00
RASTER           7 7 5 3 3
SEPARATION      0.665  0.665
BEAM            219.875  212.612
# end of parameter file for BEST
"""
    )

@@ -1607,18 +1607,18 @@
       0.
-77
+50
        1
@@ -1638,20 +1638,20 @@
 5       
-1.59
+0.63
        1
@@ -1651,18 +1651,18 @@
       1.
-81
+90
 %0A   38.2
@@ -1665,34 +1665,34 @@
 38.2372       1.
-87
+15
        1.71%0A   2
@@ -1688,10 +1688,10 @@
   1.
-71
+93
 %0A   
@@ -1710,10 +1710,10 @@
   1.
-8
 4
+1
     
@@ -1717,17 +1717,17 @@
       1.
-5
+7
 5%0A   21.
@@ -1731,34 +1731,34 @@
 21.3655       1.
-89
+57
        1.51%0A   1
@@ -1750,18 +1750,18 @@
       1.
-51
+69
 %0A   17.5
@@ -1772,18 +1772,18 @@
       1.
-88
+62
        1
@@ -1783,18 +1783,18 @@
       1.
-49
+65
 %0A   14.8
@@ -1797,34 +1797,34 @@
 14.8254       1.
+6
 8
-9
        1.45%0A   1
@@ -1812,26 +1812,26 @@
 .68       1.
-4
 5
+9
 %0A   12.8580 
@@ -1838,18 +1838,18 @@
       1.
-91
+72
        1
@@ -1853,10 +1853,10 @@
   1.
-4
 5
+7
 %0A   
@@ -1871,18 +1871,18 @@
       1.
-89
+72
        1
@@ -1882,18 +1882,18 @@
       1.
-42
+53
 %0A   10.1
@@ -1904,18 +1904,18 @@
       1.
-8
 7
+2
        1
@@ -1915,17 +1915,17 @@
       1.
-4
+5
 1%0A%22%22%22%0A  

fix export_best test, fallout from #1087