Python實(shí)現(xiàn)將數(shù)據(jù)寫(xiě)入netCDF4中的方法示例
本文實(shí)例講述了Python實(shí)現(xiàn)將數(shù)據(jù)寫(xiě)入netCDF4中的方法。分享給大家供大家參考,具體如下:
nc文件為處理氣象數(shù)據(jù)文件。用戶(hù)可以去https://www.lfd.uci.edu/~gohlke/pythonlibs/ 搜索netCDF4,下載相應(yīng)平臺(tái)的whl文件,使用pip安裝即可。
這里演示的寫(xiě)入數(shù)據(jù)操作代碼如下:
# -*- coding:utf-8 -*-
import numpy as np
'''
輸入的data的shape=(627,652)
'''
def write_to_nc_canque(data,file_name_path):
import netCDF4 as nc
lonS=np.linspace(119.885,120.536,652)
latS=np.linspace(29.984,29.358,627)
da=nc.Dataset(file_name_path,'w',format='NETCDF4')
da.createDimension('lons',652) #創(chuàng)建坐標(biāo)點(diǎn)
da.createDimension('lats',627) #創(chuàng)建坐標(biāo)點(diǎn)
da.createVariable("lon",'f',("lons")) #添加coordinates 'f'為數(shù)據(jù)類(lèi)型,不可或缺
da.createVariable("lat",'f',("lats")) #添加coordinates 'f'為數(shù)據(jù)類(lèi)型,不可或缺
da.variables['lat'][:]=latS #填充數(shù)據(jù)
da.variables['lon'][:]=lonS #填充數(shù)據(jù)
da.createVariable('u','f8',('lats','lons')) #創(chuàng)建變量,shape=(627,652) 'f'為數(shù)據(jù)類(lèi)型,不可或缺
da.variables['u'][:]=data #填充數(shù)據(jù)
da.close()
write_to_nc_canque(one,'D://new.nc')
'''
輸入的data的shape=(627,652)
'''
def write_to_nc_wanmei(data,file_name_path):
import netCDF4 as nc
lonS=np.linspace(119.885,120.536,652)
latS=np.linspace(29.984,29.358,627)
da=nc.Dataset(file_name_path,'w',format='NETCDF4')
da.createDimension('lon',652) #創(chuàng)建坐標(biāo)點(diǎn)
da.createDimension('lat',627) #創(chuàng)建坐標(biāo)點(diǎn)
da.createVariable("lon",'f',("lon")) #添加coordinates 'f'為數(shù)據(jù)類(lèi)型,不可或缺
da.createVariable("lat",'f',("lat")) #添加coordinates 'f'為數(shù)據(jù)類(lèi)型,不可或缺
da.variables['lat'][:]=latS #填充數(shù)據(jù)
da.variables['lon'][:]=lonS #填充數(shù)據(jù)
da.createVariable('u','f8',('lat','lon')) #創(chuàng)建變量,shape=(627,652) 'f'為數(shù)據(jù)類(lèi)型,不可或缺
da.variables['u'][:]=data #填充數(shù)據(jù)
da.close()
write_to_nc_wanmei(one,'D://new1.nc')
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
numba提升python運(yùn)行速度的實(shí)例方法
在本篇文章里小編給大家整理的是一篇關(guān)于numba提升python運(yùn)行速度的實(shí)例方法,有興趣的朋友們可以參考下。2021-01-01
Python字符串對(duì)象實(shí)現(xiàn)原理詳解
這篇文章主要介紹了Python字符串對(duì)象實(shí)現(xiàn)原理詳解,在Python世界中將對(duì)象分為兩種:一種是定長(zhǎng)對(duì)象,比如整數(shù),整數(shù)對(duì)象定義的時(shí)候就能確定它所占用的內(nèi)存空間大小,另一種是變長(zhǎng)對(duì)象,在對(duì)象定義時(shí)并不知道是多少,需要的朋友可以參考下2019-07-07
Python二進(jìn)制數(shù)據(jù)結(jié)構(gòu)Struct的具體使用
在C/C++語(yǔ)言中,struct被稱(chēng)為結(jié)構(gòu)體。而在Python中,struct是一個(gè)專(zhuān)門(mén)的庫(kù),用于處理字節(jié)串與原生Python數(shù)據(jù)結(jié)構(gòu)類(lèi)型之間的轉(zhuǎn)換。本文就詳細(xì)介紹struct的使用方式2021-06-06
如何用Python寫(xiě)一個(gè)簡(jiǎn)單的通訊錄
這篇文章主要介紹了如何用Python寫(xiě)一個(gè)簡(jiǎn)單的通訊錄,對(duì)著幾串代碼感興趣的朋友一起來(lái)看看吧2021-08-08

