-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_gendata.py
74 lines (63 loc) · 1.91 KB
/
mod_gendata.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
# _*_ coding : utf-8 _*_
import numpy as np
import glob
import copy
import os
class gen_grads_data:
def _init_(self):
pass
def const(self):
lat = 181
lon = 360
return lat, lon
def cal_fsize(self):
lat, lon = self.const()
return 4*lat*lon
def get_filelist(self, inputdir):
return glob.glob(inputdir+'/*')
def get_keyfilelist(self, inputdir, keyward):
return glob.glob(inputdir+'/'+keyward+'*')
def read_binfile(self, inputfile):
with open(inputfile, 'rb') as ifile:
data = np.fromfile(ifile, '>f', sep='')
return data
def n2s(self, data):
# FNL data direction is S2N
# data sorting : north pole to south pole
lat, lon = self.const()
array2d = data.reshape(lat, lon)
n2s_array2d = copy.deepcopy(array2d)
for i in range(lon):
n2s_array2d[:,i] = array2d[::-1,i]
return n2s_array2d.flatten()
def load_data(self, inputdir):
filelist = self.get_filelist(inputdir)
# open file
datalist = []
for ifile in filelist:
idata = self.read_binfile(ifile)
rdata = self.n2s(idata)
datalist += [rdata]
# max value for 0-1 normalization
imax = np.asarray(datalist).max()
print( ' Max value ', imax)
normed_data = np.asarray(datalist)/imax
#
return normed_data
def load_key_data(self, inputdir, keyward):
filelist = self.get_keyfilelist(inputdir, keyward)
# open file
datalist = []
fsize = self.cal_fsize()
for ifile in filelist:
isize = os.path.getsize(ifile)
if isize == fsize:
idata = self.read_binfile(ifile)
rdata = self.n2s(idata)
datalist += [rdata]
# max value for 0-1 normalization
imax = np.asarray(datalist).max()
print( ' Max value ', imax)
normed_data = np.asarray(datalist)/imax
#
return normed_data