Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
python
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tom Rink
python
Commits
1f5c46b6
Commit
1f5c46b6
authored
3 months ago
by
tomrink
Browse files
Options
Downloads
Patches
Plain Diff
snapshot...
parent
9e23c569
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
modules/util/split_nc4.py
+69
-1
69 additions, 1 deletion
modules/util/split_nc4.py
with
69 additions
and
1 deletion
modules/util/split_nc4.py
+
69
−
1
View file @
1f5c46b6
...
...
@@ -64,4 +64,72 @@ def concatenate_nc4_files(nc_files, output_file, concat_dim_name='time'):
print
(
f
"
All files combined and saved to
{
output_file
}
"
)
# Call the function
# split_dataset('input.nc', 'output_{}.nc', 'time', 10)
\ No newline at end of file
# split_dataset('input.nc', 'output_{}.nc', 'time', 10)
def
aggregate_output_files
(
nc_files
,
output_file
,
track_dim_name
=
'
nj
'
,
xtrack_dim_name
=
'
ni
'
):
input_datasets
=
[
nc
.
Dataset
(
nc_file
,
'
r
'
,
format
=
'
NETCDF4
'
)
for
nc_file
in
nc_files
]
output_rootgrp
=
nc
.
Dataset
(
output_file
,
'
w
'
,
format
=
'
NETCDF4
'
)
# calculate new track_dim length
track_dim_len
=
0
xtrack_dim_len
=
None
for
ds
in
input_datasets
:
for
name
,
dim
in
ds
.
dimensions
.
items
():
if
name
==
track_dim_name
:
track_dim_len
+=
len
(
dim
)
elif
name
==
xtrack_dim_name
and
xtrack_dim_len
is
None
:
xtrack_dim_len
=
len
(
dim
)
# create the dimensions for the new aggregation target file
output_rootgrp
.
createDimension
(
'
time
'
,
1
)
output_rootgrp
.
createDimension
(
track_dim_name
,
track_dim_len
)
output_rootgrp
.
createDimension
(
xtrack_dim_name
,
xtrack_dim_len
)
# use the time value from the first file for aggregated time
var
=
input_datasets
[
0
].
variables
[
'
time
'
]
time_var
=
output_rootgrp
.
createVariable
(
'
time
'
,
var
.
datatype
,
'
time
'
)
time_var
.
setncatts
({
k
:
var
.
getncattr
(
k
)
for
k
in
var
.
ncattrs
()})
# assign the value of var to time_var
time_var
[:]
=
var
[:]
# create lon, lat variables
var
=
input_datasets
[
0
].
variables
[
'
lon
'
]
lon_var
=
output_rootgrp
.
createVariable
(
'
lon
'
,
var
.
datatype
,
[
'
nj
'
,
'
ni
'
])
lon_var
.
set_auto_maskandscale
(
False
)
lon_var
.
setncatts
({
k
:
var
.
getncattr
(
k
)
for
k
in
var
.
ncattrs
()})
var
=
input_datasets
[
0
].
variables
[
'
lat
'
]
lat_var
=
output_rootgrp
.
createVariable
(
'
lat
'
,
var
.
datatype
,
[
'
nj
'
,
'
ni
'
])
lat_var
.
set_auto_maskandscale
(
False
)
lat_var
.
setncatts
({
k
:
var
.
getncattr
(
k
)
for
k
in
var
.
ncattrs
()})
# create the other variables
for
name
,
var
in
input_datasets
[
0
].
variables
.
items
():
if
name
not
in
[
'
time
'
,
'
lon
'
,
'
lat
'
]:
out_var
=
output_rootgrp
.
createVariable
(
name
,
var
.
datatype
,
[
'
time
'
,
track_dim_name
,
xtrack_dim_name
])
out_var
.
set_auto_maskandscale
(
False
)
out_var
.
setncatts
({
k
:
var
.
getncattr
(
k
)
for
k
in
var
.
ncattrs
()})
# copy from input files to the single output file
start_idx
=
0
for
ds
in
input_datasets
:
track_len
=
len
(
ds
.
dimensions
[
track_dim_name
])
lon_var
[
start_idx
:
start_idx
+
track_len
,
:]
=
ds
.
variables
[
'
lon
'
][:,
:]
lat_var
[
start_idx
:
start_idx
+
track_len
,
:]
=
ds
.
variables
[
'
lat
'
][:,
:]
start_idx
+=
track_len
start_idx
=
0
for
ds
in
input_datasets
:
track_len
=
len
(
ds
.
dimensions
[
track_dim_name
])
for
name
,
var
in
ds
.
variables
.
items
():
if
name
not
in
[
'
time
'
,
'
lon
'
,
'
lat
'
]:
out_var
=
output_rootgrp
.
variables
[
name
]
out_var
[
0
,
start_idx
:
start_idx
+
track_len
,
:]
=
var
[
0
,
:,
:]
start_idx
+=
track_len
output_rootgrp
.
close
()
return
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment