diff --git a/workflows/archive/macav2_historical_monthly_create_collection_from_zarr.ipynb b/workflows/archive/macav2_historical_monthly_create_collection_from_zarr.ipynb index 7cfacd9c78f58ca4aee1664ff6a8ecab53ca5306..a2a3cfae07bcd41cf1117bd62f32baa0d3b37e9f 100644 --- a/workflows/archive/macav2_historical_monthly_create_collection_from_zarr.ipynb +++ b/workflows/archive/macav2_historical_monthly_create_collection_from_zarr.ipynb @@ -33,7 +33,8 @@ "import pandas as pd\n", "import json\n", "import numpy as np\n", - "import metpy\n", + "import pyproj\n", + "from pyproj import Transformer\n", "import cartopy.crs as ccrs\n", "import cfunits\n", "import json\n", @@ -213,8 +214,37 @@ "metadata": {}, "outputs": [], "source": [ - "ds = ds.metpy.parse_cf()\n", - "crs = ds[list(ds.keys())[0]].metpy.cartopy_crs" + "# use pyproj to automatically extract crs info\n", + "crs = pyproj.CRS.from_cf(ds.crs.attrs)\n", + "\n", + "# alternatively, create the appropriate cartopy projection\n", + "# crs = ccrs.LambertConformal(central_longitude=crs_info.longitude_of_central_meridian, \n", + "# central_latitude=crs_info.latitude_of_projection_origin,\n", + "# standard_parallels=crs_info.standard_parallel)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2e4b4c28-1a02-4fdf-8498-274728ea0d2c", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "ds.crs" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8566c769-1638-48eb-8717-fccdb6cfc0c1", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "crs.to_proj4()" ] }, { @@ -245,14 +275,63 @@ "# coordinates must be from WGS 84 datum\n", "# left, bottom, right, top\n", "\n", - "# Note: try changing around the commented out lines below to get type float ratherthan a numpy float\n", - "#coord_bounds = [ds[dim_names_dict['X']].data.min().compute().astype(float), ds[dim_names_dict['Y']].data.min().compute().astype(float), ds[dim_names_dict['X']].data.max().compute().astype(float), ds[dim_names_dict['Y']].data.max().compute().astype(float)]\n", - "#coord_bounds = [ds[dim_names_dict['X']].data.min().compute().astype(float).tolist(), ds[dim_names_dict['Y']].data.min().compute().astype(float).tolist(), ds[dim_names_dict['X']].data.max().compute().astype(float).tolist(), ds[dim_names_dict['Y']].data.max().compute().astype(float).tolist()]\n", - "coord_bounds = [ds[dim_names_dict['X']].data.min().astype(float).item(), ds[dim_names_dict['Y']].data.min().astype(float).item(), ds[dim_names_dict['X']].data.max().astype(float).item(), ds[dim_names_dict['Y']].data.max().astype(float).item()]\n", - "print(coord_bounds)\n", - "print(f'\\ncoord_bounds data type: {type(coord_bounds[0])}')\n", + "# Note: try changing around the commented out lines below to get type float rather than a numpy float\n", + "#spatial_bounds = [ds[dim_names_dict['X']].data.min().compute().astype(float), ds[dim_names_dict['Y']].data.min().compute().astype(float), ds[dim_names_dict['X']].data.max().compute().astype(float), ds[dim_names_dict['Y']].data.max().compute().astype(float)]\n", + "#spatial_bounds = [ds[dim_names_dict['X']].data.min().compute().astype(float).tolist(), ds[dim_names_dict['Y']].data.min().compute().astype(float).tolist(), ds[dim_names_dict['X']].data.max().compute().astype(float).tolist(), ds[dim_names_dict['Y']].data.max().compute().astype(float).tolist()]\n", + "spatial_bounds = [ds[dim_names_dict['X']].data.min().astype(float).item(), ds[dim_names_dict['Y']].data.min().astype(float).item(), ds[dim_names_dict['X']].data.max().astype(float).item(), ds[dim_names_dict['Y']].data.max().astype(float).item()]\n", + "print(spatial_bounds)\n", + "print(f'\\nspatial_bounds data type: {type(spatial_bounds[0])}')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1fa7c721-aecd-4ea6-8f8d-226cc75e10bb", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "XX, YY = np.meshgrid(ds[dim_names_dict['X']].data, ds[dim_names_dict['Y']].data)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5fa66fb7-ea2f-4a09-b5db-7c6f42a2694c", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "transformer = Transformer.from_crs(crs, \"EPSG:4326\", always_xy=True)\n", + "lon, lat = transformer.transform(XX.ravel(), YY.ravel())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c28a05a0-8bde-4a99-80ee-f0385cfed090", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "print(f'lower left coordinates (WGS84): {min(lon)}, {min(lat)}')\n", + "print(f'upper right coordinates (WGS84): {max(lon)}, {max(lat)}')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "84a630f9-c527-43c5-8da4-1ee386c78c59", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ "# create a spatial extent object \n", - "spatial_extent = pystac.SpatialExtent(bboxes=[coord_bounds])" + "spatial_extent = pystac.SpatialExtent(bboxes=[[min(lon).item(), min(lat).item(), max(lon).item(), max(lat).item()]])" ] }, { @@ -439,17 +518,6 @@ "reference list of cartopy projections: https://scitools.org.uk/cartopy/docs/latest/reference/projections.html" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "ea452f62-5644-49b6-8a4e-7dc4f649fd1a", - "metadata": {}, - "outputs": [], - "source": [ - "# print out crs information in dataset\n", - "print(crs)" - ] - }, { "cell_type": "code", "execution_count": null, @@ -666,8 +734,8 @@ "# we do not recommend including redundant dimensions (do not include x,y if you have lon,lat)\n", "# note that the extent of each dimension should be pulled from the dataset\n", "dims_dict = {dim_names_dict['T']: pystac.extensions.datacube.Dimension({'type': 'temporal', 'description': stac_helpers.get_long_name(ds, dim_names_dict['T']), 'extent': [temporal_extent_lower.strftime('%Y-%m-%dT%XZ'), temporal_extent_upper.strftime('%Y-%m-%dT%XZ')], 'step':time_step}),\n", - " dim_names_dict['X']: pystac.extensions.datacube.Dimension({'type': 'spatial', 'axis': 'x', 'description': stac_helpers.get_long_name(ds, dim_names_dict['X']), 'extent': [xy_bounds[0], xy_bounds[2]], 'step': x_step, 'reference_system': projjson}),\n", - " dim_names_dict['Y']: pystac.extensions.datacube.Dimension({'type': 'spatial', 'axis': 'y', 'description': stac_helpers.get_long_name(ds, dim_names_dict['Y']), 'extent': [xy_bounds[1], xy_bounds[3]], 'step': y_step, 'reference_system': projjson}),\n", + " dim_names_dict['X']: pystac.extensions.datacube.Dimension({'type': 'spatial', 'axis': 'x', 'description': stac_helpers.get_long_name(ds, dim_names_dict['X']), 'extent': [spatial_bounds[0], spatial_bounds[2]], 'step': x_step, 'reference_system': projjson}),\n", + " dim_names_dict['Y']: pystac.extensions.datacube.Dimension({'type': 'spatial', 'axis': 'y', 'description': stac_helpers.get_long_name(ds, dim_names_dict['Y']), 'extent': [spatial_bounds[1], spatial_bounds[3]], 'step': y_step, 'reference_system': projjson}),\n", " }" ] },