-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert2dat.py
139 lines (120 loc) · 5.16 KB
/
convert2dat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
"""
/***************************************************************************
convert2dat
Extract user's desired data from his file and save them in istSOS
acceptable format in .dat file
-------------------
begin : 2017-07-30 (funny, it's my birthday)
author : Ondrej Pesek
email : [email protected]
copyright : (C) Norwegian Institute for Nature Research
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import argparse
from os import sep
from scripts.csv2dat import csv2dat
from scripts.swd2dat import swd2dat
from scripts.xls2dat import xls2dat
def main():
if args.__dict__['d'] is True and args.__dict__['path'][-1] != sep:
print("WARNING: Your path doesn't end with '{}'. It will parse all "
"files beginning with '{}' and ending with '{}' in the path"
"'{}{}'".format(sep,
args.__dict__['path'].split(sep)[-1],
args.__dict__['file_extension'],
args.__dict__['path'].rsplit(sep, 1)[0],
sep))
if 'csv' in args.__dict__['file_extension'] or \
'CSV' in args.__dict__['file_extension']:
csv2dat(args.__dict__['path'],
args.__dict__['observation_columns'],
args.__dict__['timestamp_column'],
args.__dict__['timestamp_format'],
args.__dict__['timestamp_offset'],
args.__dict__['procedure'],
args.__dict__['d'])
elif 'swd' in args.__dict__['file_extension'] or \
'SWD' in args.__dict__['file_extension']:
swd2dat(args.__dict__['path'],
args.__dict__['observation_columns'],
args.__dict__['timestamp_column'],
args.__dict__['timestamp_format'],
args.__dict__['timestamp_offset'],
args.__dict__['procedure'],
args.__dict__['d'],
args.__dict__['t'])
elif 'xls' in args.__dict__['file_extension'] or \
'XLS' in args.__dict__['file_extension']:
xls2dat(args.__dict__['path'],
args.__dict__['timestamp_column'],
args.__dict__['timestamp_format'],
args.__dict__['timestamp_offset'],
args.__dict__['procedure'],
args.__dict__['d'])
else:
print("END: Your file extension is not supported")
if __name__ == '__main__':
timestampFormats = ['YYYY-MM-DDTHH:MM:SS.SSSSSS+HH:MM', 'YYYYMMDD',
'DD.MM.YYYY', 'DD.MM.YY HH:MM:SS', 'YYYYMMDDHH',
'DD.MM.YY HH:MM:SS AM/PM', 'YYYY-MM-DD HH:MM',
'DATE+TIME']
parser = argparse.ArgumentParser(
description='Import data from a file (or files) on an istSOS server.')
parser.add_argument(
'-path',
type=str,
dest='path',
required=True,
help='Path to a file with observations '
'(only working directory with files when using -d flag)')
parser.add_argument(
'-file_extension',
dest='file_extension',
default='.SWD',
help='Extension of files with observations')
parser.add_argument(
'-observation_columns',
type=str,
dest='observation_columns',
help='Name of columns with observations (separated with ",")')
parser.add_argument(
'-timestamp_column',
type=str,
dest='timestamp_column',
default='urn:ogc:def:parameter:x-istsos:1.0:time:iso8601',
help='Name of the column with timestamps')
parser.add_argument(
'-timestamp_format',
type=str,
default='YYYY-MM-DDTHH:MM:SS.SSSSSS+HH:MM',
choices=timestampFormats,
help='Format in which timestamps are provided')
parser.add_argument(
'-timestamp_offset',
type=str,
default='+01:00',
help='Offset of timestamp in format +HH:MM')
parser.add_argument(
'-procedure',
type=str,
help='Who provides the observations')
parser.add_argument(
'-d',
action='store_true',
help='Use if you would like to convert all files with defined '
'extension in the directory')
parser.add_argument(
'-t',
action='store_true',
help='Use template for observation_columns names (INDEX.SWD)')
args = parser.parse_args()
main()