Skip to content

Commit 4533b7f

Browse files
working verion
1 parent a2fa829 commit 4533b7f

File tree

4 files changed

+72
-32
lines changed

4 files changed

+72
-32
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
'get_promice_data = pypromice.get.get_promice_data:get_promice_data',
4545
'get_l0tx = pypromice.tx.get_l0tx:get_l0tx',
4646
'join_l2 = pypromice.process.join_l2:join_l2',
47+
'join_l3 = pypromice.process.join_l3:join_l3',
4748
'get_l2 = pypromice.process.get_l2:get_l2',
4849
'get_l3 = pypromice.process.get_l3:get_l3',
4950
'get_l2tol3 = pypromice.process.get_l2tol3:get_l2tol3',

src/pypromice/process/join_l3.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,12 @@ def gcnet_postprocessing(l3):
165165
def join_l3():
166166
args = parse_arguments_joinl3()
167167

168+
168169
config_file = os.path.join(args.config_folder, args.site+'.toml')
169170
conf = toml.load(config_file)
170-
171-
if not site:
172-
site_list=conf['list_station_id']
173-
else:
174-
site_list=site
175171

176172
l3m = xr.Dataset()
177-
for stid in site_list:
173+
for stid in conf['stations']:
178174
print(stid)
179175

180176
is_promice = False
@@ -221,8 +217,10 @@ def join_l3():
221217
for v in l3.data_vars:
222218
if v not in l3m.data_vars:
223219
if v != 'z_stake':
224-
print(v)
225-
l3 = l3.drop(v)
220+
print(v)
221+
l3 = l3.drop(v)
222+
else:
223+
l3m[v] = ('time', l3m.t_u.data*np.nan)
226224

227225
# saving attributes of station under an attribute called $stid
228226
l3m = l3m.assign_attrs({stid : l3.attrs.copy()})
@@ -243,7 +241,7 @@ def join_l3():
243241

244242
# Assign site id
245243
l3m.attrs['site_id'] = args.site
246-
l3m.attrs['list_station_id'] = conf['list_station_id']
244+
l3m.attrs['stations'] = conf['stations']
247245
v = getVars()
248246
m = getMeta()
249247
if args.outpath is not None:

src/pypromice/process/utilities.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ def reformat_lon(dataset, exempt=['UWN', 'Roof_GEUS', 'Roof_PROMICE']):
4141
'''Switch gps_lon to negative values (degrees_east). We do this here, and
4242
NOT in addMeta, otherwise we switch back to positive when calling getMeta
4343
in joinL2'''
44-
if dataset.attrs['station_id'] not in exempt:
44+
if 'station_id' in dataset.attrs.keys():
45+
id = dataset.attrs['station_id']
46+
else:
47+
id = dataset.attrs['site_id']
48+
49+
if id not in exempt:
4550
dataset['gps_lon'] = dataset['gps_lon'] * -1
4651
return dataset
4752

@@ -163,7 +168,10 @@ def addMeta(ds, meta):
163168
# ds[k].attrs['units'] = 'degrees_C'
164169

165170
# https://wiki.esipfed.org/Attribute_Convention_for_Data_Discovery_1-3#geospatial_bounds
166-
ds.attrs['id'] = 'dk.geus.promice:' + str(uuid.uuid3(uuid.NAMESPACE_DNS, ds.attrs['station_id']))
171+
if 'station_id' in ds.attrs.keys():
172+
ds.attrs['id'] = 'dk.geus.promice:' + str(uuid.uuid3(uuid.NAMESPACE_DNS, ds.attrs['station_id']))
173+
else:
174+
ds.attrs['id'] = 'dk.geus.promice:' + str(uuid.uuid3(uuid.NAMESPACE_DNS, ds.attrs['site_id']))
167175
ds.attrs['history'] = 'Generated on ' + datetime.datetime.utcnow().isoformat()
168176
ds.attrs['date_created'] = str(datetime.datetime.now().isoformat())
169177
ds.attrs['date_modified'] = ds.attrs['date_created']
@@ -230,4 +238,4 @@ def _addAttr(ds, key, value):
230238
pass
231239
# logger.info(f'Unable to add metadata to {key.split(".")[0]}')
232240
else:
233-
ds.attrs[key] = value
241+
ds.attrs[key] = value

src/pypromice/process/write.py

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,36 @@ def prepare_and_write(dataset, outpath, vars_df, meta_dict, time='60min', resamp
4646

4747
# Round all values to specified decimals places
4848
d2 = utilities.roundValues(d2, vars_df)
49-
50-
# Create out directory
51-
outdir = os.path.join(outpath, d2.attrs['station_id'])
52-
if not os.path.isdir(outdir):
53-
os.mkdir(outdir)
54-
49+
5550
# Get variable names to write out
5651
col_names = write.getColNames(
5752
vars_df,
58-
d2.attrs['number_of_booms'],
59-
d2.attrs['format'],
60-
d2.attrs['bedrock'],
61-
)
62-
53+
d2)
54+
6355
# Define filename based on resample rate
6456
t = int(pd.Timedelta((d2['time'][1] - d2['time'][0]).values).total_seconds())
57+
if 'station_id' in d2.attrs.keys():
58+
name = d2.attrs['station_id']
59+
else:
60+
name = d2.attrs['site_id']
61+
62+
# Create out directory
63+
outdir = os.path.join(outpath, name)
64+
if not os.path.isdir(outdir):
65+
os.mkdir(outdir)
66+
6567
if t == 600:
66-
out_csv = os.path.join(outdir, d2.attrs['station_id']+'_10min.csv')
67-
out_nc = os.path.join(outdir, d2.attrs['station_id']+'_10min.nc')
68+
out_csv = os.path.join(outdir, name+'_10min.csv')
69+
out_nc = os.path.join(outdir, name+'_10min.nc')
6870
elif t == 3600:
69-
out_csv = os.path.join(outdir, d2.attrs['station_id']+'_hour.csv')
70-
out_nc = os.path.join(outdir, d2.attrs['station_id']+'_hour.nc')
71+
out_csv = os.path.join(outdir, name+'_hour.csv')
72+
out_nc = os.path.join(outdir, name+'_hour.nc')
7173
elif t == 86400:
72-
out_csv = os.path.join(outdir, d2.attrs['station_id']+'_day.csv')
73-
out_nc = os.path.join(outdir, d2.attrs['station_id']+'_day.nc')
74+
out_csv = os.path.join(outdir, name+'_day.csv')
75+
out_nc = os.path.join(outdir, name+'_day.nc')
7476
else:
75-
out_csv = os.path.join(outdir, d2.attrs['station_id']+'_month.csv')
76-
out_nc = os.path.join(outdir, d2.attrs['station_id']+'_month.nc')
77+
out_csv = os.path.join(outdir, name+'_month.csv')
78+
out_nc = os.path.join(outdir, name+'_month.nc')
7779
if not os.path.isdir(outdir):
7880
os.mkdir(outdir)
7981
# Write to csv file
@@ -150,7 +152,38 @@ def writeNC(outfile, Lx, col_names=None):
150152
names = list(Lx.keys())
151153
Lx[names].to_netcdf(outfile, mode='w', format='NETCDF4', compute=True)
152154

153-
def getColNames(vars_df, booms=None, data_type=None, bedrock=False):
155+
def getColNames(vars_df, ds):
156+
'''Get all variable names for a given data type, based on a variables
157+
look-up table. This is mainly for exporting purposes
158+
159+
Parameters
160+
----------
161+
vars_df : pd.DataFrame
162+
Variables look-up table
163+
ds: xr.dataset
164+
Dataset to write
165+
Returns
166+
-------
167+
list
168+
Variable names
169+
'''
170+
if 'data_type' in ds.attrs.keys():
171+
if ds.attrs['data_type']=='TX':
172+
vars_df = vars_df.loc[vars_df['data_type'].isin(['TX','all'])]
173+
elif ds.attrs['data_type']=='STM' or ds.attrs['data_type']=='raw':
174+
vars_df = vars_df.loc[vars_df['data_type'].isin(['raw','all'])]
175+
176+
var_list = list(vars_df.index)
177+
for v in var_list:
178+
if v not in ds.keys():
179+
var_list.remove(v)
180+
continue
181+
if ds[v].isnull().all():
182+
var_list.remove(v)
183+
return var_list
184+
185+
186+
def getColNames_old(vars_df, booms=None, data_type=None, bedrock=False):
154187
'''Get all variable names for a given data type, based on a variables
155188
look-up table. This is mainly for exporting purposes
156189

0 commit comments

Comments
 (0)