1 | #!/bin/bash |
---|
2 | |
---|
3 | ################################################################################ |
---|
4 | # |
---|
5 | # Author : Jan Vatant d'Ollone (2018) |
---|
6 | # ------ |
---|
7 | # |
---|
8 | # Purpose : * Modify Titan startfi.nc files to fill them with upper chemistry |
---|
9 | # ------- fields from old comp_XX files that were used in ye old days with |
---|
10 | # a 32x48x55 GCM grid which implied 70 upper levels. |
---|
11 | # |
---|
12 | # * You can either just fill the fields or create them if needed |
---|
13 | # |
---|
14 | # * Allows you to do the same for pressure grid "preskim" field : |
---|
15 | # just give a vertical grid as second input file !i |
---|
16 | # |
---|
17 | # * You need to run it in a folder containing comp_01->49 files ! |
---|
18 | # |
---|
19 | # * Requirements : NCO tool ncap2 |
---|
20 | # |
---|
21 | # Inputs : $1 : startfi.nc file |
---|
22 | # ------ |
---|
23 | # $2 (optional) : upper mid-layers pressure grid ASCII file |
---|
24 | # NB : It should be 1D with decreasing pressure |
---|
25 | # |
---|
26 | # Execution exemple : ./prepare_startkim.bash startfi.nc preskim.dat |
---|
27 | # ----------------- |
---|
28 | # |
---|
29 | # Approx. run time ~ 1h30-2h |
---|
30 | # ---------------- |
---|
31 | # |
---|
32 | ############################################################################### |
---|
33 | |
---|
34 | ncfile="$1" # startfi.nc file |
---|
35 | |
---|
36 | tmpfile="tmp" # temporary file |
---|
37 | |
---|
38 | |
---|
39 | # User choice : |
---|
40 | # * 1 Create upper chemistry dimension and fields |
---|
41 | # * 2 Just fill them, assuming you already have them in file |
---|
42 | |
---|
43 | while true; do |
---|
44 | read -p "Choose : - 1 - Create upper chemistry dimension and fields - 2 - Just fill fields assuming they pre-exist : " yn |
---|
45 | case $yn in |
---|
46 | [1]* ) echo "Ok I will create dimension upper_chemistry_layers=70 and upper chemistry fields..." ; create=true ; break ;; |
---|
47 | [2]* ) echo "Ok I assume you have upper chemistry fields in startfi.nc file, I will just fill them..."; create=false; break ;; |
---|
48 | * ) echo "Please enter 1 or 2 !";; |
---|
49 | esac |
---|
50 | done |
---|
51 | |
---|
52 | # If needed, we create the 70 levels dimension in the startfi.nc file |
---|
53 | |
---|
54 | if [ "$create" = true ] ; then |
---|
55 | echo "I add to startfi a upper_chemistry_layers=70 dimension ..." |
---|
56 | ncap2 -s 'defdim("upper_chemistry_layers",70)' -O $ncfile |
---|
57 | fi |
---|
58 | |
---|
59 | # If a second argument is given it will create and fill preskim |
---|
60 | |
---|
61 | if [ -z "$2" ] ; then |
---|
62 | echo " No second argument found, skipping pressure grid stuff ... " |
---|
63 | else |
---|
64 | presfile="$2" |
---|
65 | if [ "$create" = true ] ; then |
---|
66 | echo "I create an empty preskim field..." |
---|
67 | ncap2 -s 'preskim[$upper_chemistry_layers]=0.0;preskim@title="Upper chemistry mid-layer pressure"' $ncfile |
---|
68 | fi |
---|
69 | |
---|
70 | echo "I fill preskim field with input profile ..." |
---|
71 | i=0 |
---|
72 | while read -r pres |
---|
73 | do |
---|
74 | ncap2 -s "preskim($i)=$pres" -h -A $ncfile |
---|
75 | i=$((i+1)) |
---|
76 | done < "$presfile" |
---|
77 | fi |
---|
78 | |
---|
79 | # Loop on all 44 Titan chemistry species |
---|
80 | echo "Starting loop on all 44 Titan chemistry species ..." |
---|
81 | for ((iq=1;iq<=44;iq++)) ; do |
---|
82 | |
---|
83 | # First calculates lines where specie name and values are written in old comp file |
---|
84 | qline=$(( 2 + 71*$((iq-1)) )) # Specie name |
---|
85 | |
---|
86 | qdeb=$((qline+1)) # First line of composition for this specie |
---|
87 | qend=$((qline+70)) # Last " " " " " " |
---|
88 | |
---|
89 | specie=$(sed -n "${qline}p" comp_01) # Read specie name |
---|
90 | specie=$(echo $specie) # Trim it |
---|
91 | |
---|
92 | echo $specie |
---|
93 | |
---|
94 | # Create field if needed |
---|
95 | if [ "$create" = true ] ; then |
---|
96 | echo "I create an empty upper chemistry field for", $specie |
---|
97 | ncap2 -s "${specie}_up[$upper_chemistry_layers,$physical_points]=0.0;${specie}_up@title='${specie} in upper atmosphere'" $ncfile |
---|
98 | fi |
---|
99 | |
---|
100 | # Loop on latitudes |
---|
101 | |
---|
102 | for ((ilat=1;ilat<=49;ilat++)) ; do |
---|
103 | |
---|
104 | # Deal with comp_0X filenames |
---|
105 | if [ $ilat -lt 10 ] ; then |
---|
106 | lat=0$ilat |
---|
107 | else |
---|
108 | lat=$ilat |
---|
109 | fi |
---|
110 | |
---|
111 | # Extract the current specie section in latitude file |
---|
112 | sed -n "${qdeb},${qend}p" comp_$lat > tmp |
---|
113 | |
---|
114 | i=0 |
---|
115 | |
---|
116 | echo "Filling : " $specie "-" comp_$lat |
---|
117 | |
---|
118 | # Deal with special mono-gridpoint for North and South Poles |
---|
119 | if [ $ilat -eq 1 ] ; then |
---|
120 | ngrid0=0 |
---|
121 | ngrid1=0 |
---|
122 | elif [ $lat -eq 49 ] ; then |
---|
123 | ngrid0=1505 |
---|
124 | ngrid1=1505 |
---|
125 | else |
---|
126 | ngrid0=$((1+$(($ilat-2))*32)) |
---|
127 | ngrid1=$(($ngrid0+31)) |
---|
128 | fi |
---|
129 | |
---|
130 | # Read tmp file and fill the grid points |
---|
131 | while read -r dum1 dum2 ykim |
---|
132 | do |
---|
133 | ncap2 -s "${specie}_up($i,$ngrid0:$ngrid1)=$ykim" -h -A $ncfile |
---|
134 | i=$((i+1)) |
---|
135 | done < "$tmpfile" |
---|
136 | |
---|
137 | done # Latitude loop |
---|
138 | |
---|
139 | rm tmp |
---|
140 | |
---|
141 | done # Specie loop |
---|
142 | |
---|
143 | echo "Everything's finished !" |
---|