2020cfg = get_ctdcal_config ()
2121
2222
23+ # Constants
24+ # ---------
25+
26+
2327# Function definitions
2428# --------------------
2529
@@ -38,6 +42,7 @@ def load_user_config(cfgfile):
3842 -------
3943 Munch object
4044 """
45+ cfgfile = validate_file (cfgfile )
4146 with open (cfgfile , 'r' ) as f :
4247 cfg = yaml .safe_load (f )
4348 return munchify (cfg )
@@ -202,6 +207,22 @@ def load_exchange_btl(btl_file: Union[str, Path]) -> pd.DataFrame:
202207 )
203208
204209
210+ # File exports
211+ def list_to_file (fname , outdir , lst ):
212+ """
213+ Writes a list to a file, one list item to a line.
214+
215+ Parameters
216+ ----------
217+ fname : str or Path-like
218+ outdir : str or Path-like
219+ lst : iterable
220+ """
221+ outdir = validate_dir (outdir )
222+ outfile = Path (outdir , fname )
223+ with open (outfile , 'w' ) as f :
224+ f .write ('\n ' .join (lst ))
225+
205226def load_exchange_ctd (
206227 ctd_file : Union [str , Path , BufferedIOBase ],
207228 n_files = None ,
@@ -300,17 +321,74 @@ def load_exchange_ctd(
300321 )
301322
302323
303- ## Cast list manipulation
304- def make_ssscc_list ( fname = "data/ssscc .csv" ):
324+ ## Cast list functions
325+ def make_cast_id_list ( rawdir , outdir = None , pattern = '*.hex' , fname = 'cast_id_list .csv' ):
305326 """
306327 Attempt to automatically generate list of station/casts from raw files.
307328 """
308- raw_files = Path (cfg .dirs ["raw" ]).glob ("*.hex" )
309- ssscc_list = sorted ([f .stem for f in raw_files ])
310- pd .Series (ssscc_list , dtype = str ).to_csv (fname , header = None , index = False , mode = "x" )
329+ search_dir = validate_dir (Path (rawdir ))
330+ raw_files = Path (search_dir ).glob (pattern )
331+ cast_id_list = sorted ([f .stem for f in raw_files ])
332+ if len (cast_id_list ) < 1 :
333+ raise FileNotFoundError ('No raw data files found.' )
334+ # pd.Series(casts, dtype=str).to_csv(fname, header=None, index=False, mode="x")
335+ if outdir is not None :
336+ outdir = validate_dir (outdir , create = True )
337+ list_to_file (fname , outdir , cast_id_list )
338+ return cast_id_list
339+
340+
341+ def load_fit_groups (casts , fit_groups ):
342+ fit_groups_all = dict ()
343+ for name , fit_group in fit_groups .items ():
344+ for value in fit_group :
345+ ct = casts .count (value )
346+ if ct != 1 :
347+ raise ValueError ("Invalid fit group definitions: %s must occur only once, but %s occurrances were found."
348+ % (value , ct ))
349+ group_casts = []
350+ for starting_cast in fit_group :
351+ start_idx = casts .index (starting_cast )
352+ if starting_cast == fit_group [- 1 ]:
353+ end_idx = None
354+ else :
355+ next_cast = fit_group [fit_group .index (starting_cast ) + 1 ]
356+ end_idx = casts .index (next_cast )
357+ group_casts .append ([cast for cast in casts [start_idx : end_idx ]])
358+ fit_groups_all [name ] = group_casts
359+ return munchify (fit_groups_all )
360+
361+ def get_cast_id_list (fname , rawdir , outdir , auto_generate = True ):
362+ """
363+ Loads a cast list from a file. If the file is not found, the cast list will be
364+ auto-generated from a directory of raw files when auto_generate is True (default
365+ behavior).
311366
312- return ssscc_list
367+ Parameters
368+ ----------
369+ fname
370+ auto_generate
371+
372+ Returns
373+ -------
374+
375+ """
376+ fname = Path (outdir , fname )
377+ try :
378+ # test if file exists
379+ fname = validate_file (fname )
380+ except FileNotFoundError :
381+ # auto-generate or die
382+ if auto_generate is True :
383+ make_cast_id_list (rawdir , outdir , fname = fname )
384+ else :
385+ raise
313386
387+ # file exists or has been auto-generated
388+ with open (fname , "r" ) as f :
389+ # skip comment lines
390+ id_list = [line .strip () for line in f .readlines () if not line .startswith ('#' )]
391+ return id_list
314392
315393def get_ssscc_list (fname = "data/ssscc.csv" ):
316394 """
@@ -327,6 +405,7 @@ def get_ssscc_list(fname="data/ssscc.csv"):
327405 list
328406 Cast names or identifiers, as a list of strings.
329407 """
408+ log .warning ("Use of get_ssscc_list() is deprecated. Use get_cast_id_list() instead." )
330409 ssscc_list = []
331410 with open (fname , "r" ) as lines :
332411 for line in lines :
0 commit comments