Micro_era5: Issue with running model; missing era5 variables

130 views
Skip to first unread message

陈紫暄

unread,
Feb 10, 2025, 3:44:36 AM2/10/25
to NicheMapR
Hi all,  
I  have the same problem just like   Micro_era5: Issue with running model; missing era5 variables · Issue #32 · mrke/NicheMapR,   the variable name for "msnlwrf" in the micro_era5 function is not the same as the name in the NetCDF ("avg_snlwrf"). 
The parameter name has been changed by ERA5 data, causing micro_era5 to not function properly, as it reports an error indicating that msnlwrf cannot be found. Is there a solution to this issue?
Here shows the new name of msnlwrf:

Here shows the code:
If program is crashing, try run.gads = 2. terra 1.8.21 载入程序包:‘microclima’ The following objects are masked from ‘package:mcera5’: clearskyrad, julday, siflat, solalt, solartime, windheight downloading DEM via package elevatr Accessing raster elevation [=========================] 100% Mosaicing & Projecting extracting weather data locally from F:/swot/36/36_era5 Error in `dplyr::mutate()`: In argument: `netlong = abs(msnlwrf) * 0.0036`. Caused by error: ! 找不到对象'msnlwrf' Run `rlang::last_trace()` to see where the error occurred. Called from: signal_abort(cnd, .file) 警告信息: 1: replacing previous import ‘terra::time<-’ by ‘zoo::time<-’ when loading ‘microclima’ 2: replacing previous import ‘terra::median’ by ‘stats::median’ when loading ‘microclima’

Please could someone help? 
Zixuan Chen. 

NicheMapR

unread,
Feb 10, 2025, 5:08:50 AM2/10/25
to NicheMapR
Dear Zixuan Chen,

The problem is in the mcera5 package, so an issue for Dave Klinges to fix. But I traced it to the mcera5:::nc_to_df internal function. If you source the modified version below after loading the mcera5 package that should get you back up and running.

All the best,
Mike

nc_to_df <- function (nc, long, lat, start_time, end_time, dtr_cor = TRUE, dtr_cor_fac = 1) { dat <- tidync::tidync(nc) %>% tidync::hyper_filter(longitude = longitude == long, latitude = latitude == lat) %>% tidync::hyper_tibble() %>% dplyr::mutate(., obs_time = lubridate::ymd_hms("1900:01:01 00:00:00") + (time * 3600), timezone = lubridate::tz(obs_time)) %>% dplyr::filter(., obs_time >= start_time & obs_time < end_time + 1) %>% dplyr::rename(., pressure = sp) %>% dplyr::mutate(., temperature = t2m - 273.15, lsm = dplyr::case_when(lsm < 0 ~ 0, lsm >= 0 ~ lsm), temperature = dplyr::case_when(dtr_cor == TRUE ~ coastal_correct(temperature, lsm, dtr_cor_fac), dtr_cor == FALSE ~ temperature), humidity = humfromdew(d2m - 273.15, temperature, pressure), windspeed = sqrt(u10^2 + v10^2), windspeed = windheight(windspeed, 10, 2), winddir = (atan2(u10, v10) * 180/pi + 180)%%360, cloudcover = tcc * 100, netlong = abs( avg_snlwrf ) * 0.0036, downlong = msdwlwrf * 0.0036, uplong = netlong + downlong, emissivity = downlong/uplong, jd = julday(lubridate::year(obs_time), lubridate::month(obs_time), lubridate::day(obs_time)), si = siflat(lubridate::hour(obs_time), lat, long, jd, merid = 0)) %>% dplyr::mutate(., rad_dni = fdir * 1e-06, rad_glbl = ssrd * 1e-06, rad_glbl = rad_calc(rad_glbl, obs_time, long, lat), rad_dni = rad_calc(rad_dni, obs_time, long, lat), rad_dif = rad_glbl - rad_dni * si) %>% dplyr::mutate(., szenith = 90 - solalt(lubridate::hour(obs_time), lat, long, jd, merid = 0)) %>% dplyr::select(., obs_time, temperature, humidity, pressure, windspeed, winddir, emissivity, cloudcover, netlong, uplong, downlong, rad_dni, rad_dif, szenith, timezone) return(dat) }

陈紫暄

unread,
Feb 10, 2025, 5:12:41 AM2/10/25
to NicheMapR
Sorry to bother you, but I’ve solved the problem. I used Python with xarray to change the variable names 'avg_snlwrf' and 'avg_sdlwrf' back to 'msnlwrf' and 'msdwlwrf'.
The code helps:
import xarray as xr
import os

# 文件路径和年份范围
base_path = "F:/swot/34"
years = range(2000, 2011)

# 变量重命名映射
rename_dict = {
    "avg_snlwrf": "msnlwrf",
    "avg_sdlwrf": "msdwlwrf"
}

# 遍历年份
for year in years:
    file_path = os.path.join(base_path, f"34_era5_{year}.nc")
    new_file = os.path.join(base_path, f"34_era5_{year}_renamed.nc")
   
    if not os.path.exists(file_path):
        print(f"文件 {file_path} 不存在,跳过处理。")
        continue
   
    # 打开 NetCDF 文件
    ds = xr.open_dataset(file_path)
   
    # 变量重命名
    ds = ds.rename(rename_dict)
   
    # 保存为新文件
    ds.to_netcdf(new_file, format="NETCDF4")

    print(f"年份 {year} 变量重命名完成!新的文件: {new_file}")

陈紫暄

unread,
Feb 10, 2025, 5:16:26 AM2/10/25
to NicheMapR
  Thank you very much for your reply! I replaced the original function, and the problem is solved. I appreciate your help!  
Sincerely,
Zixuan Chen

David Klinges

unread,
Feb 10, 2025, 12:11:49 PM2/10/25
to 陈紫暄, NicheMapR
Hi all,

Thanks for flagging this— ECMWF has been changing some variable names recently on their Climate Data Store so that might be why. I’ll add a fix in mcera5 shortly.



--
You received this message because you are subscribed to the Google Groups "NicheMapR" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nichemapr+...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/nichemapr/6c236e49-ae54-4f7d-b0c0-0e6c2518c9b0n%40googlegroups.com.

David Klinges

unread,
Feb 11, 2025, 10:39:11 AM2/11/25
to 陈紫暄, NicheMapR
Hi all,

This fix has now been made in mcera5 (see here).

Feel free to email to this Google Group or submit an issue here for related issues on mcera5/ micro_era5.

Cheers, Dave
--
Reply all
Reply to author
Forward
0 new messages