def generates_summary(self): Ypre = self.Yp Ymes = self.Ym for i in range(len(Ypre)): RMSE = (np.mean((Ypre[i] - Ymes[i]) ** 2)) ** 0.5 MAE = np.mean(np.abs(Ypre[i] - Ymes[i])) ME = np.mean(Ypre[i] - Ymes[i]) r = np.corrcoef(Ypre[i], Ymes[i])[1, 0] Emax = np.min(Ypre[i] - Ymes[i]) Emin = np.max(Ypre[i] - Ymes[i]) dirname = 'summary/' if not os.path.exists(dirname): os.mkdir(dirname) filename = dirname + self.varNames[i] + '.csv' # ---- Generate File ---- if not os.path.exists(filename): header = [['Station', 'RMSE', 'MAE', 'ME', 'r', 'Emax', 'Emin']] save_content_to_csv(filename, header) # ---- Write Stats to File ---- rowcontent = [[self.staName, '%0.1f' % RMSE, '%0.1f' % MAE, '%0.2f' % ME, '%0.3f' % r, '%0.1f' % Emax, '%0.1f' % Emin]] save_content_to_csv(filename, rowcontent, mode='a')
def datatmpdir(tmpdir): """Create a set of water level datafile in various format.""" save_content_to_csv(osp.join(str(tmpdir), FILENAME + '.csv'), DATA) save_content_to_excel(osp.join(str(tmpdir), FILENAME + '.xls'), DATA) save_content_to_excel(osp.join(str(tmpdir), FILENAME + '.xlsx'), DATA) return str(tmpdir)
def save_colors_db(self): """Save the color settings to Colors.db.""" fname = 'Colors.db' fcontent = [] for key in self.RGB.keys(): fcontent.append([key]) fcontent[-1].extend(self.RGB[key]) save_content_to_csv(fname, fcontent)
def save_pref_file(self): print('\nSaving WHAT preferences to file...') fcontent = [['Project File:', os.path.relpath(self.projectfile)], ['Language:', self.language], ['Font-Size-General:', self.fontsize_general], ['Font-Size-Console:', self.fontsize_console], ['Font-Size-Menubar:', self.fontsize_menubar]] save_content_to_csv('WHAT.pref', fcontent) print('WHAT preferences saved.')
def datatmpdir(tmpdir): """Create a set of water level datafile in various format.""" save_content_to_csv( osp.join(str(tmpdir), FILENAME + '.csv'), DATA) save_content_to_excel( osp.join(str(tmpdir), FILENAME + '.xls'), DATA) save_content_to_excel( osp.join(str(tmpdir), FILENAME + '.xlsx'), DATA) return str(tmpdir)
def init_waterlvl_measures(dirname): """ Create an empty waterlvl_manual_measurements.csv file with headers if it does not already exist. """ for ext in FILE_EXTS: fname = os.path.join(dirname, "waterlvl_manual_measurements" + ext) if os.path.exists(fname): return else: fname = os.path.join(dirname, 'waterlvl_manual_measurements.csv') fcontent = [['Well_ID', 'Time (days)', 'Obs. (mbgs)']] if not os.path.exists(dirname): os.makedirs(dirname) save_content_to_csv(fname, fcontent)
def add_PET_to_weather_datafile(filename): """Add PET to weather data file.""" print('Adding PET to %s...' % os.path.basename(filename), end=' ') # Load and store original data. reader = open_weather_datafile(filename) if reader is None: # pragma: no cover print('failed') return else: for i, row in enumerate(reader): if len(row) == 0: continue if row[0] == 'Latitude': lat = float(row[1]) elif row[0] == 'Year': istart = i + 1 vrbs = row data = np.array(reader[istart:]).astype('float') break Year = data[:, vrbs.index('Year')].astype(int) Month = data[:, vrbs.index('Month')].astype(int) Day = data[:, vrbs.index('Day')].astype(int) Dates = [Year, Month, Day] Tavg = data[:, vrbs.index('Mean Temp (deg C)')] x = calc_monthly_mean(Year, Month, Tavg) Ta = calcul_monthly_normals(x[0], x[1], x[2]) PET = calcul_Thornthwaite(Dates, Tavg, lat, Ta) # Extend dataset with PET and save the dataset to csv. if 'ETP (mm)' in vrbs: indx = vrbs.index('ETP (mm)') for i in range(len(PET)): reader[i + istart][indx] = PET[i] else: reader[istart - 1].append('ETP (mm)') for i in range(len(PET)): reader[i + istart].append(PET[i]) # Save data. save_content_to_csv(filename, reader) print('done')
def add_PET_to_weather_datafile(filename): """Add PET to weather data file.""" print('Adding PET to %s...' % os.path.basename(filename), end=' ') # Load and store original data. reader = open_weather_datafile(filename) if reader is None: # pragma: no cover print('failed') return else: for i, row in enumerate(reader): if len(row) == 0: continue if row[0] == 'Latitude': lat = float(row[1]) elif row[0] == 'Year': istart = i+1 vrbs = row data = np.array(reader[istart:]).astype('float') break Year = data[:, vrbs.index('Year')].astype(int) Month = data[:, vrbs.index('Month')].astype(int) Day = data[:, vrbs.index('Day')].astype(int) Dates = [Year, Month, Day] Tavg = data[:, vrbs.index('Mean Temp (deg C)')] x = calc_monthly_mean(Year, Month, Tavg) Ta = calcul_monthly_normals(x[0], x[1], x[2]) PET = calcul_Thornthwaite(Dates, Tavg, lat, Ta) # Extend dataset with PET and save the dataset to csv. if 'ETP (mm)' in vrbs: indx = vrbs.index('ETP (mm)') for i in range(len(PET)): reader[i+istart][indx] = PET[i] else: reader[istart-1].append('ETP (mm)') for i in range(len(PET)): reader[i+istart].append(PET[i]) # Save data. save_content_to_csv(filename, reader) print('done')
def test_load_waterlvl_measures_withcsv(): filename = os.path.join(os.getcwd(), "waterlvl_manual_measurements.csv") save_content_to_csv(filename, WLMEAS) # Test init_waterlvl_measures to be sure the file is not overriden. init_waterlvl_measures(os.getcwd()) # Assert that it loads the right data. filename = os.path.join(os.getcwd(), "waterlvl_manual_measurements") time, wl = load_waterlvl_measures(filename, 'Dummy') assert len(time) == 0 and isinstance(time, np.ndarray) assert len(wl) == 0 and isinstance(wl, np.ndarray) time, wl = load_waterlvl_measures(filename, 'Test') assert np.all(time == np.array([40623.54167, 40842.54167, 41065.54167])) assert np.all(wl == np.array([1.43, 1.6, 1.57])) time, wl = load_waterlvl_measures(filename, "é@#^'") assert np.all(time == np.array([41240.8125])) assert np.all(wl == np.array([3.75]))
def compute_wet_days_LatexTable(dirname): fname = 'wet_days_0.5mm.csv' fcontent = [['station', 'Meas. wet days', 'Pred. wet days', 'Err.(days)', 'Err.(%)']] for root, directories, filenames in os.walk(dirname): for filename in filenames: if os.path.splitext(filename)[1] == '.err': print('---- %s ----' % os.path.basename(root)) pperr = PostProcessErr(os.path.join(root, filename)) preWetDays = np.where(pperr.Yp[3] > 0.5)[0] mesWetDays = np.where(pperr.Ym[3] > 0.5)[0] Npre = len(preWetDays) Nmes = len(mesWetDays) f = (Npre - Nmes) / float(Nmes) * 100 print('Averaged nbr. of meas. wet days per year = %0.1f days' % (Nmes/30.)) print('Averaged nbr. of pred. wet days per year = %0.1f days' % (Npre/30.)) print('Estimation Error = %0.1f days' % ((Npre-Nmes)/30.)) print('Estimation Error = %0.1f %%' % (f)) MI = np.mean(pperr.Ym[3][mesWetDays]) SD = np.std(pperr.Ym[3][mesWetDays]) print('Precipitation intensity = %0.1f mm/day' % MI) print('Precipitation sdt = %0.1f mm/day' % SD) fcontent.append([pperr.staName, '%d' % (Nmes/30.), '%d' % (Npre/30.), '%d' % ((Npre-Nmes)/30.), '%0.1f' % f]) save_content_to_csv(fname, fcontent, mode='a')
delete_file) from gwhat.projet.reader_waterlvl import (load_waterlvl_measures, init_waterlvl_measures, read_water_level_datafile) # Test reading water level datafiles # ---------------------------------- DATA = [['Well name = ', "êi!@':i*"], ['well id : ', '1234ABC'], ['Province', 'Qc'], ['latitude ', 45.36], ['Longitude=', -72.4234665345], ['Elevation:', 123], [], [], ['Date', 'WL(mbgs)', 'BP(m)', 'ET'], [41241.69792, 3.667377006, 10.33327435, 383.9680352], [41241.70833, 3.665777025, 10.33127437, 387.7404819], [41241.71875, 3.665277031, 10.33097437, 396.9950643]] save_content_to_csv("water_level_datafile.csv", DATA) save_content_to_excel("water_level_datafile.xls", DATA) save_content_to_excel("water_level_datafile.xlsx", DATA) def test_reading_waterlvl(): df1 = read_water_level_datafile("water_level_datafile.csv") df2 = read_water_level_datafile("water_level_datafile.xls") df3 = read_water_level_datafile("water_level_datafile.xlsx") assert list(df1.keys()) == list(df2.keys()) assert list(df2.keys()) == list(df3.keys()) expected_results = { 'Well': "êi!@':i*", 'Well ID': '1234ABC',