client.py 8.5 KB
Newer Older
Mijian Xu's avatar
Mijian Xu 已提交
1
2
3
from cmd import Cmd
import readline
import sys
xu's avatar
xu 已提交
4
from mgpro import mgpro
Mijian Xu's avatar
Mijian Xu 已提交
5
import glob
Mijian Xu's avatar
Mijian Xu 已提交
6
import subprocess
Mijian Xu's avatar
Mijian Xu 已提交
7
8
9
10
11
12
13
14
15
16
17
18


def complete(text, state):
    return (glob.glob(text+'*')+[None])[state]


class Client(Cmd):
    def __init__(self):
        Cmd.__init__(self)
        self.prompt = 'MGPro>'
        self.aliases = {'c': self.do_continuation,
                        'g': self.do_gradient,
Mijian Xu's avatar
Mijian Xu 已提交
19
                        'p': self.do_plot,
Mijian Xu's avatar
Mijian Xu 已提交
20
21
                        'q': self.do_quit,
                        'r': self.do_read,
Mijian Xu's avatar
Mijian Xu 已提交
22
                        's': self.do_powspec,
Mijian Xu's avatar
Mijian Xu 已提交
23
                        'w': self.do_write,
Mijian Xu's avatar
Mijian Xu 已提交
24
25
                        'x': self.do_dt2xa,
                        'y': self.do_dt2ya,
Mijian Xu's avatar
Mijian Xu 已提交
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
                        'z': self.do_dt2za,
                        'h': self.do_help}
        self.result = None

    def do_quit(self, arg):
        'Close MGPro'
        print('Bye...')
        sys.exit(0)

    def do_read(self, arg):
        '''
read (r): read a raw data file with 3 columns (x, y, value)

Syntax: read filename dx dy [\'geo\']
    filename: Path to the raw data file
    dx: sampling interval along x directory in meter
    dy: sampling interval along y directory in meter
    geo: Specify this keyword to convert spherical coordinate to geo coordinate
        '''
        arg_lst = arg.split()
        if len(arg_lst) < 3:
            print('Error: Not enough arguments')
            return
        elif len(arg_lst) == 3:
            filename = arg_lst[0]
            dx = float(arg_lst[1])
            dy = float(arg_lst[2])
            to_geo = False
        elif len(arg_lst) == 4:
xu_mijian's avatar
xu_mijian 已提交
55
56
57
58
            if arg_lst[3] == 'geo':
                filename = arg_lst[0]
                dx = float(arg_lst[1])
                dy = float(arg_lst[2])
Mijian Xu's avatar
Mijian Xu 已提交
59
60
61
62
63
64
65
                to_geo = True
            else:
                print('Error: Specify \'geo\' to convert spherical coordinate to geo coordinate')
                return
        else:
            print('Error: Too many argumenrs')
            return
Mijian Xu's avatar
Mijian Xu 已提交
66
67
68
69
        try:
            self.mg = mgpro.mgmat(filename, dx, dy, to_geo=to_geo)
        except Exception as e:
            print('Error: Cannot read {}\n{}'.format(filename, e))
Mijian Xu's avatar
Mijian Xu 已提交
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

    def do_continuation(self, arg):
        '''
continuation (c): Calculate continuation and derivative for the data read

Syntax: continuation h order
    h: continuation in h km
    order: derivative order
        '''
        if len(arg.split()) != 2:
            print('Error: Two arguments required')
            return
        h, order = [float(value) for value in arg.split()]
        self.result = self.mg.continuation(h, order)

    def do_gradient(self, arg):
        '''
Mijian Xu's avatar
Mijian Xu 已提交
87
88
gradient (g): Calculare horizontal gradient or the module.
    NOTE: The unit of the input must be meter. The unit of the output has converted to km.
Mijian Xu's avatar
Mijian Xu 已提交
89
90
91
92
93
94

Syntax: gradient option
    option: Specify the parameter in [0|45|90|135|mod], which represent the \
horizontal gradient in azimuth of 0, 45, 90 or 135 or module of horizontal gradient
        '''
        if len(arg.split()) != 1:
Mijian Xu's avatar
Mijian Xu 已提交
95
            print('Error: Only 1 argument required, but {} arguments got'.format(arg.split()))
Mijian Xu's avatar
Mijian Xu 已提交
96
97
98
99
100
101
102
            return
        elif arg not in ['0', '90', '45', '135', 'mod']:
            print('Error: Degree or module must be in 0, 45, 90, 135 and \'mod\'')
            return
        self.result = self.mg.gradient(arg)

    def do_dt2za(self, arg):
Mijian Xu's avatar
Mijian Xu 已提交
103
104
105
        '''
dt2za (z): Vertical magnetization

106
Syntax: dt2za i0 d0 ['mem']
Mijian Xu's avatar
Mijian Xu 已提交
107
108
    i0: Magnetic dip of the region
    d0: Magnetic declination of the region
109
    mem: Switch for saving Za to memory and replacing the data read before
Mijian Xu's avatar
Mijian Xu 已提交
110
111
        '''
        if len(arg.split()) == 2:
Mijian Xu's avatar
Mijian Xu 已提交
112
113
            i0, d0 = [float(value) for value in arg.split()]
            self.result = self.mg.dt2za(i0, d0)
114
115
116
117
118
119
120
        elif len(arg.split()) == 3:
            if arg.split()[2].lower() == 'mem':
                i0, d0 = [float(value) for value in arg.split()[0:2]]
                self.result = self.mg.dt2za(i0, d0, replace=True)
            else:
                print('Error: \'mem\' should be specified to save Za to memory')
                return
Mijian Xu's avatar
Mijian Xu 已提交
121
        else:
122
            print('Two or three argumrnts required')
Mijian Xu's avatar
Mijian Xu 已提交
123
124
125
126
127

    def do_dt2xa(self, arg):
        '''
dt2xa (x): Horizontal magnetization along x direction

128
Syntax: dt2xa i0 d0 ['mem']
Mijian Xu's avatar
Mijian Xu 已提交
129
130
    i0: Magnetic dip of the region
    d0: Magnetic declination of the region
131
    mem: Switch for saving Xa to memory and replacing the data read before
Mijian Xu's avatar
Mijian Xu 已提交
132
133
134
135
        '''
        if len(arg.split()) == 2:
            i0, d0 = [float(value) for value in arg.split()]
            self.result = self.mg.dt2xa(i0, d0)
136
137
138
139
140
141
142
        elif len(arg.split()) == 3:
            if arg.split()[2].lower() == 'mem':
                i0, d0 = [float(value) for value in arg.split()[0:2]]
                self.result = self.mg.dt2xa(i0, d0, replace=True)
            else:
                print('Error: \'mem\' should be specified to save Xa to memory')
                return
Mijian Xu's avatar
Mijian Xu 已提交
143
        else:
144
            print('Two or three argumrnts required')
Mijian Xu's avatar
Mijian Xu 已提交
145
146
147
148
149

    def do_dt2ya(self, arg):
        '''
dt2xa (y): Horizontal magnetization along y direction

150
Syntax: dt2ya i0 d0 ['mem']
Mijian Xu's avatar
Mijian Xu 已提交
151
152
    i0: Magnetic dip of the region
    d0: Magnetic declination of the region
153
    mem: Switch for saving Ya to memory and replacing the data read before
Mijian Xu's avatar
Mijian Xu 已提交
154
155
156
157
        '''
        if len(arg.split()) == 2:
            i0, d0 = [float(value) for value in arg.split()]
            self.result = self.mg.dt2ya(i0, d0)
158
159
160
161
162
163
164
        elif len(arg.split()) == 3:
            if arg.split()[2].lower() == 'mem':
                i0, d0 = [float(value) for value in arg.split()[0:2]]
                self.result = self.mg.dt2ya(i0, d0, replace=True)
            else:
                print('Error: \'mem\' should be specified to save Ya to memory')
                return
Mijian Xu's avatar
Mijian Xu 已提交
165
166
        else:
            print('Two argumrnts required')
Mijian Xu's avatar
Mijian Xu 已提交
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

    def do_write(self, arg):
        '''
write (w): Write any result to a txt file

Syntax: write filename [\'lalo\']
    filename: path to txt file you want to write
    lalo: Specify the keyword to convert geo coordinate to spherical coordinate'
        '''
        if self.result is None:
            print('Error: No result in the memery')
            return
        if len(arg.split()) > 2:
            print('Error: Too many arguments')
        elif len(arg.split()) == 1:
            filename = arg
            to_latlon = False
        elif len(arg.split()) == 2:
            filename = arg.split()[0]
            if arg.split()[1] == 'lalo':
                to_latlon = True
            else:
                print('Error: Specify \'lalo\' to convert geo coordinate to spherical coordinate')
        try:
            self.mg.savetxt(filename, self.result, to_latlon)
        except Exception as e:
            print('{}'.format(e))
            return

Mijian Xu's avatar
Mijian Xu 已提交
196
197
198
199
    def do_plot(self, arg):
        '''
plot (p): Preview the result.

200
201
Syntax: plot [data|result]
    Specify 'data' or 'result' to draw map view of the raw data or the result (default)
Mijian Xu's avatar
Mijian Xu 已提交
202
        ''' 
203
204
205
206
207
208
        if arg.lower() == 'data':
            self.mg.pltmap(self.mg.data_expand[self.mg.row_begin: self.mg.row_end + 1, self.mg.col_begin: self.mg.col_end + 1])
        elif  arg.lower() == 'result' or arg == '':
            self.mg.pltmap(self.result)
        else:
            print('Error: argument should be in \'data\' and \'result\'')
Mijian Xu's avatar
Mijian Xu 已提交
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
    
    def do_powspec(self, arg):
        '''
powspec (s): compute power spectrum of the data.

Syntax: powspec [w1, w2]
    Specify begining and ending frequency 
        '''
        if self.mg.power is None:
            self.mg.power_specf()
        if arg == '':
            self.mg.plotpower()
        elif len(arg.split()) == 2:
            try:
                w1, w2 = [float(w) for w in arg.split()]
            except:
                print('arguments should be in float type')
                return
            if w1 >= w2:
                print('w1 should be less than w2')
            self.mg.power_fit(w1, w2)
            self.mg.plotpower()
Mijian Xu's avatar
bug fix    
Mijian Xu 已提交
231
            self.mg.pcot = None
Mijian Xu's avatar
Mijian Xu 已提交
232
233
        else:
            print('Only two arguments can be accepted')
Mijian Xu's avatar
Mijian Xu 已提交
234

Mijian Xu's avatar
Mijian Xu 已提交
235
236
237
238
239
240
241
242
243
244
    def completedefault(self, *args):
        readline.set_completer_delims(' \t\n;')
        readline.parse_and_bind("tab: complete")
        readline.set_completer(complete)

    def default(self, line):
        cmd, arg, line = self.parseline(line)
        if cmd in self.aliases:
            self.aliases[cmd](arg)
        else:
Mijian Xu's avatar
Mijian Xu 已提交
245
246
247
248
            try:
                subprocess.call(line, shell=True)
            except:
                print('Command not found')
Mijian Xu's avatar
Mijian Xu 已提交
249
250


Mijian Xu's avatar
Mijian Xu 已提交
251
def main():
Mijian Xu's avatar
Mijian Xu 已提交
252
    Client().cmdloop()
Mijian Xu's avatar
Mijian Xu 已提交
253
254
255
256


if __name__ == "__main__":
    main()