#Makes the PDB_REDO menu. Modelled after the routines for EDS in get_ebi.py.
#Version 1.0 20101127

import pygtk, gtk, pango
import time
import string
import urllib


def get_redo_full(pdbid,state):
  tempdir = "pdb_redo"
  r = coot_mkdir(tempdir)

  if (r):
    pdbid = string.lower(pdbid[0:4])
    mid = pdbid[1:3]
    redosite = "http://www.cmbi.ru.nl/pdb_redo/"
    pdb_url = redosite + mid + "/" + pdbid + "/" + pdbid + state + ".pdb"
    mtz_url = redosite + mid + "/" + pdbid + "/" + pdbid + state + ".mtz"
    pdb_path = os.path.join(tempdir, pdbid + state + ".pdb")
    mtz_path = os.path.join(tempdir, pdbid + state + ".mtz")

    try:
      s1 = urllib.urlretrieve(pdb_url, pdb_path)
      print "INFO:: read model status: ",s1
    except IOError:
      print "ERROR:: We can't open ", pdb_url
    try:
      s2 = urllib.urlretrieve(mtz_url, mtz_path)
      print "INFO:: read mtz   status: ",s2
    except IOError:
      print "ERROR:: We can't open ", mtz_url 

    r_imol = handle_read_draw_molecule(pdb_path)
    sc_map = make_and_draw_map(mtz_path,"FWT","PHWT","",0,0)
    make_and_draw_map(mtz_path,"DELFWT","PHDELWT","",0,1)
    set_scrollable_map(sc_map)
    if (valid_model_molecule_qm(r_imol)):
      return r_imol
    else:
      return False

  else:
    print "Can't make directory ",tempdir

  
        
def add_coot_menu_separator(menu):
  sep = gtk.MenuItem()
  menu.add(sep)
  #   sep.props.sensitive = False
  sep.show()

have_coot_python = False
try: 
  import coot_python
  have_coot_python = True
except:
  print """BL WARNING:: could not import coot_python module!!
Some things, esp. extensions, may be crippled!"""

if (have_coot_python):
  if coot_python.main_menubar():


     menu = coot_menubar_menu("PDB__REDO")
     if (menu):
       add_simple_coot_menu_menuitem(menu, "Fetch fully optimised PDB from PDB__REDO...",
                                     lambda func: generic_single_entry("Fetch PDB_REDO structure with accession code: ",
                                                      "", " Fetch! ",
			                              lambda text: get_redo_full(text,"_final")))

       add_simple_coot_menu_menuitem(menu, "Fetch conservatively optimised PDB from PDB__REDO...",
                                     lambda func: generic_single_entry("Fetch PDB_REDO structure with accession code: ",
                                                      "", " Fetch! ",
			                              lambda text: get_redo_full(text,"_besttls")))

  else:
	print "BL WARNING:: could not find the main_menubar! Sorry, no PDB__REDO menu!"



