#!/usr/bin/env python
#
# Python script for checking versions of python, java and mosflm
#

VERBOSE = 0
def vprint(message=""):
    if VERBOSE:
        print message


#
# Welcome message
#

print "-------------------------------------------------"
print
print "Test of the DNA installation"
print

import os, sys, string
if not "DNAHOME" in os.environ.keys():
    print "ERROR! DNAHOME not defined."
    sys.exit(1)
DNAHOME=os.getenv("DNAHOME")

print "DNAHOME currently set to:"
print DNAHOME
print

#
# First test versions of Python and Java
#

print "Testing the versions of Python and Java"

status_flag_1 = os.system(os.path.join(DNAHOME,'scripts','test_dna_python_java'))

if not status_flag_1:
    print "Tests ok!"
else:
    print "DNA installation test failed!"
    sys.exit(1)

#
# Now let's try to run some data through the DNA system!
#
# Check that the data is there...
#
# Start with DNAHOME/dna_testdata
#

dna_testdata_dir = os.path.join(DNAHOME, "dna_testdata", "trypsin")
if not os.access(dna_testdata_dir, os.F_OK):
    #
    # No, it's not there...
    # Try one directory level higher:
    #
    dna_testdata_dir = os.path.join(DNAHOME, "..", "dna_testdata", "trypsin")
    if not os.access(dna_testdata_dir, os.F_OK):
        #
        # No, it's not there either!
        #
        print "ERROR - dna_testdata directory not found."
        sys.exit(1)

if VERBOSE:
    print "Test data direcory:"
    print dna_testdata_dir
    print

#
# Ok, we found the data directory - now are the files there?
#

image1 = os.path.join(dna_testdata_dir, "ref-trypsin_1_001.img")
image2 = os.path.join(dna_testdata_dir, "ref-trypsin_1_091.img")

if not os.access(image1, os.F_OK):
        print "ERROR - image %s not accessible!" % image1
        sys.exit(1)

if not os.access(image2, os.F_OK):
        print "ERROR - image %s not accessible!" % image2
        sys.exit(1)

#
# Ok, the images are there - now construct a user defaults config
# for these images.
#

user_defaults_xml = """<?xml version="1.0"?>
<user_defaults>
  <default_values>
    <fileinfo>
      <directory>%s</directory>
      <prefix>trypsin</prefix>
      <suffix>img</suffix>
      <run_number>1</run_number>
    </fileinfo>
    <oscillation_sequence>
      <start>1.000000</start>
      <range>1.000000</range>
      <number_of_images>1</number_of_images>
      <exposure_time>1.000000</exposure_time>
      <start_image_number>1</start_image_number>
      <number_of_passes>3</number_of_passes>
    </oscillation_sequence>
    <detector>
      <type>adsc</type>
    </detector>
    <beam>
      <x>94.8</x>
      <y>92.9</y>
    </beam>
    <resolution>2.500000</resolution>
  </default_values>
  <index_parameters>
    <warning_index_spot_rms_error>0.200000</warning_index_spot_rms_error>
    <max_index_spot_rms_error>0.400000</max_index_spot_rms_error>
    <warning_beam_shift>0.100000</warning_beam_shift>
    <max_beam_shift>0.200000</max_beam_shift>
    <warning_index_spot_frac_rejected>0.100000</warning_index_spot_frac_rejected>
    <max_index_spot_frac_rejected>0.200000</max_index_spot_frac_rejected>
    <min_threshold_I_sigma>10.000000</min_threshold_I_sigma>
  </index_parameters>
</user_defaults>""" % dna_testdata_dir

sys.path.append(os.path.join(DNAHOME,'xsd','python'))
try:
    import XSD
except:
    print "ERROR importing the XSD - something is wrong with your"
    print "DNA installation!"

user_defaults = XSD.User_defaults()
user_defaults.unmarshal(user_defaults_xml)

#
# Save the new configuration
#

DNA_USER_DEFAULTS = os.path.join(os.getcwd(), "dnatest_user_defaults.xml")

try:
    ud_file=open(DNA_USER_DEFAULTS,"w")
    ud_file.write(user_defaults.marshal())
    ud_file.close()
except:
    print "Error when writing the new user defaults"
    print
    raise
    
#
# Now check that the configuration and MOSFLM version are ok
#

print "Testing the configuration and version of MOSFLM"

status_flag_2 = os.system(os.path.join(DNAHOME,'scripts','test_dna_config_mosflm'))

if not status_flag_2:
    print "Tests ok!"
else:
    print "DNA installation test failed!"
    sys.exit(1)

#
# Start the DNA system!
#

os.system("export DNA_USER_DEFAULTS=%s; %s/scripts/dna" % \
                  (DNA_USER_DEFAULTS, DNAHOME))
