Skip to content

Commit

Permalink
Merge pull request #71 from karnesh/main
Browse files Browse the repository at this point in the history
Functionality to output SWE preditions to GeoJSON file
  • Loading branch information
whitelightning450 authored Sep 12, 2024
2 parents de3ce28 + f11c307 commit 59a4ad5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ Data/WBD/
*.tif.aux.xml
*.zip
*.ipynb_checkpoints/
*.nfs*
*.nfs*
*.geojson
*.nc
4 changes: 3 additions & 1 deletion Model/Neural_Network/sweml_hindcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down
18 changes: 12 additions & 6 deletions Model/shared_scripts/National_Snow_Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit 59a4ad5

Please sign in to comment.