source: flex_extract.git/python/UIOFiles.py @ efdb01a

ctbtodev
Last change on this file since efdb01a was efdb01a, checked in by Anne Philipp <anne.philipp@…>, 6 years ago

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

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[64cf353]1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
[507d47f]3#************************************************************************
4# TODO AP
[64cf353]5#AP
6# - checken welche regelmässigen methoden auf diese Files noch angewendet werden
[507d47f]7# und dann hier implementieren
[efdb01a]8# - add description of file!
[507d47f]9#************************************************************************
[9ac56ea]10"""
11@Author: Anne Fouilloux (University of Oslo)
12
13@Date: October 2014
14
15@ChangeHistory:
[efdb01a]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):
[64cf353]21        - applied PEP8 style guide
22        - added documentation
[efdb01a]23        - optimisation of method listFiles since it didn't work correctly
24          for sub directories
25        - additional speed up of method listFiles
[9ac56ea]26
27@License:
[efdb01a]28    (C) Copyright 2014-2018.
[9ac56ea]29
30    This software is licensed under the terms of the Apache Licence Version 2.0
31    which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
[507d47f]32
[64cf353]33@Requirements:
[efdb01a]34    A standard python 2.6 or 2.7 installation
[64cf353]35
36@Description:
[efdb01a]37    ...
[64cf353]38"""
[ab3dbdd]39# ------------------------------------------------------------------------------
40# MODULES
41# ------------------------------------------------------------------------------
[d69b677]42import os
43import glob
[efdb01a]44import fnmatch
45import time
46import profiling
[ab3dbdd]47# ------------------------------------------------------------------------------
[efdb01a]48# CLASS
[ab3dbdd]49# ------------------------------------------------------------------------------
[d69b677]50class UIOFiles:
[507d47f]51    '''
52    Class to manipulate files. At initialisation it has the attribute
53    suffix which stores a list of suffixes of the files associated
54    with the instance of the class.
55    '''
[ab3dbdd]56    # --------------------------------------------------------------------------
[efdb01a]57    # CLASS FUNCTIONS
[ab3dbdd]58    # --------------------------------------------------------------------------
[507d47f]59    def __init__(self, suffix):
60        '''
61        @Description:
62            Assignes the suffixes of the files which should be
63            associated with the instance of the class.
64
65        @Input:
66            self: instance of UIOFiles
67                Description see class documentation.
68
69            suffix: list of strings
[02c8c50]70                The types of files which should be manipulated such as
[64cf353]71                ['grib', 'grb', 'grib1', 'grib2', 'grb1', 'grb2']
[507d47f]72
73        @Return:
74            <nothing>
75        '''
[02c8c50]76
[507d47f]77        self.suffix = suffix
[02c8c50]78
[507d47f]79        return
80
[efdb01a]81    #@profiling.timefn
82    def listFiles(self, path, pattern, callid=0):
[507d47f]83        '''
84        @Description:
85            Lists all files in the directory with the matching
86            regular expression pattern. The suffixes are already stored
87            in a list attribute "suffix".
88
89        @Input:
90            self: instance of UIOFiles
91                Description see class documentation.
92
[02c8c50]93            path: string
[507d47f]94                Directory where to list the files.
95
96            pattern: string
97                Regular expression pattern. For example:
98                '*OG_acc_SL*.'+c.ppid+'.*'
99
[efdb01a]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
[507d47f]105        @Return:
106            <nothing>
107        '''
[02c8c50]108
[efdb01a]109        # initialize variable in first function call
110        if callid == 0:
111            self.files = []
112
[02c8c50]113        # Get the absolute path
114        path = os.path.abspath(path)
[507d47f]115
[efdb01a]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])
121
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))]
125
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)
130
131        return
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG