-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_months_pr.py
51 lines (43 loc) · 2.16 KB
/
merge_months_pr.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
##################################################################
# Description: ClimEx data is organized by months. In order to apply statistical
# computation, data needs to be merged.
# Code name: merge_months_pr.py
# Date of creation: 2019/01/14
# Date of last modification: 2019/01/14
# Contacts: chavaillaz.yann@ouranos.ca
##################################################################
# Needed packages
import os
import xarray as xr
import numpy as np
import sys
import glob
import functions_detrending as fct_d
import time as tt
start_time = tt.time()
# Initialization
input_path = '/exec/yanncha/sea_ice/pr/daily/'
output_path = '/exec/yanncha/sea_ice/pr/'
simulations = ["kda","kdb","kdc","kdd","kde","kdf","kdg","kdh","kdi","kdj","kdk","kdl","kdm","kdn","kdo","kdp","kdq","kdr","kds","kdt","kdu","kdv","kdw","kdx","kdy","kdz","kea","keb","kec","ked","kee","kef","keg","keh","kei","kej","kek","kel","kem","ken","keo","kep","keq","ker","kes","ket","keu","kev","kew","kex"]
months = (4,5,6)
season = "AMJ"
### LOOP ON CLIMEX SIMULATIONS
for sim in simulations:
# selection of the filenames corresponding to our criteria
filepaths = glob.glob(input_path+'pr_daily_'+sim+'*')
# initialization of the data for the selected season
dataset = xr.open_mfdataset(filepaths)
if np.size(months)==3:
dsSub = dataset.where((dataset['time.month']==months[0]) | (dataset['time.month']==months[1]) | (dataset['time.month']==months[2]),drop=True)
elif np.size(months)==4:
dsSub = dataset.where((dataset['time.month']==months[0]) | (dataset['time.month']==months[1]) | (dataset['time.month']==months[2]) | (dataset['time.month']==months[3]),drop=True)
# writing data in a new netcdf file
pr = dsSub['pr'][:,:,:]
pr.to_netcdf((output_path+'/pr_rearranged_'+season+'_'+sim+'.nc'))
dataset.close()
dsSub.close()
file_txt = open('/home/yanncha/GitHub/sea-ice/outputs_from_code/merge_months_pr.txt','a')
file_txt.write(('### Simulation '+sim+' done! (%d seconds)' % (tt.time() - start_time)))
file_txt.close()
print('### Simulation '+sim+' done! (%d seconds)' % (tt.time() - start_time))
start_time = tt.time()