1 | #------------------------------------------------------------------------------- |
---|
2 | # Name: ParamEdit |
---|
3 | # Purpose: |
---|
4 | # |
---|
5 | # Author: Kelvin |
---|
6 | # |
---|
7 | # Created: 27/11/2013 |
---|
8 | # Copyright: (c) Kelvin 2013 |
---|
9 | # Licence: <your licence> |
---|
10 | #------------------------------------------------------------------------------- |
---|
11 | |
---|
12 | # Note to self: Make this program more modular and readable by splitting it into distinct parts and use perhaps use functions to implement them. |
---|
13 | |
---|
14 | # Firstly, determine the default list of parameters |
---|
15 | defaultParameters = {}; |
---|
16 | defaultParamFile = open('/u/hsu004/IntroToDuchamp/duchampDefaultParameters'); |
---|
17 | |
---|
18 | for line in defaultParamFile: |
---|
19 | if line[0] != '#' and len(line) > 1: |
---|
20 | lineblocks = line.split() |
---|
21 | defaultParameters[lineblocks[0]] = lineblocks[1] |
---|
22 | |
---|
23 | # Display the default set of parameters. |
---|
24 | print('Original set of Parameters: \n'); |
---|
25 | for par in parameters: |
---|
26 | print(par, parameters[par]); |
---|
27 | |
---|
28 | print('\n'); |
---|
29 | |
---|
30 | # Initialise an empty Dictionary. |
---|
31 | parameters = {}; |
---|
32 | |
---|
33 | # Open the given parameter file. |
---|
34 | paramfile = open('/u/hsu004/IntroToDuchamp/duchampHIPASS.in'); |
---|
35 | |
---|
36 | # Load files for lines of parameter data in the specified format and |
---|
37 | # store them into the dictionary. |
---|
38 | for line in paramfile: |
---|
39 | if line[0] != '#' and len(line) > 1: |
---|
40 | linebits = line.split() |
---|
41 | parameters[linebits[0]] = linebits[1] |
---|
42 | |
---|
43 | # Go back to the start of the file. |
---|
44 | paramfile.seek(0, 0); |
---|
45 | |
---|
46 | # Display the original set of parameters. |
---|
47 | print('Original set of Parameters: \n'); |
---|
48 | for par in parameters: |
---|
49 | print(par, parameters[par]); |
---|
50 | |
---|
51 | print('\n'); |
---|
52 | |
---|
53 | # Define a simple function to determine the user's intent. |
---|
54 | # Note that this may be very inefficient. |
---|
55 | def toEdit(keepEditting): |
---|
56 | if (keepEditting == 'y') | (keepEditting == 'Y') | (keepEditting == 'yes'): |
---|
57 | return(1) |
---|
58 | elif (keepEditting == 'n') | (keepEditting == 'N') | (keepEditting == 'no'): |
---|
59 | return(0) |
---|
60 | else: |
---|
61 | return(-1) |
---|
62 | |
---|
63 | # Prompt the user about whether they want to edit any parameters. |
---|
64 | keepEditting = raw_input('\nWould you like to edit any of the parameters? (Type "y" for yes): ') |
---|
65 | |
---|
66 | # This handles the case when no parameters are to be editted. |
---|
67 | if toEdit(keepEditting) == 0: |
---|
68 | print('\nNo parameters will be editted.') |
---|
69 | |
---|
70 | if toEdit(keepEditting) == -1: |
---|
71 | print('\nInvalid Input. Did you mistype something? Did you want to edit any of the parameters? (Type "y" for yes): ') |
---|
72 | if toEdit(keepEditting) == -1: |
---|
73 | print('\nInvalid Input. No parameters will be editted.') |
---|
74 | |
---|
75 | # This handles the case when the parameters are to be editted. |
---|
76 | while toEdit(keepEditting) == 1: |
---|
77 | |
---|
78 | # Prompt the user for the parameter name. |
---|
79 | parameter2edit = raw_input('\nWhich parameter would you like to edit? \n\tParameter: ') |
---|
80 | |
---|
81 | # If the parameter exists, edit the parameter value. |
---|
82 | # Note that the value is always stored as a string, even if the value is a 'number'. |
---|
83 | if parameter2edit in parameters: |
---|
84 | |
---|
85 | oldValue = parameters[parameter2edit] |
---|
86 | newValue = raw_input('\nPlease enter a new value or setting for "' + parameter2edit + '": ') |
---|
87 | parameters[parameter2edit] = newValue |
---|
88 | print('\n"' + parameter2edit + '" has been updated from ' + str(oldValue) + ' to ' + str(newValue) + '!') |
---|
89 | |
---|
90 | # Otherwise, if no such parameters exist, tell the user. |
---|
91 | else: |
---|
92 | print('\nThere is no such parameter as "' + parameter2edit + '"') |
---|
93 | |
---|
94 | # Prompt the user again if editting is to be continued. |
---|
95 | keepEditting = raw_input('\nWould you like to edit any other parameters? (Type "y" for yes): ') |
---|
96 | |
---|
97 | # Sometimes people mistype... |
---|
98 | if toEdit(keepEditting) == -1: |
---|
99 | keepEditting = raw_input('\nInvalid Input. Did you mistype something? Would you like to edit any other parameters? (Type "y" for yes): ') |
---|
100 | |
---|
101 | # Let the user know that the editting process has ended. |
---|
102 | if toEdit(keepEditting) == 0: |
---|
103 | print('\nFinished Editting.') |
---|
104 | elif toEdit(keepEditting) == -1: |
---|
105 | print('\nInvalid Input. No parameters will be editted.') |
---|
106 | |
---|
107 | |
---|
108 | # Display the editted set of parameters. |
---|
109 | print('\nEditted set of Parameters: \n'); |
---|
110 | for par in parameters: |
---|
111 | print(par, parameters[par]); |
---|
112 | |
---|
113 | print('\n'); |
---|
114 | |
---|
115 | # Create a new file. |
---|
116 | newfile = open('/u/hsu004/Kelvin/duchampHIPASSedited.in', 'w') |
---|
117 | |
---|
118 | # Write the editted set of parameters into the new file. |
---|
119 | for par in parameters: |
---|
120 | newfile.write('%s %s\n'%(par, parameters[par])); |
---|
121 | |
---|
122 | newfile.close(); |
---|