最近拿到了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 = file_in.variables["latitude"][:]
lonList = file_in.variables["longitude"][:]
# print(u1,v1,time)
for i in range(len(time)):
# print(time[i])
print(v1[i][0], u1[i][0])
# print(i,type(f))
newName = f[:-3] + "_" + str(i) + ".nc"
pathOut = os.path.join(out_folder, newName) # output file
# g = Dataset(pathOut, 'w') # w if for creating a file
outputfile = Dataset(pathOut, 'w', format='NETCDF3_CLASSIC')
outputfile.createDimension('lat', 628)
outputfile.createDimension('lon', 1440)
outputfile.createDimension('time', 1)
lat = outputfile.createVariable('lat', np.float32, ('lat'))
lon = outputfile.createVariable('lon', np.float32, ('lon'))
time = outputfile.createVariable('time', np.float32, ('time'))
u = outputfile.createVariable('U', np.float32, ('lat', 'lon'))
v = outputfile.createVariable('V', np.float32, ('lat', 'lon'))
lat.units = 'degrees_north'
lat.long_name = 'latitude'
# _FillValue = NaN; // double
# :units = "degrees_north";
# :long_name = "Latitude";
lat.standard_name = "latitude"
lat.point_spacing = "even"
lat.axis = "Y";
# :NAVO_code = 1; // int
lat._CoordinateAxisType = "Lat"
lon.units = 'degrees_east'
lon.long_name = 'longitude'
lon.standard_name = "longitude";
# lon.modulo = "360 degrees";
lon.axis = "X";
# :NAVO_code = 2; // int
lon._CoordinateAxisType = "Lon";
time.units = "hours since 1987-01-01 00:00:00"
time.long_name = "Time of analysis"
# u(time=72, depth=1, latitude=481, longitude=1201);
u.units = "m s-1";
u.long_name = "u-wind vector component at 10 meters"
u.coordinates = "latitude longitude";
u.height = "10 meters above sea-surface";
u.standard_name = "eastward_wind";
v.units = "m s-1"
v.long_name = "v-wind vector component at 10 meters"
v.standard_name = "northward_wind"
v.height = "10 meters above sea-surface"
# missing_value = NaN; // double
outputfile.variables['lon'][:] = lonList
# np.linspace(0, 360, 1201)
# outputfile.variables['lon'][:] = np.linspace(-180 + 0.5, 180 - 0.5, 1201)
outputfile.variables['lat'][:] = latList
# np.linspace(80, -80 , 481)
# np.array()file_in.variables['longitude'][:]
# np.linspace(-80, 80 , 481)
outputfile.variables['U'][:] = u1[i]
outputfile.variables['V'][:] = v1[i]
outputfile.variables['time'][:] = time[i:i + 1]
outputfile.close()
file_in.close()
if __name__ == '__main__':
in_folder = "F:\\Data\\CCMP"
out_folder = "F:\\Data\\CCMP\\Daily"
for year in range(2019,2010,-1):
folder = in_folder + "\\" + str(year)
ChangeNC(folder, out_folder)