From 2ccb6c17f97ecb5b119c0af8634b95104e3b3d7a Mon Sep 17 00:00:00 2001
From: tomrink <rink@ssec.wisc.edu>
Date: Fri, 16 Jun 2023 11:55:03 -0500
Subject: [PATCH] snapshot...

---
 modules/util/util.py | 83 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)

diff --git a/modules/util/util.py b/modules/util/util.py
index 76a9090c..384e93f3 100644
--- a/modules/util/util.py
+++ b/modules/util/util.py
@@ -1431,6 +1431,89 @@ def write_icing_file_nc4_viirs(clvrx_str_time, output_dir, preds_dct, probs_dct,
     rootgrp.close()
 
 
+def write_cld_frac_file_nc4(clvrx_str_time, output_dir, cloud_fraction,
+                            x, y, elems, lines, satellite='GOES16', domain='CONUS',
+                            has_time=False):
+    outfile_name = output_dir + 'icing_prediction_'+clvrx_str_time+'.nc'
+    rootgrp = Dataset(outfile_name, 'w', format='NETCDF4')
+
+    rootgrp.setncattr('Conventions', 'CF-1.7')
+
+    dim_0_name = 'x'
+    dim_1_name = 'y'
+    time_dim_name = 'time'
+    geo_coords = 'time y x'
+
+    dim_0 = rootgrp.createDimension(dim_0_name, size=x.shape[0])
+    dim_1 = rootgrp.createDimension(dim_1_name, size=y.shape[0])
+    dim_time = rootgrp.createDimension(time_dim_name, size=1)
+
+    tvar = rootgrp.createVariable('time', 'f8', time_dim_name)
+    tvar[0] = get_timestamp(clvrx_str_time)
+    tvar.units = 'seconds since 1970-01-01 00:00:00'
+
+    if not has_time:
+        var_dim_list = [dim_1_name, dim_0_name]
+    else:
+        var_dim_list = [time_dim_name, dim_1_name, dim_0_name]
+
+    cld_frac_ds = rootgrp.createVariable('cloud_fraction', 'i1', var_dim_list)
+    cld_frac_ds.setncattr('coordinates', geo_coords)
+    cld_frac_ds.setncattr('grid_mapping', 'Projection')
+    cld_frac_ds.setncattr('missing', -1)
+    if has_time:
+        cloud_fraction = cloud_fraction.reshape((1, y.shape[0], x.shape[0]))
+    cld_frac_ds[:, ] = cloud_fraction
+
+    cf_nav_dct = get_cf_nav_parameters(satellite, domain)
+
+    if satellite == 'H08':
+        long_name = 'Himawari Imagery Projection'
+    elif satellite == 'H09':
+        long_name = 'Himawari Imagery Projection'
+    elif satellite == 'GOES16':
+        long_name = 'GOES-16/17 Imagery Projection'
+
+    proj_ds = rootgrp.createVariable('Projection', 'b')
+    proj_ds.setncattr('long_name', long_name)
+    proj_ds.setncattr('grid_mapping_name', 'geostationary')
+    proj_ds.setncattr('sweep_angle_axis', cf_nav_dct['sweep_angle_axis'])
+    proj_ds.setncattr('semi_major_axis', cf_nav_dct['semi_major_axis'])
+    proj_ds.setncattr('semi_minor_axis', cf_nav_dct['semi_minor_axis'])
+    proj_ds.setncattr('inverse_flattening', cf_nav_dct['inverse_flattening'])
+    proj_ds.setncattr('perspective_point_height', cf_nav_dct['perspective_point_height'])
+    proj_ds.setncattr('latitude_of_projection_origin', cf_nav_dct['latitude_of_projection_origin'])
+    proj_ds.setncattr('longitude_of_projection_origin', cf_nav_dct['longitude_of_projection_origin'])
+
+    if x is not None:
+        x_ds = rootgrp.createVariable(dim_0_name, 'f8', [dim_0_name])
+        x_ds.units = 'rad'
+        x_ds.setncattr('axis', 'X')
+        x_ds.setncattr('standard_name', 'projection_x_coordinate')
+        x_ds.setncattr('long_name', 'fixed grid viewing angle')
+        x_ds.setncattr('scale_factor', cf_nav_dct['x_scale_factor'])
+        x_ds.setncattr('add_offset', cf_nav_dct['x_add_offset'])
+        x_ds[:] = x
+
+        y_ds = rootgrp.createVariable(dim_1_name, 'f8', [dim_1_name])
+        y_ds.units = 'rad'
+        y_ds.setncattr('axis', 'Y')
+        y_ds.setncattr('standard_name', 'projection_y_coordinate')
+        y_ds.setncattr('long_name', 'fixed grid viewing angle')
+        y_ds.setncattr('scale_factor', cf_nav_dct['y_scale_factor'])
+        y_ds.setncattr('add_offset', cf_nav_dct['y_add_offset'])
+        y_ds[:] = y
+
+    if elems is not None:
+        elem_ds = rootgrp.createVariable('elems', 'i2', [dim_0_name])
+        elem_ds[:] = elems
+        line_ds = rootgrp.createVariable('lines', 'i2', [dim_1_name])
+        line_ds[:] = lines
+        pass
+
+    rootgrp.close()
+
+
 def downscale_2x(original, smoothing=False, samples_axis_first=False):
     # if smoothing:
     #     original = scipy.ndimage.gaussian_filter(original, sigma = 1/2)
-- 
GitLab