【问题标题】:Convert netCDF files to csv将 netCDF 文件转换为 csv
【发布时间】:2020-12-07 02:11:13
【问题描述】:

我正在努力将几个 Berekeley Earth netCDF 文件转换为 CSV 或其他表格格式。我意识到以前也有人提出过类似的问题,但我无法应用我遇到的任何解决方案。

例如,this dataset

  • netCDF 实用程序中的ncdump 似乎不会生成实际的 CSV 文件。我找不到任何有关如何执行此操作的说明。
  • 我尝试使用xarray.to_dataframe() 将数据加载到pandas 数据帧中,但我的笔记本无法分配所需的内存。
In [1]: import xarray as xr

In [2]: import pandas as pd

In [3]: nc = xr.open_dataset('Complete_TAVG_Daily_EqualArea.nc')

In [4]: nc
Out[4]:
<xarray.Dataset>
Dimensions:      (map_points: 5498, time: 50769)
Dimensions without coordinates: map_points, time
Data variables:
    longitude    (map_points) float32 ...
    latitude     (map_points) float32 ...
    date_number  (time) float64 ...
    year         (time) float64 ...
    month        (time) float64 ...
    day          (time) float64 ...
    day_of_year  (time) float64 ...
    land_mask    (map_points) float64 ...

In [5]: df = nc.to_dataframe()
---------------------------------------------------------------------------
MemoryError                               Traceback (most recent call last)
(...)

MemoryError: Unable to allocate 532. MiB for an array with shape (279127962,) and data type int16
  • 我尝试使用Panoply 进行转换。 CSV 导出似乎只能将单个变量(我希望将其视为一列)导出到单行文件中。

我一定错过了什么。有人可以帮我吗?

【问题讨论】:

  • 您使用的是 Jupyter Notebook?您是否尝试过将其作为标准脚本运行?
  • 这没什么区别。顺便说一句,在 Windows 和 Linux 上的结果相同......

标签: python pandas netcdf


【解决方案1】:

您缺少的是 netCDF 是一种比 CVS 复杂得多的格式。一个 netCDF 文件可以包含多个任意形状和大小的数组。 CSV 文件只能包含最大 2 维的单个数组(或一组 1D 数组,如果它们都具有相同的长度)。因此,您不能简单地将任何 netCDF 文件转换为 CSV。

让我们看一下您提供的示例文件。我用我的 Xarray 版本重复这里的信息,这似乎有点冗长......

In [16]: ds = xr.open_dataset('Complete_TAVG_EqualArea.nc')

In [17]: ds
Out[17]:
<xarray.Dataset>
Dimensions:      (map_points: 5498, month_number: 12, time: 3240)
Coordinates:
    longitude    (map_points) float32 ...
    latitude     (map_points) float32 ...
  * time         (time) float64 1.75e+03 1.75e+03 1.75e+03 ... 2.02e+03 2.02e+03
Dimensions without coordinates: map_points, month_number
Data variables:
    land_mask    (map_points) float64 ...
    temperature  (time, map_points) float32 ...
    climatology  (month_number, map_points) float32 ...
Attributes:
    Conventions:          Berkeley Earth Internal Convention (based on CF-1.5)
    title:                Native Format Berkeley Earth Surface Temperature An...
    history:              16-Jan-2020 06:51:38
    institution:          Berkeley Earth Surface Temperature Project
    source_file:          Complete_TAVG.50985s.20200116T064041.mat
    source_history:       13-Jan-2020 17:22:52
    source_data_version:  ca6f26341938dae0ea7dd619bce6f15e
    comment:              This file contains Berkeley Earth surface temperatu...

有三个数据变量(land_mask、温度、气候),加上三个坐标向量(经度、纬度、时间)。也许您可以将坐标向量包含在 CSV 文件的第一行和第一列中,但即便如此,这意味着每个 netCDF 文件至少需要三个单独的 CSV 文件。

例如,对于 climatology 数据框,您可以按如下方式写入 CVS:

In [31]: clim = ds['climatology']  

In [32]: clim.to_pandas().to_csv('clim.csv') 

所以climxarray.DataFrame,原则上可以写入CSV 文件。不幸的是,xarray.DataFrame 类没有to_csv 方法。但是 pandas.DataFrame 类确实如此,所以我们首先将其转换为 pandas 数据框。查看其参数文档here 以调整生成的输出文件。

【讨论】:

  • 感谢一百万,这是一个非常清晰的解释,感谢您提供更明智的方法!
【解决方案2】:

您可以使用 CDO 软件包套件将 .nc 转换为 .csv。

示例代码(您需要编辑一些 outputtab 参数:

cdo -outputtab,date,lon,lat,value infile.nc | awk 'FNR==1{ row=$2","$3","$4","$5;print row  } FNR!=1{ row=$1","$2","$3","$4; print row}' > outfile.csv

【讨论】:

    猜你喜欢
    • 2015-05-19
    • 2014-05-20
    • 2018-05-10
    • 2021-06-21
    • 2021-10-21
    • 2017-11-05
    • 2019-12-17
    • 2021-06-02
    • 2013-06-28
    相关资源
    最近更新 更多