budayang_ 2023-05-14 18:40 采纳率: 10%
浏览 46

如何用python将nc转为csv

请问如何将ERA5的nc文件转为csv或者xls格式,比如2m温度,表头为time lat lon t2m,主要是不知道关于时间的代码怎么写,它的信息是这样的:PS:看了很多相关代码,都没有将时间问题解决,感谢各位帮忙!


```python
Source:
           D:\meteorologicaldata\2m_temperature\year\2015.nc
Format:
           64bit
Global Attributes:
           Conventions = 'CF-1.6'
           history     = '2023-04-07 11:04:31 GMT by grib_to_netcdf-2.25.1: /opt/ecmwf/mars-client/bin/grib_to_netcdf.bin -S param -o /cache/data9/adaptor.mars.internal-1680865445.5369513-21211-3-2f04ee57-a8af-4c46-bb0b-9b562ce85b7e.nc /cache/tmp/2f04ee57-a8af-4c46-bb0b-9b562ce85b7e-adaptor.mars.internal-1680864979.6155574-21211-4-tmp.grib'
Dimensions:
           longitude = 37
           latitude  = 25
           time      = 8760
Variables:
    longitude
           Size:       37x1
           Dimensions: longitude
           Datatype:   single
           Attributes:
                       units     = 'degrees_east'
                       long_name = 'longitude'
    latitude 
           Size:       25x1
           Dimensions: latitude
           Datatype:   single
           Attributes:
                       units     = 'degrees_north'
                       long_name = 'latitude'
    time     
           Size:       8760x1
           Dimensions: time
           Datatype:   int32
           Attributes:
                       units     = 'hours since 1900-01-01 00:00:00.0'
                       long_name = 'time'
                       calendar  = 'gregorian'
    t2m      
           Size:       37x25x8760
           Dimensions: longitude,latitude,time
           Datatype:   int16
           Attributes:
                       scale_factor  = 0.0010827
                       add_offset    = 279.938
                       _FillValue    = -32767
                       missing_value = -32767
                       units         = 'K'
                       long_name     = '2 metre temperature'

```

  • 写回答

1条回答 默认 最新

  • IT小辉同学 Java领域优质创作者 2023-07-28 10:11
    关注

    要将ERA5的nc文件转为csv或xls格式,可以使用Python中的xarray库进行处理和转换。以下是一个示例代码,演示了如何提取并保存2m温度数据为csv格式:

    import xarray as xr
    
    # 读取nc文件
    data = xr.open_dataset('D:\meteorologicaldata\2m_temperature\year\2015.nc')
    
    # 提取时间、经度、纬度和2m温度数据
    time = data['time'].values
    lat = data['latitude'].values
    lon = data['longitude'].values
    t2m = data['t2m'].values
    
    # 将时间转换为字符串格式
    time_str = [str(t) for t in time]
    
    # 转换为csv格式并保存
    with open('data.csv', 'w') as f:
        f.write('time,lat,lon,t2m\n')
        for i in range(len(time_str)):
            for j in range(len(lat)):
                for k in range(len(lon)):
                    f.write(f'{time_str[i]},{lat[j]},{lon[k]},{t2m[k, j, i]}\n')
    

    上述代码将nc文件读取为xarray.Dataset对象,然后提取时间、经度、纬度和2m温度数据。时间转换为字符串格式后,通过循环将数据写入csv文件中,每行包含时间、经度、纬度和对应的2m温度值。

    你可以根据需要更改输出文件的名称和格式。如果希望保存为xls格式,可以使用Python的pandas库将csv文件转换为xls。

    请注意,上述代码是一个基本示例,具体的操作和数据处理可能需要根据你的数据结构和要求进行适当调整。

    评论

报告相同问题?

问题事件

  • 创建了问题 5月14日

悬赏问题

  • ¥15 手机被安装黑客软件怎么办?
  • ¥15 Windows C++ PaddleOcr 中文模型的训练方法
  • ¥15 c# 用scottplot画 以时间为纵坐标,数值为横坐标画曲线图
  • ¥15 手机应用程序安装异常
  • ¥15 grbl的G92修改MPos的问题。
  • ¥15 vue2中,Ant Design Pro s-table中,使用服务端排序怎么做
  • ¥15 阿里巴巴国际站的商品详细页如何做到图文分离
  • ¥15 根据质因子个数求因式个数
  • ¥15 matlab模糊控制隶属函数设计
  • ¥45 C++上传文件到SFTP