-
Notifications
You must be signed in to change notification settings - Fork 18
/
1.COMPILE.WIND.PROFILE (merra2).R
156 lines (104 loc) · 3.47 KB
/
1.COMPILE.WIND.PROFILE (merra2).R
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
##
#####
## ##################################################################################################
#####
##
## this requires us to have a template NetCDF file with the right variables available
## can't do that within R easily, so need to download NCO from http://nco.sourceforge.net/
## save an single MERRA SLV file as MERRA_IN.nc and run the following commands:
##
#### MERRA 2
# ncks -x -v CLDPRS,CLDTMP,DISPH,H1000,H250,H500,H850,OMEGA500,PBLTOP,PS,Q250,Q500,Q850,QV10M,QV2M,SLP,T10M,T250,T2M,T2MDEW,T2MWET,T500,T850,TO3,TOX,TQI,TQL,TQV,TROPPB,TROPPT,TROPPV,TROPQ,TROPT,TS,U10M,U250,U500,U50M,U850,V10M,V250,V500,V50M,V850,ZLCL MERRA_IN.nc4 MERRA_OUT.nc4
# ncrename -h -O -v U2M,A MERRA_OUT.nc4
# ncrename -h -O -v V2M,z MERRA_OUT.nc4
# ncatted -a long_name,A,o,c,"Scale factor for log-law extrapolation: W = A log(h / z)" MERRA_OUT.nc4
# ncatted -a long_name,z,o,c,"Ref height for log-law extrapolation: W = A log(h / z)" MERRA_OUT.nc4
#
##
##
#####
## ##################################################################################################
#####
##
## this runs through all merra files in the specified folders
## reads the wind speed data and calculates the extrapolation parameters
## then saves the A and z values to NetCDF files
##
## after running, you should move the resulting files into their own folder
##
##
#####
## ## SETUP
#####
# the folders we wish to read data from
merraFolder = 'X:/MERRA2_WIND/'
# the folders we wish to write to
outputFolder = 'X:/MERRA2_PROFILES/'
# our blank NetCDF template file to be filled
templateFile = 'M:/WORK/Wind Modelling/VWF CODE/TOOLS/MERRA2.log.law.template.A.z.nc4'
# path to the VWF model
VWFMODEL = 'Q:/VWF/lib/VWF.R'
#####
## ## READ IN DATA
#####
# load the VWF model
source(VWFMODEL)
# find and prepare all our merra files
merra = prepare_merra_files(merraFolder)
# prepare a NetCDF file handler so our format is known
f = merra$files[1]
nc = NetCdfClass(f, 'MERRA2', TRUE)
# subset if necessary
if (exists('region'))
nc$subset_coords(region)
# close this input file
nc$close_file()
#####
## ## PREPARE OUR CLUSTER
#####
# build a parallel cluster
# note that it doesn't make sense going much beyond 8 cores
library(doParallel)
cl = makeCluster(8)
registerDoParallel(cl)
# provide the extrapoalte function to each core
clusterExport(cl, varlist=c('extrapolate_log_law'))
#####
## ## RUN
#####
# run through each input merra file
for (f in merra$files)
{
# get this file's time attributes
nc$open_file(f)
myTime = ncatt_get(nc$ncdf, "time", "units")$value
nc$close_file()
# do the extrapolation, getting the A,z parameters
profile = extrapolate_ncdf(f)
# safety checks
err = which(profile$z > 100)
profile$z[err] = 100
err = which(profile$z < 10^-10)
profile$z[err] = 10^-10
err = which(profile$A < 0)
profile$A[err] = 0
# build a filename for this output data
o = gsub('prod.assim.tavg1_2d_slv', 'wind_profile', f)
o = gsub('tavg1_2d_slv_Nx', 'wind_profile', f)
o = gsub(merraFolder, outputFolder, o)
# create the NetCDF file for this month
file.copy(templateFile, o)
# open this file for writing
ncout = nc_open(o, write=TRUE)
# set its time attributes
ncatt_put(ncout, "time", "units", myTime)
# put our data in
for (var in names(profile))
{
ncvar_put(ncout, var, profile[[var]])
}
# save and close
nc_close(ncout)
cat("Written", o, "\n")
}
cat("\n\n\nFLAWLESS!\n\n")