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