1111import numbers
1212import numpy as np
1313import click
14- import datetime
1514
1615
1716__all__ = ["load_boundary" ]
@@ -210,10 +209,32 @@ def __init__(self, hgrid,bc_yaml=None,boundary_segments=None):
210209 "none" : 0 ,
211210 }
212211
213- ## also reading tracer modules defined in the bctides yaml here
212+ ## also read in tracer modules defined in the bctides yaml here
214213 self .tracer_modules = bc_yaml .get ("modules" , [])
215-
216-
214+ ## find out number of tracer modules and set up tracer order list
215+ ## accroding to the self.tracer_lst defined above, if module not found
216+ ## module section in yaml file looks like:
217+ ## modules:
218+ ## - name: GEN
219+ ## num_tracers: 1
220+ ## - name: SED
221+ ## num_tracers: 5
222+
223+ self .tracer_mod_pos = {}
224+ self .tracer_mod_num = {}
225+ tracer_mods = []
226+ for mod in self .tracer_modules :
227+ mod_id = mod .get ("name" )
228+ num_tracers = mod .get ("num_tracers" , 0 )
229+ if mod_id in self .tracer_lst .keys ():
230+ tracer_mods .append (mod_id )
231+ self .tracer_mod_num [mod_id ] = num_tracers
232+ else :
233+ raise ValueError ("tracer module %s not recognized" % mod )
234+ tracer_mods .sort (key = lambda d : self .tracer_lst [d ])
235+ for kk in range (len (tracer_mods )):
236+ self .tracer_mod_pos [tracer_mods [kk ]] = kk
237+
217238
218239 def _norm_earth (self ,consts ):
219240 out = []
@@ -593,28 +614,28 @@ def write_bctides(self, bctides_file):
593614 % (bname_yaml , bname_hgrid )
594615 )
595616
596- num_tracer_mod = 0
597- tracer_mods = []
598- tracer_mod_pos = {}
599- for i in range (num_open_boundaries ):
600- if "tracers" in self .open_boundaries [i ].keys ():
601- boundary_tracer_mod_num = len (
602- self .open_boundaries [i ]["tracers" ]
603- )
604- if boundary_tracer_mod_num > num_tracer_mod :
605- num_tracer_mod = boundary_tracer_mod_num
606- for j in range (boundary_tracer_mod_num ):
607- tracer_mod = self .open_boundaries [i ]["tracers" ][j ]["tracer" ]
608- if not (tracer_mod in self .tracer_lst .keys ()):
609- raise ValueError (
610- tracer_mod + " is not a supported tracer module\n "
611- )
612- else :
613- if not (tracer_mod in tracer_mods ):
614- tracer_mods .append (tracer_mod )
615- tracer_mods .sort (key = lambda d : self .tracer_lst [d ])
616- for kk in range (len (tracer_mods )):
617- tracer_mod_pos [tracer_mods [kk ]] = kk
617+ # num_tracer_mod = 0
618+ # tracer_mods = []
619+ # tracer_mod_pos = {}
620+ # for i in range(num_open_boundaries):
621+ # if "tracers" in self.open_boundaries[i].keys():
622+ # boundary_tracer_mod_num = len(
623+ # self.open_boundaries[i]["tracers"]
624+ # )
625+ # if boundary_tracer_mod_num > num_tracer_mod:
626+ # num_tracer_mod = boundary_tracer_mod_num
627+ # for j in range(boundary_tracer_mod_num):
628+ # tracer_mod = self.open_boundaries[i]["tracers"][j]["tracer"]
629+ # if not (tracer_mod in self.tracer_lst.keys()):
630+ # raise ValueError(
631+ # tracer_mod + " is not a supported tracer module\n"
632+ # )
633+ # else:
634+ # if not (tracer_mod in tracer_mods):
635+ # tracer_mods.append(tracer_mod)
636+ # tracer_mods.sort(key=lambda d: self.tracer_lst[d])
637+ # for kk in range(len(tracer_mods)):
638+ # tracer_mod_pos[tracer_mods[kk]] = kk
618639
619640 for i in range (num_open_boundaries ):
620641 print ("processing open boundary %s " % self .open_boundaries [i ]["name" ])
@@ -693,7 +714,7 @@ def write_bctides(self, bctides_file):
693714 )
694715
695716 ## output tracer boundary
696- tracer_boundary_sources = [0 ] * num_tracer_mod
717+ tracer_boundary_sources = [0 ] * len ( self . tracer_mod_pos )
697718 ## this list save sorted tracer boundary index according to SCHISM code order
698719 tracer_boundary_lst_sorted = []
699720 if "tracers" in self .open_boundaries [i ].keys ():
@@ -702,11 +723,30 @@ def write_bctides(self, bctides_file):
702723 )
703724 for j in range (boundary_tracer_mod_num ):
704725 tracer_boundary = self .open_boundaries [i ]["tracers" ][j ]["source" ]
705- tracer_mod = self .open_boundaries [i ]["tracers" ][j ]["tracer" ]
726+ tracer_mod = self .open_boundaries [i ]["tracers" ][j ]["module" ]
727+ ## if tracer mod not in self.tracer_mod_pos, raise error
728+ if not (tracer_mod in self .tracer_mod_pos .keys ()):
729+ raise ValueError (
730+ tracer_mod + " is not in specified tracer module list at the begining \
731+ of bctide yamal\n " )
732+
706733 tracer_boundary_key = tracer_boundary
734+ ## set boundary key to "constant" if source is a number or list of numbers
707735 if isinstance (tracer_boundary , numbers .Number ):
708736 tracer_boundary_key = "constant"
709737 elif isinstance (tracer_boundary , list ):
738+ boundary_tracer_mod_num = len (tracer_boundary )
739+ ## if curent tracer mod has multiple tracers, check if the
740+ ## length of tracer_boundary match the number of tracers defined
741+ ## in self.tracer_mod_num
742+ if boundary_tracer_mod_num != self .tracer_mod_num [tracer_mod ]:
743+ raise ValueError ( self .open_boundaries [i ]["name" ] + ": "
744+ tracer_mod + " boundary source length "
745+ + str (boundary_tracer_mod_num )
746+ + " does not match number of tracers defined in module section: "
747+ + str (self .tracer_mod_num [tracer_mod ])
748+ )
749+
710750 if all (
711751 isinstance (x , numbers .Number )
712752 for x in tracer_boundary
@@ -720,12 +760,12 @@ def write_bctides(self, bctides_file):
720760 + " boundary is not supported"
721761 )
722762 tracer_boundary_source = self .tracer_source [tracer_boundary_key ]
723- pos = tracer_mod_pos [tracer_mod ]
763+ pos = self . tracer_mod_pos [tracer_mod ]
724764 tracer_boundary_sources [pos ] = tracer_boundary_source
725765 tracer_boundary_lst_sorted .append (j )
726766
727767 tracer_boundary_lst_sorted .sort (
728- key = lambda jj : tracer_mod_pos [
768+ key = lambda jj : self . tracer_mod_pos [
729769 self .open_boundaries [i ]["tracers" ][jj ]["tracer" ]
730770 ]
731771 )
@@ -734,7 +774,7 @@ def write_bctides(self, bctides_file):
734774 outf .write (str (vel_id ) + " " )
735775 outf .write (str (temp_id ) + " " )
736776 outf .write (str (salt_id ) + " " )
737- for ii in range (num_tracer_mod ):
777+ for ii in range (len ( self . tracer_mod_pos ) ):
738778 outf .write (str (tracer_boundary_sources [ii ]) + " " )
739779 outf .write ("\n " )
740780
0 commit comments