Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
nc_to_std
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package 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
Maria Tarasevich
nc_to_std
Commits
75a96487
Commit
75a96487
authored
3 years ago
by
Maria Tarasevich
Browse files
Options
Downloads
Patches
Plain Diff
Initial commit
parents
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
extract_nc.py
+46
-0
46 additions, 0 deletions
extract_nc.py
with
46 additions
and
0 deletions
extract_nc.py
0 → 100644
+
46
−
0
View file @
75a96487
import
netCDF4
as
nc
import
numpy
as
np
import
argparse
def
summary
(
vars
,
dims
):
for
var
in
vars
:
dims_str
=
'
x
'
.
join
(
'
%d(%s)
'
%
(
dims
[
dim
],
dim
)
for
dim
in
reversed
(
var
.
dimensions
))
print
(
'
name =
'
,
var
.
name
,
'
, dims =
'
,
dims_str
,
'
, type =
'
,
var
.
dtype
)
attrs
=
{
attr
:
var
.
getncattr
(
attr
)
for
attr
in
var
.
ncattrs
()}
print
(
'
long_name =
'
,
attrs
.
get
(
"
long_name
"
),
'
, standard_name =
'
,
attrs
.
get
(
"
standard_name
"
))
def
main
():
parser
=
argparse
.
ArgumentParser
(
description
=
'
Extract STD fields from netCDF4 file
'
)
parser
.
add_argument
(
'
-i
'
,
'
--input
'
,
help
=
'
input netCDF4 file
'
,
required
=
True
)
parser
.
add_argument
(
'
-v
'
,
'
--var
'
,
action
=
'
append
'
,
help
=
'
Field variable to extract, may be specified multiple times
'
)
parser
.
add_argument
(
'
-o
'
,
'
--out-format
'
,
default
=
'
{v}.std
'
,
help
=
'
Output filename pattern. Patter %s will be replaced with variable name
'
)
args
=
parser
.
parse_args
()
root
=
nc
.
Dataset
(
args
.
input
,
"
r
"
)
dims
=
{
dim
.
name
:
dim
.
size
for
dim
in
root
.
dimensions
.
values
()}
vars
=
root
.
variables
if
not
args
.
var
:
print
(
'
No variables to extract, printing variable summary
'
)
summary
(
vars
.
values
(),
dims
)
else
:
for
var_name
in
args
.
var
:
if
var_name
not
in
vars
:
print
(
f
"
Variable
{
var_name
}
is not present in the file
"
)
return
if
len
(
args
.
var
)
>
1
and
(
'
{v}
'
not
in
args
.
out_format
):
print
(
"
Several vars are requested but output file is the same for all vars. Please use {v} to produce different files
"
)
return
for
var_name
in
args
.
var
:
var
=
vars
[
var_name
]
outfn
=
args
.
out_format
.
format
(
v
=
var_name
)
print
(
f
'
Extracting
{
var_name
}
with type
{
var
.
dtype
}
and size
{
tuple
(
reversed
(
var
.
shape
))
}
into
{
outfn
}
'
)
np
.
array
(
var
).
tofile
(
outfn
)
if
__name__
==
"
__main__
"
:
main
()
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