-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-gizmo.py
More file actions
1174 lines (909 loc) · 48.5 KB
/
data-gizmo.py
File metadata and controls
1174 lines (909 loc) · 48.5 KB
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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
from tkinter import Tk, Label, Button, StringVar, Entry,NONE, END,HORIZONTAL,N, W, E, S, Checkbutton,Radiobutton, IntVar, Radiobutton, Scrollbar, Listbox, LEFT, BOTH, Spinbox, Menu, Text, NORMAL
import tkinter as tk
from tkinter import ttk
#For more themes
#from ttkthemes import ThemedTk
#import math
# to create a dialog interface for the input file
from tkinter.filedialog import askopenfilename
#from tkinter.filedialog import asksavefilename
from tkinter.filedialog import asksaveasfile
# to create a dialog interface for the input file
from tkinter.filedialog import askdirectory
from tkinter import scrolledtext
# to manage a different font
from tkinter import font
# need this to check if a a file exists
import os
import sys
# to manage date
import datetime
import time
# to manage images
#from PIL import Image, ImageTk
# to manage plots
import matplotlib.pyplot as plt
#from mpl_toolkits.mplot3d import Axes3D
import numpy
import xarray as xr
import pandas as pd
import pandas
import uuid
#import requests
import random
import string
import requests as rq
from bs4 import BeautifulSoup
import json
import xmltodict
import csv
#from pandastable import Table, TableModel
from ttkthemes import ThemedTk
#For the webserver
import http.server
import socketserver
import threading
'''
To create an exe (Linux/Windows):
https://pypi.org/project/auto-py-to-exe/
pip install auto-py-to-exe
auto-py-to-exe
To include the logo use the following option inside auto-py-to-exe:
--hidden-import='PIL._tkinter_finder'
FOR WINDOWS ONLY with ANACONDA:
--exclude-module scikit-learn,PyQt5,PyQt4,2to3,IPython,Jinja2,pycparser,scipy
TO ADD NEW THEMES:
pip install ttkthemes
N.B. The themes plastik, clearlooks and elegance are recommended to make your
UI look nicer on all platforms when using Tkinter and the ttk extensions in Python.
When you are targeting Ubuntu, consider using the great radiance theme.
'''
class GUItemplate:
def __init__(self, master):
self.master = master
#master.title("DATA-GIZMO")
'''
THE FOLLOWING THREE ROWS EXPLAIN HOW TO CHANGE THE STATE OF A TAB
DISABLED: the tab is visible but not active
NORMAL: the tab is in nomral state
HIDDEN: the tab is not visible
these options could be useful if the software must manage different
input/output LabelFrameINFO and it's necessary show only some tabs
N.B. in the example the tab is 2 (self.LabelFrameD)
'''
#self.nb.tab(2, state="disabled")
#self.nb.tab(2, state="normal")
#self.nb.tab(2, state="hidden")
frameFont = ttk.Style()
frameFont.configure('new.TFrame', family='Verdana', size=8, weight='bold', underline=1)
# Defines and places the notebook widget
self.nb = ttk.Notebook(self.master)
self.nb.grid(row=1, column=0, columnspan=50, rowspan=49, sticky='NESW')
# Adds tab of the notebook
self.LabelFrameINFO = ttk.Frame(self.nb, style='new.TFrame')
self.nb.add(self.LabelFrameINFO, text='INFO')
# Adds tab of the notebook
self.LabelFrameMerger = ttk.Frame(self.nb)
self.nb.add(self.LabelFrameMerger, text='Merger')
# Adds tab of the notebook
self.LabelFrameDataJSON = ttk.Frame(self.nb)
self.nb.add(self.LabelFrameDataJSON, text='JSON')
# Adds tab of the notebook
self.LabelFrameNETCDF = ttk.Frame(self.nb)
self.nb.add(self.LabelFrameNETCDF, text='NETCDF')
# Adds tab of the notebook
self.LabelFrameMix = ttk.Frame(self.nb)
self.nb.add(self.LabelFrameMix, text='MIX')
#Common parts we have to wrap each command
openfile = master.register(self.OpenInputFile)
openfileXLS = master.register(self.OpenInputFileSheet)
openfileCSV = master.register(self.OpenInputFileCSV)
openfileXLSOneOutput = master.register(self.OpenInputFileSheetOneOutput)
plotNetcdf = master.register(self.PlotOpenInputFileNetcdf)
csvNetcdf = master.register(self.CSVOpenInputFileNetcdf)
jsonNetcdf = master.register(self.JSONOpenInputFileNetcdf)
xlsxNetcdf = master.register(self.XLSXOpenInputFileNetcdf)
openURLlinks = master.register(self.OpenInputURLlinks)
searchinfilesFunc = master.register(self.searchinfiles)
generatePWD = master.register(self.generateRNDPWD)
cropCSV = master.register(self.CropCSVfile)
startwebserver = master.register(self.webServerStarted)
guidgenerate = master.register(self.generateGUID)
openXml2Json = master.register(self.OpenInputFileXml2Json)
openCsv2Json = master.register(self.OpenInputFileCSV2Json)
openfileODV = master.register(self.OpenInputFileODV)
openODVMerge = master.register(self.OpenInputODVMerge)
vcmd = master.register(self.validatenumber)
mytext=''
self.create=0
'''
START here we add a cascading menu
'''
# START here we add a cascading menu
self.preferences = Menu(self.master)
self.menuFieldA = Menu(self.master)
self.menuFieldB = Menu(self.master)
appHighlightFont = font.Font(family='helvetica', size=12, weight='normal', underline=0)
# print(font.families())
# AVAILABLE FONTS on TKINTER
# ('fangsong ti',
# 'fixed',
# 'clearlyu alternate glyphs',
# 'charter',
# 'lucidatypewriter',
# 'courier 10 pitch',
# 'lucidabright',
# 'times',
# 'open look glyph',
# 'bitstream charter',
# 'song ti', 'helvetica',
# 'open look cursor',
# 'newspaper',
# 'clearlyu ligature',
# 'mincho',
# 'clearlyu devangari extra',
# 'clearlyu pua',
# 'courier',
# 'clearlyu',
# 'lucida',
# 'clean',
# 'nil',
# 'clearlyu arabic',
# 'clearlyu devanagari',
# 'terminal',
# 'symbol',
# 'gothic',
# 'new century schoolbook',
# 'clearlyu arabic extra')
self.labelINFO = Label(self.LabelFrameINFO, text=mytext, bg="brown4", justify="left", fg="white", font=appHighlightFont, height=15, width=69)
new_text = "Remember the Zen of Python:\n"
new_text += "\n * Beautiful is better than ugly."
new_text += " Explicit is better than implicit."
new_text += "\n * Simple is better than complex."
new_text += " Complex is better than complicated."
new_text += "\n * Flat is better than nested."
new_text += " Sparse is better than dense."
new_text += "\n * Readability counts."
new_text += " Special cases aren't special enough to break the rules."
new_text += "\n * Although practicality beats purity."
new_text += " Errors should never pass silently."
new_text += "\n * Unless explicitly silenced."
new_text += " In the face of ambiguity, refuse the temptation to guess."
new_text += "\n * There should be one-- and preferably only one --obvious way to do it."
new_text += "\n * Although that way may not be obvious at first unless you're Dutch."
new_text += "\n * Now is better than never."
new_text += " Although never is often better than right now."
new_text += "\n * If the implementation is hard to explain, it's a bad idea."
new_text += "\n * If the implementation is easy to explain, it may be a good idea."
new_text += "\n * Namespaces are one honking great idea – let's do more of those!"
self.labelINFO['text'] = new_text
self.labelINFO.grid(row=0, column=0, columnspan=12, rowspan=10, padx=55, pady=55)
#START INFO
self.labelEntryInfoFieldASF = Label(self.LabelFrameNETCDF, text=mytext)
self.labelEntryInfoFieldASF['text'] = 'The value for FieldA: ' + str(mytext)
self.labelEntryInfoFieldASF.grid(row=1, column=0, sticky=E)
self.entryInfoFieldAVarsSF = tk.IntVar()
self.entryInfoFieldASF = Spinbox(self.LabelFrameNETCDF, from_=1, to=10, textvariable= self.entryInfoFieldAVarsSF, width=2, bg="gold3", validate="key", validatecommand=(vcmd, '%P'))
self.entryInfoFieldASF.grid(row=1, column=1, sticky=W)
self.openInputFileButtonSF = Button(self.LabelFrameNETCDF, text="Load NetCDF Time Series Input File", command=(openfile), fg='white', bg='brown4',width=35)
self.openInputFileButtonSF.grid(row=1, column=0, sticky=E)
self.entryInfoInputFileSF = Entry(self.LabelFrameNETCDF, width=50, bg="gold3", validate="key")
self.entryInfoInputFileSF.grid(row=1, column=1, columnspan=3, sticky=W)
self.CSVNetcdfButton = Button(self.LabelFrameNETCDF, text="To CSV", width=10, command=(csvNetcdf), fg='white', bg='brown4')
self.CSVNetcdfButton.grid(row=2, column=1, sticky=W)
self.XLSXNetcdfButton = Button(self.LabelFrameNETCDF, text="To XLSX", width=10, command=(xlsxNetcdf), fg='white', bg='brown4')
self.XLSXNetcdfButton.grid(row=2, column=2, sticky=W)
self.JSONNetcdfButton = Button(self.LabelFrameNETCDF, text="To JSON", width=10, command=(jsonNetcdf), fg='white', bg='brown4')
self.JSONNetcdfButton.grid(row=2, column=3, sticky=W)
self.PlotNetcdfButton = Button(self.LabelFrameNETCDF, text="PLOT", width=10, command=(plotNetcdf), fg='white', bg='brown4')
self.PlotNetcdfButton.grid(row=2, column=4, sticky=W)
self.LabelFrameNETCDFFieldA = scrolledtext.ScrolledText(self.LabelFrameNETCDF, height=20, width=103, bg="gold3")
self.LabelFrameNETCDFFieldA.grid(row=59, column=0, columnspan=16, rowspan=30)
self.LabelFrameNETCDFFieldA.insert(END, "Infobox:")
self.openInputFileButtonXLS = Button(self.LabelFrameMerger, text="Select directory (merge only XLSX/XLS files) - One file for each sheet", command=(openfileXLS), fg='white', bg='brown4',width=60)
self.openInputFileButtonXLS.grid(row=3, column=0, sticky=E)
self.openInputFileButtonXLSOneOutput = Button(self.LabelFrameMerger, text="Select directory (merge only XLSX/XLS files) - One file for all sheets", command=(openfileXLSOneOutput), fg='white', bg='brown4',width=60)
self.openInputFileButtonXLSOneOutput.grid(row=4, column=0, sticky=E)
self.openInputFileButtonCSV = Button(self.LabelFrameMerger, text="Select directory (merge only TXT/CSV files)", command=(openfileCSV), fg='white', bg='brown4',width=40)
self.openInputFileButtonCSV.grid(row=3, column=1, sticky=E)
self.openInputFileButtonPDT = Button(self.LabelFrameMerger, text="Load ODV directory to merge", command=(openODVMerge), fg='white', bg='brown4',width=40)
self.openInputFileButtonPDT.grid(row=4, column=1, sticky=E)
self.LabelFrameMergerFieldAXLS = scrolledtext.ScrolledText(self.LabelFrameMerger, height=20, width=103, bg="gold3")
self.LabelFrameMergerFieldAXLS.grid(row=59, column=0, columnspan=6, rowspan=10)
self.LabelFrameMergerFieldAXLS.insert(END, "Infobox:")
self.openInputFileButtonURL = Button(self.LabelFrameMix, text="Search URLs in webpage", command=(openURLlinks), fg='white', bg='brown4',width=35)
self.openInputFileButtonURL.grid(row=1, column=0, sticky=E)
self.SearchTextButton = Button(self.LabelFrameMix, text="Search text in a directory", command=(searchinfilesFunc), fg='white', bg='brown4',width=35)
self.SearchTextButton.grid(row=2, column=0, sticky=E)
self.RNDPasswordButton = Button(self.LabelFrameMix, text="Generate random password", command=(generatePWD), fg='white', bg='brown4',width=35)
self.RNDPasswordButton.grid(row=3, column=0, sticky=E)
self.CropCSVButton = Button(self.LabelFrameMix, text="CROP large CSV file", command=(cropCSV), fg='white', bg='brown4',width=35)
self.CropCSVButton.grid(row=4, column=0, sticky=E)
self.webserverButton = Button(self.LabelFrameMix, text="Start webserver (select a dir as web root, port 8000)", command=(startwebserver), fg='white', bg='brown4',width=49)
self.webserverButton.grid(row=3, column=1, sticky=W)
self.webserverButton = Button(self.LabelFrameMix, text="Generate GUID (globally unique identifier)", command=(guidgenerate), fg='white', bg='brown4',width=49)
self.webserverButton.grid(row=4, column=1, sticky=W)
self.URLinput = Text(self.LabelFrameMix, width=50, height=1, bg="gold3")
self.URLinput.grid(row=1, column=1, sticky=W)
self.textSearchinput = Text(self.LabelFrameMix, width=50, height=1, bg="gold3")
self.textSearchinput.grid(row=2, column=1, sticky=W)
self.LabelFrameFieldMix = scrolledtext.ScrolledText(self.LabelFrameMix, height=20, width=103, bg="gold3")
self.LabelFrameFieldMix.grid(row=59, column=0, columnspan=6, rowspan=10)
self.LabelFrameFieldMix.insert(END, "Infobox:")
self.openInputFileButtonJSON = Button(self.LabelFrameDataJSON, text="From XML to JSON (directory)", command=(openXml2Json), fg='white', bg='brown4',width=35)
self.openInputFileButtonJSON.grid(row=1, column=0, sticky=E)
self.openInputFileButtonCSVJSON = Button(self.LabelFrameDataJSON, text="From CSV to JSON (directory)", command=(openCsv2Json), fg='white', bg='brown4',width=35)
self.openInputFileButtonCSVJSON.grid(row=2, column=0, sticky=E)
self.LabelFrameFieldJSON = scrolledtext.ScrolledText(self.LabelFrameDataJSON, height=20, width=103, bg="gold3")
self.LabelFrameFieldJSON.grid(row=59, column=0, columnspan=6, rowspan=10)
self.LabelFrameFieldJSON.insert(END, "Infobox:")
def validatenumber(self, new_text):
if not new_text: # the field is being cleared
self.entered_number = 0
return True
try:
self.entered_number = int(new_text)
return True
except ValueError:
return False
def handler_from(self,directory):
def _init(self, *args, **kwargs):
return http.server.SimpleHTTPRequestHandler.__init__(self, *args, directory=self.directory, **kwargs)
return type(f'HandlerFrom<{directory}>',
(http.server.SimpleHTTPRequestHandler,),
{'__init__': _init, 'directory': directory})
def webServerStarted(self,):
PORT = 8000
DIRECTORY = askdirectory()
httpd=socketserver.TCPServer(("", PORT), self.handler_from(DIRECTORY))
print("serving at port", PORT)
print("Starting Server in background")
thread = threading.Thread(target = httpd.serve_forever)
thread.daemon = True
thread.start()
def generateGUID(self,):
guid = uuid.uuid4()
self.LabelFrameFieldMix.insert(END, "\n GUID: "+str(guid))
def searchinfiles(self,):
f = 0
dirname = askdirectory()
#files = os.listdir(dirname)
text = str(self.textSearchinput.get("1.0", "end-1c"))
# print(files)
if text !='' and dirname !='':
for file_name in os.listdir(dirname):
#abs_path = os.path.abspath(file_name)
#if os.path.isdir(abs_path):
#self.searchinfiles(abs_path)
if os.path.isfile(dirname+'/'+file_name):
f = open(dirname+'/'+file_name, "r")
if text in f.read():
f = 1
#print(text + " found in ")
self.LabelFrameFieldMix.insert(END, "\n found "+str(text))
final_path = os.path.abspath(file_name)
self.LabelFrameFieldMix.insert(END, "\n in file: "+str(final_path))
#print(final_path)
return True
if f == 0:
#print(text + " not found! ")
self.LabelFrameFieldMix.insert(END, "\n not found: "+str(text))
return False
if text =='':
self.LabelFrameFieldMix.insert(END, "\n Please write the search term ")
if dirname =='':
self.LabelFrameFieldMix.insert(END, "\n Please select a directory ")
def OpenInputODVMerge(self,):
#actualdirname = os.getcwd()
dirname = askdirectory()
CSVNameFullMerge = str(time.strftime("%Y%m%d%H%M%S")+'_FullMerge.csv')
CSVNameDataMerge = str(time.strftime("%Y%m%d%H%M%S")+'_DataMerge.csv')
CSVNameDataMergeTS = str(time.strftime("%Y%m%d%H%M%S")+'_DataMergeTS.csv')
mycounterDummy=1
for fn in os.listdir(dirname):
counterHeader = 1
if not os.path.isfile(dirname+'/'+fn):
return "File not found: " + fn
self.LabelFrameMergerFieldAXLS.insert(END, "\n File not found: "+fn)
sys.exit(-1)
try:
f = open(dirname+'/'+fn,'r')
CSVOutputFile = open(CSVNameFullMerge,"a")
#CSVOutputFileData = open(CSVNameDataMerge,"a")
while True:
l=f.readline()
#O.header.append(l)
if l.find('//')==-1:
#print ('finished reading semantic header')
#CSVOutputFile.write('\t'+l)
CSVOutputFile.write(l)
CSVOutputFile.close()
if mycounterDummy == 1:
CSVOutputFileData = open(CSVNameDataMerge,"a")
CSVOutputFileData.write(l.replace('\t', ','))
CSVOutputFileData.close()
CSVOutputFileDataTS = open(CSVNameDataMergeTS,"a")
CSVOutputFileDataTS.write(l.replace('\t', ','))
CSVOutputFileDataTS.close()
#mycounterDummy += 1
break
CSVOutputFile.write(l)
counterHeader += 1
#print('counterHeader:'+str(counterHeader))
except IOError:
sys.exit(-1)
data = pandas.read_csv(dirname+'/'+fn,sep='\t',index_col=False, na_values=numpy.nan, skiprows = counterHeader-1) #, parse_dates=[3], infer_datetime_format=True, date_parser=odvdatetime)
data.columns = [c.replace(' ', '_') for c in data.columns]
data['Cruise'].fillna(method='ffill', inplace = True)
data['Station'].fillna(method='ffill', inplace = True)
data['Type'].fillna(method='ffill', inplace = True)
data['YYYY-MM-DDThh:mm:ss.sss'].fillna(method='ffill', inplace = True)
data['Longitude_[degrees_east]'].fillna(method='ffill', inplace = True)
data['Latitude_[degrees_north]'].fillna(method='ffill', inplace = True)
data['LOCAL_CDI_ID'].fillna(method='ffill', inplace = True)
data['EDMO_code'].fillna(method='ffill', inplace = True)
data['Bot._Depth_[m]'].fillna(method='ffill', inplace = True)
#print(O.data.iloc[:, 0:12])
print(data)
#data.iloc[:,1:].to_csv(CSVName, mode='a', header=False)
data.to_csv(CSVNameFullMerge, mode='a', header=False,index=False)
data.to_csv(CSVNameDataMerge, mode='a', header=False,index=False)
f.close()
mycounterDummy += 1
TSDataSorted = pandas.read_csv(CSVNameDataMerge,sep=',', na_values=numpy.nan,index_col=False) #, parse_dates=[3], infer_datetime_format=True, date_parser=odvdatetime)
print(TSDataSorted.sort_values(by =['Pres_Z [dBar]', 'YYYY-MM-DDThh:mm:ss.sss']))
OutputDataSorted=TSDataSorted.sort_values(by =['Pres_Z [dBar]', 'YYYY-MM-DDThh:mm:ss.sss'])
OutputDataSorted.to_csv(CSVNameDataMergeTS, mode='a', header=False,index=False)
#df = pd.read_csv(self.name)
self.LabelFrameMergerFieldAXLS.insert(END, "\n Created file: "+str(CSVNameFullMerge))
self.LabelFrameMergerFieldAXLS.insert(END, "\n Created file: "+str(CSVNameDataMerge))
self.LabelFrameMergerFieldAXLS.insert(END, "\n Created file: "+str(CSVNameDataMergeTS))
def generateRNDPWD(self,):
total = string.ascii_letters + string.digits + string.punctuation
length = 16
password = "".join(random.sample(total, length))
print(password)
self.LabelFrameFieldMix.insert(END, "\nPassword: "+str(password))
def CropCSVfile(self,):
actualdirname = os.getcwd()
self.LabelFrameFieldMix.delete('1.0', END)
self.name = askopenfilename(initialdir=actualdirname,
filetypes =(("CSV", "*.csv"),("TXT", "*.txt"),("All Files","*.*")),
title = "Choose a file."
)
self.LabelFrameFieldMix.insert(END, "\n Read File: "+str(self.name))
chunksize = 10 ** 6
for chunk in pd.read_csv(self.name, chunksize=chunksize):
# chunk is a DataFrame. To "process" the rows in the chunk:
outputFile = str(time.strftime("%Y%m%d%H%M%S")+'_output-file.csv')
chunk.to_csv(outputFile, encoding='utf-8')
self.LabelFrameFieldMix.insert(END, "\n Created File: "+str(outputFile))
def OpenInputURLlinks(self,):
url = str(self.URLinput.get("1.0", "end-1c"))+ '/'
self.LabelFrameFieldMix.insert(END, "\n"+str(url))
if ("https" or "http") in url:
data = rq.get(url , timeout=10)
else:
data = rq.get("https://" + url , timeout=10)
soup = BeautifulSoup(data.text, "html.parser")
links = []
for link in soup.find_all("a"):
links.append(link.get("href"))
for foundLink in links:
self.LabelFrameFieldMix.insert(END, "\n"+str(foundLink))
#Define an input file for data.
def OpenInputFile(self,):
actualdirname = os.getcwd()
self.name = askopenfilename(initialdir=actualdirname,
filetypes =(("NetCDF", "*.nc"),("All Files","*.*")),
title = "Choose a file."
)
self.entryInfoInputFileSF.delete(0,END)
self.entryInfoInputFileSF.insert(0,self.name)
#self.src = self.name
#self.entryInfoInputFileSF.insert(END, str(self.name))
self.LabelFrameNETCDFFieldA.delete('1.0', END)
self.LabelFrameNETCDFFieldA.insert(END, "\n NetCDF Input File: "+str(self.name))
def JSONOpenInputFileNetcdf(self,):
try:
ds = xr.open_dataset(self.name)
for attr in ds.attrs.items():
#print(str(attr))
self.LabelFrameNETCDFFieldA.insert(END, "\n "+str(attr))
try:
subset = []
for param in ds.data_vars:
if ("_DM" not in param) and ("_QC" not in param):
subset.append(param)
var_w_units = []
for i in subset:
try:
var_w_units.append(f"{i} [{ds[i].units}]")
except:
var_w_units.append(f"")
#coordType=0
df = pd.DataFrame()
for i in subset:
try:
df[i] = ds[i][:,0].values
except:
try:
df[i] = ds[i][:].values
#coordType=1
except:
pass
df.columns = var_w_units
try:
df["TIME"] = ds["TIME"].values
except:
pass
try:
df["TIME"] = ds["time"].values
except:
pass
df.set_index("TIME")
df.set_index("TIME",inplace=True)
d = df.index
d.strftime('%Y-%m-%d %H:%M')
df.set_index(d.strftime('%Y-%m-%d %H:%M'))
self.LabelFrameNETCDFFieldA.insert(END, "\n"+str(df))
#if coordType==0:
df.to_json(str(self.name)+'.json')
except:
try:
df = ds.to_dataframe()
df_without_duplicates = df.drop_duplicates()
df_without_duplicates.to_json(str(self.name)+'.json')
except Exception as e:
#self.LabelFrameNETCDFFieldA.insert(END, "\n Error, something went wrong....")
self.LabelFrameNETCDFFieldA.insert(END, "\n"+e)
except Exception as e:
#self.LabelFrameNETCDFFieldA.insert(END, "\n Error, something went wrong....")
self.LabelFrameNETCDFFieldA.insert(END, "\n"+e)
def CSVOpenInputFileNetcdf(self,):
try:
ds = xr.open_dataset(self.name)
for attr in ds.attrs.items():
#print(str(attr))
self.LabelFrameNETCDFFieldA.insert(END, "\n "+str(attr))
try:
subset = []
for param in ds.data_vars:
if ("_DM" not in param) and ("_QC" not in param):
subset.append(param)
var_w_units = []
for i in subset:
try:
var_w_units.append(f"{i} [{ds[i].units}]")
except:
var_w_units.append(f"")
#coordType=0
df = pd.DataFrame()
for i in subset:
try:
df[i] = ds[i][:,0].values
except:
try:
df[i] = ds[i][:].values
#coordType=1
except:
pass
df.columns = var_w_units
try:
df["TIME"] = ds["TIME"].values
except:
pass
try:
df["TIME"] = ds["time"].values
except:
pass
df.set_index("TIME")
df.set_index("TIME",inplace=True)
d = df.index
d.strftime('%Y-%m-%d %H:%M')
df.set_index(d.strftime('%Y-%m-%d %H:%M'))
self.LabelFrameNETCDFFieldA.insert(END, "\n"+str(df))
#if coordType==0:
df.to_csv(str(self.name)+'.csv', encoding='utf-8')
except:
try:
df = ds.to_dataframe()
df_without_duplicates = df.drop_duplicates()
df_without_duplicates.to_csv(str(self.name)+'.csv', encoding='utf-8')
except Exception as e:
#self.LabelFrameNETCDFFieldA.insert(END, "\n Error, something went wrong....")
self.LabelFrameNETCDFFieldA.insert(END, "\n"+e)
except Exception as e:
#self.LabelFrameNETCDFFieldA.insert(END, "\n Error, something went wrong....")
self.LabelFrameNETCDFFieldA.insert(END, "\n"+e)
def XLSXOpenInputFileNetcdf(self,):
try:
ds = xr.open_dataset(self.name)
for attr in ds.attrs.items():
#print(str(attr))
self.LabelFrameNETCDFFieldA.insert(END, "\n "+str(attr))
try:
subset = []
for param in ds.data_vars:
if ("_DM" not in param) and ("_QC" not in param):
subset.append(param)
var_w_units = []
for i in subset:
try:
var_w_units.append(f"{i} [{ds[i].units}]")
except:
var_w_units.append(f"")
#coordType=0
df = pd.DataFrame()
for i in subset:
try:
df[i] = ds[i][:,0].values
except:
try:
df[i] = ds[i][:].values
#coordType=1
except:
pass
df.columns = var_w_units
try:
df["TIME"] = ds["TIME"].values
except:
pass
try:
df["TIME"] = ds["time"].values
except:
pass
df.set_index("TIME")
df.set_index("TIME",inplace=True)
d = df.index
d.strftime('%Y-%m-%d %H:%M')
df.set_index(d.strftime('%Y-%m-%d %H:%M'))
self.LabelFrameNETCDFFieldA.insert(END, "\n"+str(df))
#if coordType==0:
df.to_excel(str(self.name)+'.xlsx')
except:
try:
df = ds.to_dataframe()
df_without_duplicates = df.drop_duplicates()
df_without_duplicates.to_excel('test.xlsx')
except Exception as e:
#self.LabelFrameNETCDFFieldA.insert(END, "\n Error, something went wrong....")
self.LabelFrameNETCDFFieldA.insert(END, "\n"+e)
except Exception as e:
#self.LabelFrameNETCDFFieldA.insert(END, "\n Error, something went wrong....")
self.LabelFrameNETCDFFieldA.insert(END, "\n"+e)
def PlotOpenInputFileNetcdf(self,):
try:
ds = xr.open_dataset(self.name)
for attr in ds.attrs.items():
#print(str(attr))
self.LabelFrameNETCDFFieldA.insert(END, "\n "+str(attr))
try:
subset = []
for param in ds.data_vars:
if ("_DM" not in param) and ("_QC" not in param):
subset.append(param)
var_w_units = []
for i in subset:
try:
var_w_units.append(f"{i} [{ds[i].units}]")
except:
var_w_units.append(f"")
#coordType=0
df = pd.DataFrame()
for i in subset:
try:
df[i] = ds[i][:,0].values
except:
try:
df[i] = ds[i][:].values
#coordType=1
except:
pass
df.columns = var_w_units
try:
df["TIME"] = ds["TIME"].values
except:
pass
try:
df["TIME"] = ds["time"].values
except:
pass
df.set_index("TIME")
df.set_index("TIME",inplace=True)
d = df.index
d.strftime('%Y-%m-%d %H:%M')
df.set_index(d.strftime('%Y-%m-%d %H:%M'))
self.LabelFrameNETCDFFieldA.insert(END, "\n"+str(df))
#if coordType==0:
df.plot()
plt.show()
except:
try:
for u in ds.data_vars.keys():
tempVar = ds[u]
tempVar_0=tempVar[0, 0, :, :]
tempVar_0.plot(cmap='plasma')
plt.show()
except Exception as e:
#self.LabelFrameNETCDFFieldA.insert(END, "\n Error, something went wrong....")
self.LabelFrameNETCDFFieldA.insert(END, "\n"+e)
except Exception as e:
#self.LabelFrameNETCDFFieldA.insert(END, "\n Error, something went wrong....")
self.LabelFrameNETCDFFieldA.insert(END, "\n"+e)
#To make it work with PyInstaller (this is due to an update of PyInstaller)
#this functions is used to define the right file path between two differents
#enviroments: your PC and PInstaller wrapping
def resource_path(self,relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
#Define an input file for data TXT/CSV.
def OpenInputFileCSV(self,):
self.nameSF = askdirectory()
self.srcSF = self.nameSF
self.LabelFrameNETCDFFieldB.insert(END, "\n The directory is: "+str(self.nameSF))
outputFileSF = str(time.strftime("%Y%m%d%H%M%S")+'.txt')
t=open(outputFileSF, 'w')
t.close()
f=open(outputFileSF, 'a')
for fileSF in os.listdir(self.srcSF):
if fileSF.endswith(".csv"):
self.LabelFrameNETCDFFieldB.insert(END, "\n Working the file: "+str(fileSF))
file1 = open(self.srcSF+'/'+fileSF, 'r')
#The slicing [1:] to skip the header row
Lines = file1.readlines()
for line in Lines:
f.write(str(line))
#print("\n Reading the file: "+str(fileSF))
#print('scrivo')
f.close()
#Define an input file for data XLSX.
def OpenInputFileSheet(self,):
#actualdirname = os.getcwd()
self.name = askdirectory()
#self.LabelFrameNETCDFFieldA.delete(0,END)
#self.LabelFrameNETCDFFieldA.insert(0,self.name)
self.srcXLS = self.name
self.LabelFrameMergerFieldAXLS.delete('1.0', END)
self.LabelFrameMergerFieldAXLS.insert(END, "\n The directory is: "+str(self.name))
#outputFile = str(time.strftime("%Y%m%d%H%M%S")+'.xlsx')
# list of excel files we want to merge.
# pd.read_excel(file_path) reads the excel
# data into pandas dataframe.
excl_list = []
recovery_sheet_list = []
for file in os.listdir(self.srcXLS):
xls = pd.ExcelFile(self.srcXLS+'/'+file)
for sheetsName in xls.sheet_names:
# List all sheets in the file
#xls.sheet_names
#print(file+" -- "+sheetsName)
# ['house', 'house_extra', ...]
if sheetsName not in recovery_sheet_list:
recovery_sheet_list.append(sheetsName)
print('All sheets are: '+str(recovery_sheet_list))
for mySheet in recovery_sheet_list:
excl_list = []
#try:
for file in os.listdir(self.srcXLS):
try:
excl_list.append(pd.read_excel(self.srcXLS+'/'+file, sheet_name=mySheet))
except:
pass
excl_merged = pd.DataFrame()
for excl_file in excl_list:
# appends the data into the excl_merged
# dataframe.
excl_merged = excl_merged.append(excl_file, ignore_index=True)
# exports the dataframe into excel file with
# specified name.
outputFile = str(mySheet+'_'+time.strftime("%Y%m%d%H%M%S")+'.xlsx')
excl_merged.to_excel(outputFile+'.xlsx', index=False)
#except:
# print('Sorry something went wrong')
#Define an input file for data XLSX.
def OpenInputFileODV(self,):
#actualdirname = os.getcwd()
self.name = askdirectory()
#self.LabelFrameNETCDFFieldA.delete(0,END)
#self.LabelFrameNETCDFFieldA.insert(0,self.name)
#self.srcXLS = self.name
dirname=self.name
mycounterDummy=1
#CSVNameFullMerge = str('FullMerge_'+time.strftime("%Y%m%d%H%M%S")+'.csv')
#CSVNameDataMerge = str('DataMerge_'+time.strftime("%Y%m%d%H%M%S")+'.csv')
#CSVNameDataMergeTS = str('DataMergeTS_'+time.strftime("%Y%m%d%H%M%S")+'.csv')
CSVNameFullMerge = str(time.strftime("%Y%m%d%H%M%S")+'_FullMerge.csv')
CSVNameDataMerge = str(time.strftime("%Y%m%d%H%M%S")+'_DataMerge.csv')
CSVNameDataMergeTS = str(time.strftime("%Y%m%d%H%M%S")+'_DataMergeTS.csv')
for fn in os.listdir(dirname):
#print(u)
print(fn)
#filename = fn
counterHeader = 1
if not os.path.isfile(dirname+'/'+fn):
return "File not found: " + fn
sys.exit(-1)
try:
f = open(dirname+'/'+fn,'r')
CSVOutputFile = open(CSVNameFullMerge,"a")
#CSVOutputFileData = open(CSVNameDataMerge,"a")
while True:
l=f.readline()
#O.header.append(l)
if l.find('//')==-1:
#print ('finished reading semantic header')
#CSVOutputFile.write('\t'+l)
CSVOutputFile.write(l)
CSVOutputFile.close()
if mycounterDummy == 1:
CSVOutputFileData = open(CSVNameDataMerge,"a")
CSVOutputFileData.write(l.replace('\t', ','))
CSVOutputFileData.close()
CSVOutputFileDataTS = open(CSVNameDataMergeTS,"a")
CSVOutputFileDataTS.write(l.replace('\t', ','))
CSVOutputFileDataTS.close()
#mycounterDummy += 1
break
CSVOutputFile.write(l)
counterHeader += 1
print('counterHeader:'+str(counterHeader))
except IOError:
sys.exit(-1)
data = pd.read_csv(dirname+'/'+fn,sep='\t',index_col=False, na_values=numpy.nan, skiprows = counterHeader-1) #, parse_dates=[3], infer_datetime_format=True, date_parser=odvdatetime)
data.columns = [c.replace(' ', '_') for c in data.columns]
data['Cruise'].fillna(method='ffill', inplace = True)
data['Station'].fillna(method='ffill', inplace = True)
data['Type'].fillna(method='ffill', inplace = True)
data['YYYY-MM-DDThh:mm:ss.sss'].fillna(method='ffill', inplace = True)
data['Longitude_[degrees_east]'].fillna(method='ffill', inplace = True)
data['Latitude_[degrees_north]'].fillna(method='ffill', inplace = True)
data['LOCAL_CDI_ID'].fillna(method='ffill', inplace = True)
data['EDMO_code'].fillna(method='ffill', inplace = True)
data['Bot._Depth_[m]'].fillna(method='ffill', inplace = True)
#print(O.data.iloc[:, 0:12])
print(data)
#CSVNameFullMerge = str(time.strftime("%Y%m%d%H%M%S")+'_FullMerge.csv')
#CSVNameDataMerge = str(time.strftime("%Y%m%d%H%M%S")+'_DataMerge.csv')
#CSVNameDataMergeTS = str(time.strftime("%Y%m%d%H%M%S")+'_DataMergeTS.csv')
#data.iloc[:,1:].to_csv(CSVName, mode='a', header=False)
data.to_csv(CSVNameFullMerge, mode='a', header=False,index=False)
data.to_csv(CSVNameDataMerge, mode='a', header=False,index=False)
f.close()
mycounterDummy += 1