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)文章
Autopep8的使用(python自動(dòng)編排工具)
這篇文章主要介紹了Autopep8的使用(python自動(dòng)編排工具),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Python中讀取文件名中的數(shù)字的實(shí)例詳解
在本篇文章里小編給大家整理了一篇關(guān)于Python中讀取文件名中的數(shù)字的實(shí)例詳解內(nèi)容,有興趣的朋友們可以參考下。2020-12-12
python類(lèi)中super()和__init__()的區(qū)別
這篇文章主要介紹了python類(lèi)中super()和__init__()的區(qū)別,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
Python構(gòu)建機(jī)器學(xué)習(xí)API服務(wù)的操作過(guò)程
這篇文章主要介紹了Python構(gòu)建機(jī)器學(xué)習(xí)API服務(wù)的操作過(guò)程,通過(guò)本文的指導(dǎo),讀者可以學(xué)習(xí)如何使用Python構(gòu)建機(jī)器學(xué)習(xí)模型的API服務(wù),并了解到在實(shí)際應(yīng)用中需要考慮的一些關(guān)鍵問(wèn)題和解決方案,從而為自己的項(xiàng)目提供更好的支持和服務(wù),需要的朋友可以參考下2024-04-04
Python爬蟲(chóng)框架NewSpaper使用詳解
這篇文章主要為大家介紹了Python爬蟲(chóng)框架NewSpaper使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
基于Python實(shí)現(xiàn)自動(dòng)摳圖小程序
這篇文章主要為了大家利用用Python制作一款界面化的摳圖小程序,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,感興趣的可以學(xué)習(xí)一下2022-01-01
詳解如何用Flask中的Blueprints構(gòu)建大型Web應(yīng)用
Blueprints是Flask中的一種模式,用于將應(yīng)用程序分解為可重用的模塊,這篇文章主要為大家詳細(xì)介紹了如何使用Blueprints構(gòu)建大型Web應(yīng)用,需要的可以參考下2024-03-03
Python中使用Inotify監(jiān)控文件實(shí)例
這篇文章主要介紹了Python中使用Inotify監(jiān)控文件實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-02-02

