Shell脚本的应用

工作中用到的脚本,用来实现WRF气象模式的预测。首先使用脚本下载天气数据。

#!/bin/bash
# FTP_SERVER=192.168.10.241
FILECONFIG=/home/NWP/RIXINWRF/source/file.down
DOWNCONFIG=/home/NWP/RIXINWRF/download/download.config
FTP_SERVER=`cat $DOWNCONFIG | grep '^server' | awk -F= '{print $2}'`
FTP_USER=`cat $DOWNCONFIG | grep '^user' | awk -F= '{print $2}'`
FTP_PASS=`cat $DOWNCONFIG | grep '^passwd' | awk -F= '{print $2}'`
FTP_DIR=`cat $DOWNCONFIG | grep '^fdir' | awk -F= '{print $2}'`
FTP_HOUR=`cat $DOWNCONFIG | grep '^hour' | awk -F= '{print $2}'`
FTP_TIMES=`echo $FTP_HOUR | awk -F, '{print NF}'`

while true
do
for y in `seq 1 $FTP_TIMES`
do
THISHOUR=`echo $FTP_HOUR | awk -F, -v fly=$y '{print $fly}'`
#THISHOUR=12
FTP_DDIR=gfs.`date -d '-1day' +%Y%m%d`$THISHOUR
LOC_DIR=`cat $DOWNCONFIG | grep '^ldir' | awk -F= '{print $2}'`
LOC_DDIR=`date -d '-1day' +%Y%m%d`$THISHOUR
DOW_DAYS=`cat $DOWNCONFIG | grep '^days' | awk -F= '{print $2}'`

#del tmp.sh
rm -rf tmp.sh


if [ ! -x "$LOC_DIR/$LOC_DDIR" ]; then
	echo "【`date +%Y-%m-%d_%H:%M:%S`】新建目录$LOC_DIR/$LOC_DDIR"
	mkdir "$LOC_DIR/$LOC_DDIR"
else
	echo "【`date +%Y-%m-%d_%H:%M:%S`】目录已经存在,检查GFS文件是否已经下载"
	num=`ls $LOC_DIR/$LOC_DDIR -l | wc -l`
	if [ $num -ge $(($DOW_DAYS*4)) ]; then
		echo "【`date +%Y-%m-%d_%H:%M:%S`】目录$LOC_DIR/$LOC_DDIR下面的GFS文件已经下载,不需要再下载"
		continue
	fi
fi

echo "【`date +%Y-%m-%d_%H:%M:%S`】下载GFS$FTP_DDIR文件开始......"

cat >> tmp.sh <<HEAD
ftp -n -v << Fly 2>&1
open $FTP_SERVER
user $FTP_USER $FTP_PASS
cd ${FTP_DIR}${FTP_DDIR}
lcd $LOC_DIR/$LOC_DDIR
tick 1024
prompt off
HEAD

for x in `seq 0 $(($DOW_DAYS*4))`;
do
#	echo "开始下载第$x个文件"
	day=$(($x*6))
	if [ $day -lt 10 ]
	then
		day=0$day
	fi
	#all_gfs[x]=gfs.t${FTP_HOUR}z.pgrbf${day}.grib2
	#echo ${all_gfs[@]}
	echo "mget gfs.t${THISHOUR}z.pgrbf${day}.grib2" >> tmp.sh
#	echo "第$x个文件下载完成"
done

cat >> tmp.sh <<END
close
bye
Fly
END

if [ $y -eq 1 ];then
	#update download file 
	sed -i "/f_down/ s/=.*/= 0/g" $FILECONFIG
	sed -i "/f_start/ s/=.*/= `date +'%Y-%m-%d %H:%M:%S'`/g" $FILECONFIG
	echo "【`date +%Y-%m-%d_%H:%M:%S`】开始下载GFS文件"
	sh tmp.sh && echo "【`date +%Y-%m-%d_%H:%M:%S`】下载$FTP_DDIR文件结束."
	rm -rf tmp.sh

	#Update download file info
	sed -i "/f_down/ s/=.*/= 1/g" $FILECONFIG
	sed -i "/f_end/ s/=.*/= `date +'%Y-%m-%d %H:%M:%S'`/g" $FILECONFIG
	sed -i "/f_file/ s/=.*/= $LOC_DDIR/g" $FILECONFIG
	sed -i "/f_fname/ s/=.*/= `date -d '-1day' +'%Y-%m-%d'`_${THISHOUR}/g" $FILECONFIG
else
	#update download file 
	sed -i "/s_down/ s/=.*/= 0/g" $FILECONFIG
	sed -i "/s_start/ s/=.*/= `date +'%Y-%m-%d %H:%M:%S'`/g" $FILECONFIG
	echo "【`date +%Y-%m-%d_%H:%M:%S`】开始下载GFS文件"
	sh tmp.sh && echo "【`date +%Y-%m-%d_%H:%M:%S`】下载$FTP_DDIR文件结束."
	rm -rf tmp.sh

	#Update download file info
	sed -i "/s_down/ s/=.*/= 1/g" $FILECONFIG
	sed -i "/s_end/ s/=.*/= `date +'%Y-%m-%d %H:%M:%S'`/g" $FILECONFIG
	sed -i "/s_file/ s/=.*/= $LOC_DDIR/g" $FILECONFIG
	sed -i "/s_fname/ s/=.*/= `date -d '-1day' +'%Y-%m-%d'`_${THISHOUR}/g" $FILECONFIG	
fi

done
echo "【`date +%Y-%m-%d_%H:%M:%S`】暂停30分钟后,重新下载文件......"
sleep 30m
done
这里面有两个重要的配置文件,是我为了用来配置下载时间和文件夹的,如下

第一个是file.down

f_down		= 1
f_start		= 2013-05-11 10:32:10
f_end		= 2013-05-11 10:50:48
f_file		= 2013051018
f_fname		= 2013-05-10_18

s_down		= 0
s_start		= 20130505 18:00:00
s_end		= 
s_file		= 2013050518
s_fname		= 2013-05-05_18
接着是down.config

days	=8
hour	=12,18
server	=ftpprd.ncep.noaa.gov
#server	=192.168.10.241
user	=anonymous
#user	=fly
passwd	[email protected]
#passwd	=sprixin2012
fdir	=pub/data/nccf/com/gfs/prod/
#fdir	=FTP
ldir	=/home/NWP/RIXINWRF/source
下载完成后,接着运行预测脚本nwp.sh

#!/bin/bash
HOMEDIR=/home/NWP/RIXINWRF
WRFDIR=${HOMEDIR}/WRF1
#下载的配置文件
SOURCE=${HOMEDIR}/source
CONFIG=${SOURCE}/file.down
#要计算的天数
COMPUTEDAY=7
DATEDIR=`date +%Y%m%d%H`
YEAR=`date +%Y%m%d`
HOUR=`date +%H`
NAME=`date +%H-%m-%d_%H`
DOWN=0
E_NOTROOT=67

#check download nwp file
#检查气象数据文件是否在下载
checkFile(){
	echo "【`date +%Y-%m-%d_%H:%M:%S`】检查下载文件"
	s_down=`cat $CONFIG | grep "s_down" |awk -F'= ' '{print$2}'`
	if [ $s_down -ne 0 ]
	then
		DATEDIR=`cat $CONFIG | grep "s_file" |awk -F'= ' '{print$2}'`
		YEAR=`cat $CONFIG | grep "s_file" |awk -F'= ' '{print$2}' | sed 's/ //g' | cut -c1-8`
		HOUR=`cat $CONFIG | grep "s_file" |awk -F'= ' '{print$2}' | sed 's/ //g' | cut -c9-10`
		NAME=`cat $CONFIG | grep "s_fname" |awk -F'= ' '{print$2}' | sed 's/ //g'`
		DOWN=2
		echo "【`date +%Y-%m-%d_%H:%M:%S`】下载的文件目录为:$DATEDIR"
	else
		f_down=`cat $CONFIG | grep "f_down" |awk -F'= ' '{print$2}'`
		if [ $f_down -ne 0 ]
		then
			DATEDIR=`cat $CONFIG | grep "f_file" |awk -F'= ' '{print$2}'`
			YEAR=`cat $CONFIG | grep "f_file" |awk -F'= ' '{print$2}' | sed 's/ //g' | cut -c1-8`
			HOUR=`cat $CONFIG | grep "f_file" |awk -F'= ' '{print$2}' | sed 's/ //g' | cut -c9-10`
			NAME=`cat $CONFIG | grep "f_fname" |awk -F'= ' '{print$2}' | sed 's/ //g'`
			DOWN=1
			echo "【`date +%Y-%m-%d_%H:%M:%S`】下载的文件目录为:$DATEDIR"
		else
			echo '【`date +%Y-%m-%d_%H:%M:%S`】文件未下载成功!等待下载中...'
			sleep 1m
			checkFile
		fi
	fi
}

#config namelist.wps file
#配置操作文件
MAX_DOM=4
#HOUR=00
CONFIGFILE=${WRFDIR}/WPS/namelist.wps
config(){
	printf "【`date +%Y-%m-%d_%H:%M:%S`】配置【namelist.wps】开始************************************\n"
	START=\'`date -d "$YEAR" +%F`_${HOUR}:00:00\'
	END=\'`date -d "$YEAR +${COMPUTEDAY}day" +%F`_${HOUR}:00:00\'
	#echo $START $END
	sed -i "/max_dom/ s/=.*,/= $MAX_DOM,/g" $CONFIGFILE
	sed -i "/start_date/ s/=.*,/= $START, $START, $START, $START,/g" $CONFIGFILE
	sed -i "/end_date/ s/=.*,/= $END, $END, $END, $END,/g" $CONFIGFILE
	printf "【`date +%Y-%m-%d_%H:%M:%S`】配置【namelist.wps】完毕!**********************************\n"
}

#update namelist.input
INPUTFILE=${WRFDIR}/WRFV3/test/em_real/namelist.input
#如果计算三天的话,运行时间设置为3*24小时
#如需其他参数设置,在这个地方初始化,然后在下面用sed命令更新
RUN_HOURS=$((${COMPUTEDAY}*24))
updateInput(){
	printf "【`date +%Y-%m-%d_%H:%M:%S`】更新【namelist.input】开始************************************\n"
	START_YEAR=`date -d "$YEAR" +%Y`
	START_MONTH=`date -d "$YEAR" +%m`
	START_DAY=`date -d "$YEAR" +%d`
	# START_MINUTE=`date +%M`
	# START_HOUR=`date +%H`
	# START_SECOND=`date +%S`
	START_HOUR=$HOUR
	START_MINUTE=00	
	START_SECOND=00
	END_YEAR=`date -d "$YEAR +${RUN_HOURS}hour" +%Y`
	END_MONTH=`date -d "$YEAR +${RUN_HOURS}hour" +%m`
	END_DAY=`date -d "$YEAR +${RUN_HOURS}hour" +%d`
	# END_HOUR=`date -d "+${RUN_HOURS}hour" +%H`
	# END_MINUTE=`date -d "+${RUN_HOURS}hour" +%M`
	# END_SECOND=`date -d "+${RUN_HOURS}hour" +%S`
	# END_HOUR=$HOUR
	END_HOUR=$HOUR
	END_MINUTE=00
	END_SECOND=00
	sed -i "/run_hours/ s/=.*/= $RUN_HOURS,/g" $INPUTFILE
	sed -i "/start_year/ s/=.*/= $START_YEAR,$START_YEAR,$START_YEAR,$START_YEAR,/g" $INPUTFILE
	sed -i "/start_month/ s/=.*/= $START_MONTH,$START_MONTH,$START_MONTH,$START_MONTH,/g" $INPUTFILE
	sed -i "/start_day/ s/=.*/= $START_DAY,$START_DAY,$START_DAY,$START_DAY,/g" $INPUTFILE
	sed -i "/start_hour/ s/=.*/= $START_HOUR,$START_HOUR,$START_HOUR,$START_HOUR,/g" $INPUTFILE
	sed -i "/start_minute/ s/=.*/= $START_MINUTE,$START_MINUTE,$START_MINUTE,$START_MINUTE,/g" $INPUTFILE
	sed -i "/start_second/ s/=.*/= $START_SECOND,$START_SECOND,$START_SECOND,$START_SECOND,/g" $INPUTFILE
	sed -i "/end_year/ s/=.*/= $END_YEAR,$END_YEAR,$END_YEAR,$END_YEAR,/g" $INPUTFILE
	sed -i "/end_month/ s/=.*/= $END_MONTH,$END_MONTH,$END_MONTH,$END_MONTH,/g" $INPUTFILE
	sed -i "/end_day/ s/=.*/= $END_DAY,$END_DAY,$END_DAY,$END_DAY,/g" $INPUTFILE
	sed -i "/end_hour/ s/=.*/= $END_HOUR,$END_HOUR,$END_HOUR,$END_HOUR,/g" $INPUTFILE
	sed -i "/end_minute/ s/=.*/= $END_MINUTE,$END_MINUTE,$END_MINUTE,$END_MINUTE,/g" $INPUTFILE
	sed -i "/end_second/ s/=.*/= $END_SECOND,$END_SECOND,$END_SECOND,$END_SECOND,/g" $INPUTFILE
	printf "【`date +%Y-%m-%d_%H:%M:%S`】更新【namelist.input】结束************************************\n"
}

#Link Grib
WPSDIR=${WRFDIR}/WPS
#Grid
grid(){
	cd $WPSDIR || echo "【`date +%Y-%m-%d_%H:%M:%S`】目录错误,无法打开!"
	printf "【`date +%Y-%m-%d_%H:%M:%S`】【GeoGrib.exe】开始************************************\n"
	cd $WPSDIR && ./geogrid.exe
	#Link 文件
	printf "【`date +%Y-%m-%d_%H:%M:%S`】【Link WPS】开始************************************\n"
	cd $WPSDIR || echo "【`date +%Y-%m-%d_%H:%M:%S`】目录$WPSDIR不存在,无法打开!"
	cd $WPSDIR && ./link_grib.csh $SOURCE/$DATEDIR/gfs.t${HOUR}z.pgrbf* ./ && echo "Link_Grid GFS文件成功"
	printf "【`date +%Y-%m-%d_%H:%M:%S`】【Link WPS】结束************************************\n"
	printf "【`date +%Y-%m-%d_%H:%M:%S`】【UnGrib.exe】开始************************************\n"
	cd $WPSDIR && ./ungrib.exe
	printf "【`date +%Y-%m-%d_%H:%M:%S`】【MetGrid.exe】开始************************************\n"
	cd $WPSDIR && ./metgrid.exe
	printf "【`date +%Y-%m-%d_%H:%M:%S`】【Met WPS】结束************************************\n"
}

#Real Data
REALDIR=${WRFDIR}/WRFV3/test/em_real/
realAndWrf(){
	printf "【`date +%Y-%m-%d_%H:%M:%S`】【Real WPS】开始,当前时间:`date +%Y-%m-%d_%H:%M:%S`************************************\n"
	cd $REALDIR && ln -s $WPSDIR/met_em.d* ./
	cd $REALDIR && ./real.exe
	mpiexec -n 6 ./wrf.exe
	printf "【`date +%Y-%m-%d_%H:%M:%S`】【Real WPS】结束,当前时间:`date +%Y-%m-%d_%H:%M:%S`************************************\n"
}

#Analysis Data And BackUp解析数据
WRFOUTDIR=${HOMEDIR}/wrfout
MAPCONFIG=${WRFOUTDIR}/mapping.config
ANACONFIG=${WRFOUTDIR}/analysis.config
SIZE=`cat $MAPCONFIG | grep size | awk -F'= ' '{print $2}'`
NCNAME=`cat $MAPCONFIG | grep ncname | awk -F'= ' '{print $2}'`
TXTNAME=`cat $MAPCONFIG | grep txtname | awk -F'= ' '{print $2}'`
analysisData(){
	s=$(($SIZE+2))
	printf "【`date +%Y-%m-%d_%H:%M:%S`】NCL解析数据开始!************************************\n"
	for i in $(seq 3 $s)
	do
		#首先把数据移动到tmp临时目录,然后在backup目录下面备份
		echo "【`date +%Y-%m-%d_%H:%M:%S`】${WRFDIR}/WRFV3/test/em_real/wrfout_d0${i}_${NAME}*"
		if [ -f ${WRFDIR}/WRFV3/test/em_real/wrfout_d0${i}_${NAME}* ]
		then
			echo "【`date +%Y-%m-%d_%H:%M:%S`】`cd ${WRFDIR}/WRFV3/test/em_real/ && ls wrfout_d0${i}_${NAME}* -l`"
			mv ${WRFDIR}/WRFV3/test/em_real/wrfout_d0${i}_${NAME}* ${HOMEDIR}/tmp/ && echo "文件wrfout_do${i}_${NAME}*移动成功!"
		else
			echo "【`date +%Y-%m-%d_%H:%M:%S`】文件wrfout_d0${i}_${NAME}*不存在,可能已经移动到tmp目录下面!"
		fi
		#引入外部变量,获取电场简写名称
		fname=`echo $TXTNAME | awk -F',' -v fly=$i '{print $fly}'`
		#获取时间
		echo "【`date +%Y-%m-%d_%H:%M:%S`】检查文件${HOMEDIR}/tmp/wrfout_d0${i}_${NAME}*是否已经存,不存在去bacukup目录下查找!"
		if [ -f ${HOMEDIR}/tmp/wrfout_d0${i}_${NAME}* ]
		then
			sname=`cd ${HOMEDIR}/tmp && ls wrfout_d0${i}_${NAME}* | sed 's/-//g' | sed 's/ //g' | sed 's/://g' |sed 's/_//g' | cut -c10-19`
			#文件备份与重命名
			echo "【`date +%Y-%m-%d_%H:%M:%S`】复制${HOMEDIR}/tmp/wrfout_d0${i}_${NAME}*到${HOMEDIR}/backup/目录下"
			cp ${HOMEDIR}/tmp/wrfout_d0${i}_${NAME}* ${HOMEDIR}/backup/
			echo "【`date +%Y-%m-%d_%H:%M:%S`】重命名${HOMEDIR}/tmp/wrfout_d0${i}_${NAME}*为${HOMEDIR}/tmp/${fname}${sname}.nc"
			mv ${HOMEDIR}/tmp/wrfout_d0${i}_${NAME}* ${HOMEDIR}/tmp/${fname}${sname}.nc
		else
			if [ -f ${HOMEDIR}/backup/wrfout_d0${i}_${NAME}* ]
			then
				echo "【`date +%Y-%m-%d_%H:%M:%S`】在bacukup中发现wrfout_d0${i}_${NAME}*,开始解析..."
				cp  ${HOMEDIR}/backup/wrfout_d0${i}_${NAME}* ${HOMEDIR}/tmp/
				sname=`cd ${HOMEDIR}/tmp && ls wrfout_d0${i}_${NAME}* | sed 's/-//g' | sed 's/ //g' | sed 's/://g' |sed 's/_//g' | cut -c10-19`
				mv ${HOMEDIR}/tmp/wrfout_d0${i}_${NAME}* ${HOMEDIR}/tmp/${fname}${sname}.nc
			else
				echo "【`date +%Y-%m-%d_%H:%M:%S`】文件wrfout_d0${i}_${NAME}*不存在,可能没有解析完成,等待解析中..."
				sleep 2m
				#重新开始解析
				analysisData
			fi
		fi
		sed -i "/wrfoutname/ s/=.*/= ${fname}${sname}.nc/g" $ANACONFIG
		sed -i "/txtpath/ s/=.*/= ${fname}${sname}.txt/g" $ANACONFIG
		sed -i "/start/ s/=.*/= $sname/g" $ANACONFIG
		sed -i "/tt/ s/=.*/= $[RUN_HOURS*4+1]/g" $ANACONFIG
		cd ${HOMEDIR}/wrfout && ncl try.ncl
		cd ${HOMEDIR}/tmp && rm -rf *
		catToWpd && echo "【`date +%Y-%m-%d_%H:%M:%S`】生成WPD文件成功!************************************\n"
		if [ $DOWN -eq 2 ]
		then
			sed -i "/s_down/ s/=.*/= 0/g" $CONFIG && echo "【`date +%Y-%m-%d_%H:%M:%S`】重置$CONFIG中文件下载属性s_down************************************\n"
		fi
		if [ $DOWN -eq 1 ]
		then
			sed -i "/f_down/ s/=.*/= 0/g" $CONFIG && echo "【`date +%Y-%m-%d_%H:%M:%S`】重置$CONFIG中文件下载属性f_down************************************\n"
		fi
	done
	printf "【`date +%Y-%m-%d_%H:%M:%S`】NCL解析数据结束!************************************\n"
}

#解析成wpd数据
catToWpd(){
	WPDDIR=${HOMEDIR}/wpd
	LAT=`cat $MAPCONFIG | grep clat | awk -F'= ' '{print $2}'`
	LON=`cat $MAPCONFIG | grep clon | awk -F'= ' '{print $2}'`
	HIGH=`cat $MAPCONFIG | grep high | awk -F'= ' '{print $2}'`
	TIME=`cat $CONFIG | grep "f_file" |awk -F'= ' '{print$2}' | sed 's/ //g'`
	#TIME=2013050712
	s=$(($SIZE+2))
	for i in $(seq 3 $s)
	do
        	fname=`echo $TXTNAME | awk -F',' -v fly=$i '{print $fly}'`
        	lat=`echo $LAT | awk -F',' -v fly=$i '{print $fly}'`
        	lon=`echo $LON | awk -F',' -v fly=$i '{print $fly}'`
        	high=`echo $HIGH | awk -F',' -v fly=$i '{print $fly}'`
        	cat $WRFOUTDIR/$fname$TIME.txt | grep "$lat" | grep "$lon" | grep " $high " > $WPDDIR/$fname$TIME.WPD
	done
}

#Delete Data
deleteData(){
	printf "【`date +%Y-%m-%d_%H:%M:%S`】删除数据开始************************************\n"
	rm ${WPSDIR}/FILE:*
	rm ${WPSDIR}/GRIBFILE.*
	rm ${WPSDIR}/met_em.d*
	rm ${WPSDIR}/*.nc
	rm ${WRFDIR}/WRFV3/test/em_real/rsl.*
	rm ${WRFDIR}/WRFV3/test/em_real/met_em.d*
	rm ${WRFDIR}/WRFV3/test/em_real/wrfinput_d*
	rm ${WRFDIR}/WRFV3/test/em_real/wrfrst_d0*
	rm ${WRFDIR}/WRFV3/test/em_real/wrfout_d01*
	rm ${WRFDIR}/WRFV3/test/em_real/wrfout_d02*
	printf "【`date +%Y-%m-%d_%H:%M:%S`】删除数据成功************************************\n"
}

#主程序
ROOT_UID=`cat /etc/passwd | grep '^NWP' | awk -F: '{print $3}'`
MY_ID=`id -u`
#ROOT_UID=0
echo $MY_ID
while true
do
	if [ $MY_ID -ne $ROOT_UID ]
	then
		echo "【`date +%Y-%m-%d_%H:%M:%S`】只有【NWP】用户才有操作权限!"
		exit E_NOTROOT
	else
		echo "【`date +%Y-%m-%d_%H:%M:%S`】数据解析开始!"
	fi
	#Delete 数据
	deleteData
	checkFile
	#配置文件
	config
	#Grid文件
	grid
	#更新输入文件
	updateInput
	#Real文件
	realAndWrf
	#Analysis Data解析文件
	analysisData 
	sleep 12h
done
这里面也会使用几个配置文件,第一个是上面使用的file.down,

然后是mapping.config

size	= 2
ncname	= _,_,WRF,WRF
txtname	= _,_,DJH,HF
rlon	= _,_,114.3863,117.7453
rlat	= _,_,41.1766,42.5437
clon	= _,_,114.3952,117.7477
clat	= _,_,41.1770,42.5304
high	= _,_,50,70
以及analysis.config

wrfoutname	= HF2013051012.nc
gx		= 8
gy		= 8
sx		= 1
sy		= 1
tt		= 673
th		= 4
tn		= 15
start		= 2013051012
txtpath		= HF2013051012.txt
最后就是我们的ncl解析脚本了,如下

load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/wrf/WRFUserARW.ncl"
;载入ncl库
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
begin
	APATH = systemfunc("cat analysis.config | grep 'wrfoutname'|awk -F'=' '{print $2}'|sed 's/ //g'")
	print("../tmp/" + APATH)
	a = addfile("../tmp/" + APATH,"r")
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;读入wrfout.nc文件
;1、当wrf计算完成后,生成的wrfout文件需要cp到存档文件夹以及供ncl解析气象要素的临时文件夹两个地方
;2、cp到ncl解析气象要素的临时文件夹后,相应所需要的wrfout文件需要改名为wrfout_d0*.nc
;3、建议将addfile("./wrfout.nc","r")中的./wrfout.nc作为变量,每次需要的时候到控制文件中读取,相应的语法为APATH =systemfunc("echo $WRF_temp_PATH")
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	T = (wrf_user_getvar(a,"T2",-1) - 273.15) 
	u10 = wrf_user_getvar(a,"U10",-1) 
	v10 = wrf_user_getvar(a,"V10",-1)
	sw = wrf_user_getvar(a,"SWDOWN",-1) 
	sh = wrf_user_getvar(a,"QVAPOR",-1) 
	pr = (wrf_user_getvar(a,"P",-1)+ wrf_user_getvar(a,"PB",-1) )*0.01
	uw = wrf_user_getvar(a,"U",-1) 
	vw = wrf_user_getvar(a,"V",-1)
	st = (wrf_user_getvar(a,"TSK",-1) - 273.15)
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;uw及vw为u及v方向上的风速
;T2为2米高温度
;u10及v10为10米高度上uv方向的风速
;sh为水汽混合比
;pr为大气压
;st为地表温度
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	gx = stringtointeger(systemfunc("cat analysis.config | grep 'gx'|awk -F'=' '{print $2}'|sed 's/ //g'"))
	gy = stringtointeger(systemfunc("cat analysis.config | grep 'gy'|awk -F'=' '{print $2}'|sed 's/ //g'"))
	sx = stringtointeger(systemfunc("cat analysis.config | grep 'sx'|awk -F'=' '{print $2}'|sed 's/ //g'"))
	sy = stringtointeger(systemfunc("cat analysis.config | grep 'sy'|awk -F'=' '{print $2}'|sed 's/ //g'"))
	tt = stringtointeger(systemfunc("cat analysis.config | grep 'tt'|awk -F'=' '{print $2}'|sed 's/ //g'"))
	th = 4
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;gx是wrf的网格格点中我们需要的x方向上的个数,gy我们需要的y方向上的个数
;sx是wrf计算结果中对应我们需要的位置在domain中的x方向起始点所在位置,sy为y方向起始点所在位置
;tt是时间序列上要素的总数量,tt的计算方法为:如果计算时长为72小时,每隔15分钟取一次风速,tt=72*4+1
;th高度序列的总个数
;建议将这些参数做成变量存在控制文件,当每次开始计算之前到控制文件中取出相应的参数,gx = stringtoitteger(systemfunc("echo $gard_number_in_x"))
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	lons = new((/gx/),float)
	do n = sx,gx+sx-1
		lons(n-sx) = n
	end do
	lats = new((/gy/),float)
	do m = sy,gy+sy-1
		lats(m-sy) = m
	end do
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
	res = 0
	loc = wrf_user_ij_to_ll(a,lons,lats,res)
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;将wrf的gard格点转换为经纬度
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	shn = new((/tt,th,gy,gx/),float)
	prn = new((/tt,th,gy,gx/),float)
	un = new((/tt,th,gy,gx/),float)
	vn = new((/tt,th,gy,gx/),float)
	swn = new((/tt,th,gy,gx/),float)
	stn = new((/tt,th,gy,gx/),float)
	Tn = new((/tt,th,gy,gx/),float)
	hn = new((/th/),integer)
	;tn = new((/tt/),integer)
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;定义sh\u\v\st\高度\时间\pr\T的四维数组
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	do k = 0,tt-1
		do j = sy,sy+gy-1
			do i = sx,sx+gx-1
				un(k,0:0,j-sy,i-sx) = (/u10(k,j,i)/)
				vn(k,0:0,j-sy,i-sx) = (/v10(k,j,i)/)
				Tn(k,0:0,j-sy,i-sx) = (/T(k,j,i)/)
				shn(k,0:0,j-sy,i-sx)= (/sh(k,1:1,j,i)/)
				prn(k,0:0,j-sy,i-sx)= (/pr(k,1:1,j,i)/)
				stn(k,0:0,j-sy,i-sx)= (/st(k,j,i)/)
				swn(k,0:0,j-sy,i-sx)= (/sw(k,j,i)/)
	 
				un(k,1:3:1,j-sy,i-sx) = (/uw(k,3:5:1,j,i)/)
				vn(k,1:3:1,j-sy,i-sx) = (/vw(k,3:5:1,j,i)/)
				Tn(k,1:3:1,j-sy,i-sx) = (/T(k,j,i)/)
				shn(k,1:3:1,j-sy,i-sx)= (/sh(k,3:5:1,j,i)/)
				prn(k,1:3:1,j-sy,i-sx)= (/pr(k,3:5:1,j,i)/)
				stn(k,1:3:1,j-sy,i-sx)= (/st(k,j,i)/)
			end do
		end do
		;tn(k)=((floattointeger(k/4))%24)*100+(k%4)*15
	end do
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;将所需高度的要素赋予相应的数组
;设定时间间隔tn(k)=((floattointeger(k/4))%24)*100+(k%4)*15需要做以下处理:tn(k)=((stringtointeger(systemfunc("echo $WRF_start_HOUR"))+floattointeger(k/4))%24)*100+(k%4)*15,建议将$WRF_start_HOUR(wrf开始计算的时间)做成变量每次从控制文件中读取。
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	hgt = (/10,30,50,70/)
	;hgt = (/10,20,50,65/) 
	;mydate = stringtointeger(systemfunc("date -d '20160416'+'%Y%m%d %h%M%s'"))
	startdate = systemfunc("cat analysis.config | grep 'start'|awk -F'=' '{print $2}'|sed 's/ //g' | cut -c1-8")
	starthour = systemfunc("cat analysis.config | grep 'start'|awk -F'=' '{print $2}'|sed 's/ //g' | cut -c9-10")
	;print(startdate)
	;print(starthour)
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;设定高度和日期,stringtointeger(20130416)建议做成变量从控制文件中读取,例如stringtointeger(systemfunc("echo $WRF_start_DATE"))
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
txtpath = systemfunc("cat analysis.config | grep 'txtpath'|awk -F'=' '{print $2}'|sed 's/ //g'")
mns= gx*gy*th*tt
pms= new(mns,"string")
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;建议将"1.txt" 做成变量从控制文件中读取
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	mn = 0
	flag = 0
	do t=0,tt-1
		;if(t%96.eq.0)
			;mydate = stringtointeger(systemfunc("date -d '" + flag + " day' +'%Y%m%d'"))
			_mm = (t+(stringtointeger(starthour)+8)*4)*15
			;print(_mm)
			mydate = systemfunc("date -d '" + startdate +" " + _mm + "minute' +'%Y-%m-%d %H:%M:%S'")
			;flag = flag + 1
		;end if
		do k=0,th-1
			do j=0,gy-1
				do i=0,gx-1
					pms(mn) = sprintf("%10.4f", loc(0,i))
					pms(mn) = pms(mn) + sprintf("%10.4f", loc(1,j))
					pms(mn) = pms(mn) + sprinti("%5i", hgt(k))
					pms(mn) = pms(mn) + "     " + mydate
					;pms(mn) = pms(mn) + sprinti("%5.4i", tn(t))
					xu = un(t,k,j,i)
					yu = vn(t,k,j,i)
					pms(mn) = pms(mn) + sprintf("%10.2f ",xu) 
					pms(mn) = pms(mn) + sprintf("%10.2f ",yu)
					pms(mn) = pms(mn) + sprintf("%10.2f ",(xu*xu + yu*yu)^0.5)
					pms(mn) = pms(mn) + sprintf("%10.2f ",Tn(t,k,j,i))
					pms(mn) = pms(mn) + sprintf("%10.2f ",shn(t,k,j,i))
					;pms(mn) = pms(mn) + sprintf("%10.2f ",swn(t,k,j,i))
					pms(mn) = pms(mn) + sprintf("%10.2f ",prn(t,k,j,i))
					pms(mn) = pms(mn) + sprintf("%10.2f ",stn(t,k,j,i))
					mn = mn + 1
				end do
			end do
		end do
	end do
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
asciiwrite (txtpath , pms)
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
end
ncl的代码是朋友教我做的,非常感谢他。

除了这些之外,我们还需要一个上传脚本,也就是当文件解析好了,需要上传到服务器catwpd.sh

#!/bin/bash
MAPCONFIG=/home/NWP/RIXINWRF/wrfout/mapping.config
CONFIG=/home/NWP/RIXINWRF/source/file.down
SIZE=`cat $MAPCONFIG | grep size | awk -F'= ' '{print $2}'`
TXTNAME=`cat $MAPCONFIG | grep txtname | awk -F'= ' '{print $2}'`
LAT=`cat $MAPCONFIG | grep clat | awk -F'= ' '{print $2}'`
LON=`cat $MAPCONFIG | grep clon | awk -F'= ' '{print $2}'`
HIGH=`cat $MAPCONFIG | grep high | awk -F'= ' '{print $2}'`
TIME=`cat $CONFIG | grep "f_file" |awk -F'= ' '{print$2}' | sed 's/ //g'`
#TIME=2013050712
s=$(($SIZE+2))
for i in $(seq 3 $s)
do
	fname=`echo $TXTNAME | awk -F',' -v fly=$i '{print $fly}'`
	lat=`echo $LAT | awk -F',' -v fly=$i '{print $fly}'`
	lon=`echo $LON | awk -F',' -v fly=$i '{print $fly}'`
	high=`echo $HIGH | awk -F',' -v fly=$i '{print $fly}'`
	cat $fname$TIME.txt | grep "$lat" | grep "$lon" | grep " $high " > $fname$TIME.WPD
done
这是所有的代码,以后有时间讲解脚本的含义。


你可能感兴趣的:(Shell脚本的应用)