@@ -94,6 +94,61 @@ def _do_append_user_nl(user_nl_filename):
9494 for i in range (1 , ninst + 1 ):
9595 _do_append_user_nl (f"user_nl_{ model } _{ str (i ).zfill (4 )} " )
9696
97+ def remove_user_nl (model , vars , log_title = True , out = None ):
98+ """Remove changes to a given user_nl file.
99+
100+ Parameters
101+ ----------
102+ model : str
103+ The model whose user_nl file will be modified.
104+ vars : list
105+ list of variables to remove
106+ log_title: bool, optional
107+ If True, print the log title "Adding parameter changes to user_nl_filename".
108+ out : Output, optional
109+ The output widget to use for displaying log messages
110+ """
111+
112+ # confirm var_val_pairs is a list of tuples:
113+ assert isinstance (var_val_pairs , list )
114+
115+ out = DummyOutput () if out is None else out
116+
117+ caseroot = cvars ["CASEROOT" ].value
118+ ninst = cvars ["NINST" ].value
119+
120+ def _do_remove_user_nl (user_nl_filename ):
121+ # Print the changes to the user_nl file:
122+ with out :
123+ if log_title :
124+ print (f"{ COMMENT } Removing parameter changes to { user_nl_filename } :{ RESET } \n " )
125+ for var in vars :
126+ print (f" { var } " )
127+ print ("" )
128+
129+ # Renove the changes to the user_nl file:
130+ with open (Path (caseroot ) / user_nl_filename , "a" ) as f :
131+ lines = f .readlines ()
132+
133+ # Keep lines that do NOT define any of the variables
134+ vars_set = set (vars )
135+ new_lines = [
136+ line for line in lines
137+ if not any (line .strip ().startswith (var ) for var in vars_set )
138+ ]
139+
140+ # Write back filtered lines
141+ with open (user_nl_path , "w" ) as f :
142+ f .writelines (new_lines )
143+
144+
145+ ninst = 1 if ninst is None else ninst
146+ if ninst == 1 :
147+ _do_remove_user_nl (f"user_nl_{ model } " )
148+ else :
149+ for i in range (1 , ninst + 1 ):
150+ _do_remove_user_nl (f"user_nl_{ model } _{ str (i ).zfill (4 )} " )
151+
97152def xmlchange (var , val , do_exec = True , is_non_local = False , out = None ):
98153 """Apply custom xml changes to the case.
99154
@@ -124,3 +179,35 @@ def xmlchange(var, val, do_exec=True, is_non_local=False, out=None):
124179 runout = subprocess .run (cmd , shell = True , capture_output = True , cwd = caseroot )
125180 if runout .returncode != 0 :
126181 raise RuntimeError (f"Error running { cmd } ." )
182+
183+ def xmlquery (var , do_exec = True , is_non_local = False , out = None ):
184+ """Query custom xml variables in the case.
185+
186+ Parameters
187+ ----------
188+ do_exec : bool
189+ If True, execute the commands. If False, only print them.
190+ is_non_local : bool
191+ If True, the case is being created on a machine different from the one
192+ that runs visualCaseGen.
193+ out : Output
194+ The output widget to use for displaying log messages.
195+ """
196+
197+ caseroot = cvars ["CASEROOT" ].value
198+
199+ cmd = f"./xmlquery { var } "
200+ if is_non_local is True :
201+ cmd += " --non-local"
202+
203+ out = DummyOutput () if out is None else out
204+ with out :
205+ print (f"{ cmd } \n " )
206+
207+ if not do_exec :
208+ return
209+
210+ runout = subprocess .run (cmd , shell = True , capture_output = True , cwd = caseroot )
211+ if runout .returncode != 0 :
212+ raise RuntimeError (f"Error running { cmd } ." )
213+ return runout .stdout .strip ()
0 commit comments