将 netCDF 文件转换为 csv

mrg*_*gou 2 python netcdf pandas

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

例如,这个数据集

  • ncdump从 netCDF 实用程序中似乎不会生成实际的 CSV 文件。我找不到任何关于如何执行此操作的说明。
  • 我尝试使用 将数据加载到pandas数据帧中xarray.to_dataframe(),但我的笔记本无法分配所需的内存。
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
Run Code Online (Sandbox Code Playgroud)
  • 我尝试过使用 进行转换Panoply。CSV 导出似乎只能将单个变量(我希望将其视为一列)导出到单行文件中。

我肯定错过了什么。有人能帮我吗?

tit*_*jan 7

您缺少的是 netCDF 是一种比 CSV 复杂得多的格式。netCDF 文件可以包含任意形状和大小的多个数组。CSV 文件只能包含最大 2 维的单个数组(或一组一维数组,如果它们都具有相同的长度)。因此,您不能简单地将任何 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...
Run Code Online (Sandbox Code Playgroud)

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

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

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

In [32]: clim.to_pandas().to_csv('clim.csv') 
Run Code Online (Sandbox Code Playgroud)

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