Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kwargs for lut params in inversion & doc review #71

Merged
merged 6 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/basic_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ windspeed.models
----------------

.. automodule:: xsarsea.windspeed.models
:members:
:members: NcLutModel

windspeed.gmfs
--------------

.. automodule:: xsarsea.windspeed.gmfs
:members:
:members: GmfModel, cmod7Model




189 changes: 169 additions & 20 deletions docs/examples/gmfs_and_luts.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,34 @@
"windspeed.available_models()"
]
},
{
"cell_type": "markdown",
"id": "f4999b03",
"metadata": {},
"source": [
"## Available models\n",
"\n",
"Adding gmfs_impl (analytical models) with [xsarsea.windspeed.gmfs.GmfModel.activate_gmfs_impl](../basic_api.rst#xsarsea.windspeed.gmfs.GmfModel.activate)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0bdf23bd",
"metadata": {},
"outputs": [],
"source": [
"windspeed.gmfs.GmfModel.activate_gmfs_impl()"
]
},
{
"cell_type": "markdown",
"id": "473e8034-d517-405a-9ca6-1774db35c5fb",
"metadata": {},
"source": [
"### Adding netcdf models (LUT)\n",
"\n",
"Netcdf models are not available by default, because they needs to be loaded from external file with [xsarsea.windspeed.register_all_nc_luts](../basic_api.rst#xsarsea.windspeed.register_all_nc_luts)\n"
"Netcdf models are not available by default, because they needs to be loaded from external file with [xsarsea.windspeed.register_nc_luts](../basic_api.rst#xsarsea.windspeed.register_nc_luts)\n"
]
},
{
Expand All @@ -80,19 +100,41 @@
"metadata": {},
"outputs": [],
"source": [
"nc_luts_path = xsar.get_test_file('xsarsea_luts')\n",
"windspeed.register_all_nc_luts(nc_luts_path)\n",
"nc_luts_path = xsar.get_test_file('nc_luts_reduce')\n",
"windspeed.register_nc_luts(nc_luts_path)\n",
"windspeed.available_models()"
]
},
{
"cell_type": "markdown",
"id": "4118ad0e-11a7-4d6e-970e-50a371af44f3",
"metadata": {},
"source": [
"## Addind CMOD7\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fc098cd5-f541-4c26-a32d-cde800e4c43a",
"metadata": {},
"outputs": [],
"source": [
"try : \n",
" path_cmod7 = xsar.get_test_file(\"cmod7_and_python_script\")\n",
" windspeed.register_cmod7(path_cmod7)\n",
"except Exception as e:\n",
" print(e)"
]
},
{
"cell_type": "markdown",
"id": "2a4321c1-e185-47a6-bb0d-e4f27dcea819",
"metadata": {},
"source": [
"### Adding Sarwing models (LUT)\n",
"\n",
"Sarwing model are not available by default, because they needs to be loaded from external file with [xsarsea.windspeed.register_all_sarwing_luts](../basic_api.rst#xsarsea.windspeed.register_all_sarwing_luts)\n",
"Sarwing model are not available by default, because they needs to be loaded from external file with [xsarsea.windspeed.register_sarwing_luts](../basic_api.rst#xsarsea.windspeed.register_sarwing_luts)\n",
"\n",
"A basic subset of sarwing lut can be retrieved with `xsar.get_test_file('sarwing_luts_subset')`. \n",
"\n",
Expand All @@ -107,7 +149,7 @@
"outputs": [],
"source": [
"sarwing_luts_subset_path = xsar.get_test_file('sarwing_luts_subset')\n",
"windspeed.register_all_sarwing_luts(sarwing_luts_subset_path)\n",
"windspeed.register_sarwing_luts(sarwing_luts_subset_path)\n",
"windspeed.available_models()"
]
},
Expand Down Expand Up @@ -177,6 +219,87 @@
"cmod5.to_lut()"
]
},
{
"cell_type": "markdown",
"id": "8022546d",
"metadata": {},
"source": [
"Man can play with **kwargs of [GmfModel(Model)](../basic_api.rst#xsarsea.windspeed.gmfs.GmfModel)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f4bc732",
"metadata": {},
"outputs": [],
"source": [
"# this will directly generate a LUT at high resolution\n",
"cmod5.to_lut(**{'wspd_step' : 0.1, 'phi_step' : 1.5, 'inc_step' : 0.5, 'resolution' : 'high'})\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c24c84f",
"metadata": {},
"outputs": [],
"source": [
"# this will generate a lut at low resolution and then interpolate at high spedified resolution ('resolution'=None)\n",
"cmod5.to_lut(**{'wspd_step' : 0.1, 'phi_step' : 1.5, 'inc_step' : 0.5, 'resolution' : None})"
]
},
{
"cell_type": "markdown",
"id": "9df945ab",
"metadata": {},
"source": [
"It won't have the same impact on SarwingLutModel or [NcLutModel](../basic_api.rst#xsarsea.windspeed.models.NcLutModel) or [cmod7Model](../basic_api.rst#xsarsea.windspeed.gmfs.cmod7Model).\n",
"\n",
"Indeed, these are saved at at desired format with a certain resolution. \n",
"\n",
"By specifying **kwargs, it forced to interpolate the gmf at the desired resolution."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "58c5c03a",
"metadata": {},
"outputs": [],
"source": [
"# here we specify the exact same params than the saved LUT have\n",
"windspeed.get_model('nc_lut_sarwing_lut_cmodms1ahw').to_lut(**{'wspd_step' : 0.1, 'phi_step' : 1, 'inc_step' : 0.1, 'resolution' : 'high'})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c006a652",
"metadata": {},
"outputs": [],
"source": [
"# here we specify different params than the saved LUT have : interpolation is made\n",
"windspeed.get_model('nc_lut_sarwing_lut_cmodms1ahw').to_lut(**{'wspd_step' : 0.1, 'phi_step' : 1, 'inc_step' : 0.2, 'resolution' : 'high'})"
]
},
{
"cell_type": "markdown",
"id": "2dd55b26",
"metadata": {},
"source": [
"## In practice\n",
"[xsarsea.windspeed.invert_from_model](../basic_api.rst#xsarsea.windspeed.invert_from_model) can be called with **kwargs.\n",
"\n",
"We can use kwargs to force the use of high resolution Luts. \n",
"\n",
"If possible, use analytical_luts (**gmf_...**).\n",
"\n",
"Else, better LUTs that has not be interpolated. \n",
"\n",
"We use kwargs = **{\"wspd_step\": 0.1, \"inc_step\": 0.1, \"phi_step\": 0.1, \"resolution\": \"high\"}**"
]
},
{
"cell_type": "markdown",
"id": "7c692f80-5802-430c-837a-dcb2445f8ce8",
Expand Down Expand Up @@ -210,7 +333,7 @@
"metadata": {},
"outputs": [],
"source": [
"@windspeed.gmfs.GmfModel.register(pol='VH', units='linear')\n",
"@windspeed.gmfs.GmfModel.register(pol='VH', units='linear', defer=False)\n",
"def gmf_dummy(inc, wspd, phi=None): \n",
" a0 = 0.00013106836021008122\n",
" a1 = -4.530598283705591e-06\n",
Expand Down Expand Up @@ -245,17 +368,51 @@
"metadata": {},
"outputs": [],
"source": [
"windspeed.available_models()"
"windspeed.get_model('gmf_dummy').to_lut()"
]
},
{
"cell_type": "markdown",
"id": "ba995dcb",
"metadata": {},
"source": [
"## HH Luts\n",
"\n",
"We also created HH LUTS using CMODs and Polarization Ratio (PR) Models : \n",
"\n",
"- PR \"mouche1\" from Mouche, A., Hauser, D., Kudryavtsev, V., and Daloze, J.-F. (2005). Multi-polarisation ocean radar\n",
"cross-section from envisat asar observations, airborne polarimetric radar measurements and empirical\n",
"or semiempirical models\n",
"& \n",
"\n",
"- PR \"zhang\" from Zhang, B., Perrie, W., and He, Y. (2011). Wind speed retrieval from radarsat-2 quad-polarization images\n",
"using a new polarization ratio model. Journal of Geophysical Research: Oceans.\n",
"\n",
"We simply used this equation and created the NcLutModels\n",
"\n",
"$$\n",
"nrcs_{HH} = \\frac{nrcs_{VV}}{PR}\n",
"$$\n",
"\n",
"For CMOD5n, we created high resolution LUTS (0.1m/s, 0.1°, 1°).\n",
"\n",
"For CMOD7, we created low resolution LUTS (base cmod7 resolution from files) and high resolution (from file + interpolation). \n",
"\n",
"Then we can directly use these luts "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "adc58f1a-48ed-4f77-9615-15f328dc7530",
"id": "e500aac1",
"metadata": {},
"outputs": [],
"source": [
"gmf_dummy.to_lut()"
"# loading low resolution & interpolating \n",
"windspeed.get_model('nc_lut_gmf_cmod7_Rlow_hh_mouche1').to_lut(**{'wspd_step' : 0.1, 'phi_step' : 1, 'inc_step' : 0.1, 'resolution' : 'high'})\n",
"# or \n",
"# loading high resolution\n",
"windspeed.get_model('nc_lut_gmf_cmod7_Rhigh_hh_mouche1').to_lut(**{'wspd_step' : 0.1, 'phi_step' : 1, 'inc_step' : 0.1, 'resolution' : 'high'})"
]
},
{
Expand Down Expand Up @@ -321,7 +478,7 @@
"metadata": {},
"outputs": [],
"source": [
"model_compare([ 'gmf_dummy', 'sarwing_lut_cmodms1ahw'] )"
"model_compare([ 'gmf_dummy', 'nc_lut_sarwing_lut_cmodms1ahw'] )"
]
},
{
Expand All @@ -337,20 +494,12 @@
{
"cell_type": "code",
"execution_count": null,
"id": "7c7cd7f8-5915-4fc5-b498-6c88bd36ce32",
"id": "5b06622d",
"metadata": {},
"outputs": [],
"source": [
"model_compare([ 'gmf_cmod5n', 'gmf_cmod5'])"
"model_compare([ 'gmf_cmod7', 'gmf_cmod5n', 'gmf_cmod5'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "78f4b231-1815-446e-8f0b-afc589e28988",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
30 changes: 28 additions & 2 deletions docs/examples/streaks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@
"logging.getLogger('xsarsea.gradients').setLevel(logging.INFO) # or .setLevel(logging.DEBUG)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f370d8c1-c1a8-46a9-bb75-c9386068156a",
"metadata": {},
"outputs": [],
"source": [
"xsarsea.windspeed.gmfs.GmfModel.activate_gmfs_impl()"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -64,7 +74,7 @@
"sar_ds = xsar.open_dataset(filename,resolution='100m').isel(line=slice(20,None,None),sample=slice(20,None,None)) # isel to skip bad image edge\n",
"\n",
"# add detrended sigma0\n",
"sar_ds['sigma0_detrend'] = xsarsea.sigma0_detrend(sar_ds.sigma0, sar_ds.incidence)\n",
"sar_ds['sigma0_detrend'] = xsarsea.sigma0_detrend(sar_ds.sigma0, sar_ds.incidence, model = \"gmf_cmod5n\")\n",
"# apply land mask\n",
"land_mask = sar_ds['land_mask'].compute()\n",
"sar_ds['sigma0_detrend'] = xr.where(land_mask, np.nan, sar_ds['sigma0_detrend']).transpose(*sar_ds['sigma0_detrend'].dims).compute()\n"
Expand Down Expand Up @@ -128,7 +138,7 @@
"streaks_geo['weight'] = streaks['weight']\n",
"\n",
"# convert directions from image convention to geographic convention\n",
"streaks_geo['streaks_dir'] = xsarsea.dir_sample_to_geo(streaks['angle'], streaks_geo['ground_heading']) \n",
"streaks_geo['streaks_dir'] = xsarsea.dir_sample_to_geo(np.rad2deg(streaks['angle']), streaks_geo['ground_heading']) \n",
"\n",
"streaks_geo = streaks_geo.compute()\n",
"\n",
Expand All @@ -143,6 +153,14 @@
").opts(pivot='mid', arrow_heads=False, tools=['hover'], magnitude='Magnitude')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4866732c-a366-4c45-ae06-aa271b8992c2",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "97c9ff4b",
Expand Down Expand Up @@ -342,6 +360,14 @@
"\n",
"gridspace.opts(plot_size=(200,200)) + (hv_img * hv_vf).opts(legend_position='right', frame_height=500)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a762fdcb-ed24-4917-8004-d6ffee51ed25",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
Loading