NC文件按时序维度拆分

这篇博客主要介绍如何处理包含一年以上数据的nc文件,由于无法直接按时间维度识别,作者分享了将nc文件拆分成多个按时间顺序的小文件的代码实现,方便后续使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近拿到了nc文件,每个nc文件里包括了一年多的数据,使用的时候无法识别时间维度,需要把nc文件按时间维度拆分为很多个nc文件。
nc文件的结构
在这里插入图片描述拆分代码如下

import xarray as xr
import os
from netCDF4 import Dataset
import numpy as np
in_folder = r"F:\Data\流场数据\data"
out_folder = r"F:\Data\OceanCurrent"

#First import the netcdf4 library
from netCDF4 import Dataset  # http://code.google.com/p/netcdf4-python/

# Read en existing NetCDF file and create a new one
# f is going to be the existing NetCDF file from where we want to import data
# and g is going to be the new file.

def ChangeNC(in_folder,out_folder):
    for root, dirs, files in os.walk(in_folder):
        for f in files:
            path = os.path.join(root, f)
            print(path)
            file_in = Dataset(path, 'r')  # r is for read only
            time = file_in.variables["time"][:]  # read time variable
            u1 = file_in.variables["uwnd"][:]
            v1 = file_in.variables["vwnd"][:]
            latList =
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值