1. 程式人生 > >Python安裝及netcdf資料讀寫

Python安裝及netcdf資料讀寫

 

一、在CentOS7系統上安裝Python3

在anaconda官網下載(http://https://www.anaconda.com/download/#linux)(Anaconda指的是一個開源的Python發行版本,是Python的包管理器和環境管理器)

下載linux安裝包:Anaconda3-5.2.0-Linux-x86_64.sh,上傳到伺服器

執行“sh Anaconda3-5.2.0-Linux-x86_64.sh”進行安裝

輸入“yes”,進行許可證確認

輸入“本地安裝路徑”,執行普通使用者目錄下安裝(注意安裝完成後,不要再更改anaconda路徑了)

注意“同意安裝程式在bashrc中新增環境變數”,安裝完成後source ~/.bashrc

(export PATH="/home/xxx/usr/local/anaconda3/bin:$PATH")

輸入“python3”測試成功與否

二、配置vim的Python3支援

通過設定~/.vimrc和在~/.vim新增外掛,來實現有程式碼高亮、程式碼自動補全(pydiction-1.2.3)、程式碼自動提示(autocomplpop.vim)等,使vim接近IDE

三、Python3讀取netcdf資料,並簡單畫圖

 

1、新增conda清華源,加快安裝速度

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

conda config --set show_channel_urls true

 

2、更新conda本身和自帶庫(前提:伺服器可以連線外網)

conda update -n base conda

conda upgrade --all

 

3、安裝netCDF4、basemap庫

分別conda install netCDF4、conda install basemap即可

分別import netCDF4 as nc和from mpl_toolkits.basemap import Basemap不報錯即可

 

4、錯誤undefined symbol: omp_get_num_procs

conda install mkl-rt

 

5、錯誤Qt: Could not determine keyboard configuration data from X server

在bashrc中新增

export XKB_DEFAULT_RULES=base

export QT_XKB_CONFIG_ROOT=/usr/share/X11/xkb

 

6、錯誤Qt: XKEYBOARD extension not present on the X server

設定xmange中的Xconfig->Default Profile->Properties->Advanced->X Extensions-> check the 'XKEYBOARD'(勾選)

 

7、錯誤QXcbConnection: Failed to initialize XRandr(彎路,請跳過)

(Qt5:Qt是諾基亞公司的C++視覺化開發平臺,最新版本為Qt5;XRandr:設定螢幕顯示的軟體包)

a、執行qtcreator,發現伺服器本身就沒有安裝qt5和qt,因此先進行安裝

感謝參考:CentOS7安裝EPEL源(https://blog.csdn.net/qq_19674905/article/details/78224125

感謝參考:配置國內y和e源(http://https://www.cnblogs.com/renpingsheng/p/7845096.html

配置yum國內源:

cd /etc/yum.repos.d/; mkdir repo_bak; mv *.repo repo_bak/

wget http://mirrors.aliyun.com/repo/Centos-7.repo

wget http://mirrors.163.com/.help/CentOS7-Base-163.repo

yum clean all && yum makecache

安裝epel並配置epel國內源:

yum -y install epel-release

wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo

yum clean all && yum makecache

檢視所有可用源和所有源(可用源和禁用源):

yum repolist enabled

yum repolist all

安裝qt-creator和qt:

yum install qt-creator

yum install qt

b、再次執行qtcreator,報錯:libGL error: No matching fbConfigs or visuals found

暫時終止排錯,因為雖然有錯誤,但是不影響簡單畫圖

8、python讀取nc資料,簡單畫圖程式

from mpl_toolkits.basemap import Basemap  # import Basemap
import numpy as np  
import matplotlib.pyplot as plt  
from netCDF4 import Dataset  # netcdf4-python module  
  
# load in the netCDF4 file  
nc = Dataset('mdbz.nc')  
  
# read the variables from the netCDF4 file and assign 
mdbz = nc.variables['mdbz'][:,:]  
lat = nc.variables['lat'][:]  
lon = nc.variables['lon'][:]  
  
# set the plotting var for 2d shaded figure,set the colormap
plt.contourf(mdbz, cmap='Accent')  
  
# show and save the figure  
plt.savefig('mdbz.png')  
plt.show()  

四、Python3讀取netcdf資料,並輸出到新的netcdf資料

print(type(mdbz)) 檢視變數型別

print(mdbz.shape[0]) 檢視變數第0維大小

print(id(mdbz)) 檢視變數記憶體地址

lat[:] = lats[:] 正確賦值;lat = lats 錯誤賦值

from netCDF4 import Dataset  # netcdf4-python module  
from scipy.io import netcdf  
  
  
# load in the netCDF4 file  
nc = Dataset('mdbz.nc')  
  
# read the variables from the netCDF4 file and assign 
mdbz = nc.variables['mdbz'][:,:]  
lats = nc.variables['lat'][:]  
lons = nc.variables['lon'][:]  
  
# output to the new netCDF4 file  
f = netcdf.netcdf_file('mdbz_new.nc', 'w')  
f.createDimension('lat', mdbz.shape[0])  
f.createDimension('lon', mdbz.shape[1])  
reflectivity = f.createVariable('reflectivity', 'f8', ('lat', 'lon'))  
lat = f.createVariable('lat', 'f8', ('lat', ))  
lon = f.createVariable('lon', 'f8', ('lon', ))  
reflectivity[:,:] = mdbz[:,:]  
lat[:] = lats[:]  
lon[:] = lons[:]  
lat.units = "degrees_north"  
lon.units = "degrees_east"  
reflectivity.units = "dBZ"  
f.close()