GVKun编程网logo

我如何能够使用 R 或 Python 来提取我需要的 netCDF/nc 数据?(怎么用python提取nc数据)

2

本文将分享我如何能够使用R或Python来提取我需要的netCDF/nc数据?的详细内容,并且还将对怎么用python提取nc数据进行详尽解释,此外,我们还将为大家带来关于Hovmoller在pyth

本文将分享我如何能够使用 R 或 Python 来提取我需要的 netCDF/nc 数据?的详细内容,并且还将对怎么用python提取nc数据进行详尽解释,此外,我们还将为大家带来关于Hovmoller 在 python 中绘制 netCDF 数据、Install netCDF under mac os、NCO 4.2.0 发布,netCDF 格式分析库、NetCDF 4.2.2.1 发布,网络通用数据格式的相关知识,希望对你有所帮助。

本文目录一览:

我如何能够使用 R 或 Python 来提取我需要的 netCDF/nc 数据?(怎么用python提取nc数据)

我如何能够使用 R 或 Python 来提取我需要的 netCDF/nc 数据?(怎么用python提取nc数据)

如何解决我如何能够使用 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: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],lat[156:301],lon[389:555],lev 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],lev

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

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

  1. destfile<-"C:/Users/Desktop/output"
  2. 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],lev"
  3. download.file(url,destfile)

解决方法

以下可能可行,更改开始和结束日期。显然它未经测试,因为您的示例不可重现。文件被输出到输出文件夹中名为“yyyymmdd”的文件中。

  1. startdate <- as.Date(''2012-01-01'')
  2. enddate <- as.Date(''2012-01-02'')
  3. dest <- "C:/Users/Desktop/output"
  4. dates <- seq(startdate,enddate)
  5. download_nasa_file <- function(date){
  6. 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")
  7. res <- try(download.file(url,file.path(dest,''%Y%m%d''))))
  8. if(inherits(res,''try-error''))
  9. return NULL
  10. res
  11. )))
  12. }
  13. results <- lapply(dates,download_nasa_file)

Hovmoller 在 python 中绘制 netCDF 数据

Hovmoller 在 python 中绘制 netCDF 数据

如何解决Hovmoller 在 python 中绘制 netCDF 数据

我有 12 个 1 月到 12 月的海面温度数据集。数据集有 3 个字段经度、纬度和 sst。

我想以这样的方式绘制 hovmoller 图,即我的 y 轴代表范围从北纬 9 度到北纬 16 度的纬度。而 x 轴代表从一月到十二月的月份。 在这里,经度将保持不变,即 15。

我想要相同的 python 代码。

这是我想要的示例图片

img

我可以使用 matplotlib 的 contourf 函数绘制各个月份的数据。

  1. #Data for january-2019 to December-2019
  2. months = ["January","February","march","April","May","June","July","August","September","October","November","December"]
  3. for i in range(0,12):
  4. data_file_name = "sst_" + months[i] + "_2019.nc"
  5. local_file_path = ''C:/Users/hp/Desktop/ISRO/MOdis DATA NEW/''
  6. local_file_path += data_file_name
  7. file_handle = Dataset(local_file_path,mode = ''r'',format ="NETCDF4")
  8. fig = plt.figure(figsize = (15,8))
  9. #Extracting all the columns from given .nc file
  10. lat_scale = file_handle.variables[''lat''][:] # 1D
  11. lon_scale = file_handle.variables[''lon''][:] # 1D
  12. sst = file_handle.variables[''sst''][:]
  13. mBase2 = Basemap(llcrnrlon=50,llcrnrlat= 0,urcrnrlon= 100,urcrnrlat=27)
  14. mBase2.drawmapboundary(color = ''green'')
  15. mBase2.drawcountries(linewidth = 2)
  16. mBase2.drawcoastlines(linewidth = 2)
  17. mBase2.drawparallels(np.arange(-90,90,5),labels=[True,False,False])
  18. mBase2.drawmeridians(np.arange(-180,180,labels=[0,1])
  19. v = np.linspace(22,32,50,endpoint=True)
  20. mymap2 = plt.contourf(lon_scale,lat_scale,sst,v,cmap=plt.cm.jet)
  21. plt.colorbar(mymap2,orientation = ''vertical'')
  22. title = "Sea Surface Temperature for " + months[i] + " - 2019"
  23. plt.title(title)
  24. image_name = str(i + 1) + "_sst_" + months[i] + "_2019"
  25. plt.savefig(image_name)

对于上面的代码,我得到了以下所有 12 个月的结果。

The above image is only for January,similarly,I got other images for 11 months.

提前致谢。

Install netCDF under mac os

Install netCDF under mac os

Tips worth of notice

  • I''m using the cpp interfaces. It requires you first install the basic netCDF C class and then install the interface for cpp, because the cpp interface is based on the classic model.

  • netcdf.h is the header file for C interface; netcdfcpp.h is the older version of CPP version, and the newest version will use the header file netcdf instead.

  • Interfaces differ greatly between versions.

Installation

Use HomeBrew to install the package. Make sure you have the parameter. This is important to add support for c++.
brew install netcdf --with-cxx-compat

Compilation

Thanks to CodingAnarchy''s question, I found the missing parameter as followed. Otherwise, error keeps apearing that linking command fails.
g++ test.cpp -lnetcdf_c++4

Reference

  • NetCDF C++ Interface Guide

  • Fail to link using Homebrew

  • Exmple netCDF programs

  • NetCDF4 C++ API

Useful commands

  • brew doctor to analyze your condition of brew packages

11/14/2016 Weiming

NCO 4.2.0 发布,netCDF 格式分析库

NCO 4.2.0 发布,netCDF 格式分析库

NCO 4.2.0 发布,该版本增加对 Visual Studio 支持,修复了一些 bug。

netCDF Operators (NCO) 工具用来操作和分析 netCDF 自描述数据存储格式。

NetCDF 4.2.2.1 发布,网络通用数据格式

NetCDF 4.2.2.1 发布,网络通用数据格式

NetCDF 4.2.2.1 发布,该版本修复了远程访问超过2G的大文件时的 DAP 性能问题,修复了 CDL 输出的bug等。

NetCDF(network Common Data Format),即网络通用数据格式。最早是由美国国家科学委员会资助之计划--Unidata --所发展,其用意是在Unidata计划中不同的应用项目下,提供一种可以通用的数据存取方式,数据的形状包括单点的观测值、时间序列、规则排列的网 格、以及人造卫星或雷达之影像档案。

NetCDF 可简单的视为一种存取接口,任何使用 NetCDF 存取格式的档案就可称为 NetCDF 档案;至于 NetCDF 这套软件的功能,在于提供C、Fortran、C++、Perl、或其它语言I/O的链接库,以让程序发展者可以读写数据文件,其本身具有说明的能力、并 且可以跨越平台和机器的限制。每一个NetCDF档案可以含括多维度的、具有名称的变量,包括长短的整数、单倍与双倍精度的实数、字符等,且每一个变量都 有其自我介绍的数据,包括量度的单位、全名及意义等文字说明,在此摘要性的檔头之后,才是真正的数据本身。

NetCDF接口是一种多维的数据分布系统,由这个接口所产生的档案,具有多维的数据格式,当你需要其中的某一笔数据时,程序将不会从第一笔数据读 到你所需要的数据处,而是由 NetCDF 软件直接存取那一个数据。如此一来将会大量的降低模式运算时数据存取的时间。但也就是因为这样, NetCDF 所需要的空间是很大的,因为他多了很多的自解释的申明。

今天的关于我如何能够使用 R 或 Python 来提取我需要的 netCDF/nc 数据?怎么用python提取nc数据的分享已经结束,谢谢您的关注,如果想了解更多关于Hovmoller 在 python 中绘制 netCDF 数据、Install netCDF under mac os、NCO 4.2.0 发布,netCDF 格式分析库、NetCDF 4.2.2.1 发布,网络通用数据格式的相关知识,请在本站进行查询。

本文标签: