今天试了一下用PyNIO写nc文件,难用的一腿
算了,还是用netcdf模块写了。
代码如下:
# -*- coding: utf-8 -*-
import numpy as np
import sys
import os
from netCDF4 import Dataset
def creatspinc(value, filename):
gridspi = Dataset(filename, 'w', format='NETCDF4')
# dimensions
gridspi.createDimension('time', None)
gridspi.createDimension('lat', 1000) #len(lat)
gridspi.createDimension('lon', 2000)
# Create coordinate variables for dimensions
times = gridspi.createVariable('time', np.float64, ('time',))
latitudes = gridspi.createVariable('lat', np.float32, ('lat',))
longitudes = gridspi.createVariable('lon', np.float32, ('lon',))
# Create the actual variable
var = gridspi.createVariable('var', np.float32, ('time', 'lat', 'lon',))
# Global Attributes
import time
gridspi.description = 'var'
gridspi.history = 'Created ' + time.ctime(time.time())
gridspi.source = 'netCDF4 python module tutorial'
# Variable Attributes
latitudes.units = 'degree_north'
longitudes.units = 'degree_east'
times.units = 'days since 2019-01-01 00:00:00'
times.calendar = 'gregorian'
# data
lats = np.linspace(25.,35.,1000) #notice: the last numb is not included
lons = np.linspace(95.,115.,2000) #notice: the last numb is not included
latitudes[:] = lats
longitudes[:] = lons
#--Fill in values
var[0:value.shape[0],:,:] = value
# Fill in times
from datetime import datetime, timedelta
from netCDF4 import num2date, date2num
dates = []
for n in range(var.shape[0]):
dates.append(datetime(2019, 1, 1) + n * timedelta(days=1))
times[:] = date2num(dates, units = times.units,calendar = times.calendar)
print 'time values (in units %s): ' % times.units +'\n', times[:]
dates = num2date(times[:], units=times.units, calendar=times.calendar)
gridspi.close()
return
out_dir='~/'
out_file_nc = out_dir+'out.nc'
data_nc = np.zeros(shape=(61,1000,2000))
creatspinc(data_nc, out_file_nc)
最后这一段是我随便写了个变量调用写.nc文件的函数来测试的。
最近有小伙伴问我为什么公众号文章不能留言,我百度了一下,说这几个月开通的公众号账户文章都没有留言功能,也不知道以后会不会给留言。不过可以赞赏的哟!