-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcoupling_ice2echam.functions
More file actions
1833 lines (1662 loc) · 99.8 KB
/
coupling_ice2echam.functions
File metadata and controls
1833 lines (1662 loc) · 99.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/ksh
## @fn ice2echam()
ice2echam() {
echo " =================================================================="
echo " *** S T A R T I N G ice2echam *** "; echo
. ${FUNCTION_PATH}/../general/general_lists.functions
. ${FUNCTION_PATH}/../general/coupling_general.functions
ISM_TO_ECHAM_update_orography=${ISM_TO_ECHAM_update_orography:-1}
ISM_TO_ECHAM_update_glacial_mask=${ISM_TO_ECHAM_update_glacial_mask:-1}
ISM_TO_ECHAM_update_land_runoff=${ISM_TO_ECHAM_update_land_runoff:-1}
HOSING_CORRECTION=${HOSING_CORRECTION:-0}
:> nco_stderr_ice2echam; :> cdo_stderr_ice2echam
counter=0
COUNT_MAX=60
while [ ${counter} -lt ${COUNT_MAX} ]
do
echo " * inside while loop "
if [ -f ${COUPLE_DIR}/ice_file_for_atmosphere.nc ]; then
break
fi
echo; echo " * File ${COUPLE_DIR}/ice_file_for_atmosphere.nc not found. Waiting for 10 seconds ..."
sleep 10
counter=$((counter+1))
done
read_names ice atmosphere
define_echam_names
if [ "$ISM_TO_ECHAM_update_glacial_mask" -eq 1 ]; then
iterative_coupling_ice_echam6_update_glacial_mask
fi
if [ "$ISM_TO_ECHAM_update_orography" -eq 1 ]; then
iterative_coupling_ice_echam6_update_orography
fi
if [ "$ISM_TO_ECHAM_update_land_runoff" -eq 1 ]; then
iterative_coupling_ice_echam6_update_land_runoff
fi
echo " *** F I N I S H E D ice2echam *** "
echo " =================================================================="
}
## @fn define_echam_names()
define_echam_names() {
echam_glacial_mask_name=glac
}
## @fn iterative_coupling_ice_echam6_update_orography()
iterative_coupling_ice_echam6_update_orography() {
echo " Updating orography and gravity wave drag parameterization in echam6"
update_orography_select_field
if [ "$MACHINE" == "levante" ]; then
hi_res_init_oro="/pool/data/ECHAM5/T511/T511_jan_surf.nc"
fi
if [ -f ${hi_res_init_oro} ]
then
regrid_to_echam ${COUPLE_DIR}/ice_orog_difference.nc remapbil T511
regrid_to_echam ${COUPLE_DIR}/ice_orog_difference.nc remapbil
update_orography_apply_orography_anomaly_high_res
update_orography_prepare_calnoro_high_res
else
regrid_to_echam ${COUPLE_DIR}/ice_orog_difference.nc remapbil
update_orography_apply_orography_anomaly
update_orography_prepare_calnoro
fi
update_orography_run_calnoro
update_orography_generate_target_orography_file
#set_in_jsbach_restart_file elevation ${COUPLE_DIR}/target_orography_echam6_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.nc OROMEA unpack
update_orography_set_namelist_modifications_next_echam_run
echo; echo " ...done."; echo
}
## @fn iterative_coupling_ice_echam6_update_glacial_mask()
iterative_coupling_ice_echam6_update_glacial_mask() {
echo " Updating Glacial Mask in echam6"
set_glacial_mask_select_field
set_glacial_mask_echam_restart_file $ifile
if [ -d ${COUPLE_DIR}/jsbach_init_file_modifications ]; then
rm -rf ${COUPLE_DIR}/jsbach_init_file_modifications
fi
mkdir ${COUPLE_DIR}/jsbach_init_file_modifications
here=$(pwd)
cd ${COUPLE_DIR}/jsbach_init_file_modifications
set_glacial_mask_echam_make_dummy_jan_surf
set_glacial_mask_jsbach_update_init_file
cd $here
rm -rf ${COUPLE_DIR}/jsbach_init_file_modifications
set_glacial_mask_jsbach_update_restart_jsbach
set_glacial_mask_jsbach_update_restart_veg
ensure_echam_restart_aliases_for_m_streams
ensure_nonempty_grib_rerun_files_for_resume_month
# === FINAL FIX: Force lgfw=.false. in namelist.echam ===
# Even with namelist modification files, lgfw sometimes persists as .true.
# causing IO_read_stream failures for 'gfw'. We patch it directly here.
if [ -n "${WORK_DIR}" ] && [ -f "${WORK_DIR}/namelist.echam" ]; then
echo " - Patching ${WORK_DIR}/namelist.echam to force lgfw = .false."
sed -i 's/lgfw[[:space:]]*=[[:space:]]*\.true\./lgfw = .false./Ig' "${WORK_DIR}/namelist.echam"
grep -i "lgfw" "${WORK_DIR}/namelist.echam"
fi
echo; echo " ...done."; echo
}
## @fn iterative_coupling_ice_echam6_update_land_runoff()
iterative_coupling_ice_echam6_update_land_runoff() {
echo " Setting Glacial Runoff for incorporation in HD-Model via oasis3-mct"
update_land_runoff_select_glacial_discharge
regrid_to_echam ${COUPLE_DIR}/ice_discharge.nc
update_land_runoff_fill_missing
if [ "x${HOSING_CORRECTION}" == "x1" ]; then
echo; echo " * Correcting freshwater input by hosing "
distribute_total_discharge_over_ocean
else
echo; echo " * NOT correcting freshwater input by hosing "
fi
update_land_runoff_prepare_for_oasis
update_land_runoff_set_namcouple_override
update_land_runoff_set_namelist_modifications_next_echam_run
update_land_runoff_set_namelist_modifications_next_jsbach_run
echo; echo " ...done."; echo
}
## @fn update_orography_select_field()
update_orography_select_field() {
echo; echo " * selecting orography difference over ice run"
cdo -s selvar,${ice_orography_difference_name} \
${COUPLE_DIR}/ice_file_for_atmosphere.nc \
${COUPLE_DIR}/ice_orog_difference.nc
rmlist="$rmlist ${COUPLE_DIR}/ice_orog_difference.nc"
}
update_orography_from_martins_script() {
cdo -L -remapbil,${RES_echam}grid /
${COUPLE_DIR}/ice_orog_difference_T511grid.nc /
${COUPLE_DIR}/ice_orog_difference_${RES_echam}grid.nc
#cdo -L -setmisstoc,0. -ifthen slf.nc ${COUPLE_DIR}/ice_orog_difference_${RES_echam}grid.nc dummy.nc
#mv dummy.nc ${COUPLE_DIR}/ice_orog_difference_${RES_echam}grid.nc
ECHAM_g=9.80665
if [ -L ${RESTART_DIR_echam}/restart_${EXP_ID}_echam.nc ]; then
rfile=$(readlink ${RESTART_DIR_echam}/restart_${EXP_ID}_echam.nc)
else
rfile=${RESTART_DIR_echam}/restart_${EXP_ID}_echam.nc
fi
cdo -L -sp2gp -gp2sp -add -selvar,GEOSP ${rfile} -mulc,${ECHAM_g} /
${COUPLE_DIR}/ice_orog_difference_${RES_echam}grid.nc /
${COUPLE_DIR}/echam6_new_orography_before_calnoro_low_res.nc
mv geosp.nc ${output_dir}/input/echam6/GEOSP_${paleo_time}_${RES_echam}.nc
}
update_oromea_from_martins_script() {
ofile=${COUPLE_DIR}/target_orography_echam6_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.nc
cdo -L -remapnn,r720x360 -setgrid,T511grid -selvar,OROMEA ${hi_res_init_oro} oromea_pi_r720x360.nc
cdo -L -remapnn,r720x360 ${COUPLE_DIR}/ice_orog_difference_T511grid.nc anom_topo_r720x360.nc
#cdo -L -setmisstoc,0. -ifthen -remapnn,r720x360grid slf.nc anom_topo_r720x360.nc dummy.nc
#mv dummy.nc anom_topo_r720x360.nc
cdo -L -add oromea_pi_r720x360.nc anom_topo_r720x360.nc oromea_r720x360.nc
cdo -L -mul oromea_r720x360.nc -gtc,0. oromea_r720x360.nc dummy.nc; mv dummy.nc oromea_r720x360.nc
CALNORO_RES=${echam_res:1:3} # use defined ECHAM6 grid resolution for calnoro
cp oromea_r720x360.nc topodata.nc
echo ${CALNORO_RES} | ${calnoro_dir}/calnoro
cdo -L -s -f nc -setgrid,${echam_res}grid -invertlat sso_par_fil.srv calnoro_output.nc
cdo -L -chvar,var51,OROMEA,var52,OROSTD,var53,OROSIG,var54,OROGAM,var55,OROTHE,var56,OROPIC,var57,OROVAL calnoro_output.nc ${ofile}
cdo -L -mul -selvar,OROMEA ${ofile} -gtc,0. -selvar,OROMEA ${ofile} oromea.tmp
# save geosp and oro-xxx in different files. why oromea from original file @PG?
cdo -L -replace ${ofile} oromea.tmp dummy.nc; mv dummy.nc ${ofile}
rm oromea.tmp
rm oromea_pi_r720x360.nc anom_topo_r720x360.nc oromea_r720x360.nc topodata.nc sso_par_fil.srv calnoro_output.nc
}
## @fn update_orography_apply_orography_anomaly_high_res()
update_orography_apply_orography_anomaly_high_res() {
echo; echo " * applying surface height changes to high-resolution echam topography"
ifile=${COUPLE_DIR}/ice_orog_difference_T511grid.nc
ofile=${COUPLE_DIR}/echam6_new_orography_before_calnoro_high_res.nc
ncrename -v ${ice_orography_difference_name},oromea $ifile
cdo -s -setmisstoc,0 $ifile tmp; mv tmp $ifile
if [[ -f ${ofile} ]]
then
rfile=${ofile}
ncks -O -v oromea $rfile tmp_oromea
else
rfile=${hi_res_init_oro}
ncks -O -v OROMEA $rfile tmp_oromea
ncrename -v OROMEA,oromea tmp_oromea
fi
change_lonlat $ifile oromea
# LA: need to backup echam6_new_orography_before_calnoro!!!
ncbo -O --operation=add $ifile tmp_oromea $ofile 2>> nco_stderr_ice2echam
update_orography_apply_orography_anomaly
}
## @fn update_orography_apply_orography_anomaly()
update_orography_apply_orography_anomaly() {
echo; echo " * applying surface height changes to low-resolution echam topography"
ifile=${COUPLE_DIR}/ice_orog_difference_${RES_echam}grid.nc
ofile=${COUPLE_DIR}/echam6_new_orography_before_calnoro_low_res.nc
ncrename -v ${ice_orography_difference_name},oromea $ifile
cdo -s -setmisstoc,0 $ifile tmp; mv tmp $ifile
if [ -L ${RESTART_DIR_echam}/restart_${EXP_ID}_echam.nc ]; then
rfile=$(readlink ${RESTART_DIR_echam}/restart_${EXP_ID}_echam.nc)
else
rfile=${RESTART_DIR_echam}/restart_${EXP_ID}_echam.nc
fi
ncks -O -v oromea $rfile tmp_oromea
change_lonlat $ifile oromea
ncbo -O --operation=add $ifile tmp_oromea $ofile 2>> nco_stderr_ice2echam
}
## @fn update_orography_prepare_calnoro_high_res()
update_orography_prepare_calnoro_high_res() {
echo; echo " * preparing for external program CALNORO"
ifile=${COUPLE_DIR}/echam6_new_orography_before_calnoro_high_res.nc
ofile=topodata.nc
cdo -s -P 28 \
-remapnn,r720x360 \
-setgrid,T511grid \
-chname,oromea,OROMEA \
$ifile \
$ofile 2>>cdo_stderr_ice2echam
}
## @fn update_orography_prepare_calnoro()
update_orography_prepare_calnoro() {
echo; echo " * preparing for external program CALNORO"
ifile=${COUPLE_DIR}/echam6_new_orography_before_calnoro_low_res.nc
ofile=topodata.nc
cdo -s -P 28 \
-remapnn,r720x360 \
-setgrid,${RES_echam}grid \
-chname,oromea,OROMEA \
$ifile \
$ofile 2>>cdo_stderr_ice2echam
}
## @fn update_orography_run_calnoro()
update_orography_run_calnoro() {
echo; echo " * running CALNORO"
ofile=${COUPLE_DIR}/calnoro_output.nc
if [ ! -f calnoro ]; then
if [ ! -f ${FUNCTION_PATH}/../utils/calnoro ]; then
if [ -f ${FUNCTION_PATH}/../utils/install_calnoro.sh ]; then
echo " install_calnoro.sh found. Running installation ..."
currentdir=$(pwd)
cd ${FUNCTION_PATH}/../utils/
./install_calnoro.sh ${MACHINE}
cd ${currentdir}
cp ${FUNCTION_PATH}/../utils/calnoro .
else
echo " !!! FATAL ERROR: Compiled external utility CALNORO not found"
echo " !!! Looking in: ${FUNCTION_PATH}/../utils/"
echo " !!! FATAL ERROR: Please compile this program using the install_calnoro.sh script!"
exit 42
fi
else
cp ${FUNCTION_PATH}/../utils/calnoro .
fi
fi
# Make sure dimension order is correct:
change_lonlat topodata.nc OROMEA
module list
if [ "$MACHINE" == "levante" ]; then
export LD_LIBRARY_PATH=/sw/spack-levante/netcdf-fortran-4.5.3-jlxcfz/lib/:$LD_LIBRARY_PATH
elif [ "$MACHINE" == "albedo" ]; then
export LD_LIBRARY_PATH=/albedo/soft/sw/spack-sw/netcdf-fortran/4.5.4-yb7woqz/lib/:$LD_LIBRARY_PATH
tmp_mods=$( module list -t | tail -n +2 )
module purge
fi
echo ${RES_echam/T/} | ./calnoro > ${COUPLE_DIR}/calnoro_stdout_stderr 2>&1
if [ "$MACHINE" == "albedo" ]; then
module load $tmp_mods
fi
cdo -s -f nc -setgrid,$RES_echam -invertlat sso_par_fil.srv $ofile 2>>cdo_stderr_ice2echam
#rm sso_par_fil.srv topodata.nc
}
## @fn update_orography_generate_target_orography_file()
update_orography_generate_target_orography_file() {
echo; echo " * generating target orography file for linear stepwise change of echam6 orography"
ifile=${COUPLE_DIR}/calnoro_output.nc
ofile=${COUPLE_DIR}/target_orography_echam6_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.nc
typeset -A orography_names
orography_names[var51]=OROMEA
orography_names[var52]=OROSTD
orography_names[var53]=OROSIG
orography_names[var54]=OROGAM
orography_names[var55]=OROTHE
orography_names[var56]=OROPIC
orography_names[var57]=OROVAL
for varname in "${!orography_names[@]}"; do
ncrename -v ${varname},${orography_names[$varname]} ${ifile} tmp; mv tmp ${ifile}
done
## Ensure that we use the OROMEA from the "raw" PISM-corrected orography,
## not what is given from the calnoro program
############################################################
# Define the method for orography update (oro_update_mod):
# 1: use low-resolution reference; take all sub-grid scale parameters from CALNORO; take GEOSP from low-res input
# 2: use low-resolution reference; take all sub-grid scale parameters from CALNORO; take OROMEA and GEOSP from low-res input
# (like it has been done before)
# 3: use high-resolution reference; take all sub-grid scale parameterds from CALNORO; take OROMEA and GEOSP from high-res input
# 4: update orography but keep sub-grid scale parameters from unit.24
# 5: update orography but change sub-grid scale parameters only in ice domain where ice mask or change of ice mask (ice loss/ice gain)
if [ "x${oro_update_mod}" == "x1" ]; then # according to Martins script: OROMEA from calnoro; GEOSP from input
ECHAM_g=9.80665
cdo -s -sp2gp -gp2sp -mulc,${ECHAM_g} -chname,oromea,GEOSP \
-remapnn,${RES_echam}grid \
${COUPLE_DIR}/echam6_new_orography_before_calnoro_low_res.nc \
${COUPLE_DIR}/echam6_new_GEOSP_for_target_orog.nc 2>>cdo_stderr_ice2echam
# Make sure dimension order is correct:
change_lonlat ${COUPLE_DIR}/echam6_new_GEOSP_for_target_orog.nc GEOSP
cdo -s -O merge \
${ifile} \
${COUPLE_DIR}/echam6_new_GEOSP_for_target_orog.nc \
${ofile} 2>>cdo_stderr_ice2echam
elif [ "x${oro_update_mod}" == "x2" ]; then # old coupling scheme: OROMEA and GEOSP from low-res input
cdo -s -replace \
${ifile} \
-chname,oromea,OROMEA -remapnn,${RES_echam}grid ${COUPLE_DIR}/echam6_new_orography_before_calnoro_low_res.nc \
tmp 2>>cdo_stderr_ice2echam
mv tmp ${ifile}
# Take the GEOSP field from the "raw" PISM-corrected orography, and not
# what is given from the calnoro program
ECHAM_g=9.80665
cdo -s -sp2gp -gp2sp -mulc,${ECHAM_g} -chname,oromea,GEOSP \
-remapnn,${RES_echam}grid \
${COUPLE_DIR}/echam6_new_orography_before_calnoro_low_res.nc \
${COUPLE_DIR}/echam6_new_GEOSP_for_target_orog.nc 2>>cdo_stderr_ice2echam
# Make sure dimension order is correct:
change_lonlat ${COUPLE_DIR}/echam6_new_GEOSP_for_target_orog.nc GEOSP
cdo -s -O merge \
${ifile} \
${COUPLE_DIR}/echam6_new_GEOSP_for_target_orog.nc \
${ofile} 2>>cdo_stderr_ice2echam
elif [ "x${oro_update_mod}" == "x3" ]; then # new coupling scheme: OROMEA and GEOSP from high-res input
cdo -s -replace \
${ifile} \
-chname,oromea,OROMEA -remapnn,${RES_echam}grid ${COUPLE_DIR}/echam6_new_orography_before_calnoro_high_res.nc \
tmp 2>>cdo_stderr_ice2echam
mv tmp ${ifile}
# Take the GEOSP field from the "raw" PISM-corrected orography, and not
# what is given from the calnoro program
ECHAM_g=9.80665
cdo -s -sp2gp -gp2sp -mulc,${ECHAM_g} -chname,oromea,GEOSP \
-remapnn,${RES_echam}grid \
${COUPLE_DIR}/echam6_new_orography_before_calnoro_high_res.nc \
${COUPLE_DIR}/echam6_new_GEOSP_for_target_orog.nc 2>>cdo_stderr_ice2echam
# Make sure dimension order is correct:
change_lonlat ${COUPLE_DIR}/echam6_new_GEOSP_for_target_orog.nc GEOSP
cdo -s -O merge \
${ifile} \
${COUPLE_DIR}/echam6_new_GEOSP_for_target_orog.nc \
${ofile} 2>>cdo_stderr_ice2echam
#
elif [ "x${oro_update_mod}" == "x4" ]; then # new coupling scheme: OROMEA and GEOSP from high-res input but overwrite with unit.24 sub-grid parameters
cdo -s -replace \
${ifile} \
-chname,oromea,OROMEA -remapnn,${RES_echam}grid ${COUPLE_DIR}/echam6_new_orography_before_calnoro_high_res.nc \
tmp 2>>cdo_stderr_ice2echam
mv tmp ${ifile}
# Take the GEOSP field from the "raw" PISM-corrected orography, and not
# what is given from the calnoro program
ECHAM_g=9.80665
cdo -s -sp2gp -gp2sp -mulc,${ECHAM_g} -chname,oromea,GEOSP \
-remapnn,${RES_echam}grid \
${COUPLE_DIR}/echam6_new_orography_before_calnoro_high_res.nc \
${COUPLE_DIR}/echam6_new_GEOSP_for_target_orog.nc 2>>cdo_stderr_ice2echam
# Make sure dimension order is correct:
change_lonlat ${COUPLE_DIR}/echam6_new_GEOSP_for_target_orog.nc GEOSP
cdo -s -O merge \
${ifile} \
${COUPLE_DIR}/echam6_new_GEOSP_for_target_orog.nc \
${ofile} 2>>cdo_stderr_ice2echam
cdo -s replace ${ofile} ${INIT_DIR_echam}/unit.24 tmp_replace; mv tmp_replace ${ofile}
#
elif [ "x${oro_update_mod}" == "x5" ]; then # masked: new coupling scheme: OROMEA and GEOSP from high-res input
cdo -s -replace \
${ifile} \
-chname,oromea,OROMEA -remapnn,${RES_echam}grid ${COUPLE_DIR}/echam6_new_orography_before_calnoro_high_res.nc \
tmp 2>>cdo_stderr_ice2echam
mv tmp ${ifile}
# Take the GEOSP field from the "raw" PISM-corrected orography, and not
# what is given from the calnoro program
ECHAM_g=9.80665
cdo -s -sp2gp -gp2sp -mulc,${ECHAM_g} -chname,oromea,GEOSP \
-remapnn,${RES_echam}grid \
${COUPLE_DIR}/echam6_new_orography_before_calnoro_high_res.nc \
${COUPLE_DIR}/echam6_new_GEOSP_for_target_orog.nc 2>>cdo_stderr_ice2echam
# Make sure dimension order is correct:
change_lonlat ${COUPLE_DIR}/echam6_new_GEOSP_for_target_orog.nc GEOSP
cdo -s -O merge \
${ifile} \
${COUPLE_DIR}/echam6_new_GEOSP_for_target_orog.nc \
${ofile} 2>>cdo_stderr_ice2echam
if [ -L ${RESTART_DIR_echam}/restart_${EXP_ID}_echam.nc ]; then
rfile=$(readlink ${RESTART_DIR_echam}/restart_${EXP_ID}_echam.nc)
else
rfile=${RESTART_DIR_echam}/restart_${EXP_ID}_echam.nc
fi
cdo -s -ifthen ${COUPLE_DIR}/ice_domain_current_T63grid.nc -gtc,0 \
-add ${COUPLE_DIR}/ice_mask_current_T63grid.nc -selname,GLAC ${INIT_DIR_echam}/unit.24 \
${COUPLE_DIR}/mask_oro_update.nc
# cdo -s selname,glac ${rfile} ${COUPLE_DIR}/ice_mask_before_T63grid.nc
for VAR in "OROMEA" "OROSTD" "OROSIG" "OROGAM" "OROTHE" "OROPIC" "OROVAL"
do
cdo -s ifthenelse -setmisstoc,0 ${COUPLE_DIR}/mask_oro_update.nc \
-selname,${VAR} ${ofile} \
-selname,${VAR} ${INIT_DIR_echam}/unit.24 ${VAR}.orotmp
done
cdo -s merge *.orotmp ORO_merged #; mv ORO_merged ${ofile}; rm *.orotmp
cdo -s replace ${ofile} ORO_merged tmp; mv tmp ${ofile}
#
fi
############################################################
ln -sf ${COUPLE_DIR}/target_orography_echam6_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.nc ${COUPLE_DIR}/latest_target_orography.nc
# Destroy# the time dimension; just in case
#if ncdump -h# $ofile | grep -q "time"; then
# ncwa -a time $ofile tmp; mv tmp $ofile
#fi
echo; echo -e "\t\t- finished with target orography generation"
}
## @fn update_orography_set_namelist_modifications_next_echam_run()
update_orography_set_namelist_modifications_next_echam_run() {
echam_variable_modify_file=${COUPLE_DIR}/echam_namelist_switches_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.dat
echo; echo " * setting echam variables for update ororgapy to be read from file in next run"
echo " - echam_variable_modify_file=$echam_variable_modify_file"
if [ ! -s $echam_variable_modify_file ]; then
:> $echam_variable_modify_file
fi
echo "submodelctl___lupdate_orog___nml_entry=.TRUE." >> $echam_variable_modify_file
echo "submodelctl___lupdate_orog___nml_file=namelist.echam" >> $echam_variable_modify_file
add_to ${echam_variable_modify_file} echam_namelist_switches.dat config echam
}
## @fn set_glacial_mask_select_field()
set_glacial_mask_select_field() {
echo; echo " * selecting glacial mask"
cdo \
-selvar,${ice_glacial_mask_name} \
${COUPLE_DIR}/ice_file_for_atmosphere.nc \
${COUPLE_DIR}/ice_mask_current.nc 2>> cdo_stderr_ice2echam
mv ${COUPLE_DIR}/ice_mask_current.nc tmp
ncdump tmp | sed 's/byte/float/g' | ncgen -o tmp.nc; mv tmp.nc tmp
mv tmp ${COUPLE_DIR}/ice_mask_current.nc
rmlist="$rmlist ${COUPLE_DIR}/ice_mask_current.nc"
}
## @fn set_glacial_mask_echam_restart_file()
set_glacial_mask_echam_restart_file() {
if [ -L ${RESTART_DIR_echam}/restart_${EXP_ID}_echam.nc ]; then
ECHAM_RESTART_FILE=$(readlink ${RESTART_DIR_echam}/restart_${EXP_ID}_echam.nc)
else
ECHAM_RESTART_FILE=${RESTART_DIR_echam}/restart_${EXP_ID}_echam.nc
fi
ifile=${COUPLE_DIR}/ice_mask_current.nc
dfile=${COUPLE_DIR}/ice_domain_current.nc
cdo -s setrtoc,0,1,1 $ifile $dfile
echo; echo " * Setting ice mask in: $ECHAM_RESTART_FILE"
echo " from: $ifile"
echo " using region: $dfile"
# Define the entire area where pism is defined:
ncrename -v ${ice_glacial_mask_name},ice_mask $ifile
ncrename -v ${ice_glacial_mask_name},ice_domain $dfile
regrid_to_echam $ifile
regrid_to_echam $dfile
ifile=${ifile%.*}_${RES_echam}grid.nc
dfile=${dfile%.*}_${RES_echam}grid.nc
cdo -s -setmisstoc,0 $ifile tmp 2>> cdo_stderr_ice2echam; mv tmp $ifile
cdo -s -gec,${ECHAM_GLACIAL_THRESHOLD} $ifile tmp 2>> cdo_stderr_ice2echam; mv tmp $ifile # PG: Ensure the mask is binary after regridding.
change_lonlat $ifile ice_mask
change_lonlat $dfile ice_domain
# Restrict the ice mask on the echam grid to only be valid where echam land
cdo -s -setmisstoc,0 -ifthen -selvar,slm $ECHAM_RESTART_FILE $ifile tmp 2>> cdo_stderr_ice2echam; mv tmp $ifile
cp $ECHAM_RESTART_FILE ${ECHAM_RESTART_FILE%.*}_backup_before_glacial_mask_replacement.nc
# FIX: Skip ECHAM restart modification to avoid malloc error
# The original code using ncdump/ncgen/ncks corrupts the NetCDF structure
# JSBACH restart updates are sufficient for vegetation recovery
echo " - Skipping ECHAM restart glac modification (using backup as-is)"
echo " JSBACH restart files will be updated separately"
ncrename -v ice_mask,glac $ifile
}
## @fn set_glacial_mask_echam_make_dummy_jan_surf()
set_glacial_mask_echam_make_dummy_jan_surf() {
echo; echo " * Generating a dummy unit.24 file with new ice orography and mask"
# Generates a dummy unit.24 file by modifying a copy of the
# The echam restart file has already been modified with the "correct"
# glacial mask", construct a dummy one from the restart
#CLEANUP cp ${BASE_DIR}/${EXP_ID}/awicm/input/echam/unit.24 dummy_jan_surf.nc
cp ${INIT_DIR_echam}/unit.24 dummy_jan_surf.nc
# vvvvvvvvvvvvv LARS的代码加在这里 vvvvvvvvvvvvv
if [[ ${DOMAIN_pism} == "greenland" ]]; then
cdo setclonlatbox,0,0,360,0,90 -selvar,GLAC dummy_jan_surf.nc tmp
cdo -replace dummy_jan_surf.nc tmp dummy_jan_surf.nc2
mv dummy_jan_surf.nc2 dummy_jan_surf.nc
rm tmp
fi
# ^^^^^^^^^^^^ LARS的代码加在这里 ^^^^^^^^^^^^
# Fix Lu: ---------------------------
if [[ ${DOMAIN_pism} == "nhem" ]]; then
cdo setclonlatbox,0,0,360,0,90 -selvar,GLAC dummy_jan_surf.nc tmp
cdo -replace dummy_jan_surf.nc tmp dummy_jan_surf.nc2
mv dummy_jan_surf.nc2 dummy_jan_surf.nc
rm tmp
fi
# Fix Lu: ---------------------------
# FIX 2025-01-24: Check if target_orography file exists before using it
# The target_orography file is generated in update_orography step which runs AFTER update_glacial_mask
# On first coupling iteration, this file may not exist yet
if [ -f ${COUPLE_DIR}/target_orography_echam6_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.nc ]; then
cdo -s replace dummy_jan_surf.nc \
${COUPLE_DIR}/target_orography_echam6_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.nc \
tmp && mv tmp dummy_jan_surf.nc
elif [ -f ${COUPLE_DIR}/latest_target_orography.nc ]; then
# Try using the latest target orography from previous iteration
echo " - Using latest_target_orography.nc from previous iteration"
cdo -s replace dummy_jan_surf.nc \
${COUPLE_DIR}/latest_target_orography.nc \
tmp && mv tmp dummy_jan_surf.nc
else
echo " - WARNING: No target_orography file found, skipping orography update in dummy_jan_surf.nc"
echo " - This is expected on the first coupling iteration"
fi
# Fix Albedo where there are glaciers:
#echam_albedo_on_glaciers=0.7
echam_albedo_on_glaciers=${ECHAM_ALBEDO_ON_GLACIERS}
cdo -s \
-setmisstoc,${echam_albedo_on_glaciers} -setrtomiss,-1,2 \
${COUPLE_DIR}/ice_mask_current_${RES_echam}grid.nc \
input_if_mask_true.nc
cdo -s \
-selvar,ALB dummy_jan_surf.nc input_if_mask_false.nc
cdo -s -ifthenelse \
${COUPLE_DIR}/ice_mask_current_${RES_echam}grid.nc \
input_if_mask_true.nc \
input_if_mask_false.nc \
dummy_jan_surf_ALB.nc
cdo -s replace \
dummy_jan_surf.nc \
-chname,${echam_glacial_mask_name},ALB dummy_jan_surf_ALB.nc \
tmp; mv tmp dummy_jan_surf.nc
# Fix lakes where there are glaciers
cdo -s \
-setrtoc,0,1,0 -selvar,ALAKE dummy_jan_surf.nc input_if_mask_true.nc
cdo -s \
-selvar,ALAKE dummy_jan_surf.nc input_if_mask_false.nc
cdo -s -ifthenelse \
${COUPLE_DIR}/ice_mask_current_${RES_echam}grid.nc \
input_if_mask_true.nc \
input_if_mask_false.nc \
dummy_jan_surf_ALAKE.nc
cdo -s replace \
dummy_jan_surf.nc \
dummy_jan_surf_ALAKE.nc tmp #-chname,${echam_glacial_mask_name},ALAKE dummy_jan_surf_ALAKE.nc \
mv tmp dummy_jan_surf.nc
# === BUG FIX: 退冰区域GLAC应该从1变为0 ===
# 原代码bug: 退冰时保持原来的GLAC=1,导致desert_fpc=1,植被无法生长
# 修复: 直接使用PISM的ice_mask作为GLAC
# ice_mask=1 -> GLAC=1 (有冰川)
# ice_mask=0 -> GLAC=0 (无冰川,允许植被生长)
cdo -s \
-setmisstoc,1 -setrtomiss,-1,2 \
${COUPLE_DIR}/ice_mask_current_${RES_echam}grid.nc \
input_if_mask_true.nc
# 退冰区域应该设为0,而不是保持原来的GLAC值
cdo -s \
-setmisstoc,0 -setrtomiss,-1,2 \
${COUPLE_DIR}/ice_mask_current_${RES_echam}grid.nc \
input_if_mask_false.nc
cdo -s -ifthenelse \
${COUPLE_DIR}/ice_mask_current_${RES_echam}grid.nc \
input_if_mask_true.nc \
input_if_mask_false.nc \
dummy_jan_surf_GLAC.nc
cdo -s replace \
dummy_jan_surf.nc \
-chname,${echam_glacial_mask_name},GLAC dummy_jan_surf_GLAC.nc \
tmp; mv tmp dummy_jan_surf.nc
# === AUTO-UPDATE unit.24 GLAC mask (input + run/input + work) ===
# Why this is needed:
# - ECHAM reads ${WORK_DIR}/unit.24 at startup
# - esm_runscripts may restore ${WORK_DIR}/unit.24 from run/input/echam/unit.24
# - Therefore BOTH must have the same (updated) GLAC mask as used in jsbach.nc (and PISM ice mask)
#
# IMPORTANT:
# We update ONLY the GLAC variable in unit.24 via "cdo replace" to avoid relying on timestamps/metadata
# and to keep other unit.24 fields untouched.
echo " - Updating GLAC in unit.24 files (input + run/input + work)"
cdo -s -chname,${echam_glacial_mask_name},GLAC dummy_jan_surf_GLAC.nc unit24_glac_update.nc
# 1) Experiment-level input (used as template in some setups)
if [ -f "${INIT_DIR_echam}/unit.24" ]; then
cdo -s replace ${INIT_DIR_echam}/unit.24 unit24_glac_update.nc tmp_unit24 && mv tmp_unit24 ${INIT_DIR_echam}/unit.24
echo " --> Updated: ${INIT_DIR_echam}/unit.24"
else
echo " WARNING: ${INIT_DIR_echam}/unit.24 not found"
fi
# 2) Current run work directory (ECHAM reads this)
if [ -n "${WORK_DIR}" ] && [ -f "${WORK_DIR}/unit.24" ]; then
cdo -s replace ${WORK_DIR}/unit.24 unit24_glac_update.nc tmp_unit24 && mv tmp_unit24 ${WORK_DIR}/unit.24
echo " --> Updated: ${WORK_DIR}/unit.24"
else
echo " WARNING: WORK_DIR not set or ${WORK_DIR}/unit.24 not found"
fi
# 3) Current run input copy (may be used to restore protected files into work/)
if [ -n "${WORK_DIR}" ] && [ -f "${WORK_DIR}/../input/echam/unit.24" ]; then
cdo -s replace ${WORK_DIR}/../input/echam/unit.24 unit24_glac_update.nc tmp_unit24 && mv tmp_unit24 ${WORK_DIR}/../input/echam/unit.24
echo " --> Updated: ${WORK_DIR}/../input/echam/unit.24"
else
echo " WARNING: run/input/echam/unit.24 not found (WORK_DIR/../input/echam/unit.24)"
fi
# === END AUTO-UPDATE ===
}
## @fn set_glacial_mask_jsbach_update_init_file()
set_glacial_mask_jsbach_update_init_file() {
echo; echo " * Generating a new JSBACH init file with updated land cover types"
# Get the Program and required .mod files
if [ -f ${FUNCTION_PATH}/../utils/jsbach_init_file ]; then
cp ${FUNCTION_PATH}/../utils/jsbach_init_file .
elif [ -f ${FUNCTION_PATH}/../utils/install_jsbach_init_file.sh ]; then
echo " install_jsbach_init_file.sh found. Running installation ..."
currentdir=$(pwd)
cd ${FUNCTION_PATH}/../utils/
./install_jsbach_init_file.sh ${MACHINE}
cd ${currentdir}
cp -v ${FUNCTION_PATH}/../utils/jsbach_init_file .
else
echo -e "\t\t E R O R R --- binary missing, don't know how to proceed"
exit
fi
cp ${FUNCTION_PATH}/../utils/_build/mo_kinds.mod .
cp ${FUNCTION_PATH}/../utils/_build/mo_vegparams.mod .
cleanup_list="$cleanup_list jsbach_init_file mo_kinds.mod mo_vegparams.mod"
# Here we place a slimmed-down version of the Veronika Gayler script:
res_atm=${RES_echam}
res_oce=GENERIC
ntiles=11 # number of jsbach tiles
dynveg=true # setup for dynamic vegetation
c3c4crop=true # differentiate between C3 and C4 crops
lpasture=true # distinguish pastures from grasses
read_pasture=LUH2v2h # LUH: read pastures and crops from LUH states as in CMIP5
# LUH2v2h: read pastures and crops from LUH2 states as in CMIP6
# false: no separate input file for crops and pastures
pasture_rule=true # allocate pastures primarily on grass lands
year_ct=1850 # year the cover_types are derived from
year_cf=1850 # year cover fractions are derived from
landcover_series=false # generate a series of files with cover_types of
# year_ct and fractions from year_cf to year_cf2
year_cf2=1859 # only used with landcover_series
echam_fractional=false # initial file for echam runs with fractional
# land sea mask
masks_file=default # file with land sea mask (default: use echam land sea mask)
mv dummy_jan_surf.nc ${RES_echam}${res_oce}_jan_surf.nc
echo " - Using unit.24 (jan surf) file: ${RES_echam}GENERIC_jan_surf.nc"
echo " --> (This is the dummy file after ice update)"
ln -sf ${INIT_DIR_echam}/unit.91 ${res_atm}${res_oce}_VGRATCLIM.nc
echo " - Using unit.91 (VGRATCLIM) file: ${INIT_DIR_echam}/unit.91"
ln -sf ${INIT_DIR_echam}/unit.90 ${res_atm}${res_oce}_VLTCLIM.nc
echo " - Using unit.90 (VLTCLIM) file: ${INIT_DIR_echam}/unit.90"
cleanup_list="$cleanup_list ${RES_echam}_jan_surf.nc"
pool=${POOL_DIR_echam}/ECHAM6/${RES_echam}
pool_land=${POOL_DIR_jsbach}/JSBACH/prepare/${RES_echam}
#------------------------------------------------------------------------------
# prepare the namelist
#------------------------------------------------------------------------------
[[ ${res_oce} = "" ]] && lcouple=.false. || lcouple=.true.
[[ ${dynveg} = true ]] && ldynveg=.true. || ldynveg=.false.
[[ ${c3c4crop} = true ]] && lc3c4crop=.true. || lc3c4crop=.false.
[[ ${read_pasture} != false ]] && lread_pasture=.true. || lread_pasture=.false.
[[ ${landcover_series} = false ]] && year_cf2=${year_cf}
desert_only=.false. # setup for a desert-only experiment
grass_only=.false. # setup for a grass-only experiment
woods_only=.false. # setup for a woods-only experiment
cat > namelist <<EOF
&INITCTL
res_atm="${res_atm}"
res_oce="${res_oce}"
ntiles=${ntiles}
nlct=21
year_ct=${year_ct}
year_cf=${year_cf}
lcouple=${lcouple}
ldynveg=${ldynveg}
lc3c4crop=${lc3c4crop}
lpasture=${lpasture}
lread_pasture=${lread_pasture}
pasture_tag="${read_pasture}"
lpasture_rule=.${pasture_rule}.
echam_fractional=.${echam_fractional}.
masks_file="${masks_file##*/}"
desert_only=${desert_only}
grass_only=${grass_only}
woods_only=${woods_only}
cover_fract_only=.${landcover_series}.
info=.true.
/
EOF
echo -e "\t\t - This namelist was passed to the jsbach_init program:"
cat namelist
ln -sf ${pool}/${res_atm}_TSLCLIM2.nc .
if [ $(echo ${res_atm} | cut -c1) != T ]; then
res_atmg=${res_atm}
else
res_atmg=${res_atm}gauss
fi
if [[ ${read_pasture} = false ]]; then
ln -sf ${pool_land}/vegtype_${year_cf}_${res_atmg}_pa14.nc\
vegtype_${year_cf}_${res_atm}gauss_pa14.nc
if [[ ${year_cf} != ${year_ct} ]]; then
ln -sf ${pool_land}/vegtype_${year_ct}_${res_atmg}_pa14.nc \
vegtype_${year_ct}_${res_atm}gauss_pa14.nc
fi
fi
ln -sf ${pool_land}/vegmax_6_${res_atm}_0-360.nc \
vegmax_6_${res_atm}.nc
ln -sf ${pool_land}/${res_atm}_topo_75.nc .
ln -sf ${pool_land}/albedo_${res_atm}.nc .
ln -sf ${pool_land}/C3C4_mask_${res_atmg}.nc \
C3C4_mask_${res_atm}gauss.nc
ln -sf ${pool_land}/potveg_${res_atm}.nc .
if [[ ${c3c4crop} = true ]]; then
if [[ ${read_pasture} = LUH2v2h ]]; then
ln -sf ${pool_land}/C3C4_crop_LUH2v2h_${res_atm}.nc \
C3C4_crop_${res_atm}.nc
else
ln -sf ${pool_land}/C3C4_crop_${res_atm}.nc \
C3C4_crop_${res_atm}.nc
fi
fi
if [[ ${read_pasture} = LUH2v2h ]]; then
if [[ ${dynveg} = true ]]; then
LUH_states=LUH2v2h_states_${res_atm}_dynveg.nc
else
LUH_states=LUH2v2h_states_${res_atm}_all-oceans_no-dynveg.nc
fi
elif [[ ${read_pasture} = LUH ]]; then
LUH_states=LUH_states_${res_atm}.nc
fi
if [[ ${read_pasture} != false ]]; then
if [[ -f ${pool_land}/${LUH_states} ]]; then
ln -sf ${pool_land}/${LUH_states} .
elif [[ -f ${pool_land}/${LUH_states}.gz ]]; then
cp ${pool_land}/${LUH_states}.gz .
gunzip ${LUH_states}.gz
fi
#typeset -Z4 yr=${year_cf}
yr=${year_cf}
while [[ ${yr} -le ${year_cf2} ]]; do
cdo -s selyear,${yr} ${LUH_states} LUH_states_${yr}_${res_atm}.nc
(( yr = yr + 1 ))
done
fi
[[ ${masks_file} != default && ! -f ${masks_file##*/} ]] && ln -sf ${masks_file} .
ln -sf ${pool_land}/soil_parameters_${res_atm}.nc .
echo " - Running jsbach_init_file (working dir: `pwd`)"
chmod 755 jsbach_init_file
./jsbach_init_file > ${COUPLE_DIR}/jsbach_init_file_stdout_stderr 2>&1
if [[ $? -eq 0 ]]; then
echo -e "\t\t - ...finished with external program jsbach_init_file!"
else
echo "error in jsbach_init_file"
exit 1
fi
# BUG: How do we know what the output file is called?
mv jsbach_T63GENERIC_11tiles_5layers_1850_dynveg.nc \
${COUPLE_DIR}/jsbach_init_file_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.nc
# === Fix tiny values (1e-10) in cover_fract ===
# The jsbach_init_file program generates 1e-10 values for tiles that should be 0.
# These tiny values cause JSBACH's fpc_to_cover_fract_pot validation to fail.
echo -e "\t\t - Fixing tiny cover_fract values in jsbach.nc..."
if [ -f ${FUNCTION_PATH}/../utils/fix_jsbach_tiny_values.py ]; then
/sw/spack-levante/mambaforge-22.9.0-2-Linux-x86_64-kptncg/bin/python3 \
${FUNCTION_PATH}/../utils/fix_jsbach_tiny_values.py \
${COUPLE_DIR}/jsbach_init_file_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.nc \
>> ${COUPLE_DIR}/jsbach_init_file_stdout_stderr 2>&1
if [ $? -eq 0 ]; then
echo -e "\t\t - ...tiny values fixed successfully!"
else
echo -e "\t\t - WARNING: fix_jsbach_tiny_values.py failed, continuing anyway"
fi
else
echo -e "\t\t - WARNING: fix_jsbach_tiny_values.py not found, skipping"
fi
# === End fix tiny values ===
echo :> ${COUPLE_DIR}/jsbach_init_override_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.dat
############################################################################
# LA UPDATE
ln -sf ${COUPLE_DIR}/jsbach_init_file_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.nc ${COUPLE_DIR}/latest_jsbach_init_file.nc
#cp ${COUPLE_DIR}/jsbach_init_file_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.nc ${WORK_DIR}/jsbach.nc
############################################################################
echo "LAND_BOUNDARY_CONDITIONS_jsbach=${COUPLE_DIR}/jsbach_init_file_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.nc" > ${COUPLE_DIR}/jsbach_init_override_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.dat
add_to ${COUPLE_DIR}/jsbach_init_override_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.dat jsbach_init_override.dat config jsbach
}
set_glacial_mask_jsbach_update_restart_jsbach() {
echo; echo " * updating land cover fractions in jsbach restart file >>jsbach<<"
# === DISABLED: pack/unpack process corrupts the restart file ===
# The unpack -> modify -> pack workflow introduces floating-point precision
# errors that cause JSBACH to fail with "sum of cover_fract_pot /= 1" error.
# The jsbach.nc file (cover_fract) is already updated by jsbach_init_file.
# Dynamic vegetation will handle the actual vegetation dynamics.
echo -e "\t\t- SKIPPED: jsbach restart modification disabled (pack/unpack corrupts file)"
newest_jsbach_restart_file=$(readlink ${RESTART_DIR_jsbach}/restart_${EXP_ID}_jsbach.nc)
ln -sf ${newest_jsbach_restart_file} ${COUPLE_DIR}/latest_jsbach_restart_file.nc
# === Fix tiny values in jsbach restart file ===
# The jsbach restart may contain 1e-10 values from previous model runs
echo -e "\t\t- Fixing tiny values in jsbach restart file..."
if [ -f ${FUNCTION_PATH}/../utils/fix_jsbach_tiny_values.py ]; then
/sw/spack-levante/mambaforge-22.9.0-2-Linux-x86_64-kptncg/bin/python3 \
${FUNCTION_PATH}/../utils/fix_jsbach_tiny_values.py \
${newest_jsbach_restart_file} --restart \
>> ${COUPLE_DIR}/jsbach_restart_fix_stdout_stderr 2>&1
if [ $? -eq 0 ]; then
echo -e "\t\t- ...jsbach restart tiny values fixed!"
else
echo -e "\t\t- WARNING: jsbach restart fix failed, continuing anyway"
fi
fi
# === End fix ===
# === FIX 2025-01-24: Update cover_fract in jsbach restart to match jsbach.nc ===
# Without this fix, cover_fract in restart shows glacier (Tile 1=1) while
# jsbach.nc shows non-glacier (e.g., Tile 7=1), causing JSBACH to crash with
# "sum of cover_fract_pot /= 1" error in fpc_to_cover_fract_pot function.
# FIX 2025-01-25: Create reference file HERE if it doesn't exist yet
# This file is needed to identify truly deglaciated areas
JSBACH_REFERENCE_FILE=${COUPLE_DIR}/jsbach_original_reference.nc
if [ ! -f ${JSBACH_REFERENCE_FILE} ]; then
echo -e "\t\t- Creating reference file: ${JSBACH_REFERENCE_FILE}"
cp ${FORCING_DIR_jsbach}/jsbach.nc ${JSBACH_REFERENCE_FILE}
fi
echo -e "\t\t- Updating cover_fract in jsbach restart to match jsbach.nc..."
if [ -f ${FUNCTION_PATH}/../utils/fix_jsbach_restart_cover_fract.py ]; then
# IMPORTANT: Use jsbach_original_reference.nc to identify truly deglaciated areas
# This avoids incorrectly modifying non-glacier Tile1 points (like tropical evergreen)
/sw/spack-levante/mambaforge-22.9.0-2-Linux-x86_64-kptncg/bin/python3 \
${FUNCTION_PATH}/../utils/fix_jsbach_restart_cover_fract.py \
${newest_jsbach_restart_file} \
${COUPLE_DIR}/jsbach_init_file_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.nc \
${JSBACH_REFERENCE_FILE} \
>> ${COUPLE_DIR}/jsbach_restart_cover_fract_fix_stdout_stderr 2>&1
if [ $? -eq 0 ]; then
echo -e "\t\t- ...cover_fract in jsbach restart updated successfully!"
else
echo -e "\t\t- WARNING: cover_fract fix failed, continuing anyway"
fi
else
echo -e "\t\t- WARNING: fix_jsbach_restart_cover_fract.py not found"
fi
# === End cover_fract fix ===
# === FIX 2025-01-24: Sync jsbach restart to WORK_DIR ===
# esm_tools copies restart files to WORK_DIR before coupling scripts run,
# so our modifications in restart directory are not reflected in WORK_DIR.
if [ -n "${WORK_DIR}" ] && [ -d "${WORK_DIR}" ]; then
echo -e "\t\t- Syncing fixed jsbach restart to WORK_DIR..."
if [ -f "${newest_jsbach_restart_file}" ]; then
cp -v "${newest_jsbach_restart_file}" "${WORK_DIR}/restart_${EXP_ID}_jsbach.nc"
echo -e "\t\t- jsbach restart synced to WORK_DIR successfully"
fi
fi
# === End WORK_DIR sync ===
return
# === END DISABLED ===
echo -e "\t\t- Making a temporary directory to work in"
startdir=$(pwd)
if [ -d ${COUPLE_DIR}/update_land_cover_fractions ]; then
# Throw away the temporary working directory from last time to
# avoid merge problems in the next coupling iteration:
rm -rf ${COUPLE_DIR}/update_land_cover_fractions
fi
mkdir -p ${COUPLE_DIR}/update_land_cover_fractions; cd ${COUPLE_DIR}/update_land_cover_fractions
echo -e "\t\t- determining where the glacial mask has advanced and retreated"
# === Fix for iterative coupling: use fixed reference file instead of updating jsbach.nc ===
JSBACH_REFERENCE_FILE=${COUPLE_DIR}/jsbach_original_reference.nc
if [ ! -f ${JSBACH_REFERENCE_FILE} ]; then
echo -e "\t\t- Creating reference file for future comparisons: ${JSBACH_REFERENCE_FILE}"
cp ${FORCING_DIR_jsbach}/jsbach.nc ${JSBACH_REFERENCE_FILE}
fi
# === End of fix ===
echo -e "\t\t- New JSBACH Init File: ${COUPLE_DIR}/jsbach_init_file_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.nc"
echo -e "\t\t- Reference JSBACH File: ${JSBACH_REFERENCE_FILE}"
cdo -s -sub \
-selvar,glac ${COUPLE_DIR}/jsbach_init_file_${CHUNK_START_DATE_echam}-${CHUNK_END_DATE_echam}.nc \
-selvar,glac ${JSBACH_REFERENCE_FILE} \
${COUPLE_DIR}/update_land_cover_fractions/jsbach_glac_diff.nc 2>> ${COUPLE_DIR}/update_land_cover_fractions/cdo_stderr_ice2echam
# Make two masks: one for glacial retreat, and one for glacial advance
cdo -s gtc,0 ${COUPLE_DIR}/update_land_cover_fractions/jsbach_glac_diff.nc ${COUPLE_DIR}/update_land_cover_fractions/new_glaciers.nc
cdo -s gtc,0 -mulc,-1 ${COUPLE_DIR}/update_land_cover_fractions/jsbach_glac_diff.nc ${COUPLE_DIR}/update_land_cover_fractions/new_non_glaciers.nc
sum_new_glaciers=$(cdo -s -output -fldsum -gtc,0 ${COUPLE_DIR}/update_land_cover_fractions/jsbach_glac_diff.nc | tr -d "[:blank:]")
sum_new_land=$(cdo -s -output -fldsum -gtc,0 -mulc,-1 ${COUPLE_DIR}/update_land_cover_fractions/jsbach_glac_diff.nc | tr -d "[:blank:]")
echo -e "\t\t- Total number of new glacial cells: >>>$sum_new_glaciers<<<"
echo -e "\t\t- Total number of new deglacial cells: >>>$sum_new_land<<<"
if [[ $sum_new_glaciers -eq 0 ]] && [[ $sum_new_land -eq 0 ]]; then
echo -e "\t\t- >>>INFO<<< No new land or new glaciers, this routine will be skipped..."
newest_jsbach_restart_file=$(readlink ${RESTART_DIR_jsbach}/restart_${EXP_ID}_jsbach.nc)
ln -sf ${newest_jsbach_restart_file} ${COUPLE_DIR}/latest_jsbach_restart_file.nc
return
fi
newest_jsbach_restart_file=$(readlink ${RESTART_DIR_jsbach}/restart_${EXP_ID}_jsbach.nc)
# Make a backup copy
#cp $(readlink ${RESTART_DIR_jsbach}/restart_${EXP_ID}_jsbach.nc) ${RESTART_DIR_jsbach}/restart_${EXP_ID}_jsbach_backup_$(date +%Y%m%d%H%M%S).nc
cp ${RESTART_DIR_jsbach}/restart_${EXP_ID}_jsbach.nc ${RESTART_DIR_jsbach}/restart_${EXP_ID}_jsbach_backup_$(date +%Y%m%d%H%M%S).nc
echo -e "\t\t- Unpacking JSBACH restart file: $newest_jsbach_restart_file"
# Ensure that the unpack binary is here:
if [ ! -f unpack ]; then
if [ -f ${FUNCTION_PATH}/../utils/unpack ]; then
#cp -v ${FUNCTION_PATH}/../utils/unpack ${COUPLE_DIR}/
cp -v ${FUNCTION_PATH}/../utils/unpack .
elif [ -f ${FUNCTION_PATH}/../utils/install_jsbach_pack_unpack.sh ]; then
echo " install_jsbach_pack_unpack.sh found. Running installation ..."
currentdir=$(pwd)