-
Notifications
You must be signed in to change notification settings - Fork 0
/
calp_ch_name.py
664 lines (574 loc) · 21.4 KB
/
calp_ch_name.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#!/usr/bin/env python3
# -*- coding: utf-8 -*
"""This script modifies dates of start and (when applied) end of calculations for input files of elements of
CALMET/CALPUFF system (CALMET, CALPUFF, PRTMET, ). More information in a readme file. Version 0.1 (first release)
"""
# Importing modules
from subprocess import Popen, PIPE
import os
from datetime import datetime
import fileinput
from os import path
import re
from collections import OrderedDict
# Defining function that allows user to choose whether he wants to replace existing file or to create new
def y_n():
while True:
del_of = input("Do you want to delete old file(s)? (Y/N) default: N") or "N"
if del_of == "y" or del_of == "yes" or del_of == "YES" or del_of == "Y":
del_of = "Y"
break
elif (
del_of == "n"
or del_of == "no"
or del_of == "NO"
or del_of == "NO"
or del_of == "N"
):
del_of = "N"
break
elif del_of != "N" or del_of != "Y":
print("Answer with Y or N")
continue
return del_of
# defining function that checks if the path exists
def ch_path():
while True:
patht = (
input("path to directory with source files")
or "/home/kamil/Desktop/calpsot_skrypt/"
)
ch_path = path.exists(patht)
if ch_path:
break
else:
print("Wrong path, try again")
return patht
# Asking user for the input he wants to be changed
def sig():
while True:
sig = (
input(
"Do you want to change input of Calmet (write 'CALM'), Calpost (write 'CLO'), Calpuff (write 'CLF'), "
"PRTMET (write 'PRT'), default 'CLO''"
)
or "CLO"
)
if (
sig == "CALM"
or sig == "calm"
or sig == "CLO"
or sig == "clo"
or sig == "CLF"
or sig == "clf"
or sig == "PRT"
or sig == "prt"
):
break
else:
print("Answer not recognized")
return sig
sig=sig()
patht = ch_path() # path
# Dictionary that assigns values used by specific programs to date variables
calpost = {
"YEAR": "ISYR",
"MONTH": "ISMO",
"DAY": "ISDY",
"HR": "ISHR",
"MIN": "ISMIN",
"SEC": "ISSEC",
"EYEAR": "IEYR",
"EMONTH": "IEMO",
"EDAY": "IEDY",
"EHR": "IEHR",
"EMIN": "IEMIN",
"ESEC": "IESEC",
}
calmet = {
"YEAR": "IBYR",
"MONTH": "IBMO",
"DAY": "IBDY",
"HR": "IBHR",
"DR": "IRLG",
"SEC": "IBSEC",
"EYEAR": "IEYR",
"EMONTH": "IEMO",
"EDAY": "IEDY",
"EHR": "IEHR",
"EMIN": "IEMIN",
"ESEC": "IESEC",
}
calpuff = {
"YEAR": "IBYR",
"MONTH": "IBMO",
"DAY": "IBDY",
"HR": "IBHR",
"DR": "IRLG",
"EYEAR": "IEYR",
"EMONTH": "IEMO",
"EDAY": "IEDY",
"EHR": "IEHR",
"EMIN": "IEMIN",
"ESEC": "IESEC",
}
prtmet = {
"YEAR": "IBYR",
"MONTH": "IBMO",
"DAY": "IBDY",
"HR": "IBHR",
"EYEAR": "IEYR",
"EMONTH": "IEMO",
"EDAY": "IEDY",
"EHR": "IEHR",
"EYEAR": "IEYR",
"EMONTH": "IEMO",
"EDAY": "IEDY",
"EHR": "IEHR",
"EMIN": "IEMIN",
"ESEC": "IESEC",
}
# Getting information about dates
def ch_date(sig):
result = {
"year_beg": None, "mon_beg": None, "day_beg": None, "hr_beg": None,
"min_beg": None, "sec_beg": None, "year_end": None, "mon_end": None,
"day_end": None, "hr_end": None, "min_end": None, "sec_end": None,
"YEAR": None, "MONTH": None, "DAY": None, "HR": None, "MIN": None,
"SEC": None, "EYEAR": None, "EMONTH": None, "EDAY": None,
"EHR": None, "EMIN": None, "ESEC": None, "DR": None
}
while True:
try:
if sig == "CLO":
print("Please name date of beginning of calculations")
# DATE=input("patht to directory with source files")
YEAR = input("Specify year (four numbers)")
MONTH = input("Specify month (two numbers)")
DAY = input("Specify day (two numbers)")
HR = input("Specify hour (default: 00)") or "00"
MIN = input("Specify minutes (default: 00)") or "00"
SEC = input("Specify seconds (default: 00") or "00"
date_time_str = (
YEAR + " " + MONTH + " " + DAY + " " + HR + " " + MIN + " " + SEC
)
date_time_obj = datetime.strptime(date_time_str, '%Y %m %d %H %M %S') # created to check if given date
# is acceptable
year_beg = calpost.get("YEAR")
mon_beg = calpost.get("MONTH")
day_beg = calpost.get("DAY")
hr_beg = calpost.get("HR")
min_beg = calpost.get("MIN")
sec_beg = calpost.get("SEC")
print("Please name date of the end of calculations")
EYEAR = input("Specify year (four numbers)")
EMONTH = input("Specify month (two numbers)")
EDAY = input("Specify day (two numbers)")
EHR = input("Specify hour (default: 00)") or "00"
EMIN = input("Specify minutes (default: 00)") or "00"
ESEC = input("Specify seconds (default: 00") or "00"
date_time_str = (
EYEAR + " " + EMONTH + " " + EDAY + " " + EHR + " " + EMIN + " " + ESEC
)
date_time_obj2 = datetime.strptime(date_time_str, "%Y %m %d %H %M %S")
year_end = calpost.get("EYEAR")
mon_end = calpost.get("EMONTH")
day_end = calpost.get("EDAY")
hr_end = calpost.get("EHR")
min_end = calpost.get("EMIN")
sec_end = calpost.get("ESEC")
result["year_beg"] = year_beg
result["mon_beg"] = mon_beg
result["day_beg"] = day_beg
result["hr_beg"] = hr_beg
result["min_beg"] = min_beg
result["sec_beg"] = sec_beg
result["year_end"] = year_end
result["mon_end"] = mon_end
result["day_end"] = day_end
result["hr_end"] = hr_end
result["min_end"] = min_end
result["sec_end"] = sec_end
result["YEAR"] = YEAR
result["MONTH"] = MONTH
result["DAY"] = DAY
result["HR"] = HR
result["MIN"] = MIN
result["SEC"] = SEC
result["EYEAR"] = EYEAR
result["EMONTH"] = EMONTH
result["EDAY"] = EDAY
result["EHR"] = EHR
result["EMIN"] = EMIN
result["ESEC"] = ESEC
date_time_obj3=(date_time_obj2-date_time_obj)
date_time_obj3=date_time_obj3.total_seconds()
print(date_time_obj3)
print(date_time_obj3/3600)
result["DR"] = str((date_time_obj3/3600))
duration = calpost.get("DR")
result["duration"] = duration
return result
#continue
if sig == "CALM":
print("Please name date of beginning of calculations")
# DATE=input("patht to directory with source files")
YEAR = input("Specify year (four numbers)")
MONTH = input("Specify month (two numbers)")
DAY = input("Specify day (two numbers)")
HR = input("Specify hour (default: 00)") or "00"
MIN = input("Specify minutes (default: 00)") or "00"
SEC = input("Specify seconds (default: 00") or "00"
date_time_str = (
YEAR + " " + MONTH + " " + DAY + " " + HR + " " + MIN + " " + SEC
)
date_time_obj = datetime.strptime(date_time_str, '%Y %m %d %H %M %S') # created to check if given date
# is acceptable
drbeg=date_time_obj
year_beg = calmet.get("YEAR")
mon_beg = calmet.get("MONTH")
day_beg = calmet.get("DAY")
hr_beg = calmet.get("HR")
min_beg = calmet.get("MIN")
sec_beg = calmet.get("SEC")
print("Please name date of the end of calculations")
EYEAR = input("Specify year (four numbers)")
EMONTH = input("Specify month (two numbers)")
EDAY = input("Specify day (two numbers)")
EHR = input("Specify hour (default: 00)") or "00"
EMIN = input("Specify minutes (default: 00)") or "00"
ESEC = input("Specify seconds (default: 00") or "00"
date_time_str = (
EYEAR + " " + EMONTH + " " + EDAY + " " + EHR + " " + EMIN + " " + ESEC
)
date_time_obj2 = datetime.strptime(date_time_str, "%Y %m %d %H %M %S")
#drend=date_time_obj
#DR=drend-drbeg
#total_seconds = DR.total_seconds()
#hours = total_seconds // 3600
# print(hours)
# DR=int(hours)
year_end = calmet.get("EYEAR")
mon_end = calmet.get("EMONTH")
day_end = calmet.get("EDAY")
hr_end = calmet.get("EHR")
min_end = calmet.get("EMIN")
sec_end = calmet.get("ESEC")
result["year_beg"] = year_beg
result["mon_beg"] = mon_beg
result["day_beg"] = day_beg
result["hr_beg"] = hr_beg
result["min_beg"] = min_beg
result["sec_beg"] = sec_beg
result["year_end"] = year_end
result["mon_end"] = mon_end
result["day_end"] = day_end
result["hr_end"] = hr_end
result["min_end"] = min_end
result["sec_end"] = sec_end
result["YEAR"] = YEAR
result["MONTH"] = MONTH
result["DAY"] = DAY
result["HR"] = HR
result["MIN"] = MIN
result["SEC"] = SEC
result["EYEAR"] = EYEAR
result["EMONTH"] = EMONTH
result["EDAY"] = EDAY
result["EHR"] = EHR
result["EMIN"] = EMIN
result["ESEC"] = ESEC
date_time_obj3=(date_time_obj2-date_time_obj)
date_time_obj3=date_time_obj3.total_seconds()
print(date_time_obj3)
print(date_time_obj3/3600)
result["DR"] = str((date_time_obj3/3600))
duration = calmet.get("DR")
result["duration"] = duration
return result
return result
if sig == "CLF":
print("Please name date of beginning of calculations")
# DATE=input("patht to directory with source files")
YEAR = input("Specify year (four numbers)")
MONTH = input("Specify month (two numbers)")
DAY = input("Specify day (two numbers)")
HR = input("Specify hour (default: 00)") or "00"
MIN = input("Specify minutes (default: 00)") or "00"
SEC = input("Specify seconds (default: 00") or "00"
date_time_str = (
YEAR + " " + MONTH + " " + DAY + " " + HR + " " + MIN + " " + SEC
)
date_time_obj = datetime.strptime(date_time_str, '%Y %m %d %H %M %S') # created to check if given date
# is acceptable
drbeg=date_time_obj
year_beg = calpuff.get("YEAR")
mon_beg = calpuff.get("MONTH")
day_beg = calpuff.get("DAY")
hr_beg = calpuff.get("HR")
min_beg = calpuff.get("MIN")
sec_beg = calpuff.get("SEC")
print("Please name date of the end of calculations")
EYEAR = input("Specify year (four numbers)")
EMONTH = input("Specify month (two numbers)")
EDAY = input("Specify day (two numbers)")
EHR = input("Specify hour (default: 00)") or "00"
EMIN = input("Specify minutes (default: 00)") or "00"
ESEC = input("Specify seconds (default: 00") or "00"
date_time_str = (
EYEAR + " " + EMONTH + " " + EDAY + " " + EHR + " " + EMIN + " " + ESEC
)
date_time_obj2 = datetime.strptime(date_time_str, "%Y %m %d %H %M %S")
#drend=date_time_obj
#DR=drend-drbeg
#total_seconds = DR.total_seconds()
#hours = total_seconds // 3600
# print(hours)
# DR=int(hours)
year_end = calpuff.get("EYEAR")
mon_end = calpuff.get("EMONTH")
day_end = calpuff.get("EDAY")
hr_end = calpuff.get("EHR")
min_end = calpuff.get("EMIN")
sec_end = calpuff.get("ESEC")
result["year_beg"] = year_beg
result["mon_beg"] = mon_beg
result["day_beg"] = day_beg
result["hr_beg"] = hr_beg
result["min_beg"] = min_beg
result["sec_beg"] = sec_beg
result["year_end"] = year_end
result["mon_end"] = mon_end
result["day_end"] = day_end
result["hr_end"] = hr_end
result["min_end"] = min_end
result["sec_end"] = sec_end
result["YEAR"] = YEAR
result["MONTH"] = MONTH
result["DAY"] = DAY
result["HR"] = HR
result["MIN"] = MIN
result["SEC"] = SEC
result["EYEAR"] = EYEAR
result["EMONTH"] = EMONTH
result["EDAY"] = EDAY
result["EHR"] = EHR
result["EMIN"] = EMIN
result["ESEC"] = ESEC
date_time_obj3=(date_time_obj2-date_time_obj)
date_time_obj3=date_time_obj3.total_seconds()
print(date_time_obj3)
print(date_time_obj3/3600)
result["DR"] = str((date_time_obj3/3600))
duration = calpuff.get("DR")
result["duration"] = duration
return result
return result
if sig == "PRT":
print("Please name date of beginning of calculations")
# DATE=input("patht to directory with source files")
YEAR = input("Specify year (four numbers)")
MONTH = input("Specify month (two numbers)")
DAY = input("Specify day (two numbers)")
HR = input("Specify hour (default: 00)") or "00"
MIN = input("Specify minutes (default: 00)") or "00"
SEC = input("Specify seconds (default: 00") or "00"
date_time_str = (
YEAR + " " + MONTH + " " + DAY + " " + HR + " " + MIN + " " + SEC
)
date_time_obj = datetime.strptime(date_time_str, '%Y %m %d %H %M %S') # created to check if given date
# is acceptable
drbeg=date_time_obj
year_beg = prtmet.get("YEAR")
mon_beg = prtmet.get("MONTH")
day_beg = prtmet.get("DAY")
hr_beg = prtmet.get("HR")
min_beg = prtmet.get("MIN")
sec_beg = prtmet.get("SEC")
print("Please name date of the end of calculations")
EYEAR = input("Specify year (four numbers)")
EMONTH = input("Specify month (two numbers)")
EDAY = input("Specify day (two numbers)")
EHR = input("Specify hour (default: 00)") or "00"
EMIN = input("Specify minutes (default: 00)") or "00"
ESEC = input("Specify seconds (default: 00") or "00"
date_time_str = (
EYEAR + " " + EMONTH + " " + EDAY + " " + EHR + " " + EMIN + " " + ESEC
)
date_time_obj2 = datetime.strptime(date_time_str, "%Y %m %d %H %M %S")
#drend=date_time_obj
#DR=drend-drbeg
#total_seconds = DR.total_seconds()
#hours = total_seconds // 3600
# print(hours)
# DR=int(hours)
year_end = prtmet.get("EYEAR")
mon_end = prtmet.get("EMONTH")
day_end = prtmet.get("EDAY")
hr_end = prtmet.get("EHR")
min_end = prtmet.get("EMIN")
sec_end = prtmet.get("ESEC")
result["year_beg"] = year_beg
result["mon_beg"] = mon_beg
result["day_beg"] = day_beg
result["hr_beg"] = hr_beg
result["min_beg"] = min_beg
result["sec_beg"] = sec_beg
result["year_end"] = year_end
result["mon_end"] = mon_end
result["day_end"] = day_end
result["hr_end"] = hr_end
result["min_end"] = min_end
result["sec_end"] = sec_end
result["YEAR"] = YEAR
result["MONTH"] = MONTH
result["DAY"] = DAY
result["HR"] = HR
result["MIN"] = MIN
result["SEC"] = SEC
result["EYEAR"] = EYEAR
result["EMONTH"] = EMONTH
result["EDAY"] = EDAY
result["EHR"] = EHR
result["EMIN"] = EMIN
result["ESEC"] = ESEC
date_time_obj3=(date_time_obj2-date_time_obj)
date_time_obj3=date_time_obj3.total_seconds()
print(date_time_obj3)
print(date_time_obj3/3600)
result["DR"] = str((date_time_obj3/3600))
duration = prtmet.get("DR")
result["duration"] = duration
return result
except ValueError as Ve:
print("Wrong date, try again")
print(f"ERROR: {Ve}")
continue
#return result
date_values = ch_date(sig)
print(date_values)
year_beg = date_values["year_beg"]
mon_beg = date_values["mon_beg"]
day_beg = date_values["day_beg"]
hr_beg = date_values["hr_beg"]
min_beg = date_values["min_beg"]
sec_beg = date_values["sec_beg"]
year_end = date_values["year_end"]
mon_end = date_values["mon_end"]
day_end = date_values["day_end"]
hr_end = date_values["hr_end"]
min_end = date_values["min_end"]
sec_end = date_values["sec_end"]
YEAR = date_values["YEAR"]
MONTH = date_values["MONTH"]
DAY = date_values["DAY"]
HR = date_values["HR"]
MIN = date_values["MIN"]
SEC = date_values["SEC"]
EYEAR = date_values["EYEAR"]
EMONTH = date_values["EMONTH"]
EDAY = date_values["EDAY"]
EHR = date_values["EHR"]
EMIN = date_values["EMIN"]
ESEC = date_values["ESEC"]
DR = date_values["DR"]
#duration = date_values["duration"]
duration = date_values["duration"]
# Function that allows user to decide whether they want to create new input file (while keeping the old) or to replace
# the existing one
def dir_path(sig, del_of):
if del_of == "N":
# continue
if sig == "CLO":
name_of = (
input("Name of the file to modify (default name: calpost.inp)")
or "calpost.inp"
)
name_nf = (
input("Name of the new file (default name: calpost.inp1)")
or "calpost.inp1"
)
if sig == "CALM":
name_of = (
input("Name of the file to modify (default name: calmet.inp)")
or "calmet.inp"
)
name_nf = (
input("Name of the new file (default name: calmet.inp1)")
or "calmet.inp1"
)
if sig == "CLF":
name_of = (
input("Name of the file to modify (default name: calpuff.inp)")
or "calpuff.inp"
)
name_nf = (
input("Name of the new file(default name: calpuff.inp1)")
or "calpuff.inp1"
)
if sig == "PRT":
name_of = (
input("Name of the file to modify (default name: prtmet.inp)")
or "prtmet.inp"
)
name_nf = (
input("Name of the new file(default name: prtmet.inp1)")
or "prtmet.inp1"
)
else:
if sig == "CLO":
name_of = (
input("Name of the file to modify (default name: calpost.inp)")
or "calpost.inp"
)
if sig == "CALM":
name_of = (
input("Name of the file to modify (default name: calmet.inp)")
or "calmet.inp"
)
if sig == "CLF":
name_of = (
input("Name of the file to modify (default name: calpuff.inp)")
or "calpuff.inp"
)
if sig == "PRT":
name_of = (
input("Name of the file to modify (default name: prtmet.inp)")
or "prtmet.inp"
)
name_nf = name_of
return name_of, name_nf
del_of = y_n() # deleting files
dir_path = dir_path(sig, del_of)
name_of = dir_path[0]
name_nf = dir_path[1]
# Opening base input file
with open(patht + name_of, "r",errors='ignore') as file:
contents = file.read()
# Finding dates and replacing them
replace_dict = OrderedDict([
('year_beg', 'YEAR'),
('mon_beg', 'MONTH'),
('day_beg', 'DAY'),
('hr_beg', 'HR'),
('min_beg', 'MIN'),
('sec_beg', 'SEC'),
('year_end', 'EYEAR'),
('mon_end', 'EMONTH'),
('day_end', 'EDAY'),
('hr_end', 'EHR'),
('min_end', 'EMIN'),
('sec_end', 'ESEC'),
('duration', 'DR'),
])
for beg, replacement in replace_dict.items():
match = re.search(f"{globals()[beg]}\s*=\s*(\d+)", contents)
if match:
print(f"Found match for {globals()[beg]}, replacing...")
old_value = int(match.group(1))
contents = re.sub(f"{globals()[beg]}\s*=\s*\d+", f"{globals()[beg]} = {globals()[replacement]}", contents)
else:
print(f"No match found for {globals()[beg]}.")
#Saving file with changed date
with open(patht + name_nf, "w") as file:
file.write(contents)