Skip to content

Commit 25b10c6

Browse files
committed
Changed default variables to reshuffle SMOS L2 and added CLI option to select variables manually
1 parent 35b27ad commit 25b10c6

File tree

6 files changed

+70
-13
lines changed

6 files changed

+70
-13
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
Changelog
33
=========
44

5+
v0.3.1
6+
======
7+
- Changed the default variables reshuffled for SMOS L2 and added option to select variables
8+
59
v0.3.0
610
======
711
- Add support for SMOS L4 RZSM product (`PR #11 <https://github.com/TUW-GEO/smos/pull/11>`_)

environment.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ dependencies:
1717
- pygeogrids
1818
- pynetcf
1919
- pyproj
20-
- git+https://github.com/TUW-GEO/repurpose@master
20+
- repurpose>=0.13.1
2121
- trollsift
2222
- ease_grid
2323
- more_itertools
2424
- cf-xarray==0.8.4
25-
- git+https://github.com/awst-austria/qa4sm-preprocessing@master
25+
- qa4sm_preprocessing==0.3

setup.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ install_requires =
3737
dask[distributed]
3838
xarray
3939
netCDF4
40-
repurpose
40+
repurpose>=0.13.1
4141
pyresample
4242
pygeogrids>=0.3.2
4343
pynetcf>=0.5.1
@@ -47,7 +47,7 @@ install_requires =
4747
h5py
4848
more_itertools
4949
cf-xarray==0.8.4
50-
qa4sm_preprocessing>=0.2
50+
qa4sm_preprocessing>=0.3
5151
# The usage of test_requires is discouraged, see `Dependency Management` docs
5252
#tests_require = pytest; pytest-cov; coverage
5353
# Require a specific Python version, e.g. Python 2.7 or >= 3.4

src/smos/smos_l2/cli.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pandas as pd
44

55
from smos.smos_l2.download import SmosDissEoFtp, L2_START_DATE
6-
from smos.smos_l2.reshuffle import swath2ts, extend_ts
6+
from smos.smos_l2.reshuffle import swath2ts, extend_ts, _default_variables
77
from smos.misc import get_first_last_day_images
88

99
@click.command(
@@ -138,14 +138,22 @@ def cli_update_img(path,
138138
default=None,
139139
help="Format YYYY-MM-DD | Last day to include in the"
140140
"time series. [default: Date of the last available image]")
141+
@click.option(
142+
'--variables',
143+
'-v',
144+
type=click.STRING,
145+
default=','.join(_default_variables),
146+
help="List of variables in the swath files to reshuffle. Multiple variables"
147+
" must be comma-separated.")
141148
@click.option(
142149
'--memory',
143150
'-m',
144151
type=click.INT,
145152
default=4,
146153
help="NUMBER | Available memory (in GB) to use to load image data. "
147154
"A larger buffer means faster processing.")
148-
def cli_reshuffle(img_path, ts_path, startdate, enddate, memory):
155+
def cli_reshuffle(img_path, ts_path, startdate, enddate, variables,
156+
memory):
149157
"""
150158
Convert SMOS L2 image data into a (5x5 degrees chunked) time series format
151159
following CF conventions (Indexed Ragged format).
@@ -158,18 +166,21 @@ def cli_reshuffle(img_path, ts_path, startdate, enddate, memory):
158166
Required Parameters
159167
-------------------
160168
IMG_PATH: string
161-
Path where previously downloaded C3S SM images are stored. Use the
162-
`c3s_sm download` command to retrieve image data.
169+
Path where previously downloaded SMOS images are stored. Use the
170+
`smos_l2 download` command to retrieve image data.
163171
TS_PATH: string
164172
Path where the newly created time series files should be stored.
165173
"""
166174
# The docstring above is slightly different to the normal python one to
167175
# display it properly on the command line.
168176
print(f"Convert image data in {img_path} to time series in {ts_path}")
169177

178+
variables = [str(v.strip()) for v in variables.split(',')]
179+
170180
swath2ts(
171181
img_path,
172182
ts_path,
183+
variables=variables,
173184
startdate=startdate,
174185
enddate=enddate,
175186
memory=int(memory))
@@ -193,7 +204,7 @@ def cli_update_ts(img_path, ts_path):
193204
Required Parameters
194205
-------------------
195206
IMG_PATH: string
196-
Path where previously downloaded C3S SM images are stored.
207+
Path where previously downloaded SMOS files are stored.
197208
TS_PATH: string
198209
Path where the time series to update are stored
199210
"""

src/smos/smos_l2/reshuffle.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1+
import numpy as np
12
import pandas as pd
23
import os
34
import yaml
45
from qa4sm_preprocessing.level2.smos import SMOSL2Reader
56
from smos.misc import read_summary_yml, get_first_last_day_images
67
from datetime import datetime
78

8-
9-
def swath2ts(img_path, ts_path, startdate=None, enddate=None, memory=4):
9+
_default_variables = (
10+
"Soil_Moisture",
11+
"Science_Flags",
12+
"Confidence_Flags",
13+
"Chi_2_P",
14+
"RFI_Prob",
15+
"N_RFI_X",
16+
"N_RFI_Y",
17+
"M_AVA0",
18+
)
19+
20+
def swath2ts(img_path, ts_path, variables=_default_variables,
21+
startdate=None, enddate=None, memory=4):
1022
"""
1123
Convert SMOS L2 swath data to time series in IndexedRaggedTs format.
1224
@@ -17,6 +29,19 @@ def swath2ts(img_path, ts_path, startdate=None, enddate=None, memory=4):
1729
swath data are found.
1830
ts_path: str
1931
Local directory where the converted time series data will be stored.
32+
variables: tuple or str, optional (default: None)
33+
List of variables to include, None will use the default variables
34+
"Soil_Moisture",
35+
"Soil_Moisture_DQX",
36+
"Science_Flags",
37+
"Confidence_Flags",
38+
"Processing_Flags",
39+
"Chi_2_P",
40+
"RFI_Prob",
41+
"N_RFI_X",
42+
"N_RFI_Y",
43+
"M_AVA0",
44+
"acquisition_time"
2045
startdate: str or datetime, optional (default: None)
2146
First day of the available swath data that should be included in the
2247
time series. If None is passed, then the first available day is used.
@@ -27,7 +52,13 @@ def swath2ts(img_path, ts_path, startdate=None, enddate=None, memory=4):
2752
Size of available memory in GB. More memory will lead to a faster
2853
conversion.
2954
"""
30-
reader = SMOSL2Reader(img_path)
55+
variables = [v for v in np.atleast_1d(variables)]
56+
57+
if "acquisition_time" not in variables:
58+
variables.append("acquisition_time")
59+
60+
reader = SMOSL2Reader(img_path, varnames=variables,
61+
add_overpass_flag=True)
3162

3263
first_day, last_day = get_first_last_day_images(img_path)
3364

@@ -123,3 +154,9 @@ def extend_ts(img_path, ts_path, memory=4):
123154
else:
124155
print(f"No extension required From: {startdate} To: {last_day}")
125156

157+
if __name__ == '__main__':
158+
ts_path = '/tmp/ts'
159+
img_path = "/home/wpreimes/shares/climers/Projects/QA4SM_HR/07_data/SERVICE_DATA/SMOS_L2/SMOSL2_v700-ext/images"
160+
# swath2ts(img_path,
161+
# ts_path=ts_path, startdate='2024-03-01', enddate='2024-03-05')
162+
extend_ts(img_path, ts_path)

tests/smos_l2/test_l2_reshuffle.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
import pandas as pd
33
from tempfile import TemporaryDirectory
4-
from smos.smos_l2.reshuffle import swath2ts, extend_ts
4+
from smos.smos_l2.reshuffle import swath2ts, extend_ts, _default_variables
55
from smos.misc import read_summary_yml
66
from pynetcf.time_series import GriddedNcIndexedRaggedTs
77
from pygeogrids.netcdf import load_grid
@@ -42,6 +42,11 @@ def test_reshuffle_and_update():
4242
ts.loc['2022-01-02', 'Soil_Moisture'].values[0],
4343
0.52442, 5
4444
)
45+
for var in _default_variables:
46+
assert var in ts.columns
47+
48+
assert 'Overpass' in ts.columns
49+
4550
assert 1 in ts.index.day
4651
assert 2 in ts.index.day
4752
assert 3 not in ts.index.day # this must be excluded

0 commit comments

Comments
 (0)