From 3203f4c8ba139fbbcecdb2c8d541589ce52a0101 Mon Sep 17 00:00:00 2001
From: jsta <stachel2@msu.edu>
Date: Mon, 5 Sep 2022 14:23:06 -0400
Subject: [PATCH] write cbofs to tif

---
 environment_ncdf.yml    |  5 +++--
 scripts/00_get_cbofs.py | 37 +++++++++++++++++++++++++++++++++++++
 scripts/noaa_opf.py     | 26 --------------------------
 3 files changed, 40 insertions(+), 28 deletions(-)
 create mode 100644 scripts/00_get_cbofs.py
 delete mode 100644 scripts/noaa_opf.py

diff --git a/environment_ncdf.yml b/environment_ncdf.yml
index ba7e920..3640848 100644
--- a/environment_ncdf.yml
+++ b/environment_ncdf.yml
@@ -1,9 +1,10 @@
 name: netcdf4
 channels:
 - conda-forge
-dependencies:        
-        # - pandas
+dependencies:
         - matplotlib
         - xarray
         - rioxarray
         - netcdf4
+        - gdal
+        - geopandas
diff --git a/scripts/00_get_cbofs.py b/scripts/00_get_cbofs.py
new file mode 100644
index 0000000..04b7d74
--- /dev/null
+++ b/scripts/00_get_cbofs.py
@@ -0,0 +1,37 @@
+# https://tidesandcurrents.noaa.gov/ofs/cbofs/cbofs.html
+# data products are every 6 hours
+# data products seem to be archived for only a month in time
+
+import os
+import rioxarray
+import xarray as xr
+import matplotlib.pyplot as plt
+
+tod = "20220904"
+tif_path = "data/cbofs/salt_{date}.tif".format(date=tod)
+
+if not os.path.exists(tif_path):
+    locs = [
+        'https://opendap.co-ops.nos.noaa.gov/thredds/dodsC/NOAA/CBOFS/MODELS/{year}/{month}/{day}/nos.cbofs.regulargrid.n001.{date}.t00z.nc'
+        .format(date=tod, year=tod[0:4], month=tod[4:6], day=tod[6:8]),
+        'https://opendap.co-ops.nos.noaa.gov/thredds/dodsC/NOAA/CBOFS/MODELS/%Y/%m/%d/nos.cbofs.regulargrid.n002.{date}.t00z.nc'
+        .format(date=tod)
+    ]
+    ds1 = xr.open_dataset(locs[0])
+    dt = ds1.sel({"Depth": 0}).isel(ocean_time=[0]).drop_vars(["Depth"])
+    dt = dt.salt.to_dataset()
+
+    # dt.salt[0,:,:].plot.imshow()
+    # plt.show()
+
+    # ---
+
+    lons = dt.Longitude.as_numpy().values[0]
+    lats = dt.Latitude.as_numpy().values[:,0]
+    data = dt["salt"].as_numpy().values[0, :, :]
+
+    test = xr.DataArray(data=data, dims=["y", "x"], coords=dict(x=(lons), y=(lats)))
+    test = test.rio.set_spatial_dims("x", "y")
+    test.rio.write_crs(4326, inplace=True)
+
+    test.rio.to_raster(tif_path)
diff --git a/scripts/noaa_opf.py b/scripts/noaa_opf.py
deleted file mode 100644
index f9959bb..0000000
--- a/scripts/noaa_opf.py
+++ /dev/null
@@ -1,26 +0,0 @@
-# https://tidesandcurrents.noaa.gov/ofs/cbofs/cbofs.html
-
-# data products are every 6 hours
-# data products seem to be archived for only a month in time
-
-import rioxarray
-import xarray as xr
-import matplotlib.pyplot as plt
-
-tod = "20220904"
-locs = [
-    'https://opendap.co-ops.nos.noaa.gov/thredds/dodsC/NOAA/CBOFS/MODELS/{year}/{month}/{day}/nos.cbofs.regulargrid.n001.{date}.t00z.nc'.format(date=tod, year=tod[0:4], month=tod[4:6], day=tod[6:8]),
-    'https://opendap.co-ops.nos.noaa.gov/thredds/dodsC/NOAA/CBOFS/MODELS/%Y/%m/%d/nos.cbofs.regulargrid.n002.{date}.t00z.nc'.format(date=tod)    
-]
-
-ds1 = xr.open_dataset(locs[0])
-
-test = ds1.sel({"Depth":0}).isel(ocean_time=[0]).drop_vars(["Depth"])
-test = test.salt.to_dataset()
-
-# test.salt[0,:,:].plot.imshow()
-# plt.show()
-
-nc_path = "data/cbofs/salt_{date}.nc".format(date=tod)
-test.to_netcdf(nc_path)
-# test = xr.open_dataset(nc_path)