Hello dear scientist,

I am a very new user of R software.

I have data (wind speed) at 10 meters in the NetCDF file.

Now I want to convert my data (wind speed) to 50 m high.

My NC (netCDF) file is attached here that has data at 10m. Please see the data file at the following link.

https://drive.google.com/file/d/1huWx7pt8m70HdA-eLcAhOtZUaMnnY9D_/view?usp=sharing

So, as a new user, I would like to request that you please share codes on how to extrapolate data from 10m to 50m.

Hi @KHAN1 ,

This is not my field of expertise, but hopefully this will start you off in the right direction. The 'stars' package allows you to read the netcdf file as a spatial-temporal data cube (lat x lon x time).

The sticking point that requires more subject-matter expertise is deciding on which model to use to calculate windspeed at 50 m (u50) from a 10m standard.

Some of the questions you will have to ask is whether the windspeed power law model is sufficient https://en.wikipedia.org/wiki/Wind_profile_power_law. That is, can you assume a uniform surface, or do you need to take into account the different roughness of the surface, in which case additional geospatial data is required as well as using different windspeed models (e.g. https://en.wikipedia.org/wiki/Log_wind_profile)?

```
library("stars")
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.11.0, GDAL 3.5.1, PROJ 9.0.1; sf_use_s2() is TRUE
library("dplyr")
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
u10 <- read_ncdf("~/Downloads/ERA5_monthlyWindSpeed.nc", var = "sfcWind")
#> Will return stars object with 3456000 cells.
#> No projection information found in nc file.
#> Coordinate variable units found to be degrees,
#> assuming WGS84 Lat/Lon.
#https://en.wikipedia.org/wiki/Wind_profile_power_law
u_calc <- function(ur, zr, z, a = 0.143) {
ur * ((z / zr) ^ a)
}
# Calculate windspeed at height z = 50
u50 <- u_calc(u10, zr = 10, z = 50)
# First time slice
u50_t1 <- u50 %>%
slice("time", 1)
plot(u50_t1)
```

^{Created on 2022-08-25 with reprex v2.0.2}

Thank you so much dear scientist @jrmuirhead for your kind help,

After the calculation, I want to export the nc file (netCDF) from the "R software".

I tried it but I could not succeed as a new user. You can see errors in a screenshot.

Please guide me in the code on how I can export nc file.

# First time slice

u50_t1 <- u50 %>%

slice("time", 540)

plot(u50_t1)

write_csv(u50_t1, file = "C:/Users/guest2/Downloads/u50_t1.csv")

# method 1

ncout <- nc_create("C:/Users/guest2/Downloads/u50_t1.nc",list(540,60.125 to 99.625,39.875 to 0.375),force_v4=TRUE)

# method 2

writeRaster(u50_t1,"output.nc",overwrite=TRUE,format='CDF',varname="sfcwind",

varunit="m/s",longname="sfcwind",xname="lon",

yname="lat")

HI @KHAN1

Since you also wanted to be able to write out back to a netcdf file, I found that the `terra`

package did a better job without creating bands in the wrong order when I saved to a netcdf file (at least when I looked at the file in the panoply app.)

```
library("terra")
u10 <- rast("~/Downloads/ERA5_monthlyWindSpeed.nc", drivers = "NETCDF")
#https://en.wikipedia.org/wiki/Wind_profile_power_law
u_calc <- function(ur, zr, z, a = 0.143) {
ur * ((z / zr) ^ a)
}
# Calculate windspeed at height z = 50
u50 <- u_calc(u10, zr = 10, z = 50)
writeCDF(u50, filename = "~/Downloads/u50.nc", varname = "sfcWind",
unit = "m/s", zname = "time", prec = "float")
```

Thank you so much dear scientist @jrmuirhead you help me to solve this problem.

Further, I have a last humble request for you how can I export my nc file (netCDF) from the following codes?

I do not understand my mistake regarding how to export. kindly guide me on how to export file in NC (netCDF).

An error can be seen in the attached screenshot.

(Explanation: I am solving this equation to correct wind speed data by the Weibull distribution method. Here are four constant values (c1 = 2.59062, k1 = 9.10817, c2 = 3.12297, k2 = 10.2287) I used

and one variable value in nc file (netCDF).

NC file is in this link: https://drive.google.com/file/d/1WlBCqCgMA116gGaTcXRt4V3aMSJoFcW9/view?usp=sharing

Now after the calculation I want to export it as an NC file.)

library("SciViews")

library("terra")

m1 <- rast("C:/Users/guest2/Desktop/wind/MPI_1961_2005.nc", drivers = "NETCDF")

# Weibull distribution equation

u_calc <- function(vmodel, c2, k2, c1 = 2.59062, k1 = 9.10817) {

((( -ln ( 1 - ( 1 - exp (- vmodel / c2) ^ k2))) ^ 1 / k1) * c1)

}

# Calculate corrected climate variable of windspeed

b1 <- u_calc(m1, c2 = 3.12297, k2 = 10.2287)

writeCDF(b1, filename = "C:/Users/guest2/Desktop/wind/BiasMPI.nc", varname = "sfcWind",

unit = "m/s", prec = "float")

Hi @KHAN1,

Just a couple of things. The natural log function in R is `log`

instead of `ln`

, and the result of your Weibull function requires double precision instead of floating point precision when saving to a netcdf file.

```
library("terra")
m1 <- rast("~/Downloads/MPI_1961_2005.nc", drivers = "NETCDF")
u_calc <- function(vmodel, c2, k2, c1 = 2.59062, k1 = 9.10817) {
((( -log( 1 - ( 1 - exp(- vmodel / c2) ^ k2))) ^ 1 / k1) * c1)
}
b1 <- u_calc(m1, c2 = 3.12297, k2 = 10.2287)
writeCDF(b1, filename = "~/Downloads/BiasMPI.nc", varname = "sfcWind",
unit = "m/s", prec = "double")
```

Thank you, dear scientist, @jrmuirhead

Based on your direction, after the calculation and succeeded to export the NC file (netCDF).

But data are missing in the NC file. I tried to find the mistake but failed. Kindly see attached screenshot where the values are missing.

Could you please guide how to export NC file (netCDF) without missing data?

HI @KHAN1 ,

I think the issue lies in the values of your input data and Weibull distribution function. The maximum value for the input data `max(m1)`

is 14.6504211 which becomes undefined (Infinite) after the Weibull transformation with the parameters you supplied. You may need to pick different parameters in order to handle the extreme values of the input data, or alternatively filter out the input data where values are > 11 m/s

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.