Package nMOLDYN :: Package GUI :: Module ASCIIToNetCDFConversionDialog
[hide private]
[frames] | no frames]

Source Code for Module nMOLDYN.GUI.ASCIIToNetCDFConversionDialog

  1  """This modules implements I{File-->Convert ASCII to NetCDF} dialog. 
  2   
  3  Classes: 
  4      * ASCIIToNetCDFConversionDialog: creates I{File-->Convert ASCII to NetCDF} dialog used to  
  5        convert a file in ASCII format to a file in NetCDF format. 
  6  """ 
  7   
  8  # The python distribution modules 
  9  import copy 
 10  import os 
 11  import sys 
 12   
 13  # The Tcl/Tk modules 
 14  from tkFileDialog import askopenfilename 
 15  from Tkinter import * 
 16   
 17  # The ScientificPython modules 
 18  from Scientific.IO.NetCDF import NetCDFFile 
 19  from Scientific import N as Num 
 20   
 21  # The nMOLDYN modules 
 22  from nMOLDYN.Core.Error import Error 
 23  from nMOLDYN.Core.IOFiles import convertASCIIToNetCDF 
 24  from nMOLDYN.Core.Logger import LogMessage 
 25  from nMOLDYN.Core.Preferences import PREFERENCES 
 26  from nMOLDYN.GUI.Widgets import * 
 27   
28 -class ASCIIToNetCDFConversionDialog(Toplevel):
29 """Sets up a dialog from where the user can convert a file with numeric data in ASCII or CDL format to NetCDF format. 30 31 The ASCII file may contain some comments introduced with the # character. These comments will also be written 32 in the NetCDF output file (|comment| attribute). The numeric datas have to be organized by column. The only 33 restriction is that all the columns should have the same length. 34 """ 35
36 - def __init__(self, parent, title = None):
37 """The constructor. 38 39 @param parent: the parent widget. 40 41 @param title: a string specifying the title of the dialog. 42 @type title: string 43 """ 44 45 Toplevel.__init__(self, parent) 46 self.transient(parent) 47 48 if title: 49 self.title(title) 50 51 self.parent = parent 52 53 body = Frame(self) 54 self.initial_focus = self.body(body) 55 body.grid(row = 0, column = 0, sticky = EW) 56 57 self.buttonbox() 58 59 self.grab_set() 60 61 if not self.initial_focus: 62 self.initial_focus = self 63 64 self.protocol("WM_DELETE_WINDOW", self.cancel) 65 66 self.resizable(width = NO, height = NO) 67 68 self.geometry("+%d+%d" % (parent.winfo_rootx()+50, parent.winfo_rooty()+50)) 69 70 self.initial_focus.focus_set() 71 72 self.wait_window(self)
73
74 - def body(self, master):
75 """ 76 Create dialog body. Return widget that should have initial focus. 77 """ 78 79 settingsFrame = LabelFrame(master, text = 'Settings', bd = 2, relief = GROOVE) 80 settingsFrame.grid(row = 0, column = 0, sticky = EW, padx = 3, pady = 3) 81 settingsFrame.grid_columnconfigure(0, weight = 1) 82 83 # The combo widget for the file browser. 84 self.inputFileBrowser = ComboFileBrowser(settingsFrame,\ 85 frameLabel = "ASCII input file",\ 86 tagName = 'convert_ascii_to_netcdf_ascii_input_file',\ 87 contents = '',\ 88 save = False,\ 89 command = self.openASCIIFile) 90 self.inputFileBrowser.grid(row = 0, column = 0, sticky = EW, padx = 2, pady = 2) 91 self.inputFileBrowser.grid_columnconfigure(0, weight = 1) 92 self.inputFileBrowser.entry.bind('<Return>', self.openASCIIFile) 93 94 # The combo widget for the file browser. 95 self.outputFileBrowser = ComboFileBrowser(settingsFrame,\ 96 frameLabel = "NetCDF output file",\ 97 tagName = 'convert_ascii_to_netcdf_netcdf_output_file',\ 98 contents = '',\ 99 save = True,\ 100 filetypes = [("NetCDF file", ".nc")]) 101 self.outputFileBrowser.grid(row = 3, column = 0, sticky = EW, padx = 2, pady = 2) 102 self.outputFileBrowser.grid_columnconfigure(0, weight = 1)
103
104 - def buttonbox(self):
105 """ 106 Add standard button box. 107 """ 108 109 # The frame that contains the 'Cancel' and 'OK' buttons. 110 box = LabelFrame(self, text = 'Actions', bd = 2, relief = GROOVE) 111 box.grid(row = 1, column = 0, sticky = EW, padx = 3, pady = 3) 112 box.grid_columnconfigure(0, weight = 1) 113 114 w = Button(box, text = "Cancel", width=10, command = self.cancel) 115 w.grid(row = 0, column = 0, sticky = E) 116 w = Button(box, text = "OK", width=10, command = self.ok, default=ACTIVE) 117 w.grid(row = 0, column = 1, sticky = E) 118 119 self.bind("<Return>", self.ok) 120 self.bind("<Escape>", self.cancel)
121 122 # Standard button semantics.
123 - def ok(self, event = None):
124 125 if not self.validate(): 126 self.initial_focus.focus_set() 127 return 128 129 self.withdraw() 130 self.update_idletasks() 131 132 self.apply() 133 134 self.cancel()
135
136 - def cancel(self, event=None):
137 138 # Put focus back to the parent window 139 self.parent.focus_set() 140 self.destroy()
141 142 # Command hooks
143 - def validate(self):
144 # An intput file must have been set. 145 self.inputFile = self.inputFileBrowser.getValue() 146 if not self.inputFile: 147 raise Error('Please enter an ASCII input file.') 148 149 self.outputFile = self.outputFileBrowser.getValue() 150 # An output file must have been set. 151 if not self.outputFile: 152 raise Error('Please enter a NetCDF output file.') 153 154 return True
155 156 # Command hooks
157 - def apply(self):
158 """ 159 This method is called when the user clicks on the OK button of the conversion dialog. It performs the 160 conversion from the loaded NetCDF file to the selected ASCII file. 161 """ 162 163 convertASCIIToNetCDF(self.inputFile, self.outputFile) 164 LogMessage('info', 'Conversion successful', ['gui'])
165
166 - def openASCIIFile(self, event = None):
167 """ 168 This method/callback is called when the user press Return on the entry of the input file browser 169 or browse directlry from the file browser. It will set the filebrowser entry to the name of the browsed 170 file and propose and set a name for the output file based on the basename of the browsed file. 171 """ 172 173 # Case where the user enters a file name directly in the entry widget without using the browser. 174 if event is not None: 175 if event.widget == self.inputFileBrowser.entry: 176 filename = self.inputFileBrowser.getValue() 177 else: 178 return 179 180 else: 181 # The name of the ASCII file to load. 182 filename = askopenfilename(parent = self,\ 183 filetypes = [('CDL file','.cdl'), ('All files', '.*')],\ 184 initialdir = PREFERENCES.outputfile_path) 185 186 if filename: 187 # The filebrowser entry is updated with the loaded filename. 188 self.inputFileBrowser.setValue(filename) 189 190 # A default filename for the output NetCDF file is built. 191 self.outputFileBrowser.setValue(os.path.splitext(filename)[0] + '.nc') 192 193 return 'break'
194