#!/bin/bash

# ----------------------------INPUT---------------------------------------

# Seting year and month (initial and final) for running
year='2018'
monthI='01'
monthF='12'

# Folder of global meteorological model data
WPS_DATA='/home/nobre/Build_WRF/DATA'
# Folder of WPS files
WPS_FOLDER='/home/nobre/Build_WRF/WPS'
#WPS_FOLDER='/home/nobre/Build_WRF/AERMOD_robao'
# WRF folder
WRF_FOLDER='/home/nobre/Build_WRF/WRF/test/em_real'
#WRF_FOLDER='/home/nobre/Build_WRF/AERMOD_robao'
# Number of processors
NOP='1'
# Seting runing days 
jan=(1 10 20 28)

#FALTA AUTOMATIZAR O DOMÍNIO EM TODOS OS NAMELISTS
# ==================================Namelist.wps==============================================================


# &geogrid
parent_id1='1'
parent_id2='1'
parent_id3='1'
parent_grid_ratio1='1'
parent_grid_ratio2='3'
parent_grid_ratio3='3'
i_parent_start1='1'
i_parent_start2='20'
i_parent_start3='6'
j_parent_start1='1'
j_parent_start2='7'
j_parent_start3='6'
e_we1='29'
e_we2='10'
e_we3='19'
e_sn1='18'
e_sn2='10'
e_sn3='13'
geog_data_res1="'30s'"
geog_data_res2="'30s'"
geog_data_res3="'30s'"
dx1='30000'
dy1='30000'
map_proj1="'mercator'"
ref_lat1='-27.388'
ref_lon1='-50.524'
truelat11='-27.388'
truelat21='0'
stand_lon1='-50.524'
geog_data_path='/home/nobre/Build_WRF/WPS_GEOG',
ref_x1='14.5'
ref_y1='9.0'
# ===================================Namelist.imput==========================================================

# &domains
time_step1='180'
time_step_fract_num1='0'
time_step_fract_den1='1'
max_dom1='3'
e_vert1='33'
e_vert2='33'
e_vert3='33'
p_top_requested1='5000'
num_metgrid_levels1='32'
num_metgrid_soil_levels1='4'
dx1='30000'
dx2='10000'
dx3='10000'
dy1='30000'
dy2='10000'
dy3='10000'
grid_id1='1'
grid_id2='2'
grid_id3='3'
parent_time_step_ratio1='1'
parent_time_step_ratio2='3'
parent_time_step_ratio3='3'
feedback1='1'
smooth_option1='0'

# &physics                 
mp_physics1='10'
mp_physics2='10'
mp_physics3='10'
ra_lw_physics1='1'
ra_lw_physics2='1'
ra_lw_physics3='1'
ra_sw_physics1='1'
ra_sw_physics2='1'
ra_sw_physics3='1'
radt1='30'
radt2='30'
radt3='30'
sf_sfclay_physics1='1'
sf_sfclay_physics2='1'
sf_sfclay_physics3='1'
sf_surface_physics1='2'
sf_surface_physics2='2'
sf_surface_physics3='2'
bl_pbl_physics1='1'
bl_pbl_physics2='1'
bl_pbl_physics3='1'
bldt1='0'
bldt2='0'
bldt3='0'
cu_physics1='1'
cu_physics2='1'
cu_physics3='1'	
cudt1='5'
cudt2='5'
cudt3='5'
isfflx1='1'
ifsnow1='0'
icloud1='1'
surface_input_source1='1'
num_soil_layers1='4'
sf_urban_physics1='0'
sf_urban_physics2='0'
sf_urban_physics3='0'
maxiens1='1'
maxens1='3'
maxens21='3'
maxens31='16'
ensdim1='144'
NUM_LAND_CAT1='24'

# &fdda

# &dynamics
hybrid_opt1='1' 
w_damping1='0'
diff_opt1='1'
diff_opt2='1'
diff_opt3='1'
km_opt1='4'
km_opt2='4'
km_opt3='4'
diff_6th_opt1='0'
diff_6th_opt2='0'
diff_6th_opt3='0'
diff_6th_factor1='0.12'
diff_6th_factor2='0.12'
diff_6th_factor3='0.12'
base_temp1='290.'
damp_opt1='0'
zdamp1='5000'
zdamp2='5000'
zdamp3='5000'
dampcoef1='0.2'
dampcoef2='0.2'
dampcoef3='0.2'
khdif1='0'
khdif2='0'
khdif3='0'
kvdif1='0'
kvdif2='0'
kvdif3='0'
non_hydrostatic1='.true.'
non_hydrostatic2='.true.'
non_hydrostatic3='.true.'
moist_adv_opt1='1'  
moist_adv_opt2='1'
moist_adv_opt3='1'     
scalar_adv_opt1='1' 
scalar_adv_opt2='1'
scalar_adv_opt3='1'     

# &bdy_control
spec_bdy_width1='5'
spec_zone1='1'
relax_zone1='4'
specified1='.true.'
specified2='.false.'
specified3='.false.'
nested1='.false.'
nested2='.true.'
nested3='.true.'

# &grib2

# &namelist_quilt
nio_tasks_per_group1='0'
nio_groups1='1'
# ===========================================================================================================

echo ""
echo ""
echo "######################## Starting program runningWRFv2 ################################"
echo " 
      ___     ______  ______      _
      | |     | ___|  | ___ |    / \     _
      | |     | |     | | | |   / | \   | \_/| 
      | |___  | |___  | |\|\|  / /_\ \  |  __/
      |_____| |_____| |___\_\ /_/   \_\ |_|     "
                     
echo "     Laboratório de Controle da Qualidade do Ar           "
echo ""
echo "     Universidade Federal de Santa Catarina - Brasil"
echo ""
echo "     email: leonardo.hoinaski@ufsc.br"
echo ""
echo "     last update: 07-10-2019  "
echo
echo "#######################################################################################"


echo ""
echo "Initializing in $year - $monthI - $monthF"
echo ""
#==============================LOOP OVER EACH MONTH=======================================

echo "======================================================================================="

k=0
for i in $(seq $monthI $monthF); do

        echo ""
	echo "############################ Month  number: $i  ############################"
        echo
	
	h=$((i + 1))
	
	rm -rf $WPS_DATA/MetInUse
	mkdir $WPS_DATA/MetInUse

        # -------------------------LINKING MET FILES------------------------
	if [ $i -lt 10 ];
	then

    		ln -sf $WPS_DATA/gfsanl_4_${year}0${i}*  $WPS_DATA/MetInUse/.

	fi

	if [ $i -lt 9 ];
	then

    		ln -sf $WPS_DATA/gfsanl_4_${year}0${h}*  $WPS_DATA/MetInUse/.

	fi

	if [ $i -gt 9 ];
	then

    		ln -sf $WPS_DATA/gfsanl_4_${year}${i}*  $WPS_DATA/MetInUse/.
	
	fi

	if [ $i -gt 8 ];
	then

    		ln -sf $WPS_DATA/gfsanl_4_${year}${h}*  $WPS_DATA/MetInUse/.


	fi

	if [ $i -eq 12 ];
	then

    		rm -rf $WPS_DATA/MetInUse/gfsanl_4_${year}${h}*  


	fi

	echo "Link of met file is done"
        echo ""
        # -------------------------LINKING MET IS DONE------------------------
	
	for j in $(seq 0 2); do
		
		k=$((j + 1))
		
		

    		echo " ======== > Day ${jan[$j]} to ${jan[$k]} ============================"
	cd $WPS_FOLDER

	if [ $i -lt 10 ];
	then
        echo "month smaller than 10"

		if [ $j -lt 1 ];
		then
                echo "day smaller than 10"
                echo "start_date = '${year}-0${i}-0${jan[$j]}_00:00:00', '${year}-0${i}-0${jan[$j]}_00:00:00', '${year}-0${i}-0${jan[$j]}_00:00:00',"
                echo "end_date   = '${year}-0${i}-${jan[$k]}_00:00:00', '${year}-0${i}-${jan[$k]}_00:00:00', '${year}-0${i}-${jan[$k]}_00:00:00'," 

cat > namelist.wps <<EOF
&share
 wrf_core = 'ARW',
 max_dom = 3,
 start_date = '${year}-0${i}-0${jan[$j]}_00:00:00', '${year}-0${i}-0${jan[$j]}_00:00:00', '${year}-0${i}-0${jan[$j]}_00:00:00', 
 end_date   = '${year}-0${i}-${jan[$k]}_00:00:00', '${year}-0${i}-${jan[$k]}_00:00:00', '${year}-0${i}-${jan[$k]}_00:00:00', 
 interval_seconds = 21600,
 io_form_geogrid = 2,
/

&geogrid
 parent_id         = $parent_id1,$parent_id2,$parent_id3,
 parent_grid_ratio = $parent_grid_ratio1,$parent_grid_ratio2,$parent_grid_ratio3,
 i_parent_start    = $i_parent_start1,$i_parent_start2,$i_parent_start3,
 j_parent_start    = $j_parent_start1,$j_parent_start2,$j_parent_start3,
 e_we          = $e_we1,$e_we2,$e_we3,
 e_sn          = $e_sn1,$e_sn2,$e_sn3,
 !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 ! The default datasets used to produce the MAXSNOALB and ALBEDO12M
 ! fields have changed in WPS v4.0. These fields are now interpolated
 ! from MODIS-based datasets.
 !
 ! To match the output given by the default namelist.wps in WPS v3.9.1,
 ! the following setting for geog_data_res may be used:
 !
 ! geog_data_res = 'maxsnowalb_ncep+albedo_ncep+default', 'maxsnowalb_ncep+albedo_ncep+default', 
 !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 !
 geog_data_res = $geog_data_res1,$geog_data_res2,$geog_data_res3,
 dx = $dx1,
 dy = $dy1,
 map_proj =  $map_proj1,
 ref_lat   = $ref_lat1,
 ref_lon   = $ref_lon1,
 truelat1  = $truelat11,
 truelat2  = $truelat21,
 stand_lon = $stand_lon1,
 geog_data_path = '/home/nobre/Build_WRF/WPS_GEOG',
 ref_x = $ref_x1,
 ref_y = $ref_y1,
/

&ungrib
 out_format = 'WPS',
 prefix = 'FILE',
/

&metgrid
 fg_name = 'FILE'
 io_form_metgrid = 2, 
/

&mod_levs
 press_pa = 201300 , 200100 , 100000 ,
             95000 ,  90000 ,
             85000 ,  80000 ,
             75000 ,  70000 ,
             65000 ,  60000 ,
             55000 ,  50000 ,
             45000 ,  40000 ,
             35000 ,  30000 ,
             25000 ,  20000 ,
             15000 ,  10000 ,
              5000 ,   1000
 /
EOF

		else
                echo "days among 10 and 28"
                echo  "start_date = '${year}-0${i}-${jan[$j]}_00:00:00', '${year}-0${i}-${jan[$j]}_00:00:00', '${year}-0${i}-${jan[$j]}_00:00:00'," 
                echo  "end_date   = '${year}-0${i}-${jan[$k]}_00:00:00', '${year}-0${i}-${jan[$k]}_00:00:00', '${year}-0${i}-${jan[$k]}_00:00:00'," 
cat > namelist.wps <<EOF
&share
 wrf_core = 'ARW',
 max_dom = 3
 start_date = '${year}-0${i}-${jan[$j]}_00:00:00', '${year}-0${i}-${jan[$j]}_00:00:00', '${year}-0${i}-${jan[$j]}_00:00:00', 
 end_date   = '${year}-0${i}-${jan[$k]}_00:00:00', '${year}-0${i}-${jan[$k]}_00:00:00', '${year}-0${i}-${jan[$k]}_00:00:00', 
 interval_seconds = 21600,
 io_form_geogrid = 2,
/

&geogrid
 parent_id         = $parent_id1,$parent_id2,$parent_id3,
 parent_grid_ratio = $parent_grid_ratio1,$parent_grid_ratio2,$parent_grid_ratio3,
 i_parent_start    = $i_parent_start1,$i_parent_start2,$i_parent_start3,
 j_parent_start    = $j_parent_start1,$j_parent_start2,$j_parent_start3,
 e_we          = $e_we1,$e_we2,$e_we3,
 e_sn          = $e_sn1,$e_sn2,$e_sn3,
 !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 ! The default datasets used to produce the MAXSNOALB and ALBEDO12M
 ! fields have changed in WPS v4.0. These fields are now interpolated
 ! from MODIS-based datasets.
 !
 ! To match the output given by the default namelist.wps in WPS v3.9.1,
 ! the following setting for geog_data_res may be used:
 !
 ! geog_data_res = 'maxsnowalb_ncep+albedo_ncep+default', 'maxsnowalb_ncep+albedo_ncep+default', 
 !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 !
 geog_data_res = $geog_data_res1,$geog_data_res2,$geog_data_res3,
 dx = $dx1,
 dy = $dy1,
 map_proj =  $map_proj1,
 ref_lat   = $ref_lat1,
 ref_lon   = $ref_lon1,
 truelat1  = $truelat11,
 truelat2  = $truelat21,
 stand_lon = $stand_lon1,
 geog_data_path = '/home/nobre/Build_WRF/WPS_GEOG',
 ref_x = $ref_x1,
 ref_y = $ref_y1,
/

&ungrib
 out_format = 'WPS',
 prefix = 'FILE',
/

&metgrid
 fg_name = 'FILE'
 io_form_metgrid = 2, 
/

&mod_levs
 press_pa = 201300 , 200100 , 100000 ,
             95000 ,  90000 ,
             85000 ,  80000 ,
             75000 ,  70000 ,
             65000 ,  60000 ,
             55000 ,  50000 ,
             45000 ,  40000 ,
             35000 ,  30000 ,
             25000 ,  20000 ,
             15000 ,  10000 ,
              5000 ,   1000
 /
EOF
		fi

	else
        echo 'Month greater than 9'
		if [ $j -lt 1 ];                
		then
                echo 'Day smaller than 10'
                echo "start_date = '${year}-${i}-0${jan[$j]}_00:00:00', '${year}-${i}-0${jan[$j]}_00:00:00', '${year}-${i}-0${jan[$j]}_00:00:00', "
                echo "end_date   = '${year}-${i}-${jan[$k]}_00:00:00', '${year}-${i}-${jan[$k]}_00:00:00', '${year}-${i}-${jan[$k]}_00:00:00', "

cat > namelist.wps <<EOF
&share
 wrf_core = 'ARW',
 max_dom = 3,
 start_date = '${year}-${i}-0${jan[$j]}_00:00:00', '${year}-${i}-0${jan[$j]}_00:00:00', '${year}-${i}-0${jan[$j]}_00:00:00', 
 end_date   = '${year}-${i}-${jan[$k]}_00:00:00', '${year}-${i}-${jan[$k]}_00:00:00', '${year}-${i}-${jan[$k]}_00:00:00', 
 interval_seconds = 21600,
 io_form_geogrid = 2,
/
&geogrid
 parent_id         = $parent_id1,$parent_id2,$parent_id3,
 parent_grid_ratio = $parent_grid_ratio1,$parent_grid_ratio2,$parent_grid_ratio3,
 i_parent_start    = $i_parent_start1,$i_parent_start2,$i_parent_start3,
 j_parent_start    = $j_parent_start1,$j_parent_start2,$j_parent_start3,
 e_we          = $e_we1,$e_we2,$e_we3,
 e_sn          = $e_sn1,$e_sn2,$e_sn3,
 !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 ! The default datasets used to produce the MAXSNOALB and ALBEDO12M
 ! fields have changed in WPS v4.0. These fields are now interpolated
 ! from MODIS-based datasets.
 !
 ! To match the output given by the default namelist.wps in WPS v3.9.1,
 ! the following setting for geog_data_res may be used:
 !
 ! geog_data_res = 'maxsnowalb_ncep+albedo_ncep+default', 'maxsnowalb_ncep+albedo_ncep+default', 
 !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 !
 geog_data_res = $geog_data_res1,$geog_data_res2,$geog_data_res3,
 dx = $dx1,
 dy = $dy1,
 map_proj =  $map_proj1,
 ref_lat   = $ref_lat1,
 ref_lon   = $ref_lon1,
 truelat1  = $truelat11,
 truelat2  = $truelat21,
 stand_lon = $stand_lon1,
 geog_data_path = '/home/nobre/Build_WRF/WPS_GEOG',
 ref_x = $ref_x1,
 ref_y = $ref_y1,
/

&ungrib
 out_format = 'WPS',
 prefix = 'FILE',
/

&metgrid
 fg_name = 'FILE'
 io_form_metgrid = 2, 
/

&mod_levs
 press_pa = 201300 , 200100 , 100000 ,
             95000 ,  90000 ,
             85000 ,  80000 ,
             75000 ,  70000 ,
             65000 ,  60000 ,
             55000 ,  50000 ,
             45000 ,  40000 ,
             35000 ,  30000 ,
             25000 ,  20000 ,
             15000 ,  10000 ,
              5000 ,   1000
 /
EOF

		
		else
                echo 'Days among 10 and 28'
                echo "start_date = '${year}-${i}-${jan[$j]}_00:00:00', '${year}-${i}-${jan[$j]}_00:00:00', '${year}-${i}-${jan[$j]}_00:00:00', "
                echo "end_date   = '${year}-${i}-${jan[$k]}_00:00:00', '${year}-${i}-${jan[$k]}_00:00:00', '${year}-${i}-${jan[$k]}_00:00:00', "

cat > namelist.wps <<EOF
&share
 wrf_core = 'ARW',
 max_dom = 3,
 start_date = '${year}-${i}-${jan[$j]}_00:00:00', '${year}-${i}-${jan[$j]}_00:00:00', '${year}-${i}-${jan[$j]}_00:00:00', 
 end_date   = '${year}-${i}-${jan[$k]}_00:00:00', '${year}-${i}-${jan[$k]}_00:00:00', '${year}-${i}-${jan[$k]}_00:00:00', 
 interval_seconds = 21600,
 io_form_geogrid = 2,
/

&geogrid
 parent_id         = $parent_id1,$parent_id2,$parent_id3,
 parent_grid_ratio = $parent_grid_ratio1,$parent_grid_ratio2,$parent_grid_ratio3,
 i_parent_start    = $i_parent_start1,$i_parent_start2,$i_parent_start3,
 j_parent_start    = $j_parent_start1,$j_parent_start2,$j_parent_start3,
 e_we          = $e_we1,$e_we2,$e_we3,
 e_sn          = $e_sn1,$e_sn2,$e_sn3,
 !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 ! The default datasets used to produce the MAXSNOALB and ALBEDO12M
 ! fields have changed in WPS v4.0. These fields are now interpolated
 ! from MODIS-based datasets.
 !
 ! To match the output given by the default namelist.wps in WPS v3.9.1,
 ! the following setting for geog_data_res may be used:
 !
 ! geog_data_res = 'maxsnowalb_ncep+albedo_ncep+default', 'maxsnowalb_ncep+albedo_ncep+default', 
 !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 !
 geog_data_res = $geog_data_res1,$geog_data_res2,$geog_data_res3,
 dx = $dx1,
 dy = $dy1,
 map_proj =  $map_proj1,
 ref_lat   = $ref_lat1,
 ref_lon   = $ref_lon1,
 truelat1  = $truelat11,
 truelat2  = $truelat21,
 stand_lon = $stand_lon1,
 geog_data_path = '/home/nobre/Build_WRF/WPS_GEOG',
 ref_x = $ref_x1,
 ref_y = $ref_y1,
/

&ungrib
 out_format = 'WPS',
 prefix = 'FILE',
/

&metgrid
 fg_name = 'FILE'
 io_form_metgrid = 2, 
/

&mod_levs
 press_pa = 201300 , 200100 , 100000 ,
             95000 ,  90000 ,
             85000 ,  80000 ,
             75000 ,  70000 ,
             65000 ,  60000 ,
             55000 ,  50000 ,
             45000 ,  40000 ,
             35000 ,  30000 ,
             25000 ,  20000 ,
             15000 ,  10000 ,
              5000 ,   1000
 /
EOF
		fi

	fi
	cd $WPS_FOLDER
	
	./link_grib.csh $WPS_DATA/MetInUse/
	
	echo ' --------------------------Running WPS----------------------------'
        echo "Runnign geogrid.exe"
	$WPS_FOLDER/geogrid.exe >& log.geogrid

	echo "Linking Vtable"
	ln -sf $WPS_FOLDER/ungrib/Variable_Tables/Vtable.GFS Vtable

	echo "Runnig ungrib.exe"
	$WPS_FOLDER/ungrib.exe >& log.ungrib


	echo "Runnig metgrid.exe"
	$WPS_FOLDER/metgrid.exe >& log.metgrid
        echo ""
	
	cd $WRF_FOLDER


        echo " ** Writing WRF namelist.input file ** "
        echo ""
  	rm -rf namelist.input 
cat > namelist.input <<EOF
 &time_control
 run_days                            = 0,
 run_hours                           = 0,
 run_minutes                         = 0,
 run_seconds                         = 0,
 start_year                          = $year, $year, $year,
 start_month                         = $i,   $i,   $i,
 start_day                           = ${jan[$j]},   ${jan[$j]},   ${jan[$j]},
 start_hour                          = 00,   00,   00,
 end_year                            = $year, $year, $year,
 end_month                           = $i,   $i,   $i,
 end_day                             = ${jan[$k]},   ${jan[$k]},   ${jan[$k]},
 end_hour                            = 00,   00,   00,
 interval_seconds                    = 21600
 input_from_file                     = .true.,.true.,.true.,
 history_interval                    = 60,  60,   60,
 frames_per_outfile                  = 1000, 1000, 1000,
 restart                             = .false.,
 restart_interval                    = 5000,
 io_form_history                     = 2,
 io_form_restart                     = 2,
 io_form_input                       = 2,
 io_form_boundary                    = 2,
debug_level                          = 0,
 /

 &domains
 time_step                           = $time_step1,
 time_step_fract_num                 = $time_step_fract_num1,
 time_step_fract_den                 = $time_step_fract_den1,
 max_dom                             = $max_dom1,
 e_we                                = $e_we1,     $e_we2,     $e_we3,
 e_sn                      	     = $e_sn1,     $e_sn2,     $e_sn3,
 e_vert                   	     = $e_vert1,     $e_vert2,     $e_vert3,
 p_top_requested                     = $p_top_requested1,
 num_metgrid_levels                  = $num_metgrid_levels1,
 num_metgrid_soil_levels             = $num_metgrid_soil_levels1,
 dx                                  = $dx1, $dx2, $dx3,
 dy                                  = $dy1, $dy2, $dy3,
 grid_id                             = $grid_id1,     $grid_id2,     $grid_id3,
 parent_id                           = $parent_id1,     $parent_id2,     $parent_id3,
 i_parent_start                      = $i_parent_start1,     $i_parent_start2,    $i_parent_start3,
 j_parent_start                      = $j_parent_start1,     $j_parent_start2,    $j_parent_start3,
 parent_grid_ratio                   = $parent_grid_ratio1,     $parent_grid_ratio2,     $parent_grid_ratio3,
 parent_time_step_ratio              = $parent_time_step_ratio1,     $parent_time_step_ratio2,     $parent_time_step_ratio3,
 feedback                            = $feedback1,
 smooth_option                       = $smooth_option1,
 /


 &physics                 
 mp_physics                          = $mp_physics1,        $mp_physics2,        $mp_physics3,
 ra_lw_physics                       = $ra_lw_physics1,        $ra_lw_physics2,        $ra_lw_physics3,
 ra_sw_physics                       = $ra_sw_physics1,        $ra_sw_physics2,        $ra_sw_physics3,
 radt                                = $radt1,       $radt2,       $radt3,
 sf_sfclay_physics                   = $sf_sfclay_physics1,        $sf_sfclay_physics2,        $sf_sfclay_physics3,
 sf_surface_physics                  = $sf_surface_physics1,        $sf_surface_physics2,        $sf_surface_physics3,
 bl_pbl_physics                      = $bl_pbl_physics1,        $bl_pbl_physics1,        $bl_pbl_physics1,
 bldt                                = $bldt1,        $bldt2,        $bldt3,
 cu_physics                          = $cu_physics1,        $cu_physics2,        $cu_physics3,
 cudt                                = $cudt1,        $cudt2,        $cudt3,
 isfflx                              = $isfflx1,
 ifsnow                              = $ifsnow1,
 icloud                              = $icloud1,
 surface_input_source                = $surface_input_source1,
 num_soil_layers                     = $num_soil_layers1,
 sf_urban_physics                    = $sf_urban_physics1,        $sf_urban_physics2,        $sf_urban_physics3,
 maxiens                             = $maxiens1,
 maxens                              = $maxens1,
 maxens2                             = $maxens21,
 maxens3                             = $maxens31,
 ensdim                              = $ensdim1,
 NUM_LAND_CAT                        = $NUM_LAND_CAT1,
 /

 &fdda
 /

 &dynamics
 hybrid_opt                          = $hybrid_opt1, 
 w_damping                           = $w_damping1,
 diff_opt                            = $diff_opt1,      $diff_opt2,      $diff_opt3,
 km_opt                              = $km_opt1,      $km_opt2,      $km_opt3,
 diff_6th_opt                        = $diff_6th_opt1,      $diff_6th_opt2,      $diff_6th_opt3,
 diff_6th_factor                     = $diff_6th_factor1,   $diff_6th_factor2,   $diff_6th_factor3,
 base_temp                           = $base_temp1,
 damp_opt                            = $damp_opt1,
 zdamp                               = $zdamp1,  $zdamp2,  $zdamp3,
 dampcoef                            = $dampcoef1,    $dampcoef2,    $dampcoef3,
 khdif                               = $khdif1,      $khdif2,      $khdif3,
 kvdif                               = $kvdif1,      $kvdif2,      $kvdif3,
 non_hydrostatic                     = $non_hydrostatic1, $non_hydrostatic2, $non_hydrostatic3,
 moist_adv_opt                       = $moist_adv_opt1,      $moist_adv_opt2,      $moist_adv_opt3,     
 scalar_adv_opt                      = $scalar_adv_opt1,      $scalar_adv_opt2,      $scalar_adv_opt3,     
 /

 &bdy_control
 spec_bdy_width          	     = $spec_bdy_width1,
 spec_zone                	     = $spec_zone1,
 relax_zone               	     = $relax_zone1,
 specified                	     = $specified1,  $specified2,  $specified3,
 nested                   	     = $nested1,   $nested2,   $nested3,
 /

 &grib2
 /

 &namelist_quilt
 nio_tasks_per_group	             = $nio_tasks_per_group1,
 nio_groups                          = $nio_groups1,
 /
EOF
        echo "       ~~~~~~~~~ Running WRF ~~~~~~~~~      "
        echo "real"
	ln -sf $WPS_FOLDER/met_em* . 
	mpirun -np 1 ./real.exe
	echo "wrf"
	tail rsl.error.0000
	mpirun -np $NOP ./wrf.exe

	done

	

        echo ' ======== > Day 28 to 01 ============================'
	cd $WPS_FOLDER		
	if [ $i -lt 9 ];
	then
        echo "Month smaller than 10"
        echo "start_date = '${year}-0${i}-28_00:00:00', '${year}-0${i}-28_00:00:00', '${year}-0${i}-28_00:00:00', "
        echo "end_date   = '${year}-0${h}-01_00:00:00', '${year}-0${h}-01_00:00:00', '${year}-0${h}-01_00:00:00',  "
# Wrinting WPS file from day 31 to 01

	
cat > namelist.wps <<EOF
&share
 wrf_core = 'ARW',
 max_dom = 3,
 start_date = '${year}-0${i}-28_00:00:00', '${year}-0${i}-28_00:00:00', '${year}-0${i}-28_00:00:00', 
 end_date   = '${year}-0${h}-01_00:00:00', '${year}-0${h}-01_00:00:00', '${year}-0${h}-01_00:00:00', 
 interval_seconds = 21600,
 io_form_geogrid = 2,
/

&geogrid
 parent_id         = $parent_id1,$parent_id2,$parent_id3,
 parent_grid_ratio = $parent_grid_ratio1,$parent_grid_ratio2,$parent_grid_ratio3,
 i_parent_start    = $i_parent_start1,$i_parent_start2,$i_parent_start3,
 j_parent_start    = $j_parent_start1,$j_parent_start2,$j_parent_start3,
 e_we          = $e_we1,$e_we2,$e_we3,
 e_sn          = $e_sn1,$e_sn2,$e_sn3,
 !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 ! The default datasets used to produce the MAXSNOALB and ALBEDO12M
 ! fields have changed in WPS v4.0. These fields are now interpolated
 ! from MODIS-based datasets.
 !
 ! To match the output given by the default namelist.wps in WPS v3.9.1,
 ! the following setting for geog_data_res may be used:
 !
 ! geog_data_res = 'maxsnowalb_ncep+albedo_ncep+default', 'maxsnowalb_ncep+albedo_ncep+default', 
 !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 !
 geog_data_res = $geog_data_res1,$geog_data_res2,$geog_data_res3,
 dx = $dx1,
 dy = $dy1,
 map_proj =  $map_proj1,
 ref_lat   = $ref_lat1,
 ref_lon   = $ref_lon1,
 truelat1  = $truelat11,
 truelat2  = $truelat21,
 stand_lon = $stand_lon1,
 geog_data_path = '/home/nobre/Build_WRF/WPS_GEOG',
 ref_x = $ref_x1,
 ref_y = $ref_y1,
/

&ungrib
 out_format = 'WPS',
 prefix = 'FILE',
/

&metgrid
 fg_name = 'FILE'
 io_form_metgrid = 2, 
/

&mod_levs
 press_pa = 201300 , 200100 , 100000 ,
             95000 ,  90000 ,
             85000 ,  80000 ,
             75000 ,  70000 ,
             65000 ,  60000 ,
             55000 ,  50000 ,
             45000 ,  40000 ,
             35000 ,  30000 ,
             25000 ,  20000 ,
             15000 ,  10000 ,
              5000 ,   1000
 /
EOF

	elif [ $i -eq 9 ]
	then
        echo 'Month equal to 9'	
        echo "start_date = '${year}-0${i}-28_00:00:00', '${year}-0${i}-28_00:00:00', '${year}-0${i}-28_00:00:00', "
        echo "end_date   = '${year}-${h}-01_00:00:00', '${year}-${h}-01_00:00:00', '${year}-${h}-01_00:00:00', "
# Wrinting WPS file from day 31 to 01

cat > namelist.wps <<EOF
&share
 wrf_core = 'ARW',
 max_dom = 3,
 start_date = '${year}-0${i}-28_00:00:00', '${year}-0${i}-28_00:00:00', '${year}-0${i}-28_00:00:00', 
 end_date   = '${year}-${h}-01_00:00:00', '${year}-${h}-01_00:00:00', '${year}-${h}-01_00:00:00', 
 interval_seconds = 21600,
 io_form_geogrid = 2,
/

&geogrid
 parent_id         = $parent_id1,$parent_id2,$parent_id3,
 parent_grid_ratio = $parent_grid_ratio1,$parent_grid_ratio2,$parent_grid_ratio3,
 i_parent_start    = $i_parent_start1,$i_parent_start2,$i_parent_start3,
 j_parent_start    = $j_parent_start1,$j_parent_start2,$j_parent_start3,
 e_we          = $e_we1,$e_we2,$e_we3,
 e_sn          = $e_sn1,$e_sn2,$e_sn3,
 !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 ! The default datasets used to produce the MAXSNOALB and ALBEDO12M
 ! fields have changed in WPS v4.0. These fields are now interpolated
 ! from MODIS-based datasets.
 !
 ! To match the output given by the default namelist.wps in WPS v3.9.1,
 ! the following setting for geog_data_res may be used:
 !
 ! geog_data_res = 'maxsnowalb_ncep+albedo_ncep+default', 'maxsnowalb_ncep+albedo_ncep+default', 
 !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 !
 geog_data_res = $geog_data_res1,$geog_data_res2,$geog_data_res3,
 dx = $dx1,
 dy = $dy1,
 map_proj =  $map_proj1,
 ref_lat   = $ref_lat1,
 ref_lon   = $ref_lon1,
 truelat1  = $truelat11,
 truelat2  = $truelat21,
 stand_lon = $stand_lon1,
 geog_data_path = '/home/nobre/Build_WRF/WPS_GEOG',
 ref_x = $ref_x1,
 ref_y = $ref_y1,
/

&ungrib
 out_format = 'WPS',
 prefix = 'FILE',
/

&metgrid
 fg_name = 'FILE'
 io_form_metgrid = 2, 
/

&mod_levs
 press_pa = 201300 , 200100 , 100000 ,
             95000 ,  90000 ,
             85000 ,  80000 ,
             75000 ,  70000 ,
             65000 ,  60000 ,
             55000 ,  50000 ,
             45000 ,  40000 ,
             35000 ,  30000 ,
             25000 ,  20000 ,
             15000 ,  10000 ,
              5000 ,   1000
 /
EOF

	elif [ $i -gt 9 -a $i -lt 12 ];
	then
        echo 'Month greater than 9 and smaller than 12'
        echo "start_date = '${year}-${i}-28_00:00:00', '${year}-${i}-28_00:00:00', '${year}-${i}-28_00:00:00', "
        echo "end_date   = '${year}-${h}-01_00:00:00', '${year}-${h}-01_00:00:00', '${year}-${h}-01_00:00:00', "
# Wrinting WPS file from day 31 to 01
cat > namelist.wps <<EOF
&share
 wrf_core = 'ARW',
 max_dom = 3,
 start_date = '${year}-${i}-28_00:00:00', '${year}-${i}-28_00:00:00', '${year}-${i}-28_00:00:00', 
 end_date   = '${year}-${h}-01_00:00:00', '${year}-${h}-01_00:00:00', '${year}-${h}-01_00:00:00', 
 interval_seconds = 21600,
 io_form_geogrid = 2,
/

&geogrid
 parent_id         = $parent_id1,$parent_id2,$parent_id3,
 parent_grid_ratio = $parent_grid_ratio1,$parent_grid_ratio2,$parent_grid_ratio3,
 i_parent_start    = $i_parent_start1,$i_parent_start2,$i_parent_start3,
 j_parent_start    = $j_parent_start1,$j_parent_start2,$j_parent_start3,
 e_we          = $e_we1,$e_we2,$e_we3,
 e_sn          = $e_sn1,$e_sn2,$e_sn3,
 !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 ! The default datasets used to produce the MAXSNOALB and ALBEDO12M
 ! fields have changed in WPS v4.0. These fields are now interpolated
 ! from MODIS-based datasets.
 !
 ! To match the output given by the default namelist.wps in WPS v3.9.1,
 ! the following setting for geog_data_res may be used:
 !
 ! geog_data_res = 'maxsnowalb_ncep+albedo_ncep+default', 'maxsnowalb_ncep+albedo_ncep+default', 
 !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 !
 geog_data_res = $geog_data_res1,$geog_data_res2,$geog_data_res3,
 dx = $dx1,
 dy = $dy1,
 map_proj =  $map_proj1,
 ref_lat   = $ref_lat1,
 ref_lon   = $ref_lon1,
 truelat1  = $truelat11,
 truelat2  = $truelat21,
 stand_lon = $stand_lon1,
 geog_data_path = '/home/nobre/Build_WRF/WPS_GEOG',
 ref_x = $ref_x1,
 ref_y = $ref_y1,
/

&ungrib
 out_format = 'WPS',
 prefix = 'FILE',
/

&metgrid
 fg_name = 'FILE'
 io_form_metgrid = 2, 
/

&mod_levs
 press_pa = 201300 , 200100 , 100000 ,
             95000 ,  90000 ,
             85000 ,  80000 ,
             75000 ,  70000 ,
             65000 ,  60000 ,
             55000 ,  50000 ,
             45000 ,  40000 ,
             35000 ,  30000 ,
             25000 ,  20000 ,
             15000 ,  10000 ,
              5000 ,   1000
 /
EOF

	else
        echo 'Month equal to 12'
        echo "start_date = '${year}-${i}-28_00:00:00', '${year}-${i}-28_00:00:00', '${year}-${i}-28_00:00:00', "
        echo "end_date   = '${year}-${i}-31_00:00:00', '${year}-${i}-31_00:00:00', '${year}-${i}-31_00:00:00', "
# Wrinting WPS file from day 31 to 01	

cat > namelist.wps <<EOF
&share
 wrf_core = 'ARW',
 max_dom = 3,
 start_date = '${year}-${i}-28_00:00:00', '${year}-${i}-28_00:00:00', '${year}-${i}-28_00:00:00', 
 end_date   = '${year}-${i}-31_00:00:00', '${year}-${i}-31_00:00:00', '${year}-${i}-31_00:00:00', 
 interval_seconds = 21600,
 io_form_geogrid = 2,
/

&geogrid
 parent_id         = $parent_id1,$parent_id2,$parent_id3,
 parent_grid_ratio = $parent_grid_ratio1,$parent_grid_ratio2,$parent_grid_ratio3,
 i_parent_start    = $i_parent_start1,$i_parent_start2,$i_parent_start3,
 j_parent_start    = $j_parent_start1,$j_parent_start2,$j_parent_start3,
 e_we          = $e_we1,$e_we2,$e_we3,
 e_sn          = $e_sn1,$e_sn2,$e_sn3,
 !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 ! The default datasets used to produce the MAXSNOALB and ALBEDO12M
 ! fields have changed in WPS v4.0. These fields are now interpolated
 ! from MODIS-based datasets.
 !
 ! To match the output given by the default namelist.wps in WPS v3.9.1,
 ! the following setting for geog_data_res may be used:
 !
 ! geog_data_res = 'maxsnowalb_ncep+albedo_ncep+default', 'maxsnowalb_ncep+albedo_ncep+default', 
 !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT NOTE !!!!!!!!!!!!!!!!!!!!!!!!!!!!
 !
 geog_data_res = $geog_data_res1,$geog_data_res2,$geog_data_res3,
 dx = $dx1,
 dy = $dy1,
 map_proj =  $map_proj1,
 ref_lat   = $ref_lat1,
 ref_lon   = $ref_lon1,
 truelat1  = $truelat11,
 truelat2  = $truelat21,
 stand_lon = $stand_lon1,
 geog_data_path = '/home/nobre/Build_WRF/WPS_GEOG',
 ref_x = $ref_x1,
 ref_y = $ref_y1,
/

&ungrib
 out_format = 'WPS',
 prefix = 'FILE',
/

&metgrid
 fg_name = 'FILE'
 io_form_metgrid = 2, 
/

&mod_levs
 press_pa = 201300 , 200100 , 100000 ,
             95000 ,  90000 ,
             85000 ,  80000 ,
             75000 ,  70000 ,
             65000 ,  60000 ,
             55000 ,  50000 ,
             45000 ,  40000 ,
             35000 ,  30000 ,
             25000 ,  20000 ,
             15000 ,  10000 ,
              5000 ,   1000
 /
EOF

					
	fi

	cd $WPS_FOLDER
	
	./link_grib.csh $WPS_DATA/MetInUse/
	
	echo ' --------------------------Running WPS----------------------------'
	echo "Runnign geogrid.exe"
	$WPS_FOLDER/geogrid.exe >& log.geogrid

	echo "Linking Vtable"
	ln -sf $WPS_FOLDER/ungrib/Variable_Tables/Vtable.GFS Vtable

	echo "Runnig ungrib.exe"
	$WPS_FOLDER/ungrib.exe >& log.ungrib


	echo "Runnig metgrid.exe"
	$WPS_FOLDER/metgrid.exe >& log.metgrid


	cd $WRF_FOLDER

  	rm -rf namelist.input 

	if [ $i -lt 12 ];
	then
        echo " ** Writing WRF namelist.input files ** "
        echo ""
cat > namelist.input <<EOF
 &time_control
 run_days                            = 0,
 run_hours                           = 0,
 run_minutes                         = 0,
 run_seconds                         = 0,
 start_year                          = $year, $year, $year,
 start_month                         = $i,   $i,   $i,
 start_day                           = 28,   28,   28,
 start_hour                          = 00,   00,   00,
 end_year                            = $year, $year, $year,
 end_month                           = $h,   $h,   $h,
 end_day                             = 01,   01,   01,
 end_hour                            = 00,   00,   00,
 interval_seconds                    = 21600
 input_from_file                     = .true.,.true.,.true.,
 history_interval                    = 60,  60,   60,
 frames_per_outfile                  = 1000, 1000, 1000,
 restart                             = .false.,
  restart_interval                    = 5000,
 io_form_history                     = 2,
 io_form_restart                     = 2,
 io_form_input                       = 2,
 io_form_boundary                    = 2,
debug_level                          = 0,
 /

 &domains
 time_step                           = $time_step1,
 time_step_fract_num                 = $time_step_fract_num1,
 time_step_fract_den                 = $time_step_fract_den1,
 max_dom                             = $max_dom1,
 e_we                                = $e_we1,     $e_we2,     $e_we3,
 e_sn                      	     = $e_sn1,     $e_sn2,     $e_sn3,
 e_vert                   	     = $e_vert1,     $e_vert2,     $e_vert3,
 p_top_requested                     = $p_top_requested1,
 num_metgrid_levels                  = $num_metgrid_levels1,
 num_metgrid_soil_levels             = $num_metgrid_soil_levels1,
 dx                                  = $dx1, $dx2, $dx3,
 dy                                  = $dy1, $dy2, $dy3,
 grid_id                             = $grid_id1,     $grid_id2,     $grid_id3,
 parent_id                           = $parent_id1,     $parent_id2,     $parent_id3,
 i_parent_start                      = $i_parent_start1,     $i_parent_start2,    $i_parent_start3,
 j_parent_start                      = $j_parent_start1,     $j_parent_start2,    $j_parent_start3,
 parent_grid_ratio                   = $parent_grid_ratio1,     $parent_grid_ratio2,     $parent_grid_ratio3,
 parent_time_step_ratio              = $parent_time_step_ratio1,     $parent_time_step_ratio2,     $parent_time_step_ratio3,
 feedback                            = $feedback1,
 smooth_option                       = $smooth_option1,
 /


 &physics                 
 mp_physics                          = $mp_physics1,        $mp_physics2,        $mp_physics3,
 ra_lw_physics                       = $ra_lw_physics1,        $ra_lw_physics2,        $ra_lw_physics3,
 ra_sw_physics                       = $ra_sw_physics1,        $ra_sw_physics2,        $ra_sw_physics3,
 radt                                = $radt1,       $radt2,       $radt3,
 sf_sfclay_physics                   = $sf_sfclay_physics1,        $sf_sfclay_physics2,        $sf_sfclay_physics3,
 sf_surface_physics                  = $sf_surface_physics1,        $sf_surface_physics2,        $sf_surface_physics3,
 bl_pbl_physics                      = $bl_pbl_physics1,        $bl_pbl_physics1,        $bl_pbl_physics1,
 bldt                                = $bldt1,        $bldt2,        $bldt3,
 cu_physics                          = $cu_physics1,        $cu_physics2,        $cu_physics3,
 cudt                                = $cudt1,        $cudt2,        $cudt3,
 isfflx                              = $isfflx1,
 ifsnow                              = $ifsnow1,
 icloud                              = $icloud1,
 surface_input_source                = $surface_input_source1,
 num_soil_layers                     = $num_soil_layers1,
 sf_urban_physics                    = $sf_urban_physics1,        $sf_urban_physics2,        $sf_urban_physics3,
 maxiens                             = $maxiens1,
 maxens                              = $maxens1,
 maxens2                             = $maxens21,
 maxens3                             = $maxens31,
 ensdim                              = $ensdim1,
 NUM_LAND_CAT                        = $NUM_LAND_CAT1,
 /

 &fdda
 /

 &dynamics
 hybrid_opt                          = $hybrid_opt1, 
 w_damping                           = $w_damping1,
 diff_opt                            = $diff_opt1,      $diff_opt2,      $diff_opt3,
 km_opt                              = $km_opt1,      $km_opt2,      $km_opt3,
 diff_6th_opt                        = $diff_6th_opt1,      $diff_6th_opt2,      $diff_6th_opt3,
 diff_6th_factor                     = $diff_6th_factor1,   $diff_6th_factor2,   $diff_6th_factor3,
 base_temp                           = $base_temp1,
 damp_opt                            = $damp_opt1,
 zdamp                               = $zdamp1,  $zdamp2,  $zdamp3,
 dampcoef                            = $dampcoef1,    $dampcoef2,    $dampcoef3,
 khdif                               = $khdif1,      $khdif2,      $khdif3,
 kvdif                               = $kvdif1,      $kvdif2,      $kvdif3,
 non_hydrostatic                     = $non_hydrostatic1, $non_hydrostatic2, $non_hydrostatic3,
 moist_adv_opt                       = $moist_adv_opt1,      $moist_adv_opt2,      $moist_adv_opt3,     
 scalar_adv_opt                      = $scalar_adv_opt1,      $scalar_adv_opt2,      $scalar_adv_opt3,     
 /

 &bdy_control
 spec_bdy_width          	     = $spec_bdy_width1,
 spec_zone                	     = $spec_zone1,
 relax_zone               	     = $relax_zone1,
 specified                	     = $specified1,  $specified2,  $specified3,
 nested                   	     = $nested1,   $nested2,   $nested3,
 /

 &grib2
 /

 &namelist_quilt
 nio_tasks_per_group	             = $nio_tasks_per_group1,
 nio_groups                          = $nio_groups1,
 /
EOF


	else
cat > namelist.input <<EOF
 &time_control
 run_days                            = 0,
 run_hours                           = 0,
 run_minutes                         = 0,
 run_seconds                         = 0,
 start_year                          = $year, $year, $year,
 start_month                         = $i,   $i,   $i,
 start_day                           = 28,   28,   28,
 start_hour                          = 00,   00,   00,
 end_year                            = $year, $year, $year,
 end_month                           = $i,   $i,   $i,
 end_day                             = 31,   31,   31,
 end_hour                            = 00,   00,   00,
 interval_seconds                    = 21600
 input_from_file                     = .true.,.true.,.true.,
 history_interval                    = 60,  60,   60,
 frames_per_outfile                  = 1000, 1000, 1000,
 restart                             = .false.,
  restart_interval                    = 5000,
 io_form_history                     = 2,
 io_form_restart                     = 2,
 io_form_input                       = 2,
 io_form_boundary                    = 2,
debug_level                          = 0,
 /

 &domains
 time_step                           = $time_step1,
 time_step_fract_num                 = $time_step_fract_num1,
 time_step_fract_den                 = $time_step_fract_den1,
 max_dom                             = $max_dom1,
 e_we                                = $e_we1,     $e_we2,     $e_we3,
 e_sn                      	     = $e_sn1,     $e_sn2,     $e_sn3,
 e_vert                   	     = $e_vert1,     $e_vert2,     $e_vert3,
 p_top_requested                     = $p_top_requested1,
 num_metgrid_levels                  = $num_metgrid_levels1,
 num_metgrid_soil_levels             = $num_metgrid_soil_levels1,
 dx                                  = $dx1, $dx2, $dx3,
 dy                                  = $dy1, $dy2, $dy3,
 grid_id                             = $grid_id1,     $grid_id2,     $grid_id3,
 parent_id                           = $parent_id1,     $parent_id2,     $parent_id3,
 i_parent_start                      = $i_parent_start1,     $i_parent_start2,    $i_parent_start3,
 j_parent_start                      = $j_parent_start1,     $j_parent_start2,    $j_parent_start3,
 parent_grid_ratio                   = $parent_grid_ratio1,     $parent_grid_ratio2,     $parent_grid_ratio3,
 parent_time_step_ratio              = $parent_time_step_ratio1,     $parent_time_step_ratio2,     $parent_time_step_ratio3,
 feedback                            = $feedback1,
 smooth_option                       = $smooth_option1,
 /


 &physics                 
 mp_physics                          = $mp_physics1,        $mp_physics2,        $mp_physics3,
 ra_lw_physics                       = $ra_lw_physics1,        $ra_lw_physics2,        $ra_lw_physics3,
 ra_sw_physics                       = $ra_sw_physics1,        $ra_sw_physics2,        $ra_sw_physics3,
 radt                                = $radt1,       $radt2,       $radt3,
 sf_sfclay_physics                   = $sf_sfclay_physics1,        $sf_sfclay_physics2,        $sf_sfclay_physics3,
 sf_surface_physics                  = $sf_surface_physics1,        $sf_surface_physics2,        $sf_surface_physics3,
 bl_pbl_physics                      = $bl_pbl_physics1,        $bl_pbl_physics1,        $bl_pbl_physics1,
 bldt                                = $bldt1,        $bldt2,        $bldt3,
 cu_physics                          = $cu_physics1,        $cu_physics2,        $cu_physics3,
 cudt                                = $cudt1,        $cudt2,        $cudt3,
 isfflx                              = $isfflx1,
 ifsnow                              = $ifsnow1,
 icloud                              = $icloud1,
 surface_input_source                = $surface_input_source1,
 num_soil_layers                     = $num_soil_layers1,
 sf_urban_physics                    = $sf_urban_physics1,        $sf_urban_physics2,        $sf_urban_physics3,
 maxiens                             = $maxiens1,
 maxens                              = $maxens1,
 maxens2                             = $maxens21,
 maxens3                             = $maxens31,
 ensdim                              = $ensdim1,
 NUM_LAND_CAT                        = $NUM_LAND_CAT1,
 /

 &fdda
 /

 &dynamics
 hybrid_opt                          = $hybrid_opt1, 
 w_damping                           = $w_damping1,
 diff_opt                            = $diff_opt1,      $diff_opt2,      $diff_opt3,
 km_opt                              = $km_opt1,      $km_opt2,      $km_opt3,
 diff_6th_opt                        = $diff_6th_opt1,      $diff_6th_opt2,      $diff_6th_opt3,
 diff_6th_factor                     = $diff_6th_factor1,   $diff_6th_factor2,   $diff_6th_factor3,
 base_temp                           = $base_temp1,
 damp_opt                            = $damp_opt1,
 zdamp                               = $zdamp1,  $zdamp2,  $zdamp3,
 dampcoef                            = $dampcoef1,    $dampcoef2,    $dampcoef3,
 khdif                               = $khdif1,      $khdif2,      $khdif3,
 kvdif                               = $kvdif1,      $kvdif2,      $kvdif3,
 non_hydrostatic                     = $non_hydrostatic1, $non_hydrostatic2, $non_hydrostatic3,
 moist_adv_opt                       = $moist_adv_opt1,      $moist_adv_opt2,      $moist_adv_opt3,     
 scalar_adv_opt                      = $scalar_adv_opt1,      $scalar_adv_opt2,      $scalar_adv_opt3,     
 /

 &bdy_control
 spec_bdy_width          	     = $spec_bdy_width1,
 spec_zone                	     = $spec_zone1,
 relax_zone               	     = $relax_zone1,
 specified                	     = $specified1,  $specified2,  $specified3,
 nested                   	     = $nested1,   $nested2,   $nested3,
 /

 &grib2
 /

 &namelist_quilt
 nio_tasks_per_group	             = $nio_tasks_per_group1,
 nio_groups                          = $nio_groups1,
 /
EOF


	fi
        echo "       ~~~~~~~~~ Running WRF ~~~~~~~~~      "
	echo "real"
	ln -sf $WPS_FOLDER/met_em* . 
	mpirun -np 1 ./real.exe
	echo "wrf"
	tail rsl.error.0000
	mpirun -np $NOP ./wrf.exe
	
done




