From 141342b431df5859fe6156709cbd77f1693347cd Mon Sep 17 00:00:00 2001
From: Lukas Kugler <lukas.kugler@univie.ac.at>
Date: Fri, 14 Feb 2025 11:32:05 +0100
Subject: [PATCH] obskind_read moved

---
 dartwrf/assimilate.py            |  4 +-
 dartwrf/create_obskind_table.py  | 63 --------------------------------
 dartwrf/obs/__init__.py          | 28 --------------
 dartwrf/obs/create_obsseq_in.py  |  3 +-
 dartwrf/obs/create_obsseq_out.py |  5 +--
 dartwrf/utils.py                 | 28 ++++++++++++++
 6 files changed, 33 insertions(+), 98 deletions(-)
 delete mode 100644 dartwrf/create_obskind_table.py

diff --git a/dartwrf/assimilate.py b/dartwrf/assimilate.py
index 6368061..1f88ce7 100755
--- a/dartwrf/assimilate.py
+++ b/dartwrf/assimilate.py
@@ -7,10 +7,10 @@ import time as time_module
 import datetime as dt
 import numpy as np
 
-from dartwrf.utils import Config, symlink, copy, try_remove, print, shell, write_txt, load_dict
+from dartwrf.utils import Config, symlink, copy, try_remove, print, shell, write_txt, obskind_read
 from dartwrf import wrfout_add_geo
 from dartwrf.obs import error_models as err
-from dartwrf.obs import obsseq, obskind_read
+from dartwrf.obs import obsseq
 from dartwrf import dart_nml
 
 
diff --git a/dartwrf/create_obskind_table.py b/dartwrf/create_obskind_table.py
deleted file mode 100644
index 5994018..0000000
--- a/dartwrf/create_obskind_table.py
+++ /dev/null
@@ -1,63 +0,0 @@
-"""
-To be able to generate obs_seq.in files, we need a dictionary to convert obs kinds to numbers
-
-  a) we read the obs kind definitions (obs_kind_mod.f90 from DART code) 
-  b) we generate a python file with this dictionary
-
-# Note: to include it in the documentary, the file needs to exist also in the repository 
-# (so the documentation generator SPHINX can read it)
-"""
-import os, sys
-import shutil
-
-
-def _dict_to_py(d, outfile):
-    """Write a python dictionary to a .py file
-
-    Args:
-        d (dict): dictionary to write
-        outfile (str): path to output file
-
-    Returns:
-        None
-    """
-    with open(outfile, 'w') as f:
-        txt = '""" NOTE: This file is autogenerated! \nUse dartwrf/create_obskind_table.py to regenerate!\n"""\nobs_kind_nrs = {\n'
-        for k, v in d.items():
-            txt += '"'+k+'": '+str(v)+', \n'
-        txt += '}'
-        f.write(txt)
-
-
-def _save_config_to_scriptsdir(server_config, original_scripts_dir):
-    try:
-        dir_path = os.path.dirname(os.path.realpath(__file__))
-        shutil.copyfile(dir_path+'/../config/'+server_config,
-                        original_scripts_dir+'/server_config.py')
-    except shutil.SameFileError:
-        pass
-
-
-def run(server_config='jet.py'):
-    """Create obskind.py from obs_kind_mod.f90
-    """
-
-    # usually /home/DART-WRF/dartwrf/
-    original_scripts_dir = '/'.join(__file__.split('/')[:-1])
-
-    # copy the original config to "scripts_dir"
-    _save_config_to_scriptsdir(server_config, original_scripts_dir)
-
-    # import the config from scripts_dir
-    sys.path.append(original_scripts_dir)
-    from server_config import cluster
-    from dartwrf import obs
-
-    obskind_dictionary = obs.obskind_read(cluster.dart_srcdir)
-
-    _dict_to_py(obskind_dictionary, original_scripts_dir+'/obs/obskind.py')
-    print('>>>', original_scripts_dir+'/obs/obskind.py', 'created')
-
-
-if __name__ == '__main__':
-    run(server_config='jet_ACF.py')
diff --git a/dartwrf/obs/__init__.py b/dartwrf/obs/__init__.py
index 5c23b21..e69de29 100644
--- a/dartwrf/obs/__init__.py
+++ b/dartwrf/obs/__init__.py
@@ -1,28 +0,0 @@
-
-def obskind_read(dart_srcdir: str) -> dict:
-    """Read dictionary of observation types + ID numbers ("kind") 
-    from DART f90 script and return it as python dictionary
-    """
-    definitionfile = dart_srcdir + \
-        '/../../../assimilation_code/modules/observations/obs_kind_mod.f90'
-    with open(definitionfile, 'r') as f:
-        kind_def_f = f.readlines()
-
-    obskind_nrs = {}
-    for i, line in enumerate(kind_def_f):
-        if 'Integer definitions for DART OBS TYPES' in line:
-            # data starts below this line
-            i_start = i
-            break
-    for line in kind_def_f[i_start+1:]:
-        if 'MAX_DEFINED_TYPES_OF_OBS' in line:
-            # end of data
-            break
-        if '::' in line:
-            # a line looks like this
-            # integer, parameter, public ::       MSG_4_SEVIRI_TB =   261
-            data = line.split('::')[-1].split('=')
-            kind_str = data[0].strip()
-            kind_nr = int(data[1].strip())
-            obskind_nrs[kind_str] = kind_nr
-    return obskind_nrs
\ No newline at end of file
diff --git a/dartwrf/obs/create_obsseq_in.py b/dartwrf/obs/create_obsseq_in.py
index b256f0b..5038384 100755
--- a/dartwrf/obs/create_obsseq_in.py
+++ b/dartwrf/obs/create_obsseq_in.py
@@ -12,7 +12,6 @@ from pysolar.solar import get_altitude, get_azimuth
 from dartwrf import utils
 from dartwrf.utils import Config
 from dartwrf.obs import calculate_obs_locations as col
-from dartwrf.obs import obskind_read
 
 # position on earth for RTTOV ray geometry
 lat0 = 45.
@@ -218,7 +217,7 @@ def create_obs_seq_in(cfg: Config, output_path='./obs_seq.in'):
     
     os.makedirs(os.path.dirname(output_path), exist_ok=True)
     
-    obs_kind_nrs = obskind_read(cfg.dir_dart_src)
+    obs_kind_nrs = utils.obskind_read(cfg.dir_dart_src)
 
     print('creating obs_seq.in:')
     time_dt = _add_timezone_UTC(time_dt)
diff --git a/dartwrf/obs/create_obsseq_out.py b/dartwrf/obs/create_obsseq_out.py
index 99bf135..5f0a33c 100644
--- a/dartwrf/obs/create_obsseq_out.py
+++ b/dartwrf/obs/create_obsseq_out.py
@@ -3,10 +3,9 @@ import shutil
 import glob
 import warnings
 
-from dartwrf.utils import Config, try_remove, print, shell, symlink, copy
+from dartwrf.utils import Config, try_remove, print, shell, symlink, copy, obskind_read
 import dartwrf.obs.create_obsseq_in as osi
-from dartwrf.obs import obsseq, obskind_read
-from dartwrf import assimilate as aso
+from dartwrf.obs import obsseq
 from dartwrf import wrfout_add_geo
 
 
diff --git a/dartwrf/utils.py b/dartwrf/utils.py
index 1a578b4..15368e9 100755
--- a/dartwrf/utils.py
+++ b/dartwrf/utils.py
@@ -365,3 +365,31 @@ def save_dict(dictionary, fpath):
 def load_dict(fpath):
     with open(fpath, 'rb') as f:
         return pickle.load(f)
+
+def obskind_read(dart_srcdir: str) -> dict:
+    """Read dictionary of observation types + ID numbers ("kind") 
+    from DART f90 script and return it as python dictionary
+    """
+    definitionfile = dart_srcdir + \
+        '/../../../assimilation_code/modules/observations/obs_kind_mod.f90'
+    with open(definitionfile, 'r') as f:
+        kind_def_f = f.readlines()
+
+    obskind_nrs = {}
+    for i, line in enumerate(kind_def_f):
+        if 'Integer definitions for DART OBS TYPES' in line:
+            # data starts below this line
+            i_start = i
+            break
+    for line in kind_def_f[i_start+1:]:
+        if 'MAX_DEFINED_TYPES_OF_OBS' in line:
+            # end of data
+            break
+        if '::' in line:
+            # a line looks like this
+            # integer, parameter, public ::       MSG_4_SEVIRI_TB =   261
+            data = line.split('::')[-1].split('=')
+            kind_str = data[0].strip()
+            kind_nr = int(data[1].strip())
+            obskind_nrs[kind_str] = kind_nr
+    return obskind_nrs
\ No newline at end of file
-- 
GitLab