Changeset 904 in lmdz_wrf for trunk/tools


Ignore:
Timestamp:
Jun 19, 2016, 7:59:25 PM (8 years ago)
Author:
lfita
Message:

Adding `files_folder_HMT': Function to retrieve a list of files from a folder [fold] and files with [head]*[middle]*[tail]

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r896 r904  
    3030# dictionary_key: Function to provide the first key in a dictionay with a given value
    3131# dictionary_key_list: Function to provide the first key in a dictionay of lists with a given value
     32# files_folder_HMT: Function to retrieve a list of files from a folder [fold] and files with [head]*[middle]*[tail]
    3233# ijlonlat: Function to provide the imin,jmin imax,jmax of a lon,lat box
    3334# incomming_flow: Function to determine if a fgrid-flow inflows to the central grid point
     
    52695270    return runmean
    52705271
     5272def files_folder_HMT(folder,head,middle,tail):
     5273    """ Function to retrieve a list of files from a folder [fold] and files with [head]*[middle]*[tail]
     5274    >>> files_folder_HMT('.','t','wrf','mean.nc')
     5275    ['clt_wrfout_tmean.nc', 'tas_wrfout_tmean.nc', 'ta_wrfout_xmean.nc']
     5276    """
     5277    import subprocess as sub
     5278    fname = 'files_folder'
     5279
     5280    if folder == 'h':
     5281        print fname + '_____________________________________________________________'
     5282        print files_folder.__doc__
     5283        quit()
     5284
     5285#    ins = folder + "/" + head + "*" + middle + '*' + tail
     5286    ins = folder + "/"
     5287#    print 'ins:',ins
     5288    files = sub.Popen(["/bin/ls","-1",ins], stdout=sub.PIPE)
     5289    fileslist = files.communicate()
     5290    listfiles0 = str(fileslist).replace("'",'').replace('(','').replace(')','').split('\\n')
     5291
     5292# Filtering output
     5293    listfiles = []
     5294    for ifile in listfiles0:
     5295        if ifile.find(head) != -1 and  ifile.find(middle) != -1 and                  \
     5296          ifile.find(tail) != -1:
     5297            indhead = ifile.index(head)
     5298            indmiddle = ifile.index(middle)
     5299            indtail = ifile.index(tail)
     5300            if indhead < indmiddle < indtail: listfiles.append(ifile)
     5301
     5302    return listfiles
     5303
    52715304def files_folder(folder,headfile):
    52725305    """ Function to retrieve a list of files from a folder [fold] and head [headfile]
Note: See TracChangeset for help on using the changeset viewer.