Changeset efdb01a in flex_extract.git for python/UIOFiles.py


Ignore:
Timestamp:
May 9, 2018, 12:15:00 PM (6 years ago)
Author:
Anne Philipp <anne.philipp@…>
Branches:
master, ctbto, dev
Children:
991df6a
Parents:
02c8c50
Message:

whole bunch of modifications due to new structure of ECMWFDATA, added basics of documentation, minor programming corrections

File:
1 moved

Legend:

Unmodified
Added
Removed
  • python/UIOFiles.py

    r02c8c50 refdb01a  
    44# TODO AP
    55#AP
    6 # - File name und Klassenname gleichsetzen?
    76# - checken welche regelmässigen methoden auf diese Files noch angewendet werden
    87# und dann hier implementieren
    9 # - löschen?
     8# - add description of file!
    109#************************************************************************
    1110"""
     
    1514
    1615@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):
    1821        - applied PEP8 style guide
    1922        - added documentation
     23        - optimisation of method listFiles since it didn't work correctly
     24          for sub directories
     25        - additional speed up of method listFiles
    2026
    2127@License:
    22     (C) Copyright 2014 UIO.
     28    (C) Copyright 2014-2018.
    2329
    2430    This software is licensed under the terms of the Apache Licence Version 2.0
     
    2632
    2733@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
    3435
    3536@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    ...
    4138"""
    4239# ------------------------------------------------------------------------------
     
    4542import os
    4643import glob
    47 
     44import fnmatch
     45import time
     46import profiling
    4847# ------------------------------------------------------------------------------
    49 # Class
     48# CLASS
    5049# ------------------------------------------------------------------------------
    5150class UIOFiles:
     
    5655    '''
    5756    # --------------------------------------------------------------------------
    58     # FUNCTIONS
     57    # CLASS FUNCTIONS
    5958    # --------------------------------------------------------------------------
    6059    def __init__(self, suffix):
     
    8079        return
    8180
    82     def listFiles(self, path, pattern):
     81    #@profiling.timefn
     82    def listFiles(self, path, pattern, callid=0):
    8383        '''
    8484        @Description:
     
    9898                '*OG_acc_SL*.'+c.ppid+'.*'
    9999
     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
    100105        @Return:
    101106            <nothing>
    102107        '''
    103108
     109        # initialize variable in first function call
     110        if callid == 0:
     111            self.files = []
     112
    104113        # Get the absolute path
    105114        path = os.path.abspath(path)
    106115
    107         # Get a list of files in pathname
    108         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])
    112121
    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))]
    118125
    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)
    132130
    133131        return
Note: See TracChangeset for help on using the changeset viewer.
hosted by ZAMG