#!/bin/csh

set year=2005
set wrfreal=wrf_real_input_em.d01.${year}-08-09_12:00:00
set wrfreal_ruc=wrf_real_input_em.d01.${year}-08-09_12:00:00_ruc
set wrfreal_eta=wrf_real_input_em.d01.${year}-08-09_12:00:00_eta
set wrfreal_gfs=wrf_real_input_em.d01.${year}-08-09_12:00:00_gfs

goto 120
# Dump contents of eta wrf_real file to use as a template

ncdump ${wrfreal_eta} > wrfreal_eta.txt

# dump soil moisture from ruc and gfs models for each 
# layer

if ( ${year} == 2003) then
 ncks -s "%9.7f\n" -H -v SOILM005 ${wrfreal_ruc} > soilm005_ruc
 ncks -s "%9.7f\n" -H -v SM000010 ${wrfreal_gfs} > sm000010_gfs
 ncks -s "%9.7f\n" -H -v SOILM020 ${wrfreal_ruc} > soilm020_ruc
 ncks -s "%9.7f\n" -H -v SOILM040 ${wrfreal_ruc} > soilm040_ruc
 ncks -s "%9.7f\n" -H -v SOILM160 ${wrfreal_ruc} > soilm160_ruc
 ncks -s "%9.7f\n" -H -v SM010200 ${wrfreal_gfs} > sm010200_gfs
 ncks -s "%9.7f\n" -H -v SOILM300 ${wrfreal_ruc} > soilm300_ruc
else
 ncks -s "%9.7f\n" -H -v SOILM005 ${wrfreal_ruc} > soilm005_ruc
 ncks -s "%9.7f\n" -H -v SM000010 ${wrfreal_gfs} > sm000010_gfs
 ncks -s "%9.7f\n" -H -v SOILM020 ${wrfreal_ruc} > soilm020_ruc
 ncks -s "%9.7f\n" -H -v SM010040 ${wrfreal_gfs} > sm010040_gfs
 ncks -s "%9.7f\n" -H -v SOILM040 ${wrfreal_ruc} > soilm040_ruc
 ncks -s "%9.7f\n" -H -v SM040100 ${wrfreal_gfs} > sm040100_gfs
 ncks -s "%9.7f\n" -H -v SOILM160 ${wrfreal_ruc} > soilm160_ruc
 ncks -s "%9.7f\n" -H -v SM100200 ${wrfreal_gfs} > sm100200_gfs
 ncks -s "%9.7f\n" -H -v SOILM300 ${wrfreal_ruc} > soilm300_ruc
endif

# find line number of first soil layer in eta wrf_real file 

set line_number = `grep -n ' SM000010 =' wrfreal_eta.txt | cut -d":" -f1`
echo 'line number of top layer soil moisture '${line_number}

# save part of wrfreal_eta down to first soil layer inluding 
# the title

head -n ${line_number} wrfreal_eta.txt > top.txt

# retrieve soil moisture from the Eta from all layers

ncks -s "%9.7f\n" -H -v SM000010 ${wrfreal_eta} > sm000010_eta
ncks -s "%9.7f\n" -H -v SM010040 ${wrfreal_eta} > sm010040_eta
ncks -s "%9.7f\n" -H -v SM040100 ${wrfreal_eta} > sm040100_eta
ncks -s "%9.7f\n" -H -v SM100200 ${wrfreal_eta} > sm100200_eta

# Extract soiltype from netcdf file and dump into ascii file
# (need to do only once and one must use wrf output file)
#ncks -s "%i\n" -H -v ISLTYP wrf_050610_12Z_RUCLSM_RUCSMOIS > 
#rucsoiltype.txt

# run program to read in ruc,eta and gfs soil data and perform
# various computations including interpolations and perturbations
120:

# perturb1 assigns all negative -2 sd perturbation to 0

# perturbyear allows multiple years to be used.  GFS model had
# only 2 soil layers prior to June 2005.  

#./perturb1
#./perturbyear ${year}
#./perturbexe

./dryrucsm_new
#./wetrucsm_new
#./drysm
#./wetsm
#./a.out
goto 130
set line_number2 = `grep -n ' SNOW =' wrfreal_eta.txt | cut -d":" -f1`
#set line_number2 = `grep -n ' SM010040 =' all.txt | cut -d":" -f1`
echo 'line number at start of next field (ice) '${line_number2}

grep -n '}' wrfreal_eta.txt > end.txt
set line_number3 = `tail -1 end.txt | cut -d":" -f1`  
echo 'line number of the last line is '${line_number3}

@ bottom_lines = ${line_number3} - ${line_number2}
@ bottom_thick = ${bottom_lines} + 2
tail -${bottom_thick} wrfreal_eta.txt > bottom.txt
echo 'thickness of bottom text is '${bottom_thick}
130:
foreach perturb (16)
#foreach perturb (16 19 20 21 22 23 24 25)
 cat top.txt fort.${perturb} bottom.txt > all.txt

 ncgen -b -o ${wrfreal}_${perturb}_dry all.txt 
end
#130:
111:
