source: flex_extract.git/python/UIOTools.py @ 02c8c50

ctbtodev
Last change on this file since 02c8c50 was 02c8c50, checked in by Anne Philipp <bscannephilipp@…>, 6 years ago

more changes in PEP8 style and slight modifications in coding style and naming. More documentation of functions.

  • Property mode set to 100644
File size: 4.3 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#************************************************************************
4# TODO AP
5#AP
6# - File name und Klassenname gleichsetzen?
7# - checken welche regelmässigen methoden auf diese Files noch angewendet werden
8# und dann hier implementieren
9# - löschen?
10#************************************************************************
11"""
12@Author: Anne Fouilloux (University of Oslo)
13
14@Date: October 2014
15
16@ChangeHistory:
17   February 2018 - Anne Philipp (University of Vienna):
18        - applied PEP8 style guide
19        - added documentation
20
21@License:
22    (C) Copyright 2014 UIO.
23
24    This software is licensed under the terms of the Apache Licence Version 2.0
25    which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
26
27@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
35@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
41"""
42# ------------------------------------------------------------------------------
43# MODULES
44# ------------------------------------------------------------------------------
45import os
46import glob
47
48# ------------------------------------------------------------------------------
49# Class
50# ------------------------------------------------------------------------------
51class UIOFiles:
52    '''
53    Class to manipulate files. At initialisation it has the attribute
54    suffix which stores a list of suffixes of the files associated
55    with the instance of the class.
56    '''
57    # --------------------------------------------------------------------------
58    # FUNCTIONS
59    # --------------------------------------------------------------------------
60    def __init__(self, suffix):
61        '''
62        @Description:
63            Assignes the suffixes of the files which should be
64            associated with the instance of the class.
65
66        @Input:
67            self: instance of UIOFiles
68                Description see class documentation.
69
70            suffix: list of strings
71                The types of files which should be manipulated such as
72                ['grib', 'grb', 'grib1', 'grib2', 'grb1', 'grb2']
73
74        @Return:
75            <nothing>
76        '''
77
78        self.suffix = suffix
79
80        return
81
82    def listFiles(self, path, pattern):
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
93            path: string
94                Directory where to list the files.
95
96            pattern: string
97                Regular expression pattern. For example:
98                '*OG_acc_SL*.'+c.ppid+'.*'
99
100        @Return:
101            <nothing>
102        '''
103
104        # Get the absolute path
105        path = os.path.abspath(path)
106
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])
112
113        self.counter = 0
114        self.files = []
115        # Traverse through all files
116        for file in filesInCurDir:
117            curFile = os.path.join(path, file)
118
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)
132
133        return
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG