最近拿到了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 =