#!/usr/bin/env python
# (C) 2022 Cadence Design Systems, Inc. (Cadence) 
# All rights reserved.
# TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
# provided to current licensees or subscribers of Cadence products or
# SaaS offerings (each a "Customer").
# Customer is hereby permitted to use, copy, and modify the Sample Code,
# subject to these terms. Cadence claims no rights to Customer's
# modifications. Modification of Sample Code is at Customer's sole and
# exclusive risk. Sample Code may require Customer to have a then
# current license or subscription to the applicable Cadence offering.
# THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED.  OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall Cadence be
# liable for any damages or liability in connection with the Sample Code
# or its use.

import sys
from openeye import oechem
from openeye import oeshape
from openeye import oegrid


def main(argv=[__name__]):
    itf = oechem.OEInterface(InterfaceData, argv)

    NPolyMax = itf.GetInt("-NPolyMax")

    ifrefname = itf.GetString("-inputreffile")
    iffitname = itf.GetString("-inputfitfile")
    ofname = itf.GetString("-outputfile")

    refgrid = oegrid.OEScalarGrid()
    if not oegrid.OEReadGrid(ifrefname, refgrid):
        oechem.OEThrow.Fatal("Unable to open %s for reading", ifrefname.c_str());
    
    fitgrid = oegrid.OEScalarGrid()
    if not oegrid.OEReadGrid(iffitname, fitgrid):
        oechem.OEThrow.Fatal("Unable to open %s for reading", iffitname.c_str());

    hermiteoptions = oeshape.OEHermiteOptions()
    hermiteoptions.SetNPolyMax(NPolyMax)
    hermiteoptions.SetUseOptimalLambdas(True)

    options = oeshape.OEHermiteOverlayOptions()
    options.SetHermiteOptions(hermiteoptions)
    overlay = oeshape.OEHermiteOverlay(options)
    overlay.SetupRef(refgrid)

    score = oeshape.OEBestOverlayScore()
    overlay.BestOverlay(score, fitgrid)
    print("Hermite Tanimoto = ", score.GetTanimoto())

    score.Transform(fitgrid)
    if not oegrid.OEWriteGrid(ofname, fitgrid):
        oechem.OEThrow.Fatal("Unable to write grid file");


#############################################################################
InterfaceData = '''
!BRIEF [-inputreffile] <InputReferenceFileName> [-inputfitfile] <InputFitFileName> \
[-outputfile] <OutputFileName> [-NPolyMax] <NPolyMax>

!CATEGORY "input/output options :" 1

  !PARAMETER -inputreffile 1
    !ALIAS -inref
    !TYPE string
    !REQUIRED true
    !BRIEF Input reference grid file name (must have extension .grd)
    !KEYLESS 1
  !END
  !PARAMETER -inputfitfile 2
    !ALIAS -infit
    !TYPE string
    !REQUIRED true
    !BRIEF Input fit grid file name (must have extension .grd)
    !KEYLESS 2
  !END
  !PARAMETER -outputfile 3
    !ALIAS -out
    !TYPE string
    !REQUIRED true
    !BRIEF Output file name
    !KEYLESS 3
  !END

!END

!CATEGORY "Hermite options :" 2

  !PARAMETER -NPolyMax 4
    !ALIAS -NP
    !TYPE int
    !REQUIRED false
    !DEFAULT 4
    !LEGAL_RANGE 0 100
    !BRIEF Resolution parameter of the Hermite Prep
    !KEYLESS 4
  !END

!END
'''

#############################################################################
if __name__ == "__main__":
    sys.exit(main(sys.argv))
