Python實現(xiàn)將數(shù)據(jù)寫入netCDF4中的方法示例
本文實例講述了Python實現(xiàn)將數(shù)據(jù)寫入netCDF4中的方法。分享給大家供大家參考,具體如下:
nc文件為處理氣象數(shù)據(jù)文件。用戶可以去https://www.lfd.uci.edu/~gohlke/pythonlibs/ 搜索netCDF4,下載相應(yīng)平臺的whl文件,使用pip安裝即可。
這里演示的寫入數(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ù)類型,不可或缺
da.createVariable("lat",'f',("lats")) #添加coordinates 'f'為數(shù)據(jù)類型,不可或缺
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ù)類型,不可或缺
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ù)類型,不可或缺
da.createVariable("lat",'f',("lat")) #添加coordinates 'f'為數(shù)據(jù)類型,不可或缺
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ù)類型,不可或缺
da.variables['u'][:]=data #填充數(shù)據(jù)
da.close()
write_to_nc_wanmei(one,'D://new1.nc')
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python類中super()和__init__()的區(qū)別
這篇文章主要介紹了python類中super()和__init__()的區(qū)別,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2016-10-10
Python構(gòu)建機(jī)器學(xué)習(xí)API服務(wù)的操作過程
這篇文章主要介紹了Python構(gòu)建機(jī)器學(xué)習(xí)API服務(wù)的操作過程,通過本文的指導(dǎo),讀者可以學(xué)習(xí)如何使用Python構(gòu)建機(jī)器學(xué)習(xí)模型的API服務(wù),并了解到在實際應(yīng)用中需要考慮的一些關(guān)鍵問題和解決方案,從而為自己的項目提供更好的支持和服務(wù),需要的朋友可以參考下2024-04-04
詳解如何用Flask中的Blueprints構(gòu)建大型Web應(yīng)用
Blueprints是Flask中的一種模式,用于將應(yīng)用程序分解為可重用的模塊,這篇文章主要為大家詳細(xì)介紹了如何使用Blueprints構(gòu)建大型Web應(yīng)用,需要的可以參考下2024-03-03

