From c93238eebe52fe405aac80e3157cabad2cb656d3 Mon Sep 17 00:00:00 2001 From: karnesh Date: Tue, 27 Aug 2024 14:16:42 -0400 Subject: [PATCH 1/2] added functionality to output SWE preditions to GeoJSOn file --- Model/Neural_Network/sweml_hindcast.py | 4 +++- Model/shared_scripts/National_Snow_Model.py | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Model/Neural_Network/sweml_hindcast.py b/Model/Neural_Network/sweml_hindcast.py index df84510..ccc66db 100644 --- a/Model/Neural_Network/sweml_hindcast.py +++ b/Model/Neural_Network/sweml_hindcast.py @@ -77,7 +77,9 @@ def sweml_hindcast(new_year, threshold, Region_list, fSCA, frequency, NewSim, si # Make predictions, set NewSim to False Look to multiprocess, each region can do a prediction to speed things # up. set NewSim to true for New simulation, turn to false once all data has been proces and saved. snow.SWE_Predict(NewSim=NewSim, Corrections=False, fSCA=fSCA) - + snow.netCDF_compressed(plot=False) + snow.Geo_df() + modelname = 'Neural_Network' folderpath = 'Predictions/Hold_Out_Year/Daily/fSCA_True/' AWSpath = f"Hold_Out_Year/Daily/" diff --git a/Model/shared_scripts/National_Snow_Model.py b/Model/shared_scripts/National_Snow_Model.py index c2d00fb..a964848 100644 --- a/Model/shared_scripts/National_Snow_Model.py +++ b/Model/shared_scripts/National_Snow_Model.py @@ -29,7 +29,7 @@ import geopandas as gpd from shapely.geometry import Point import xarray as xr -#import netCDF4 as nc +import netCDF4 as nc # from mpl_toolkits.basemap import Basemap from matplotlib.colors import LinearSegmentedColormap import folium @@ -55,6 +55,9 @@ from botocore import UNSIGNED from botocore.client import Config import os +from osgeo import gdal, osr +from matplotlib.dates import date2num +from rasterio.crs import CRS # import contextily as ctx import ulmo @@ -1281,7 +1284,8 @@ def netCDF_compressed(self, plot): self.SWE_array = self.DFG['SWE'].values.reshape(1, len(self.latrange), len(self.lonrange)) # create nc filepath - fn = self.cwd + '/Data/NetCDF/SWE_' + self.date + '_compressed.nc' + #fn = self.cwd + '/Data/NetCDF/SWE_' + self.date + '_compressed.nc' + fn = f"{HOME}/SWEML/Data/NetCDF/SWE_{self.date}_compressed.nc" # make nc file, set lat/long, time ncfile = nc.Dataset(fn, 'w', format='NETCDF4') @@ -1338,7 +1342,8 @@ def netCDF_compressed(self, plot): # Set date/time information times_arr = time[:] dates = [dt.datetime(int(self.date[0:4]), int(self.date[5:7]), int(self.date[8:]), 0)] - times = date2num(dates, time.units) + #times = date2num(dates, time.units) + times = date2num(dates) time[:] = times print(ncfile) @@ -1548,8 +1553,8 @@ def plot_interactive_SWE(self, pinlat, pinlong, web): def Geo_df(self): print('loading file') - fnConus = self.cwd + '/Data/NetCDF/SWE_' + self.date + '_compressed.nc' - + #fnConus = self.cwd + '/Data/NetCDF/SWE_' + self.date + '_compressed.nc' + fnConus = f"{HOME}/SWEML/Data/NetCDF/SWE_{self.date}_compressed.nc" # requires the netCDF4 package rather than rioxarray xrConus = nc.Dataset(fnConus) @@ -1574,7 +1579,8 @@ def Geo_df(self): Chorocols = ['geoid', 'SWE', 'geometry'] self.SWE_gdf = SWE_gdf[Chorocols] self.SWE_gdf.crs = CRS.from_epsg(4326) - + file = f"{HOME}/SWEML/Data/GeoJSON/SWE_{self.date}.geojson" + SWE_gdf.to_file(file, driver='GeoJSON') xrConus.close() # produce an interactive plot using Folium From f11c30752596aa956ff80c7669eb3ea77d905f35 Mon Sep 17 00:00:00 2001 From: karnesh Date: Wed, 28 Aug 2024 14:22:16 -0400 Subject: [PATCH 2/2] modified gitignore to ignore netcdf and geojson files --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8cfd3c9..17451fd 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,6 @@ Data/WBD/ *.tif.aux.xml *.zip *.ipynb_checkpoints/ -*.nfs* \ No newline at end of file +*.nfs* +*.geojson +*.nc \ No newline at end of file