Changeset efdb01a in flex_extract.git for python/UIOFiles.py
- Timestamp:
- May 9, 2018, 12:15:00 PM (6 years ago)
- Branches:
- master, ctbto, dev
- Children:
- 991df6a
- Parents:
- 02c8c50
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
python/UIOFiles.py
r02c8c50 refdb01a 4 4 # TODO AP 5 5 #AP 6 # - File name und Klassenname gleichsetzen?7 6 # - checken welche regelmässigen methoden auf diese Files noch angewendet werden 8 7 # und dann hier implementieren 9 # - löschen?8 # - add description of file! 10 9 #************************************************************************ 11 10 """ … … 15 14 16 15 @ChangeHistory: 17 February 2018 - Anne Philipp (University of Vienna): 16 November 2015 - Leopold Haimberger (University of Vienna): 17 - modified method listFiles to work with glob instead of listdir 18 - added pattern search in method listFiles 19 20 February 2018 - Anne Philipp (University of Vienna): 18 21 - applied PEP8 style guide 19 22 - added documentation 23 - optimisation of method listFiles since it didn't work correctly 24 for sub directories 25 - additional speed up of method listFiles 20 26 21 27 @License: 22 (C) Copyright 2014 UIO.28 (C) Copyright 2014-2018. 23 29 24 30 This software is licensed under the terms of the Apache Licence Version 2.0 … … 26 32 27 33 @Requirements: 28 - A standard python 2.6 or 2.7 installation 29 - dateutils 30 - matplotlib (optional, for debugging) 31 - ECMWF specific packages, all available from https://software.ecmwf.int/ 32 ECMWF WebMARS, gribAPI with python enabled, emoslib and 33 ecaccess web toolkit 34 A standard python 2.6 or 2.7 installation 34 35 35 36 @Description: 36 Further documentation may be obtained from www.flexpart.eu. 37 38 Functionality provided: 39 Prepare input 3D-wind fields in hybrid coordinates + 40 surface fields for FLEXPART runs 37 ... 41 38 """ 42 39 # ------------------------------------------------------------------------------ … … 45 42 import os 46 43 import glob 47 44 import fnmatch 45 import time 46 import profiling 48 47 # ------------------------------------------------------------------------------ 49 # C lass48 # CLASS 50 49 # ------------------------------------------------------------------------------ 51 50 class UIOFiles: … … 56 55 ''' 57 56 # -------------------------------------------------------------------------- 58 # FUNCTIONS57 # CLASS FUNCTIONS 59 58 # -------------------------------------------------------------------------- 60 59 def __init__(self, suffix): … … 80 79 return 81 80 82 def listFiles(self, path, pattern): 81 #@profiling.timefn 82 def listFiles(self, path, pattern, callid=0): 83 83 ''' 84 84 @Description: … … 98 98 '*OG_acc_SL*.'+c.ppid+'.*' 99 99 100 callid: integer 101 Id which tells the function if its the first call 102 or a recursive call. Default and first call is 0. 103 Everything different from 0 is ment to be a recursive case. 104 100 105 @Return: 101 106 <nothing> 102 107 ''' 103 108 109 # initialize variable in first function call 110 if callid == 0: 111 self.files = [] 112 104 113 # Get the absolute path 105 114 path = os.path.abspath(path) 106 115 107 # Get a list of files in pathname108 filesInCurDir0 = glob.glob(path + '/' + pattern)109 filesInCurDir = []110 for f in filesInCurDir0:111 filesInCurDir.append(f.split('/')[-1])116 # get the file list of the path if its not a directory and 117 # if it contains one of the suffixes 118 self.files.extend([os.path.join(path, k) for k in os.listdir(path) 119 if fnmatch.fnmatch(k, pattern) and 120 os.path.splitext(k)[-1] in self.suffix]) 112 121 113 self.counter = 0 114 self.files = [] 115 # Traverse through all files 116 for file in filesInCurDir: 117 curFile = os.path.join(path, file) 122 # find possible sub-directories in the path 123 subdirs = [s for s in os.listdir(path) 124 if os.path.isdir(os.path.join(path, s))] 118 125 119 # Check if it's a normal file or directory 120 if os.path.isfile(curFile): 121 # Get the file extension 122 fileNoExt, curFileExtension = os.path.splitext(curFile) 123 # Check if the file has an extension of typical video files 124 if curFileExtension in self.suffix: 125 # We have got a file file! Increment the counter 126 self.counter += 1 127 # add this filename in the list 128 self.files.append(curFile) 129 else: 130 # We got a directory, enter into it for further processing 131 self.listFiles(curFile) 126 # do recursive calls for sub-direcorties 127 if subdirs: 128 for subdir in subdirs: 129 self.listFiles(os.path.join(path, subdir), pattern, callid=1) 132 130 133 131 return
Note: See TracChangeset
for help on using the changeset viewer.