-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuncs_solver_new.py
60 lines (45 loc) · 1.63 KB
/
funcs_solver_new.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 16 17:44:54 2024
@author: Mino Sorribas
"""
import numpy as np
import pandas as pd
from datetime import datetime,timedelta
import itertools
def dump_mgb_binary_to_stats_npy(filebin, fileout, nt, nc, itini):
""" Read binary file (MGB format) and dump content to .npy """
# read from file
#'<f4' indicates little-endian (<) float(f) 4 byte (4)
dados = np.fromfile(filebin,'<f4').reshape(nt,nc)
# filter hotstart
dados = dados[itini:,:]
# statistics
stats_qm = np.mean(dados,axis=0)
stats_q95 = np.quantile(dados,0.05,axis=0)
dados_stats = np.vstack((stats_qm, stats_q95))
# dump fo hard disk
np.save(fileout,dados_stats)
return None
def stats_mmap_to_dataframe(stats_mmap, list_c, list_st):
""" Read data from memmap as dataframe
Args:
stats_mmap (np.memmap) :: memory map of stats binary .npy
list_c (list) :: list of integer of selected catchments
such as generated by
.make_dict_bho_ixc()
list_st (integer) :: rows of statistic
Returns:
df (pd.DataFrame) :: time-series of selected values
"""
# adjust loc in array
ixt_ = [i for i in list_st]
ixc_ = [int(i-1) for i in list_c] # mini column [1,nc] -> python [0,nc-1]
# get selection
idx = np.ix_(ixt_, ixc_)
a = stats_mmap[idx]
# make dataframe
#times = [dstart + timedelta(days=i) for i in list_t]
st_headers = ['qm', 'q95']
df = pd.DataFrame(a, columns=list_c, index=st_headers)
return df