Parse the nc file and save it as a csv file
write at the front
I would like to call it: a very perfect version that supports private customization.
Reference:
Analysis part reference:
https://blog.csdn.net/qq_40105563/article/details/119871620
Time conversion reference:
https://blog.csdn.net /weixin_51015047/article/details/122571396
{Irregular time}:
https://blog.csdn.net/weixin_43646592/article/details/113427937
Writing csv file reference:
https://blog.csdn.net/Cqh__/article /details/109750908
Parse the nc file (the code summary is placed at the end)
Facilitate subsequent code adjustments
Read nc file
I don’t know the reason. There is a problem with reading the xarray library. I can only use the netCDF4 library.
Get all variables in the weather file
parsing time
The generated real_time is a numpy array, and the datetime object is nested in the array.
It started in 1800, and some of the codes on the Internet started in 1900.
Convert the data into string form
Note: Since this data is on the 1st of each month, you only need to save 年/月
the data in the format.
If you need to save 年/月/日
the data in the format, the code can be changed to:
data0 = str(real_time[i].year)+"/"+str(real_time[i].month)+str(real_time[i].day)
Year, month, day, hour, minute, second, and so on
Analysis of partial code summary
import netCDF4
from netCDF4 import Dataset
dir = r'sst.mnmean.nc' # 替换为自己的nc文件
nc = Dataset(dir)
# 获取气象文件中所有变量
vars=nc.variables.keys()
#取出各variable的数据看看,数据格式为numpy数组
for var in vars:
#读取每个变量的值
var_data=nc.variables[var][:].data
print(var,var_data.shape)
time = nc.variables['time'] # 读取时间
real_time = netCDF4.num2date(time,time.units)
print(real_time)
#查看一下time的属性
nc.variables['time']
time = nc.variables['time'] # 读取时间
real_time = netCDF4.num2date(time,time.units)
print(real_time)
print(real_time[0].year,real_time[0].month)
print(str(real_time[0].year)+"/"+str(real_time[0].month))
data = []
for i in range(len(real_time)):
data0 = str(real_time[i].year)+"/"+str(real_time[i].month)
data.append(data0)
print(data)
Write to csv file
from netCDF4 import Dataset
import csv
nc = Dataset('sst.mnmean.nc')
print(nc.variables.keys())
lat = nc.variables['lat'][:]
lon = nc.variables['lon'][:]
sst = nc.variables['sst'][:]
time = nc.variables['time'] # 读取时间
# 将时间转化格式:cftime.DatetimeGregorian(1854, 1, 1, 0, 0, 0, 0, has_year_zero=False)
real_time = netCDF4.num2date(time,time.units)
print(real_time)
# 将时间转化格式:['1854/1', '1854/2', '1854/3', '1854/4', '1854/5', '1854/6', '1854/7']
data = []
for i in range(len(real_time)):
data0 = str(real_time[i].year)+"/"+str(real_time[i].month)
data.append(data0)
print(data)
# def getDate(num):
# y = 1801 + num/12
# m = 1 + num % 12
# return "%(year)d-%(month)02d"%{'year':y,'month':m}
with open('New_nc v2.0版本.csv', 'a', newline='') as fp:
writer = csv.writer(fp, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['time', 'lat', 'lon', 'sst'])
# 输入经纬度的维数
for i in range(len(data)):
for j in range(len(lat)):
for k in range(len(lon)):
if str(sst[i][j][k]) not in '--':
writer.writerow([data[i], lat[j], lon[k], sst[ i, j, k]])
Finished✿✿ヽ(°▽°)ノ✿