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 | # ------------------------------------------------------------------------------ |
---|
45 | import os |
---|
46 | import glob |
---|
47 | |
---|
48 | # ------------------------------------------------------------------------------ |
---|
49 | # Class |
---|
50 | # ------------------------------------------------------------------------------ |
---|
51 | class 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 | Types of files to manipulate such as |
---|
72 | ['grib', 'grb', 'grib1', 'grib2', 'grb1', 'grb2'] |
---|
73 | |
---|
74 | @Return: |
---|
75 | <nothing> |
---|
76 | ''' |
---|
77 | self.suffix = suffix |
---|
78 | return |
---|
79 | |
---|
80 | def listFiles(self, pathname, pattern): |
---|
81 | ''' |
---|
82 | @Description: |
---|
83 | Lists all files in the directory with the matching |
---|
84 | regular expression pattern. The suffixes are already stored |
---|
85 | in a list attribute "suffix". |
---|
86 | |
---|
87 | @Input: |
---|
88 | self: instance of UIOFiles |
---|
89 | Description see class documentation. |
---|
90 | |
---|
91 | pathname: string |
---|
92 | Directory where to list the files. |
---|
93 | |
---|
94 | pattern: string |
---|
95 | Regular expression pattern. For example: |
---|
96 | '*OG_acc_SL*.'+c.ppid+'.*' |
---|
97 | |
---|
98 | @Return: |
---|
99 | <nothing> |
---|
100 | ''' |
---|
101 | #AP pathname zu path ändern |
---|
102 | #AP is it possible for each possible file extension ? mabye regexx? |
---|
103 | # Get the absolute path of the pathname parameter |
---|
104 | pathname = os.path.abspath(pathname) |
---|
105 | |
---|
106 | # Get a list of files in pathname |
---|
107 | filesInCurDir0 = glob.glob(pathname + '/' + pattern) |
---|
108 | filesInCurDir = [] |
---|
109 | for f in filesInCurDir0: |
---|
110 | filesInCurDir.append(f.split('/')[-1]) |
---|
111 | self.counter = 0 |
---|
112 | self.files = [] |
---|
113 | # Traverse through all files |
---|
114 | for file in filesInCurDir: |
---|
115 | curFile = os.path.join(pathname, file) |
---|
116 | |
---|
117 | # Check if it's a normal file or directory |
---|
118 | if os.path.isfile(curFile): |
---|
119 | # Get the file extension |
---|
120 | fileNoExt, curFileExtension = os.path.splitext(curFile) |
---|
121 | # Check if the file has an extension of typical video files |
---|
122 | if curFileExtension in self.suffix: |
---|
123 | # We have got a file file! Increment the counter |
---|
124 | self.counter += 1 |
---|
125 | # add this filename in the list |
---|
126 | self.files.append(curFile) |
---|
127 | else: |
---|
128 | # We got a directory, enter into it for further processing |
---|
129 | self.listFiles(curFile) |
---|
130 | |
---|
131 | return |
---|