-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriteoutfile.py
More file actions
92 lines (63 loc) · 2.08 KB
/
Copy pathwriteoutfile.py
File metadata and controls
92 lines (63 loc) · 2.08 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
"""
Python modules for plotting time series
"""
import os
import numpy as np
import pandas as pd
import xarray as xr
__author__ = 'Eva Sinha'
__email__ = 'eva.sinha@pnnl.gov'
#----------------------------------------------------------
def write_netcdf_output(ds, dirname, filename):
"""Save output xarray as a netcdf file
:param: ds: input data array
:param: dirname: directory for writing netcdf file
:param: filename: name of output netcdf file
"""
# Change directory
if not os.path.exists(dirname):
os.mkdir(dirname)
os.chdir(dirname)
if os.path.exists(filename):
os.remove(filename)
# Write xarray to a new NetCDF file
ds.to_netcdf(path=filename, mode='w')
os.chdir('../')
#----------------------------------------------------------
def write_csv_output(ds, dirname, filename):
"""Save output xarray as a csv file
:param: ds: input data array
:param: dirname: directory for writing netcdf file
:param: filename: name of output csv file
"""
# Change directory
if not os.path.exists(dirname):
os.mkdir(dirname)
os.chdir(dirname)
if os.path.exists(filename):
os.remove(filename)
# Convert to pandas Dataframe for writing csv file
df = ds.to_dataframe()
# Drop longitude and latitude columns
df = df.drop(['longitude', 'latitude'], axis=1)
# Save to csv file
df.to_csv(filename)
print('Finished writing csv')
os.chdir('../')
#----------------------------------------------------------
def write_df_to_csv(df, dirname, filename):
"""Save output pandas dataframe as a csv file
:param: df: input dataframe
:param: dirname: directory for writing netcdf file
:param: filename: name of output csv file
"""
# Change directory
if not os.path.exists(dirname):
os.mkdir(dirname)
os.chdir(dirname)
if os.path.exists(filename):
os.remove(filename)
# Save to csv file
df.to_csv(filename, index=True)
print('Finished writing csv')
os.chdir('../')