wrf生成文件查看,安装:
sudo apt install ncview
安装后可以用ncview或ncdump -h命令查看生成的wrfout文件
1.ncl后处理方式
本内容为利用ncl绘图,也可以通过matlab和Arcgis绘图nc文件
1.1安装后处理ncl软件
(1)利用miniconda安装ncl(官方推荐,不受linux影响)
参考:https://blog.csdn.net/weixin_47288291/article/details/136758601
① 安装miniconda
# 更新系统包
sudo apt update
sudo apt upgrade
# 下载miniconda
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
#授权
chmod +x Miniconda3-latest-Linux-x86_64.sh
#安装,一直回车,yes
./Miniconda3-latest-Linux-x86_64.sh
vi ~/.bashrc
#添加以下代码
export PATH=/home/ubuntu(用户名)/miniconda3/bin:$PATH
#运行环境
source ~/.bashrc
#检查安装
which conda
conda --version
如果出现:cannot verify repo.anaconda.com's certificate, issued by ‘CN=E1,O=Let's Encrypt,C=US’: Unable to locally verify the issuer's authority.问题
输入:wget --no-check-certificate https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh,绕过安全证书
② 安装ncl
# 更新conda包
conda update -n root --all
# 创建名为:ncl_stable 环境 并安装ncl
conda create -n ncl_stable -c conda-forge ncl
# 激活环境
source activate ncl_stable
③测试ncl
#测试1
ncl -V
# 测试2
ncl $NCARG_ROOT/lib/ncarg/nclex/gsun/gsun02n.ncl
# 测试3
ncl $NCARG_ROOT/lib/ncarg/nclex/nug/NUG_multi_timeseries.ncl
④linux每次使用ncl 都要进行如下操作
#激活环境
source activate ncl_stable
#退出环境
conda deactivate
(2)普通安装ncl(受linux影响)
参考官网:https://www.ncl.ucar.edu/Download/install_from_binary.shtml
在官网http://www.ncl.ucar.edu/Download/下载
我选择的版本是:
ncl_ncarg-6.6.2-Debian7.11_64bit_nodap_gnu472.tar.gz
把下载好的ncl压缩包放到文件夹中,进入文件夹:
sudo apt install ncl-ncarg #安装ncl-ncarg
tar -zxvf ncl_ncarg-6.6.2-Debian7.11_64bit_nodap_gnu472.tar.gz #解压ncl
vi ~/.bashrc #设置环境变量
#在最后一行添加以下代码
export NCARG_ROOT=/home/wrf/DATA/app/ncl #ncl软件所在目录
export PATH=$NCARG_ROOT/bin:$PATH
NCARG_ROOT=/home/wrf/DATA/app/ncl #ncl软件所在目录
PATH=$NCARG_ROOT/bin:$PATH
export NCARG_ROOT
export PATH
source ~/.bashrc
安装依赖库 ,如果是ubuntu18.04及以下版本,输入以下代码
sudo apt-get install libgfortran3 #安装所需依赖
如果是ubuntu22以上版本,输入以下代码
sudo vim /etc/apt/sources.list #加入安装源
deb http://gb.archive.ubuntu.com/ubuntu/ bionic main universe
sudo apt update #更新源,将会报错,记住报错的数字序列,如果没报错直接安装g++-6
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32 #这个数字序列根据你的报错填写
sudo apt update #将不会报错
sudo apt-get install g++-6 #安装g++-6
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 6 #把g++默认版本换成g++-6
sudo apt-get install libgfortran3 #安装依赖
输入ncview可以查看wrfout输出文件的各参数情况(需要单独安装)
Ubuntu 20.04 系统如何优雅地安装NCL?_ncl数据处理安装-CSDN博客
1.2ncl脚本来源
点击https://www2.mmm.ucar.edu/wrf/OnLineTutorial/Graphics/NCL/NCL_examples.php跳转ncl的WRF脚本示例页面
我选择surface 1来绘制地表温度图片,点进去复制其脚本到WRF的.ncl后缀的文件中(大家根据需要可以选择不同的示例)
1.3.修改脚本
替换文件来源,文件输出格式
色卡变化设置
2.Arcgis处理方式
通过“创建netcdf要素图层”命令处理wrfout文件
以下图片以2m气温为例
3.WRF数据验证
利用ncl输出wrfout文件的站点的数据,将该数据与气象站点的数据进行验证分析
3.1 ncl批量导出多个wrfout文件的多个站点数据
以下数据为批量导出多个wrfout文件(建议为一个wrf文件)的多个站点(2个站点及以上)的温度数据
;----------------------------------------------------------------------
; write_csv_5.ncl
;
; Concepts illustrated:
; - Writing a CSV file with a header using write_table
; - Appending data of mixed types to a CSV file inside a loop
; - Writing select WRF-ARW data to a CSV file
;----------------------------------------------------------------------
; This example calculates temperature at 2m from a WRF-ARW output
; file, and writes a subset of the data based on an array of
; lat / lon values.
;----------------------------------------------------------------------
; These files are loaded by default in NCL V6.4.0 and newer
; load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
; load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
; load "$NCARG_ROOT/lib/ncarg/nclscripts/wrf/WRFUserARW.ncl"
begin
dir = "./"
files = systemfunc (" ls -1 " + dir + "wrfout_d03* ")
a = addfiles(files+".nc","r")
times = wrf_user_list_times(a) ; "2008-09-29_18:30:00", etc
tk2 = wrf_user_getvar(a,"T2",-1) ; T2 in Kelvin
times = wrf_user_list_times(a)
ntimes = dimsizes(times)
print("ntimes = " + ntimes)
;---Calculate i,j locations of data closest to set of lat/lon points
lats = (/ 30.4, 30.35, 30.38/)
lons = (/114.13,114.17,114.23/)
nlatlon = dimsizes(lats)
loc = wrf_user_ll_to_xy(a, lons, lats, True) ; 2 x nlatnlon
; loc = wrf_user_ll_to_ij(a, lons, lats, True) ; 2 x nlatnlon
; loc = loc - 1 ; wrf_user_ll_to_ij returns 1-based indexes
;---Set up CSV file and header information for the file
csv_filename = "wrf_2m_temperature.csv"
system("rm -f " + csv_filename) ; Remove file in case it exists.
fields = (/"TIME", "LAT", "LON", "TEMPERATURE (degC)"/)
;---Create a header line for CSV file
dq = str_get_dq()
fields = dq + fields + dq ; Pre/append quotes to field names
header = [/str_join(fields,",")/] ; Header is field names separated
; by commas.
;
; Format to use for writing each variable to CSV file.
; If you don't want spaces in CSV file, use the following
; format string:
; format = "%s,%g,%g,%g"
;
format = "%s,%6.2f,%7.2f,%6.2f"
;
; Loop through each time step and desired list of lat/lon values,
; and write a single line of data to CSV file.
;
write_table(csv_filename, "w", header, "%s") ; Write header to CSV file.
do it = 0,ntimes-1
do nl = 0,nlatlon-1
nln = loc(0,nl)
nlt = loc(1,nl)
lat1 = a[0]->XLAT(0,nlt,nln) ; nearest grid point
lon1 = a[0]->XLONG(0,nlt,nln)
alist = [/times(it),lat1,lon1,tk2(it,nlt,nln)/] ; Store data to be written in a list.
write_table(csv_filename, "a", alist, format) ; Write list to CSV file.
end do
end do
end
3.2 python:数据透视表
利用python将ncl导出的csv数据进行“数据透视表“操作,也可以在excel中实现此目标,以下代码的具体功能:①在所给路径寻找csv文件;②将csv中的时间从UTC变成东八区时间;③使用数据透视表将温度值与经纬度对应
import pandas as pd
import os
# 指定目录路径,这里假设您的目录是J:\工作室\大论文\WRF\
directory_path = r'J:/工作室/大论文/WRF/'
# 列出目录下所有的CSV文件
csv_files = [f for f in os.listdir(directory_path) if f.endswith('.csv')]
# 遍历所有的CSV文件
for csv_file in csv_files:
file_path = os.path.join(directory_path, csv_file) # 获取完整的文件路径
# 读取CSV文件
df = pd.read_csv(file_path)
# 指定日期时间格式,并转换为datetime类型
df['TIME'] = pd.to_datetime(df['TIME'], format='%Y-%m-%d_%H:%M:%S')
# 将时间转换为北京时间(东八区)
df['TIME'] = df['TIME'].dt.tz_localize('UTC').dt.tz_convert('Asia/Shanghai')
# 将摄氏度转换为开尔文(Kelvin)
df.iloc[:, 3] = df.iloc[:, 3] - 273.15 # 假设第四列是TEMPERATURE (degC)
# 格式化时间列为所需的格式
df['TIME'] = df['TIME'].dt.strftime('%Y%m%d%H')
# 使用pivot_table创建数据透视表
pivot_table = df.pivot_table(values=df.columns[3], index=df['TIME'], columns=df.columns[1], aggfunc='mean')
# 定义输出文件名,基于原始文件名
output_file = csv_file.replace('.csv', '_pivot.csv')
output_file_path = os.path.join(directory_path, output_file)
# 将数据透视表保存到CSV文件
pivot_table.to_csv(output_file_path)
4.后处理报错
4.1站点的经度和纬度超出研究域的范围
fatal:syntax error: line 65 in file test1.ncl before or near [ lat1 = a[0]->XLAT[ ------------------------^ fatal:syntax error: line 66 in file test1.ncl before or near [ lon1 = a[0]->XLONG[
解决:去掉超出范围的站点