如何使用R或Python提取我需要的netCDF/nc数据



我想从EarthData(NASA(中提取一些数据,我已经获得了netCDF格式的链接。

链接示例如下:https://goldsmr5.gesdisc.eosdis.nasa.gov/opendap/MERRA2/M2I6NPANA.5.12.4/2012/01/MERRA2_400.inst6_3d_ana_Np.20120101.nc4.nc4?PS[0:3][156:301][389:555],V[0:3][0:41][156:301][389:553],T[0:3][:041][156:301][389:555]、SLP[0:3][156:301][389:553]、U[0:3][0:41][156:01][389:355]、QV[0:3][156:301][389:554]、H[0:3][0:041][156:01][389:351]、O3[0:3][:41][156:555]9:555],时间,纬度[156:301],经度[389:555],列夫https://goldsmr5.gesdisc.eosdis.nasa.gov/opendap/MERRA2/M2I6NPANA.5.12.4/2012/01/MERRA2_400.inst6_3d_ana_Np.20120102.nc4.nc4?PS[0:3][156:301][389:555],V[0:3][0:41][156:301][389:553],T[0:3][:041][156:301][389:555]、SLP[0:3][156:301][389:553]、U[0:3][0:41][156:01][389:355]、QV[0:3][156:301][389:554]、H[0:3][0:041][156:01][389:351]、O3[0:3][:41][156:555]9:555],时间,lat[156:301],lon[389:555],lev

基本上,在我的所有链接中,唯一更改的是日期(从20120101到20120102,依此类推(。我想使用循环函数遍历我所有的url列表,使用R或Python代码将数据提取为nc4格式?

为了提取单个数据,我尝试过:

destfile<-"C:/Users/Desktop/output"
url <- "https://username:password@goldsmr5.gesdisc.eosdis.nasa.gov/opendap/MERRA2/M2I6NPANA.5.12.4/2012/01/MERRA2_400.inst6_3d_ana_Np.20120102.nc4.nc4?PS[0:3][156:301][389:555],V[0:3][0:41][156:301][389:555],T[0:3][0:41][156:301][389:555],SLP[0:3][156:301][389:555],U[0:3][0:41][156:301][389:555],QV[0:3][0:41][156:301][389:555],H[0:3][0:41][156:301][389:555],O3[0:3][0:41][156:301][389:555],time,lat[156:301],lon[389:555],lev"
download.file(url, destfile)

通过更改开始和结束日期,以下内容可能会奏效。显然,这是未经测试的,因为你的例子是不可复制的。文件输出到输出文件夹中的文件名"yyyymmdd"中。

startdate <- as.Date('2012-01-01')
enddate <- as.Date('2012-01-02')
dest <- "C:/Users/Desktop/output"
dates <- seq(startdate, enddate)
download_nasa_file <- function(date){
url <- paste0("https://username:password@goldsmr5.gesdisc.eosdis.nasa.gov/opendap/MERRA2/M2I6NPANA.5.12.4/2012/01/MERRA2_400.inst6_3d_ana_Np."
, format(date, '%Y%m%d'), 
".nc4.nc4?PS[0:3][156:301][389:555],V[0:3][0:41][156:301][389:555],T[0:3][0:41][156:301][389:555],SLP[0:3][156:301][389:555],U[0:3][0:41][156:301][389:555],QV[0:3][0:41][156:301][389:555],H[0:3][0:41][156:301][389:555],O3[0:3][0:41][156:301][389:555],time,lat[156:301],lon[389:555],lev")
res <- try(download.file(url, file.path(dest, format(date, '%Y%m%d'))))
if(inherits(res, 'try-error'))
return NULL
res 
)))
}
results <- lapply(dates, download_nasa_file)

最新更新