Skip to content

Commit

Permalink
PR: Convert float nan to text before saving data in an Excel file. (#211
Browse files Browse the repository at this point in the history
)

* Create a function to convert float nan to text

* Use the new function to export GLUE

* Use the new fucntion when exporting weather data
  • Loading branch information
jnsebgosselin authored May 11, 2018
1 parent a5b7c1f commit eac2060
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
12 changes: 3 additions & 9 deletions gwhat/gwrecharge/glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# ---- Local imports

from gwhat.common.utils import save_content_to_file
from gwhat.utils.math import nan_as_text_tolist
from gwhat import __namever__


Expand Down Expand Up @@ -128,15 +129,8 @@ def _format_glue_waterlvl(self):
data[:, 2:5] = self['water levels']['predicted'] / 1000
data = np.round(data, 2)

# Merge the data header with the data. Also, convert float nan to text,
# so that it is possible to save to an Excel file.

if np.isnan(data).any():
m, n = np.shape(data)
for i in range(m):
dataf.append(['nan' if np.isnan(x) else x for x in data[i, :]])
else:
dataf.extend(data.tolist())
# Merge the data header with the data.
dataf.extend(nan_as_text_tolist(data))

return dataf

Expand Down
3 changes: 2 additions & 1 deletion gwhat/meteo/weather_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from gwhat.meteo.evapotranspiration import calcul_Thornthwaite
from gwhat.common.utils import save_content_to_csv, save_content_to_file
from gwhat.utils.math import nan_as_text_tolist
from gwhat import __namever__


Expand Down Expand Up @@ -104,7 +105,7 @@ def export_dataset_to_file(self, filename, time_frame):
data = np.zeros((N, M))
for j, vrb in enumerate(vrbs):
data[:, j] = self[time_frame][vrb]
fcontent.extend(data.tolist())
fcontent.extend(nan_as_text_tolist(data))

save_content_to_file(filename, fcontent)

Expand Down
15 changes: 15 additions & 0 deletions gwhat/utils/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@
import datetime


def nan_as_text_tolist(arr):
"""
Convert the float nan are to text while converting a numpy 2d array to a
list, so that it is possible to save to an Excel file.
"""
if np.isnan(arr).any():
m, n = np.shape(arr)
list_ = []
for i in range(m):
list_.append(['nan' if np.isnan(x) else x for x in arr[i, :]])
else:
list_ = arr.tolist()
return list_


def clip_time_series(tclip, tp, xp):
"""
Clip tp and xp on tclip. tclip and tp must be arrays of numerical
Expand Down

0 comments on commit eac2060

Please sign in to comment.