File: Process.py

package info (click to toggle)
topparser 1.3-4
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,948 kB
  • sloc: python: 2,548; makefile: 17
file content (277 lines) | stat: -rw-r--r-- 12,019 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
#!/usr/bin/python

##
## Copyright (C) 2011-2012 Andrew Atkinson
##
##-------------------------------------------------------------------
## 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/>.
##-------------------------------------------------------------------

#python modules
import os
#import re
import shutil
#import subprocess
#import sys
#import threading
import wx

#Program modules
import CheckerCreate
import ReadTop
import relshots
import Writeth
import Writeth2
import Writethconfig


def Run(settings, cavers):
    ##Check for spaces in top file and raw location, which will break things
    #if re.search(r"\s", settings['pockettopo']):
        #message = settings['pockettopo'] +
        #'''\n has spaces in it, please correct'''
        #if re.search(r"\s", settings['rawdir']):
            #message += '/nand/n' + settings['rawdir'] +
            #'\n has spaces in it, please correct'
    #elif re.search(r"\s", settings['rawdir']):
        #message += settings['rawdir'] + '\n has spaces in it, please correct'
    #

    #Check the top file location is actually a file
    if not os.path.isfile(settings['pockettopo']):
        message = settings['pockettopo'] + '''\n is not a file.'''
        dlg = wx.MessageDialog(None, message, 'PocketTop File',
                               style=wx.OK | wx.ICON_ERROR)
        temp = dlg.ShowModal()
        return

    #The project root must exist, other directories can be created
    if not os.path.exists(settings['projectroot']):
        message = settings['projectroot'] + '''
does not exist.
Topparser will not create this for you.
        '''
        dlg = wx.MessageDialog(None, message, 'Project Directory',
                               style=wx.OK | wx.ICON_ERROR)
        temp = dlg.ShowModal()
        return

    #Put a status box or something in here

    #Assemble the directories, survey names for use
    locations = {}
    topfilename = os.path.basename(settings['pockettopo'])
    locations['surveyname'] = os.path.splitext(topfilename)[0]
    locations['surveydir'] = os.path.join(settings['projectroot'],
                                            settings['surveydir'])
    if settings['addnamedir']:
        locations['surveydir'] = os.path.join(locations['surveydir'],
                                                locations['surveyname'])
    if settings['insurveydir']:
        locations['rawdir'] = os.path.join(locations['surveydir'],
                                settings['rawdir'])
    else:
        locations['rawdir'] = os.path.join(settings['projectroot'],
                    settings['rawdir'])

    #make the directories, add to repository for svn
    #These should raise errors if fail
    for dir in [locations['surveydir'], locations['rawdir']]:
        CheckerCreate.Structure(dir)
        if (settings['add2rep'] & (settings['repository'] == 'svn')):
            CheckerCreate.ShellCall(settings['repository'] + ' add \"' +
            dir + '\"')
    #copy top file
    topfile = os.path.join(locations['rawdir'], topfilename)
    if settings['copytop']:
        name = os.path.basename(topfile)
        if settings['pockettopo'] != topfile:
            copyfile = settings['overwrite'] | (not(os.path.exists(topfile)))
            if not copyfile:
                message = name + ''' already exists.
    Would you like to overwrite.
    (No, will skip the file)'''
                dlg = wx.MessageDialog(None, message, 'Existing file',
                                       style=wx.YES_NO)
                temp = dlg.ShowModal()
                if temp == wx.ID_YES:
                    copyfile = True
                else:
                    print('Not written file ' + name)
                dlg.Destroy()
            if copyfile:
                #do I really want copy2 or just copy!!!
                shutil.copy2(settings['pockettopo'], locations['rawdir'])
                print('Copied top file')
                if (settings['add2rep'] & (settings['repository'] == 'svn')):
                    CheckerCreate.ShellCall(settings['repository'] + ' add \"' +
                    topfile + '\"')
            #Really need an error check here, to see if it is actually added
                    print('Top file added to repository')
        else:
            print(name + ': Source and destination the same. Skipping copy!')

    #If only doing config file do not need to read or calculate data
    if (settings['dothfile'] | settings['dosvxfile'] | settings['doplan']
                | settings['doelevation'] | settings['doprojection']):
        #Read the file
        try:
            topodata = ReadTop.readfile(settings['pockettopo'],
                                        settings['doplan'],
                                        settings['doelevation'])
            print('Successfully read file')
        except():
            #Dialog box needed
            print('Something went wrong reading the file')

        #Output the th file and get the relative positions
        try:
            shots2 = Writeth.therionfile(topodata, settings, cavers, locations)
            if settings['dothfile']:
                print('Written ' + locations['surveyname'] +
                        '.th and calculated realtive positions')
            else:
                print('Calculated realtive positions')
        except:
            print('Something went wrong when using the Data (Writeth)')
            raise

    if settings['doplan'] | settings['doelevation'] | settings['doprojection']:
        relcoor = relshots.relshots(shots2, topodata,
                                    settings['doplan'],
                                    settings['doelevation'],
                                    settings['projangle'])

        #Output plan (th2 file)
        if settings['doplan']:
            Writeth2.th2plan(relcoor, settings, topodata['outline'],
                             relcoor['startstn'], locations, 'plan')
#            print('Writen plan th2 file: '
#                  + locations['surveyname'] + settings['plansuffix'] + '.th2')

        #Output extended elevation (th2 file)
        if settings['doelevation']:
            Writeth2.th2elev(relcoor, settings, topodata['sideview'],
                             relcoor['startstn'], locations, 'elev')
#            print('Writen elevation th2 file: '
#                  + locations['surveyname'] + settings['elevsuffix'] + '.th2')

        #Output projected elevation (th2 file)
        if settings['doprojection']:
            filesuffix = settings['projsuffix'].replace('###',
                                                    str(settings['projangle']))
            scrapsuffix = settings['pescrapsuffix'].replace('###',
                                                    str(settings['projangle']))
            #if settings['anglesuffixfile']:
                #filesuffix = settings['projsuffix'] +
                                                    #str(settings['projangle'])
            #else:
                #filesuffix = settings['projsuffix']
            #if settings['anglesuffixscrap'] == True:
                #scrapsuffix = settings['pescrapsuffix'] +
                                            #str(settings['projangle']) + '_'
            #else:
                #scrapsuffix = settings['pescrapsuffix']
            Writeth2.th2file('proj', locations, filesuffix,
                                settings['projscale'], settings['projDPI'],
                                relcoor['stnpos'], relcoor['startstn'],
                                settings['projgrid'],
                                settings,
                                pangle=settings['projangle'],
                                ins_scraps=settings['pescraps'],
                                scrapsuffix=scrapsuffix)
#            print('Writen projection th2 file: '
#                  + locations['surveyname'] + filesuffix + '.th2')

    #output thconfig
    if settings['dothconfig']:
        Writethconfig.writethconfig(settings, locations)
#        print('Produced config file: ' + settings['configname'])

    #Process data
    cfile = os.path.join(locations['surveydir'], settings['configname'])
    if settings['process']:
        #check for thconfig file
        if os.path.isfile(cfile):
            try:
                print('Therion Output\n==============')
                CheckerCreate.ShellCall(settings['therion'] + ' \"' +
                                        cfile + '\"',
                                        locations['surveydir'])
#                os.system(settings['therion'] + ' ' + cfile)
                print('Data processed')
            except:
                print('Could not process data')
        else:
            print(settings['configname'], "does not exist, cannot process data")

    #open 3d
    if settings['open3d']:
        fname = (os.path.join(locations['surveydir'],
                                       locations['surveyname'] +
                                       settings['dsuffix'] + '.3d'))
        if os.path.isfile(fname):
            viewercom = settings[settings['viewer']]
            try:
                CheckerCreate.ShellCall(viewercom + ' \"' + fname + '\"',
                                        locations['surveydir'])
#                os.system(viewercom + ' \"' + fname)
            except:
                print('Something went wrong while opening '
                      + settings['viewer'])
        else:
            print(fname + ' does not exist, cannot display 3d')

    #open xtherion files
    if settings['openfiles']:
        # make files with full paths to be opened
        print('Preparing to open files')
        thname = locations['surveyname'] + '.th'
        thpath = os.path.join(locations['surveydir'], thname)
        if settings['th2edit'] == 'xplan':
            th2name = locations['surveyname'] + settings['plansuffix'] + '.th2'
        else:
            th2name = locations['surveyname'] + settings['elevsuffix'] + '.th2'
        th2path = os.path.join(locations['surveydir'], th2name)
        # If the chosen th2 does not exist try the other
        if not os.path.isfile(th2path):
            if settings['th2edit'] == 'xelev':
                th2name = (locations['surveyname'] +
                           settings['plansuffix'] + '.th2')
            else:
                th2name = (locations['surveyname'] +
                           settings['elevsuffix'] + '.th2')
            th2path = os.path.join(locations['surveydir'], th2name)
        # Put together the command
        com = settings['xtherion']
        if os.path.isfile(thpath):
            com += ' \"' + os.path.abspath(thpath) + '\"'
        else:
            print(thname + ' does not exist to open')
        if os.path.isfile(cfile):
            com += ' \"' + os.path.abspath(cfile) + '\"'
        else:
            print(settings['configname'] + ' does not exist to open')
        if os.path.isfile(th2path):
            com += ' \"' + os.path.abspath(th2path) + '\"'
        else:
            print(th2name + ' does not exist to open')
        if com != settings['xtherion']:
            try:
#                os.system(com)
                CheckerCreate.ShellCall(com, locations['surveydir'])
            except:
                print('Something went wrong while trying to open xtherion')
        else:
            print('Cannot open xtherion: No files exist')