From 593cab32e8aa57a9f09e9d3490dbf7ef6b98368c Mon Sep 17 00:00:00 2001
From: nickb <nickb@ssec.wisc.edu>
Date: Thu, 18 Feb 2021 20:52:23 +0000
Subject: [PATCH] first cut at supporting alternative system environment
 prefixes, for example: CG_ instead of OR_

---
 gridded_glm/libexec/_minute_gridder.py | 64 ++++++++++++++++++++++++--
 1 file changed, 59 insertions(+), 5 deletions(-)

diff --git a/gridded_glm/libexec/_minute_gridder.py b/gridded_glm/libexec/_minute_gridder.py
index a3a2a29..2fd4332 100644
--- a/gridded_glm/libexec/_minute_gridder.py
+++ b/gridded_glm/libexec/_minute_gridder.py
@@ -85,6 +85,10 @@ def create_parser():
                         help="enable 'realtime' mode, where we expect only one input file,\n"
                         "find the surrounding trio, and automatically determine if a full minute\n"
                         "of data is available (default: off)")
+    parser.add_argument('--system-environment-prefix', default="CG",
+                        help="set the system environment prefix for the output grids (default: CG)")
+    parser.add_argument('--system-environment-prefix-tiles', default="OR",
+                        help="set the system environment prefix for the output tiles (default: OR)")
     # from Requirements: "Input is one or more GLM LCFA (L2+) files in mission standard format (nominally three 20-second input files)"
     parser.add_argument(dest='filenames', metavar='filename', nargs='+')
     return parser
@@ -132,6 +136,45 @@ def get_start_end(filenames, start_time=None, end_time=None):
 
     return start_time, end_time
 
+def get_sector_shortstring(args):
+    if args.goes_sector == 'full':
+        return 'F'
+    elif args.goes_sector == 'conus':
+        return 'C'
+    elif args.goes_sector == 'meso':
+        return 'M1'
+    else:
+        raise RuntimeError("sector not recognized")
+
+def get_outpath_base(args):
+    """create a base outpath string to feed glmtools
+    
+    from glmtools:
+            outpath can be a template string; defaults to {'./{dataset_name}'}
+        Available named arguments in the template are:
+            dataset_name: standard GOES imagery format, includes '.nc'. Looks like
+                OR_GLM-L2-GLMM1-M3_G16_s20181830432000_e20181830433000_c20200461148520.nc
+            start_time, end_time: datetimes that can be used with strftime syntax, e.g.
+                './{start_time:%y/%b/%d}/GLM_{start_time:%Y%m%d_%H%M%S}.nc'
+    """
+    print(parse_glm_filename(args.filenames[0]))
+    ops_environment, algorithm, platform, start_time, end_time, created_time = parse_glm_filename(args.filenames[0])
+
+    sector_short = get_sector_shortstring(args)
+    mode = "M3" # FIXME: we're not always in M3
+
+    # example string: OR_GLM-L2-GLMC-M3_G17_s20182750032000_e20182750033000_c20210431923130.nc
+    dsname = "{environment_prefix}_GLM-L2-GLM{sector_short}-{mode}_{platform}_s{start_time}_e{end_time}_c{created_time}.nc".format(
+                    environment_prefix=args.system_environment_prefix,
+                    sector_short=sector_short,
+                    mode=mode,
+                    platform=platform,
+                    start_time=start_time.strftime("%Y%j%H%M%S0"),
+                    end_time=end_time.strftime("%Y%j%H%M%S0"),
+                    created_time=datetime.utcnow().strftime("%Y%j%H%M%S0")
+    )
+    return dsname
+
 
 def grid_setup(args, work_dir=os.getcwd()):
     # When passed None for the minimum event or group counts, the gridder will skip
@@ -171,7 +214,7 @@ def grid_setup(args, work_dir=os.getcwd()):
     base_date = datetime(start_time.year, start_time.month, start_time.day)
     proj_name = 'geos'
 
-    outputpath = os.path.join(work_dir, "{dataset_name}") # GLMTools expects a template in addition to the path
+    outputpath = os.path.join(work_dir, get_outpath_base(args)) # GLMTools expects a template in addition to the path
 
     goes_position = get_goes_position(args.filenames)
 
@@ -287,10 +330,11 @@ if __name__ == '__main__':
     gridder, glm_filenames, start_time, end_time, grid_kwargs = grid_setup(args, work_dir=tempdir_path)
     gridder(glm_filenames, start_time, end_time, **grid_kwargs)
 
+    # FIXME: rather than using globs (below), gridder (above) returns some sort of zipped structure that contains the output filenames
     # pick up gridded files from the tempdir
     # output looks like: OR_GLM-L2-GLMC-M3_G17_s20202691559400_e20202691600400_c20210120141010.nc
     log.debug("gridded files in {}".format(tempdir_path))
-    gridded_path = os.path.join(tempdir_path, 'OR_GLM-L2-GLM?-M?_G??_s*_e*_c*.nc')
+    gridded_path = os.path.join(tempdir_path, '{}_GLM-L2-GLM*-M?_G??_s*_e*_c*.nc'.format(args.system_environment_prefix))
     log.debug(gridded_path)
     gridded_files = glob(gridded_path)
     log.debug(gridded_files)
@@ -302,6 +346,15 @@ if __name__ == '__main__':
 
     # (optionally) do tiling
     if args.create_tiles:
+
+        sector = get_goes_position(glm_filenames)
+        if sector == "east":
+            sector_id = "GOES_EAST"
+        elif sector == "west":
+            sector_id = "GOES_WEST"
+        else:
+            raise RuntimeError("could not determine sector_id")
+
         from satpy import Scene
         for gridded_file in gridded_files:
             log.info("TILING: {}".format(gridded_files))
@@ -315,19 +368,20 @@ if __name__ == '__main__':
 
             scn.save_datasets(writer='awips_tiled',
                               template='glm_l2_radf', 
-                              sector_id="GOES_EAST", # sector_id becomes an attribute in the output files and may be another legacy kind of thing. I'm not sure how much is is actually used here.
+                              sector_id=sector_id, # sector_id becomes an attribute in the output files and may be another legacy kind of thing. I'm not sure how much is is actually used here.
                               source_name="", # You could probably make source_name an empty string. I think it is required by the writer for legacy reasons but isn't actually used for the glm output
                               base_dir=tempdir_path, # base_dir is the output directory. I think blank is the same as current directory.
                               tile_size=(506, 904), # tile_size is set to the size of the GLMF sample tiles we were given and should match the full disk ABI tiles which is what they wanted
                               check_categories=False, # check_categories is there because of that issue I mentioned where DQF is all valid all the time so there is no way to detect empty tiles unless we ignore the "category" products
+                              environment_prefix=args.system_environment_prefix_tiles,
                               compress=True)
 
     # pick up output files from the tempdir
-    # output looks like: OR_GLM-L2-GLMC-M3_G17_T03_20200925160040.nc
+    # output looks like: CG_GLM-L2-GLMC-M3_G17_T03_20200925160040.nc
     log.debug("files in {}".format(tempdir_path))
     log.debug(os.listdir(tempdir_path))
     log.debug("moving output to {}".format(args.output_dir))
-    tiled_path = os.path.join(tempdir_path, 'OR_GLM-L2-GLM?-M?_G??_T??_*.nc')
+    tiled_path = os.path.join(tempdir_path, '{}_GLM-L2-GLM*-M?_G??_T??_*.nc'.format(args.system_environment_prefix_tiles))
     tiled_files = glob(tiled_path)
     for f in tiled_files:
         add_gglm_attrs(f, glm_filenames)
-- 
GitLab