Source code for h2ss.data

  1"""Functions to download and read data."""
  2
  3import glob
  4import os
  5from datetime import datetime, timezone
  6from zipfile import ZipFile
  7
  8import geopandas as gpd
  9import pandas as pd
 10import pooch
 11import rasterio as rio
 12import shapely
 13import xarray as xr
 14from geocube.api.core import make_geocube
 15
 16# CRS of the Kish Basin dat files
 17CRS = 23029
 18
 19
[docs] 20def download_data(url, data_dir, file_name, known_hash=None): 21 """Download data and store it in the specified directory using Pooch. 22 23 Parameters 24 ---------- 25 url : str 26 URL from which the data will be downloaded 27 data_dir : str 28 Directory to store the downloaded data 29 file_name : str 30 Name of the downloaded data file with its extension (not full path) 31 known_hash : str 32 SHA256 hash of downloaded file 33 34 Notes 35 ----- 36 This only downloads data if necessary, i.e. if the data file does not 37 already exist in the directory. 38 """ 39 data_file = os.path.join(data_dir, file_name) 40 if not os.path.isfile(data_file): 41 os.makedirs(data_dir, exist_ok=True) 42 pooch.retrieve( 43 url=url, known_hash=known_hash, fname=file_name, path=data_dir 44 ) 45 print(f"Data downloaded on: {datetime.now(tz=timezone.utc)}") 46 with open(f"{data_file}.txt", "w", encoding="utf-8") as outfile: 47 outfile.write( 48 f"Data downloaded on: {datetime.now(tz=timezone.utc)}\n" 49 f"Download URL: {url}\n" 50 f"SHA256 hash: {pooch.file_hash(data_file)}\n" 51 ) 52 else: 53 print(f"Data '{file_name}' already exists in '{data_dir}'.") 54 with open(f"{data_file}.txt", encoding="utf-8") as f: 55 print(f.read())
56 57
[docs] 58def kish_basin_extent(dat_path): 59 """Read the extent of the Kish Basin data. 60 61 Parameters 62 ---------- 63 dat_path : str 64 Path to the Kish Basin data files 65 66 Returns 67 ------- 68 geopandas.GeoSeries 69 GeoPandas geoseries of the extent polygon 70 """ 71 dat_extent = pd.read_csv( 72 os.path.join(dat_path, "Kish GIS Map Extent - Square.csv"), skiprows=2 73 ) 74 dat_extent = gpd.GeoSeries( 75 shapely.geometry.Polygon( 76 [ 77 (dat_extent[" X"][0], dat_extent[" Y"][0]), 78 (dat_extent[" X"][1], dat_extent[" Y"][1]), 79 (dat_extent[" X"][2], dat_extent[" Y"][2]), 80 (dat_extent[" X"][3], dat_extent[" Y"][3]), 81 ] 82 ), 83 crs=CRS, 84 ) 85 return dat_extent
86 87
[docs] 88def read_dat_file(dat_path): 89 """Read XYZ halite data layers into an Xarray dataset. 90 91 Parameters 92 ---------- 93 dat_path : str 94 Path to the DAT (ASCII XYZ) files 95 96 Returns 97 ------- 98 tuple[xarray.Dataset, geopandas.GeoSeries] 99 Xarray dataset of the XYZ data and GeoPandas geoseries of the extent 100 """ 101 gdf = {} 102 # for dat_file in glob.glob(os.path.join(dat_path, "*.dat")): 103 for dat_file in [ 104 x 105 for x in glob.glob(os.path.join(dat_path, "*.dat")) 106 if "Zone" not in x 107 ]: 108 # read each layer as individual dataframes into a dictionary 109 gdf[os.path.split(dat_file)[-1][:-4]] = pd.read_fwf( 110 dat_file, header=None, names=["X", "Y", "Z"] 111 ) 112 113 # assign layer name to a column 114 gdf[os.path.split(dat_file)[-1][:-4]]["data"] = os.path.split( 115 dat_file 116 )[-1][:-4] 117 118 # # find data resolution 119 # gdf_xr = gdf[list(gdf.keys())[0]].set_index(["X", "Y"]).to_xarray() 120 # resx = gdf_xr["X"][1] - gdf_xr["X"][0] 121 # resy = gdf_xr["Y"][1] - gdf_xr["Y"][0] 122 123 # combine dataframes 124 gdf = pd.concat(gdf.values()) 125 126 # convert dataframe to geodataframe 127 gdf["geometry"] = gpd.points_from_xy(gdf.X, gdf.Y, crs=CRS) 128 gdf = gpd.GeoDataFrame(gdf) 129 gdf.drop(columns=["X", "Y"], inplace=True) 130 131 # convert to Xarray dataset 132 xds = make_geocube( 133 vector_data=gdf, 134 # resolution=(-abs(resy), abs(resx)), 135 # align=(abs(resy / 2), abs(resx / 2)), 136 resolution=(-200, 200), 137 align=(100, 100), 138 group_by="data", 139 ) 140 141 # split variables and halite members 142 xds_ = {} 143 for d in xds["data"].values: 144 halite_member = d.split(" ")[0] 145 # fix halite names 146 if halite_member == "Presall": 147 halite_member = "Preesall" 148 elif halite_member == "Flyde": 149 halite_member = "Fylde" 150 # unit = d.split(" ")[-1] 151 zvar = d.split("Halite ")[-1].split(" XYZ")[0] 152 xds_[d] = ( 153 xds.sel(data=d) 154 .assign_coords(halite=halite_member) 155 .expand_dims(dim="halite") 156 .drop_vars("data") 157 ) 158 xds_[d] = xds_[d].rename({"Z": zvar.replace(" ", "")}) 159 # xds_[d][zvar.replace(" ", "")] = xds_[d][ 160 # zvar.replace(" ", "") 161 # ].assign_attrs(units=unit, long_name=zvar) 162 163 xds = xr.combine_by_coords( 164 xds_.values(), combine_attrs="override", compat="no_conflicts" 165 ) 166 167 # # keep only points corresponding to zones of interest in the dataframe 168 # zones = gdf.loc[gdf["data"].str.contains("Zone")] 169 170 # # create zones of interest polygon 171 # zones = gpd.GeoDataFrame(geometry=zones.buffer(100).envelope).dissolve() 172 173 # create extent polygon 174 dat_extent = kish_basin_extent(dat_path=dat_path) 175 176 return xds, dat_extent # zones
177 178
[docs] 179def kish_basin_data_depth_adjusted(dat_path, bathymetry_path): 180 """Read halite data with adjusted depths and its extent. 181 182 Parameters 183 ---------- 184 dat_path : str 185 Path to the DAT (ASCII XYZ) files 186 bathymetry_path : str 187 Path to the bathymetry netCDF file 188 189 Returns 190 ------- 191 tuple[xarray.Dataset, geopandas.GeoSeries] 192 Xarray dataset of the halite data and GeoPandas geoseries of the extent 193 194 Notes 195 ----- 196 A hacky workaround was used to prevent multiple grid mappings in the 197 resulting Xarray dataset by first assigning a variable of the desired 198 mapping and then multiplying it by zero prior to involving variables which 199 originally had a different mapping. 200 """ 201 bath = xr.open_dataset( 202 os.path.join(bathymetry_path, "D4_2022.nc"), decode_coords="all" 203 ) 204 bath = bath.chunk({"lat": 1000, "lon": 1000, "cdi_index_count": 1000}) 205 xds, dat_extent = read_dat_file(dat_path=dat_path) 206 bath = bath.rename({"lon": "x", "lat": "y"}).rio.reproject_match( 207 xds, resampling=rio.enums.Resampling.bilinear 208 ) 209 xds = xds.assign(TopDepthSeabed=xds["TopDepth"] + bath["elevation"]) 210 xds = xds.assign(BaseDepthSeabed=xds["BaseDepth"] + bath["elevation"]) 211 # hacky way to prevent multiple grid mappings 212 xds = xds.assign(Bathymetry=xds["TopDepth"] * 0 + bath["elevation"]) 213 # for v in ["TopDepth", "BaseDepth"]: 214 # xds[f"{v}Seabed"].attrs = xds[v].attrs 215 # xds[f"{v}Seabed"].attrs["long_name"] = ( 216 # xds[f"{v}Seabed"].attrs["long_name"] + " relative to the " 217 # "sea-floor" 218 # ) 219 # xds["Bathymetry"].attrs = bath["elevation"].attrs 220 return xds, dat_extent
221 222
[docs] 223def bathymetry_layer(dat_extent, bathymetry_path): 224 """Bathymetry layer for plotting. 225 226 Parameters 227 ---------- 228 dat_extent : geopandas.GeoSeries 229 Extent of the data 230 bathymetry_path : str 231 Path to the bathymetry netCDF file 232 233 Returns 234 ------- 235 xarray.Dataset 236 Xarray dataset of the halite data 237 """ 238 bath = xr.open_dataset( 239 os.path.join(bathymetry_path, "D4_2022.nc"), decode_coords="all" 240 ) 241 bath = bath.chunk({"lat": 1000, "lon": 1000, "cdi_index_count": 1000}) 242 bath = bath.rio.reproject(CRS).rio.clip(dat_extent.to_crs(CRS)) 243 return bath
244 245
[docs] 246def read_shapefile_from_zip(data_path, endswith=".shp"): 247 """Read the Shapefile layer from a Zip file. 248 249 Parameters 250 ---------- 251 data_path : str 252 Path to the exploration well Zip file 253 endswith : str 254 What the Shapefile's filename ends with 255 256 Returns 257 ------- 258 geopandas.GeoDataFrame 259 Geodataframe of the Shapefile's data 260 """ 261 data_shp = gpd.read_file( 262 os.path.join( 263 f"zip://{data_path}!" 264 + [ 265 x 266 for x in ZipFile(data_path).namelist() 267 if x.endswith(endswith) 268 ][0] 269 ) 270 ) 271 return data_shp
272 273
[docs] 274def halite_shape(dat_xr, halite=None): 275 """Create a vector shape of the halite data. 276 277 Parameters 278 ---------- 279 dat_xr : xarray.Dataset 280 Xarray dataset of the halite data 281 halite : str 282 Halite member 283 284 Returns 285 ------- 286 geopandas.GeoDataFrame 287 A (multi)polygon geodataframe of the halite's shape 288 """ 289 if halite: 290 shape = ( 291 dat_xr.sel(halite=halite)["Thickness"] 292 .to_dataframe() 293 .dropna() 294 .reset_index() 295 ) 296 else: 297 shape = ( 298 dat_xr.max(dim="halite")["Thickness"] 299 .to_dataframe() 300 .dropna() 301 .reset_index() 302 ) 303 shape = gpd.GeoDataFrame( 304 geometry=gpd.GeoSeries(gpd.points_from_xy(shape.x, shape.y)) 305 .buffer(100) 306 .envelope, 307 crs=CRS, 308 ).dissolve() 309 return shape