-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatherDef.py
executable file
·159 lines (128 loc) · 5.27 KB
/
gatherDef.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
##################################################################
from sys import argv, exit
from os import path, mkdir
from glob import glob
import numpy as np
from re import split
#
import mojito as mjt
from mojito import config as cfg
idebug=1
iplot=1
cprefixIn='DEFORMATIONS_' ; # Prefix of deformation files...
if __name__ == '__main__':
kk = cfg.initialize()
cslct = ''
if not len(argv) in [5,6]:
print('Usage: '+argv[0]+' <directory_input_npz_files> <dtbin_h> <creskm> <string_id_origin> (<S>)')
exit(0)
cd_in = argv[1]
cdtbin = argv[2]
creskm = argv[3]
cidorg = argv[4]
lgroup = False
if len(argv)==6:
lgroup = (argv[5]=='S')
# if lgroup: will look for re-gathered files `SLCT`+res !
# Polpulating deformation files available:
print(' *** Searching for deformation files with follwoing pattern:')
if lgroup:
cfptrn = cd_in+'/'+cprefixIn+''+cidorg+'_S*_dt'+cdtbin+'*_SLCT'+creskm+'km.npz'
else:
cfptrn = cd_in+'/'+cprefixIn+'*'+cidorg+'*'+cdtbin+'*_'+creskm+'km.npz'
print(' => '+cfptrn)
listnpz = np.sort( glob(cfptrn) )
nbFiles = len(listnpz)
if nbFiles<1:
mjt.printEE('no `npz` files found!')
print('\n *** We found '+str(nbFiles)+' deformation files into '+cd_in+' !')
print(' => files to gather into a single one:',listnpz,'\n')
kBatchName = np.zeros(nbFiles, dtype='U4')
kiDate = np.zeros(nbFiles, dtype=int ) ; # date in epoch time at which deformations were calculated
kNbPoints = np.zeros(nbFiles, dtype=int ) ; # number of points in file
#list_date = []
kf = 0
for ff in listnpz:
print('\n # File: '+ff)
with np.load(ff) as data:
rdate = int( data['time'] )
nPnts = data['Npoints']
if kf==0:
corigin = str(data['origin'])
print(' *** Getting resolution info in file '+ff+' !')
reskm = int(data['reskm_nmnl'])
print(' => '+str(reskm)+' km !')
if str(reskm) != creskm:
mjt.printEE('spatial scale (km) passed as argument does not match that found in deformation files!')
fb = path.basename(ff)
vf = split('_',fb)
#cdateh = split('-',vf[-2])[0]
#list_date.append(split('h',cdateh)[0])
if lgroup:
kBatchName[kf] = vf[-3]
else:
kBatchName[kf] = vf[-4]
kiDate[kf] = rdate
kNbPoints[kf] = nPnts
print(' * Batch: '+kBatchName[kf] )
print(' * Date = ',mjt.epoch2clock(kiDate[kf]))
print(' * Nb. of points = ',kNbPoints[kf] )
kf = kf+1
print('\n')
#exit(0)
nP = np.sum(kNbPoints)
print(' ==> Total number of points:', nP)
#print(' ==> list of dates:', list_date[:])
#cdt1, cdt2 = list_date[0],list_date[-1]
#cperiod = cdt1+'-'+cdt2
dtbin=0
cinfobin = ''
if cdtbin!='nemoTsi3_NoBin':
dtbin=int(cdtbin)
cinfobin = '_dt'+cdtbin
# Now that we know the total number of points we can allocate and fill arrays for divergence and shear
Zshr = np.zeros(nP)
ZDiv = np.zeros(nP)
Ztot = np.zeros(nP)
ZAqd = np.zeros(nP)
Zdat = np.zeros(nP,dtype=int)
jP = 0 ; # Counter from 0 to nP-1
kf = 0 ; # Counter for files, 0 to nbFiles-1
for ff in listnpz:
jPe = jP + kNbPoints[kf]
with np.load(ff) as data:
zdiv = data['divergence']
zshr = data['shear']
ztot = data['total']
za = data['quadArea']
#kf
ZDiv[jP:jPe] = cfg.rc_day2sec*zdiv ; # day^-1
Zshr[jP:jPe] = cfg.rc_day2sec*zshr ; # day^-1
Ztot[jP:jPe] = cfg.rc_day2sec*ztot ; # day^-1
ZAqd[jP:jPe] = za ; # km^2
Zdat[jP:jPe] = kiDate[kf]
#
jP = jPe
kf = kf+1
del zdiv, zshr, ztot, za
#cfroot = corigin+cinfobin+'_'+str(reskm)+'km_'+cperiod
cfroot = corigin+cinfobin+'_'+str(reskm)+'km'
# Save it for the scaling and PDFs to come ...
# , dates_batch=kiDate, period=cperiod
print(' *** Saving: '+cd_in+'/def_SHR_'+cfroot+'.npz')
np.savez_compressed( cd_in+'/def_SHR_'+cfroot+'.npz', name='shear', origin=corigin,
Nbatch=nbFiles, dates_batch=kiDate,
reskm_nmnl=reskm, dtbin=dtbin,
Np=nP, xshr=Zshr, quadArea=ZAqd, dates_point=Zdat )
print(' *** Saving: '+cd_in+'/def_DIV_'+cfroot+'.npz')
np.savez_compressed( cd_in+'/def_DIV_'+cfroot+'.npz', name='divergence', origin=corigin,
Nbatch=nbFiles, dates_batch=kiDate,
reskm_nmnl=reskm, dtbin=dtbin,
Np=nP, xdiv=ZDiv, quadArea=ZAqd, dates_point=Zdat )
print(' *** Saving: '+cd_in+'/def_TOT_'+cfroot+'.npz')
np.savez_compressed( cd_in+'/def_TOT_'+cfroot+'.npz', name='shear', origin=corigin,
Nbatch=nbFiles, dates_batch=kiDate,
reskm_nmnl=reskm, dtbin=dtbin,
Np=nP, xtot=Ztot, quadArea=ZAqd, dates_point=Zdat )