forked from brynemorgan/Dart_EnvGIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeek5-1_DataExport.py
158 lines (115 loc) · 4.42 KB
/
Week5-1_DataExport.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#------------------------------------------------------------------------------
__author__ = 'James T. Dietrich'
__contact__ = '[email protected]'
__copyright__ = '(c) James Dietrich 2016'
__license__ = 'MIT'
__date__ = 'Wed Nov 16 11:33:39 2016'
__version__ = '1.0'
__status__ = "initial release"
__url__ = "https://github.com/geojames/..."
"""
Name: Week5-1_DataExport.py
Compatibility: Python 3.5
Description: This program does stuff
URL: https://github.com/geojames/...
Requires: libraries
Dev ToDo:
AUTHOR: James T. Dietrich
ORGANIZATION: Dartmouth College
Contact: [email protected]
Copyright: (c) James Dietrich 2016
"""
#------------------------------------------------------------------------------
#%%
# you can also use the "cheaters" file path Tkinter code from the Data Import
# file to geenrate a file path to save a file...
import os
import pandas as pd
import tkinter as tk
from tkinter import filedialog as fd
# start tkinter for file choosing
app = tk.Tk()
# use the file dialog call to get a file name
# Options:
# title - text at the top of the window
# file types - filter for specific file types
# initialdir - the starting directory
# defaultextension - will automatically add this extention if not present
outfile = fd.asksaveasfilename(title='Save Corrected Poitns As...',
filetypes=[('Comma-Delimited Files (*.csv)',
'*.csv')],initialdir=os.getcwd(),
defaultextension='.csv')
print(outfile)
# clean up the tkinter app
app.destroy()
# EXPORT CODE...using outfile at the file path...
#%% File Output with the CSV library
import os
import csv
import random as rand
# create an objectID list
OID = list(range(0,11))
# blank lists
lat = []
lon = []
elev = []
# Create random latitude, longitude, and elevation values
for num in range(0,11):
lat.append(rand.uniform( 35, 45 ))
lon.append(rand.uniform( -90, -120 ))
elev.append(rand.uniform( 0, 500 ))
# change the working directory (change this to where you want yoiur output)
#Windows
os.chdir("C:/Users/James//Documents/GitHub/Dart_EnvGIS/Data/")
# MAC
os.chdir("/Users/ryanmckeon/GitHub/Dart_EnvGIS/Data/")
# ** A very useful feature for any export **
# check to see if an Exports folder exists, if not...create one
if os.path.exists("Exports") == False:
os.mkdir("Exports")
# open a new file in the Exports folder as writable 'w'
# create a csv writer, write header row
# for each row in the data lists write the values line by line
with open("Exports\\CSV_export.csv", 'w') as file_out:
writer = csv.writer(file_out)
writer.writerow( ("ID", "LAT", "LONG", "elev") )
for row in range(len(OID)):
writer.writerow( (OID[row],lat[row],lon[row],elev[row]) )
# close the file
file_out.close()
#%% Exporting with Numpy
#
# The savetxt function outputs a text file, you can specify a delimiter and
# a format code (like formated print statements)
import os
import numpy as np
# we already did this above...
#os.chdir("C:/Users/James//Documents/GitHub/Dart_EnvGIS/Data/")
if os.path.exists("Exports") == False:
os.mkdir("Exports")
x = np.arange(1.0,11.0)
y = np.random.rand(10,10)
np.savetxt("Exports/Numpy_out_01.csv", y, delimiter=",", fmt="%0.4f")
combine = np.column_stack((x.T,y))
np.savetxt("Exports/Numpy_out_02.csv", combine, delimiter=',', \
fmt=("%0.4f"), header = "ROW,LOTS OF RANDOS")
#%% Exporting with Pandas
#
# Again...easiest of all, Pandas Data Frames have a .to_csv method to export to
# a csv file
import os
import pandas as pd
#os.chdir("C:/Users/James//Documents/GitHub/Dart_EnvGIS/Data/")
if os.path.exists("Exports") == False:
os.mkdir("Exports")
# Read in sample weather data
weather = pd.read_csv("KVTNORWI2_2016-12-1_2017-01-26.csv")
# create a seubset of the weather data frame
weather_subset = weather[['Time','TemperatureC','WindDirection']]
# Output the subset to a new CSV in the Exports folder
weather_subset.to_csv("Exports/pandas_export.csv")
# There are other outputs as well - Excel, JSON, XML, HTML...
# Here is an excel output:
weather_subset.to_excel("Exports/pandas_export.xlsx")