File: raster3d.py

package info (click to toggle)
coot 1.1.18%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 219,964 kB
  • sloc: cpp: 495,934; python: 35,043; ansic: 26,143; lisp: 22,768; sh: 13,186; makefile: 2,746; awk: 441; xml: 245; csh: 14
file content (283 lines) | stat: -rw-r--r-- 10,725 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# raster3d.py
#
# Copyright 2005, 2006 by Bernhard Lohkamp
# Copyright 2007 by Bernhard Lohkamp, The University of York
# Copyright 2005, 2006 by Paul Emsley, The University of York
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


# BL pyton script for rendering images in COOT

# args not including the output filename
def povray_args():
    return ["+FN16", "+A"]
# BL says: dont know how usefull this function is/will be....

# run raster3d
#
def render_image(use_popen=False):
    import os
    import webbrowser
    import sys

    coot_r3d_file_name="coot.r3d"
    version = raster3d_version()
    if (os.name == 'nt' and version[0] == 2):
        coot_image_file_name = "coot.tif"
        image_format = " -tiff "
    else:
        coot_image_file_name = "coot.png"
        image_format = " -png "
    coot.raster3d(coot_r3d_file_name)
    r3d_exe = coot_utils.find_exe("render", "PATH")
    if (r3d_exe):
       r3d_dir = os.path.dirname(r3d_exe)
       os.environ['R3D_LIB'] = r3d_dir + "/materials"
       r3d_call = r3d_exe + image_format + " -labels " + coot_image_file_name + " < " + coot_r3d_file_name
       print("DEBUG:: r3d_call is ", r3d_call)
       print("calling render...")

       major, minor, micro, releaselevel, serial = sys.version_info
       if (major >= 2 and minor >=4):
           # new style
           import subprocess
           if not use_popen:
               status = subprocess.call(r3d_call, shell=True)
           else:
               status = popen_command(r3d_exe,
                                      ["-labels", "-png", coot_image_file_name],
                                      coot_r3d_file_name, "r3d.log", 1)
           if status:
               # something went wrong with raster3d
               # maybe same for system call?!?
               print("BL WARNING:: some error in raster3d")
               return
       else:
           status = os.system(r3d_call)
       print("calling display...")
       try:
         webbrowser.open(coot_image_file_name,1,1)
       except OSError:
         print("BL WARNING:: We can't find rendered file ",coot_image_file_name)

# Run either raster3d or povray
#
# @var{image_type} is either 'raster3d' or 'povray'
#
def raytrace(image_type, source_file_name, image_file_name, x_size, y_size):
   import os
   import webbrowser
   import sys

   if (image_type == "raster3d"):
    # BL says: this is for windows, since tif file
    version = raster3d_version()
    do_tif = False
    if version:
        if version[0] == 2:
            do_tif = True
    if (os.name == 'nt' and do_tif):
        render_exe = "render.exe"
        image_file_name += ".tif"
        image_format = " -tiff "
    else:
        render_exe = "render"
        image_format = " -png "
    r3d_exe = coot_utils.find_exe("render", "CCP4_BIN", "PATH")
    if (r3d_exe):
       r3d_dir = os.path.dirname(r3d_exe)
       os.environ['R3D_LIB'] = r3d_dir + "/materials"
       #we have to check filenames for spaces for dodgy windows path
       image_file_name_mod, source_file_name_mod, space_flag = \
       check_file_names_for_space_and_move(image_file_name, source_file_name)
       r3d_call = r3d_exe + image_format + " -labels" + image_file_name_mod + " < " + source_file_name_mod
       print("DEBUG:: r3d_call is ", r3d_call)
       print("calling render...")

       major, minor, micro, releaselevel, serial = sys.version_info
       if (major >= 2 and minor >=4):
           import subprocess
           status = subprocess.call(r3d_call, shell=True)
           if status:
               # something went wrong with raster3d
               # maybe same for system call?!?
               print("BL WARNING:: some error in raster3d")
               return
       else:
           status = os.system(r3d_call)
       # now we have to copy files back if necessary!!
       if (space_flag):
          import shutil
          shutil.move(image_file_name_mod,image_file_name)
          shutil.move(source_file_name_mod,source_file_name)

       print("calling display...")
       try:
         webbrowser.open(image_file_name,1,1)
       except OSError:
         print("BL WARNING:: We can't find rendered file ",image_file_name)

   elif (image_type == "povray"):
    image_file_name += ".png"
    #BL says: conversion of filename, again! Windows is driving me crazy...
    image_file_name = os.path.normpath(image_file_name)
    # again, we just assume povray exe is pvengine on all systems
    povray_exe = coot_utils.find_exe(povray_command_name, "PATH")
    if (povray_exe):
      image_file_name_mod, source_file_name_mod, space_flag = \
        check_file_names_for_space_and_move(image_file_name, source_file_name)
      if (os.name == 'nt'):
          args = ["/EXIT", "/RENDER"]
      else:
          args = " "
      args += [source_file_name_mod] + povray_args() + ["-UV" , "+W" + str(x_size) , "+H" + str(y_size)]
      print("BL INFO:: run povray with args: ", args)
      povray_call = [povray_exe] +  args + ["+o" + image_file_name_mod]
      print("BL DEBUG:: povray command line", povray_call)
      major, minor, micro, releaselevel, serial = sys.version_info
      if (major >= 2 and minor >=4):
          import subprocess
          status = subprocess.call(povray_call)
          if status:
              # something went wrong with raster3d
              # maybe same for system call?!?
              print("BL WARNING:: some error in povray")
              return
      else:
          os.system(povray_call)
      # now we have to copy files back if necessary!!
      if (space_flag):
         import shutil
         shutil.move(image_file_name_mod,image_file_name)
         shutil.move(source_file_name_mod,source_file_name)
      else: pass
      print("calling display...")
      try:
         webbrowser.open(image_file_name,1,1)
      except OSError:
         print("BL WARNING:: We can't find rendered file ",image_file_name)

   else:
     print("Image type ", image_type, " unknown!")

# Converts a ppm file to a bmp file (for windows users) and opens in
# browser or viewer
# actually dont use ppm any more but png, so not needed as such any more
# keep for backwards compatibility
#
def ppm2bmp(ppm_file_name):
    import os
    import webbrowser
    import sys

    bmp_file_name, extension = os.path.splitext(ppm_file_name)
    # FIXME: horrible hacks!!!
    if (extension == ".ppm"):
        bmp_file_name += ".bmp"

        # BL says: need to do some wired stuff to make sure cmd/system works with
        # space in file name , e.g. ' & " thingys
        cmd = 'ppm2bmp "'
        ppm2bmp_call = cmd + ppm_file_name + '"'
        major, minor, micro, releaselevel, serial = sys.version_info
        if (major >= 2 and minor >=4):
            import subprocess
            status = subprocess.call(ppm2bmp_call, shell=True)
            if status:
                # something went wrong with raster3d
                # maybe same for system call?!?
                print("BL WARNING:: some error in ppm2bmp")
                return
        else:
            os.system(ppm2bmp_call)
    if (extension == ".png"):
        bmp_file_name = ppm_file_name
    if (not os.path.isfile(bmp_file_name)):
        print("BL WARNING:: Cannot find png/bmp file ", bmp_file_name)
    else:
        print("calling display...")
        try:
            webbrowser.open(bmp_file_name,1,1)
        except OSError:
            print("BL WARNING:: We can't open screendump file ",bmp_file_name)

# Tests file names for spaces. there is certainly on problem on windows
# not sure about other OS, yet
def check_file_names_for_space_and_move(image_file_name,source_file_name):

    import string, os, shutil

    space_flag = False

    if (((image_file_name.find(" ") > -1) or (source_file_name.find(" ") > -1)) and (os.name == 'nt')):
       # we have spaces, so tmp copy src to C: and run there, then
       # copy tmp back to where it should be
       image_file_name_mod = "C:\\" + os.path.basename(image_file_name)
       source_file_name_mod = "C:\\" + os.path.basename(source_file_name)
       shutil.move(source_file_name,source_file_name_mod)
       space_flag = True
       return image_file_name_mod, source_file_name_mod, space_flag
    else:
       return image_file_name, source_file_name, space_flag

#raytrace("povray","coot.pov","coot.png",600,600)

# return version as a tuple of 3 numbers (or 2 plus letter)
# or False if not found or problems running raster3d
#
def raster3d_version():

    import os

    raster3d_exe = coot_utils.find_exe("render", "CCP4_BIN", "PATH")

    if not raster3d_exe:
        return False
    else:
        log_file = "raster3_version.tmp"
        # BL note: currently -i is a bogus switch, so gives info
        status = coot_utils.popen_command(raster3d_exe, ["-i"], [], log_file)
        if (status != 0):
            return False
        else:
            fin = open(log_file, 'r')
            lines = fin.readlines()
            fin.close()
            os.remove(log_file)
            for line in lines:
                if ("Raster3D" in line):
                    version_string = line[9:]
                    tmp = version_string.split(".")
                    try:
                        minor = int(tmp_min[0])
                        micro = int(tmp_min[1])
                    except:
                        print("WARNING:: problem extracting major version from raster3d")
                        return False
                    return [major, minor, micro]
                else:
                    # old style
                    if (len(tmp[1]) != 2):
                        print("WARNING:: cannot deal with this version.")
                        return False
                    else:
                        try:
                            minor = int(tmp[1][0])
                        except:
                            print("WARNING:: problem extracting minor version from raster3d")
                            return False
                        micro = tmp[1][1]
                        return [major, minor, micro]
            return False