-
Notifications
You must be signed in to change notification settings - Fork 2
/
NetCDF_LongTerm_DailyStats2.py
82 lines (71 loc) · 2.7 KB
/
NetCDF_LongTerm_DailyStats2.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
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 17 16:51:54 2013
@author:Sagy Cohen, Uni of Alabama
Calculates long-term mean raster layer (ESRI ASCII) for the WBMsed model output
layers (NetCDF)
"""
from scipy.io import netcdf
import numpy as np
sim_first = 1980
sim_last = 2010
nutrient = 'QxT_WaterTemp'#'QxT_WaterTemp'#'Discharge'#'precipitation'#'Discharge'
NetCDF_fn1 = r'/media/scohen2/Data/Data3/Aug1_2018_WaterTemp/Daily/GlobalWaterTemps/Temp/Global_qxt_watertemp_Pristine__dTS'
#r'/Volumes/LaCie/Data/Cyc_adjusted_files2/Global_'+nutrient+'_Bedload+Dist_30min_dTS'
Output_fn0 = NetCDF_fn1
#set the output ASCII file header
header=['ncols 3600\n']
header.append('nrows 1500\n')
#header=['ncols 720\n']# % 30min
#header.append('nrows 360')# % 30min
header.append('xllcorner -180\n')
header.append('yllcorner -60\n')
header.append('cellsize 0.1\n')
#header.append('cellsize 0.5\n')# %30min
header.append('NODATA_value -9999\n')
NetCDF_fn = NetCDF_fn1+str(sim_first+1)+'.nc' # set input netcdf name
f = netcdf.netcdf_file(NetCDF_fn, 'r')
#if nutrient == 'Discharge':
# valueAcc = f.variables['discharge'][:].astype(np.float32)
#else:
# valueAcc = f.variables[nutrient][:].astype(np.float16)/1000
#print valueAcc.shape
value = f.variables[nutrient][[0][:][:]].astype(np.float32)#/1000
averageRas = np.concatenate(value)
f.close()
np.putmask(averageRas, averageRas > 0, 0)
stdRas = np.copy(averageRas)
rangeRas = np.copy(averageRas)
for r in range(44, 1500):
print r
for c in range(3600):
if averageRas[r][c] > -9999:
accum = np.array([])
for sim_yr in range(sim_first, sim_last + 1):
#print sim_yr
NetCDF_fn = NetCDF_fn1 + str(sim_yr) + '.nc' # set input netcdf name
f = netcdf.netcdf_file(NetCDF_fn, 'r')
tmp = f.variables[nutrient][:, r, c].astype(np.float32)
accum = np.append(accum, tmp)
averageRas[r][c] = accum.mean()
stdRas[r][c] = accum.std()
rangeRas[r][c] = accum.max() - accum.min()
#open and write the header to the output file
Output_fn = Output_fn0[:-3]+'Average'+str(sim_first)+'-'+str(sim_last)+'.asc'
of1 = open(Output_fn, 'w')
of1.writelines(header)
averageRas = averageRas[::-1] #flip
np.savetxt(of1, averageRas, fmt='%1.4f')
of1.close()
Output_fn = Output_fn0[:-3]+'Std'+str(sim_first)+'-'+str(sim_last)+'.asc'
of2 = open(Output_fn, 'w')
of2.writelines(header)
stdRas = stdRas[::-1] #flip
np.savetxt(of2, stdRas, fmt='%1.4f')
of2.close()
Output_fn = Output_fn0[:-3]+'Range'+str(sim_first)+'-'+str(sim_last)+'.asc'
of3 = open(Output_fn, 'w')
of3.writelines(header)
rangeRas = rangeRas[::-1] #flip
np.savetxt(of3, rangeRas, fmt='%1.4f')
of3.close()