def main(): par = rsf.Par(sys.argv) ext = par.string('ext') # Filetype by extension pflt = par.int('pflt', 0) # Print Files with Lines longer Than if ext == None: ext = 'c' dirs_to_search = 'adm api framework pens plot su system user'.split() if ext == 'py': dirs_to_search.append('book') if pflt > 0: guilty_files = [] else: # This list will have at position i the number of lines of that length line_len_list = [] for d in dirs_to_search: for root,dirs,files in os.walk(d): # Avoid Subversion bookkeeping directories if '.svn' not in root: myfiles = glob.glob(os.path.join(root,'*.'+ext)) if ext == 'py': local_sconstruct = os.path.join(root,'SConstruct') if os.path.isfile(local_sconstruct): myfiles.append(local_sconstruct) if myfiles != []: for file in myfiles: f = open(file,'r') for line in f: lilen = len(line.rstrip()) if pflt > 0: if lilen > pflt: guilty_files.append(file) break else: # Make sure line_len_list is long enough l4 = len(line_len_list) ldiff = lilen+1-l4 if ldiff > 0: line_len_list.extend(ldiff*[0]) line_len_list[lilen] += 1 f.close() if pflt > 0: guilty_files.sort() for file in guilty_files: print(file) else: for i in range(len(line_len_list)): print('%d\t%d' % (i, line_len_list[i])) return unix_success
def run(main_func, cpar=[], nminarg=None, nmaxarg=None): 'For eliminating boilerplate code in main programs' # main_func must take a single argument -- par # cpar is compulsory parameter list # nminarg: minimum number of command-line arguments to the program # nmaxarg: max nr of CLI args to prog if nminarg != None: show_man_and_out(len(sys.argv) < nminarg+1) if nmaxarg != None: show_man_and_out(len(sys.argv) > nmaxarg+1) par = rsf.Par(sys.argv) # Parse arguments into a parameter table show_man_and_out(par.bool('help', False)) for p in cpar: q = par.string(p) show_man_and_out(q == None) try: status = main_func(par) # run the program except m8rex.Error, e: msg(e.msg, True) status = unix_error
#!/usr/bin/env python 'C-Wave Backus Averaging (See Marion et al., 1994)' import sys import numpy as np import rsf.api as rsf import string import math as mt # Inputs par = rsf.Par() slowness_name = par.string('slowness') # Slowness from Logs rhob_name = par.string('density') # Density from Logs depth = rsf.Input() # Depth from Logs slow = rsf.Input(slowness_name) rhob = rsf.Input(rhob_name) ratio = par.float("ratio") # Percent of dom wavelength peak_f = par.float("peak_f") # Dom wavelength depthsample = par.float("depthsample") # Depth Sampling depth_bkn = rsf.Output() # Output depth sampling vel_bkn = rsf.Output('vel_bk') # Backus Avg. velocity slow_bkn = rsf.Output('slow_bk') # Backus Avg. slowness rhob_bkn = rsf.Output('rhob_bk') # Averaged density assert 'float' == depth.type assert 'float' == slow.type assert 'float' == rhob.type
def main(argv=sys.argv): unix_success = 0 unix_error = 1 ################ get user parameters if len(argv) < 2: # print selfdoc and exit if no parameters rsfprog.selfdoc() return unix_error par = rsf.Par(argv) # get parameters levels = par.int('levels', 3) # directory search depth outfile_name = par.string('outfile') # file name for detailed inventory table, default none untested = par.bool('untested', False) # list untested examples? ################ build list of search directories books = [] for item in argv[1:]: if '=' in item: continue # skip parameter items if not os.path.exists(item): # does item exist? sys.stderr.write("Directory '%s' does not exist.\n" % item) continue if not os.path.isdir(item): # is item a directory? sys.stderr.write("File '%s' is not a directory.\n" % item) continue books.append(os.path.normpath(item)) if len(books) == 0: # nothing to search sys.stderr.write("No directories to search.\n") return unix_error sys.stdout.write("Searching in: %s\n\n" % books) ################ open outut file if outfile_name == None: outfile = None else: outfile = open(outfile_name, 'w') outfile.write('proj error ') outfile.write('test error time ') outfile.write('size type fig lock miss ext diff same dir\n') ################ search directory tree sbool = {True: 'yes', False: 'no ', None: '- '} serror = {0: 'ok ', 1: 'error'} rsftest_errors = 0 rsftest_no = 0 figdir_no = 0 lockdir_no = 0 figs_missing = 0 figs_extra = 0 figs_diff = 0 rsftest_error_list = [] rsftest_no_list = [] figdir_no_list = [] lockdir_no_list = [] figs_missing_list = [] figs_extra_list = [] figs_diff_list = [] for bookdir in books: for root, dirs, files in os.walk(bookdir): if '.svn' in dirs: dirs.remove('.svn') # don't visit .svn dirs reldir = root.replace(bookdir, "dummy", 1) # get relative dir name level = len(reldir.split(os.sep)) - 1 # get dir level if level < levels: continue # scan to depth 'levels' ... if level == levels: del dirs[:] # ... but not beyond 'levels' # skip dirs without SConstruct if 'SConstruct' not in files: continue rsfproj_vals = read_rsfproj(root, files) # read rsfproj file rsftest_vals = read_rsftest(root, files) # read rsftest file # count errors if rsftest_vals['error'] == 1: rsftest_errors = rsftest_errors + 1 rsftest_error_list.append(root) if not rsftest_vals['exist']: rsftest_no = rsftest_no + 1 rsftest_no_list.append(root) if rsftest_vals['exist_fig'] == False: figdir_no = figdir_no + 1 figdir_no_list.append(root) if rsftest_vals['exist_lock'] == False: lockdir_no = lockdir_no + 1 lockdir_no_list.append(root) if rsftest_vals['miss'] > 0: figs_missing = figs_missing + 1 figs_missing_list.append(root) if rsftest_vals['extra'] > 0: figs_extra = figs_extra + 1 figs_extra_list.append(root) if rsftest_vals['diff'] > 0: figs_diff = figs_diff + 1 figs_diff_list.append(root) # write summary table if outfile != None: outfile.write('%s ' % sbool[rsfproj_vals['exist']]) outfile.write('%s ' % serror[rsfproj_vals['error']]) outfile.write('%s ' % sbool[rsftest_vals['exist']]) outfile.write('%s ' % serror[rsftest_vals['error']]) outfile.write('"%-24s" ' % rsftest_vals['time']) outfile.write('%12s ' % int_string(rsfproj_vals['size'])) outfile.write('%s ' % rsfproj_vals['data']) outfile.write('%s ' % sbool[rsftest_vals['exist_fig']]) outfile.write('%s ' % sbool[rsftest_vals['exist_lock']]) outfile.write('%3s ' % int_string(rsftest_vals['miss'])) outfile.write('%3s ' % int_string(rsftest_vals['extra'])) outfile.write('%3s ' % int_string(rsftest_vals['diff'])) outfile.write('%3s ' % int_string(rsftest_vals['same'])) outfile.write('%s\n' % root) ################ write summary sys.stdout.write("Examples without an .rsftest file (%d):\n" % rsftest_no) if untested: rsftest_no_list.sort() for item in rsftest_no_list: sys.stdout.write("%s\n" % item) sys.stdout.write("\n") sys.stdout.write("Examples with .rsftest file errors (%d):\n" % rsftest_errors) rsftest_error_list.sort() for item in rsftest_error_list: sys.stdout.write("%s\n" % item) sys.stdout.write("\n") sys.stdout.write("Tested examples without a fig directory (%d):\n" % figdir_no) figdir_no_list.sort() for item in figdir_no_list: sys.stdout.write("%s\n" % item) sys.stdout.write("\n") sys.stdout.write("Tested examples without a lock directory (%d):\n" % lockdir_no) lockdir_no_list.sort() for item in lockdir_no_list: sys.stdout.write("%s\n" % item) sys.stdout.write("\n") sys.stdout.write("Tested examples with missing figs (%d):\n" % figs_missing) figs_missing_list.sort() for item in figs_missing_list: sys.stdout.write("%s\n" % item) sys.stdout.write("\n") sys.stdout.write("Tested examples with extra figs (%d):\n" % figs_extra) figs_extra_list.sort() for item in figs_extra_list: sys.stdout.write("%s\n" % item) sys.stdout.write("\n") sys.stdout.write("Tested examples with non-matching figs (%d):\n" % figs_diff) figs_diff_list.sort() for item in figs_diff_list: sys.stdout.write("%s\n" % item) sys.stdout.write("\n") return unix_success
def main(argv=sys.argv): unix_success = 0 unix_error = 1 ################ get user parameters if len(argv) < 2: # print selfdoc and exit if no parameters rsfprog.selfdoc() return unix_error par = rsf.Par(argv) # get parameters figdir = par.string('figdir') # fig directory, default = ./Fig lockdir = par.string( 'lockdir') # lock directory, default = lock counterpart of figdir list = par.string( 'list') # how much to list [none,diff,miss,all], default = all show = par.string( 'show') # how much to show [none,diff,miss,all], default = none rsftest = par.bool('rsftest', False) # write .rsftest file? copy = par.bool('copy', False) # copy different figs from figdir to lockdir? # check list and show parameters options = ['none', 'diff', 'miss', 'all'] if list is None: list = 'all' if options.count(list) == 0: print "Unknown list option: %s" % list return unix_error if show is None: show = 'none' if options.count(show) == 0: print "Unknown show option: %s" % show return unix_error ################ get environment variables # get $RSFFIGS variable rsffigs = os.environ.get('RSFFIGS') # use $RSFROOT/figs if $RSFFIGS not defined if rsffigs == None: rsffigs = os.path.join(RSFROOT, 'share', 'madagascar', 'figs') # get $RSFALTFIGS variable rsfaltfigs = os.environ.get('RSFALTFIGS') # use rsffigs if $RSFALTFIGS not defined if rsfaltfigs == None: rsfaltfigs = rsffigs ################ get directory paths # get fig directory path if figdir is None: figpath = os.path.abspath(os.path.join('.', 'Fig')) else: figpath = os.path.abspath(figdir) # get lock directory path if lockdir is None: # default to $RSFFIGS/book_tree book_paths = book_path_split(figpath) lockpath = os.path.expandvars(os.path.join(rsffigs, book_paths[1])) # if $RSFSRC is defined and we are not in # $RSFSRC/book/book_tree then # default to $RSFALTFIGS/book_tree if os.environ.get('RSFSRC') != None: if book_paths[0] != os.path.expandvars( os.path.join('$RSFSRC', 'book')): lockpath = os.path.expandvars( os.path.join(rsfaltfigs, book_paths[1])) # use user-supplied lockdir else: lockpath = os.path.abspath(lockdir) # check if fig & lock directories exist exist_fig = os.path.exists(figpath) exist_lock = os.path.exists(lockpath) # print fig directory path print "" if exist_fig: print "Fig directory:" print figpath else: print "Fig directory does not exist:" print figpath # print lock directory path print "" if exist_lock: print "Lock directory:" print lockpath else: print "Lock directory does not exist:" print lockpath if (not exist_fig) or (not exist_lock): if rsftest: rsftest_write(exist_fig, exist_lock, None, None, None, None, None) return unix_error ################ initialize variables miss = 0 extra = 0 diff = 0 same = 0 files = {} ################ get file lists # get lists of vpl files figlist = vpl_list(os.listdir(figpath)) locklist = vpl_list(os.listdir(lockpath)) figlist.sort() locklist.sort() # merge vpl lists for item in figlist: files[item] = ' ' for item in locklist: files[item] = ' ' filelist = files.keys() filelist.sort() ################ find missing and different files # find missing files for item in filelist: if figlist.count(item) == 0: files[item] = ' -' miss = miss + 1 for item in filelist: if locklist.count(item) == 0: files[item] = ' +' extra = extra + 1 # find different files print "" binpath = os.path.join(RSFROOT, 'bin') command = os.path.join(binpath, sfprefix + 'vplotdiff') for item in filelist: if files[item] == ' ': figfile = os.path.join(figpath, item) lockfile = os.path.join(lockpath, item) check = os.system(' '.join( [command, figfile, lockfile, '2>/dev/null'])) if check != 0: if copy: print "Copying %s from fig directory to lock directory." % item shutil.copy(figfile, lockfile) else: files[item] = '%2d' % (check // 256) diff = diff + 1 ################ print file list and show selected figs command = os.path.join(binpath, 'sfpen') for item in filelist: miss_check = (files[item] != ' ') diff_check = ((files[item] != ' ') and (files[item] != ' -') and (files[item] != ' +')) list_check = (((list == 'all')) or ((list == 'miss') and miss_check) or ((list == 'diff') and diff_check)) show_check = (((show == 'all')) or ((show == 'miss') and miss_check) or ((show == 'diff') and diff_check)) if files[item] == ' -': figfile = '' else: figfile = os.path.join(figpath, item) if files[item] == ' +': lockfile = '' else: lockfile = os.path.join(lockpath, item) if list_check: print " %s %s" % (files[item], item) if show_check: syswait(' '.join([command, figfile, lockfile])) if files[item] == ' ': same = same + 1 print "" print "Identical files: %3d" % same print "Different files: %3d" % diff print "Files missing from Fig: %3d" % miss print "Extra files in Fig: %3d" % extra print "Total vplot files: %3d" % len(filelist) print "" # write .rsftest file if rsftest: rsftest_write(exist_fig, exist_lock, miss, extra, diff, same, files) return unix_success
def main(argv=sys.argv): unix_success = 0 unix_error = 1 ################ get user parameters if len(argv) < 2: # print selfdoc and exit if no parameters rsfprog.selfdoc() return unix_error par = rsf.Par(argv) # get parameters levels = par.int('levels', 3) # directory search depth list = par.string('list') # how much to list [all,filter,none], default = all timer = par.string('timer') # output execution time [log,file,none], default = none rsfproj = par.string('rsfproj') # rsfproj filter [yes,no,both], default = yes size = par.int('size', 1024**2) # max data size filter (MB) size = size * 1024**2 uses = par.string('uses') # uses filter, default = any fetch_none = par.bool('nofetch', True) # fetch-no-data filter fetch_public = par.bool('public', False) # fetch-public-data filter fetch_private = par.bool('private', False) # fetch-private-data filter fetch_local = par.bool('local', False) # fetch-local-data filter command = par.string('command') # command to execute in each directory, default = none skipfile = par.string('skipfile') # file with list of directories to skip if list is None: list = 'all' if list not in ['all', 'filter', 'none']: sys.stderr.write('Unknown list option: %s\n' % list) return unix_error if timer is None: timer = 'none' if timer not in ['log', 'file', 'none']: sys.stderr.write('Unknown timer option: %s\n' % timer) return unix_error if rsfproj is None: rsfproj = 'yes' if rsfproj not in ['yes', 'no', 'both']: sys.stderr.write('Unknown rsfproj option: %s\n' % rsfproj) return unix_error if uses is None: uses = 'any' if fetch_none is None: fetch_none = True if fetch_public is None: fetch_public = False if fetch_private is None: fetch_private = False if fetch_local is None: fetch_local = False ################ build list of search directories books = [] for item in argv[1:]: if '=' in item: continue # skip parameter items if not os.path.exists(item): # does item exist? sys.stderr.write("Directory '%s' does not exist.\n" % item) continue if not os.path.isdir(item): # is item a directory? sys.stderr.write("File '%s' is not a directory.\n" % item) continue books.append(os.path.normpath(item)) if len(books) == 0: # nothing to search sys.stderr.write("No directories to search.\n") return unix_error sys.stdout.write("Searching in: %s\n\n" % books) sys.stdout.flush() ################ read skipfile g = {} l = {} if skipfile is not None: execfile(skipfile, g, l) if 'skiplist' in l: skiplist = l['skiplist'] else: skiplist = [] ################ get list of tools installed in $RSFROOT tool_list = os.listdir(os.path.join(RSFROOT, "bin")) ################ search directory tree if (list == 'all') or (list == 'filter'): sys.stdout.write( 'command rsfproj data size directory\n') sys.stdout.flush() total_list = 0 total_size = 0 pass_list = 0 pass_size = 0 rsfproj_yes = 0 rsfproj_no = 0 rsfproj_error = 0 command_error = 0 data_unknown = 0 data_none = 0 data_public = 0 data_private = 0 data_local = 0 for bookdir in books: for root, dirs, files in os.walk(bookdir): if '.svn' in dirs: dirs.remove('.svn') # don't visit .svn dirs reldir = root.replace(bookdir, "dummy", 1) # get relative dir name level = len(reldir.split(os.sep)) - 1 # get dir level if level < levels: continue # scan to depth 'levels' ... if level == levels: del dirs[:] # ... but not beyond 'levels' # skip dirs without SConstruct if 'SConstruct' not in files: continue # read rsfproj file tuple = read_rsfproj(root, files) (error, rsfproj_exist, uses_list, data_type, data_size) = tuple if error == 1: rsfproj_error = rsfproj_error + 1 string = " ********* .rsfproj error ********* %s\n" sys.stdout.write(string % root) sys.stdout.flush() tools = 'yes' for item in uses_list: if (item not in tool_list): tools = 'no ' # calculate directory filter options = (skiplist, rsfproj, uses, size, fetch_none, fetch_public, fetch_private, fetch_local) props = (root, rsfproj_exist, uses_list, data_type, data_size) filter = calc_filter(options, props) if filter == True: pass_list = pass_list + 1 pass_size = pass_size + data_size # print data for each directory if (list == 'all') or ((list == 'filter') and (filter == True)): total_list = total_list + 1 total_size = total_size + data_size if filter: filter_command = 'yes' else: filter_command = 'no ' if rsfproj_exist == 'yes': rsfproj_yes = rsfproj_yes + 1 if rsfproj_exist == 'no': rsfproj_no = rsfproj_no + 1 if data_type == 'unknown': data_unknown = data_unknown + 1 if data_type == 'none': data_none = data_none + 1 if data_type == 'public': data_public = data_public + 1 if data_type == 'private': data_private = data_private + 1 if data_type == 'local': data_local = data_local + 1 tuple = (filter_command, rsfproj_exist, data_type, size_string(data_size), root) sys.stdout.write('%s %-3s %10s %-7s %s\n' % tuple) sys.stdout.flush() # execute command in directory if (filter is True) and (command is not None): string = " +++++++++ running command +++++++++ %s\n" sys.stdout.write(string % root) sys.stdout.flush() tuple = command_wait(' '.join(['cd', root, ';', command])) (exit, dt_user, dt_sys, dt_real) = tuple if (timer == 'log'): string = " user %6.2f sys %6.2f real %6.2f %s\n" sys.stdout.write(string % (dt_user, dt_sys, dt_real, root)) if (timer == 'file'): rsftimer_write(root, exit, dt_user, dt_sys, dt_real) if (exit == 0): string = " --------- command success --------- %s\n" else: string = " ********* command error ********* %s\n" command_error = command_error + 1 sys.stdout.write(string % root) sys.stdout.write("\n") sys.stdout.flush() sys.stdout.write("\n") sys.stdout.write("Directories listed : %3d\n" % total_list) sys.stdout.write("Total data size : %s\n" % size_string(total_size)) sys.stdout.write("\n") sys.stdout.write("Directories with .rsfproj : %3d\n" % rsfproj_yes) sys.stdout.write("Directories without .rsfproj : %3d\n" % rsfproj_no) sys.stdout.write(".rsfproj errors : %3d\n" % rsfproj_error) sys.stdout.write("\n") sys.stdout.write("Directories for command : %3d\n" % pass_list) sys.stdout.write("Total data size for command : %s\n" % size_string(pass_size)) sys.stdout.write("Command errors : %3d\n" % command_error) sys.stdout.write("\n") sys.stdout.write("Directories using unknown external data : %3d\n" % data_unknown) sys.stdout.write("Directories using no external data : %3d\n" % data_none) sys.stdout.write("Directories using public external data : %3d\n" % data_public) sys.stdout.write("Directories using private external data : %3d\n" % data_private) sys.stdout.write("Directories using local data : %3d\n" % data_local) sys.stdout.write("\n") return unix_success
# 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, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import rsf.api as sf import numpy as np try: # Give precedence to local version from hegilles import jitter except: # Use distributed version from rsf.user.hegilles import jitter par = sf.Par() input = sf.Input() output = sf.Output() n1 = input.int("n1") n2 = input.int("n2") ni = input.size(2) assert ni == 1, "sfjitter needs 2D input" perc = par.float("perc", .75) # percentage of traces to remove assert (perc > 0 and perc < 1), "perc should be between 0 and 1" jit = par.float("jit", 1 / (1 - perc)) # maximum gap factor seed = par.int("seed", np.random.randn()) # seed for random number generator
def initialize_params(units, features): np.random.seed(42) params = { "W1": 0.1 * randn(units, features), "b1": np.zeros(shape=units), "W2": 0.1 * randn(units), "b2": np.zeros(shape=1) } return params # Get input parameters and 'source' files. These are RSF objects that works as # pointers to the actual data/file. par_obj = rsf.Par() x_train_obj = rsf.Input() # first input as default y_train_obj = rsf.Input('ytrain') x_val_obj = rsf.Input('xval') y_val_obj = rsf.Input('yval') # Assign output 'target' files. loss_train_obj = rsf.Output() # first output as default loss_val_obj = rsf.Output('lossval') # Parse parameter values from input object. num_epochs = par_obj.int('nepochs') learning_rate = par_obj.float('lr') hidden_units = par_obj.int('hidden') # number of hidden units # Get size of the input arrays and load them. (Initialize + Read)
def show_slice(slice, pnt, nx, ny, title='Constant time slice'): import matplotlib.pyplot as plt import matplotlib.cm as cm plt.imshow(slice, interpolation='nearest', cmap=cm.jet, extent=(1, nx, 1, ny), origin='lower') plt.title(title) plt.plot(pnt[:, 0], pnt[:, 1], 'k.') plt.colorbar() plt.show() # get parameters from command line command_line_par = rsf.Par() # # Survey parameters nxline = command_line_par.int("nxline") if nxline == None: print "nxline is a required parameter in interpvel" sys.exit(2) ninline = command_line_par.int("ninline") if ninline == None: print "ninline is a required parameter in interpvel" sys.exit(2) fxline = command_line_par.int("fxline") if fxline == None:
#print vs # close files for k in [vd, tvd]: os.close(k) for k in [vpath]: os.remove(k) vs.append(tvpath) return if __name__ == "__main__": mgr = multiprocessing.Manager() vs = mgr.list() par = salah.Par() n1 = par.int("n1") # size of the first axis o1 = par.float("o1") # origin of the first axis d1 = par.float("d1") # sampling in the first axis # if not (n1 or o1 or d1): #sys.stderr.write(usage) rsf.prog.selfdoc() sys.exit(2) if sys.stdout.isatty() or sys.stdin.isatty(): #sys.stderr.write(usage) rsf.prog.selfdoc() sys.exit(2) inline = collections.OrderedDict() loc = collections.OrderedDict() i = None