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

Reorganizing subset_data.py to follow SE recommendations #1461

Merged
merged 193 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
193 commits
Select commit Hold shift + click to select a range
abf2968
adding the skeleton file here...
negin513 Aug 12, 2021
efc8f9f
adding a module file for each class... suggested by Bill Sacks.
negin513 Aug 18, 2021
89497d3
updating subset_data.py remove class definition from here.
negin513 Aug 18, 2021
60d9494
adding the top-level skeleton.
negin513 Sep 7, 2021
50f8727
adding classes for subset_data.py
negin513 Sep 7, 2021
093a0ed
moving this under site_and_regional.
negin513 Sep 7, 2021
b4bb64e
moving the classes under subset_data.
negin513 Sep 7, 2021
bf8026a
adding some more instructions...
negin513 Sep 7, 2021
1f45c14
removing subset_data.py under site_and_regional...why not git mv :/
negin513 Sep 7, 2021
cb317e9
some small changes...
negin513 Sep 7, 2021
9edf9b0
running this through formatter...
negin513 Sep 7, 2021
93243c6
more comments...
negin513 Sep 7, 2021
408c604
running through formatter...
negin513 Sep 7, 2021
7f72af9
adding the deleted unit test.
negin513 Sep 7, 2021
40d6b3e
removing the capability to run ./subset_data.py directly...
negin513 Sep 17, 2021
7095f7e
not executable anymore...
negin513 Sep 17, 2021
92fbaad
updates to facilitate user mods and config file
Dec 8, 2021
6ddf11f
add CLM_USRDAT_DIR xml variable
Dec 8, 2021
6e2dd11
implementing ctsm_logging instead of logging.
negin513 Dec 8, 2021
7a176ba
add clm_usrdat_dir capabilities
Dec 9, 2021
d2b8afb
fix typo with args.outputdir
Dec 9, 2021
6e39fc5
use ctsm logging...
negin513 Dec 9, 2021
fad54a0
remove the previous logging to file, update flags.
negin513 Dec 10, 2021
77d23da
fix writing out of user_mods files
Dec 10, 2021
db56dc5
cleaning up the parsers.
negin513 Dec 10, 2021
0ffa6e9
updates to make things more pythonic
Dec 10, 2021
910a21a
bunch of changes
negin513 Dec 10, 2021
8868918
merge in changes from negin513/python_dev_meeting
Dec 13, 2021
de7ef43
fixing git sha
adrifoster Dec 13, 2021
b96352f
more minor changes
negin513 Dec 14, 2021
455197a
some documentation changes.
negin513 Dec 14, 2021
97f52b9
formatting changes
negin513 Dec 14, 2021
04b38a5
more changes to docstrings.
negin513 Dec 14, 2021
47515a9
more changes and reformatting.
negin513 Dec 14, 2021
420a0fb
just reformatting.
negin513 Dec 14, 2021
9f21042
formatting and adding some more git utils.
negin513 Dec 14, 2021
f4fdf05
small changes.
negin513 Dec 14, 2021
88d1c24
refactor to set up files/dictories in one function
Dec 14, 2021
c5613f4
updates according to pylint
Dec 14, 2021
f655b47
update docstring
Dec 14, 2021
c11e3ae
making datm_dict its own dictionary within file_dict
Dec 14, 2021
45fe47f
add docstrings
Dec 14, 2021
f98b9b1
merge in changes from negin513/python_dev_meeting
Dec 14, 2021
8e63202
fixing pylint suggestions
Dec 14, 2021
b35a572
fix typo in defaults.get
Dec 14, 2021
d8819a9
update file names to separate dir and file
Dec 14, 2021
799fc3d
fix typo with file writing
Dec 14, 2021
73d3c28
fix directory of user_mods
Dec 14, 2021
c47ae2c
add datm_dict functionality to datmdomain
Dec 14, 2021
af08452
fix typo
Dec 14, 2021
f6a3ecd
move datmdomain into datm_dict
Dec 14, 2021
12b04a7
add namedtuple
Dec 15, 2021
7bf3f12
fix typo in datm tag
Dec 15, 2021
446b90f
merge in changes from master
Dec 15, 2021
5dc0a44
update import of str2bool
Dec 15, 2021
15b0f8e
update index name for more clarity
Dec 15, 2021
2f3a6b5
update flag logic
Dec 15, 2021
a6f23ca
merge in 99a85fe53cf169615294c46c8c35da3a89b1545b and master
negin513 Dec 16, 2021
9492b04
fix pylint issues
Dec 16, 2021
f98114d
merge in changes from negin513
Dec 16, 2021
119dfe8
some minor changes and typos.
negin513 Dec 16, 2021
e3d3d03
pylint issues
Dec 16, 2021
1397941
updating the command line arguments.
negin513 Dec 16, 2021
26bf17f
minor changes
negin513 Dec 16, 2021
81b051c
update the docstrings for clarifications.
negin513 Dec 16, 2021
00dd1c9
minor change
negin513 Dec 16, 2021
2b70310
update git_utils to point to ctsm
negin513 Dec 16, 2021
3cc9fb9
remove these options for now.
negin513 Dec 16, 2021
6cbb380
updates from negin
Dec 16, 2021
9d443d2
fixing typos
Dec 17, 2021
d6268b7
fixing issues from merge
Dec 18, 2021
9998e6d
remove folders accidentally pushed
Dec 18, 2021
289d3a7
remove personal defaults file
adrifoster Dec 18, 2021
b50a3ba
Merge branch 'subsetdata_update' of https://github.com/adrifoster/CTS…
adrifoster Dec 18, 2021
cd21034
Merge branch 'python_dev_meeting' into subsetdata_update
negin513 Dec 21, 2021
2b30984
update fdomain file name
Dec 21, 2021
62a1688
Merge branch 'subsetdata_update' of https://github.com/adrifoster/CTS…
Dec 21, 2021
e04bf2f
remove duplicated argparse argument from merge
Dec 27, 2021
2e73d8a
Update externasl to point to new cdeps
adrifoster Dec 28, 2021
782c117
Merge pull request #2 from adrifoster/subsetdata_update
negin513 Jan 3, 2022
c642aa5
merge conflict resolved.
negin513 Jan 3, 2022
cc34e12
updates to ctsm_logging.py
negin513 Jan 3, 2022
0f09b38
updates.
negin513 Jan 3, 2022
f4131c9
updates.
negin513 Jan 3, 2022
d509fe2
update git_utils.
negin513 Jan 3, 2022
dde94fe
update logging
negin513 Jan 3, 2022
4e6c6a2
minor edits.
negin513 Jan 3, 2022
f9710ca
Changes to arguments and script names so that run_neon.py and subset_…
ekluzek Jan 5, 2022
6385264
Add --verbose to all of the site_and_regional script arguments, also …
ekluzek Jan 5, 2022
15f5c28
Check if History_Log attribute exists before removing it as otherwise…
ekluzek Jan 5, 2022
4afe339
verbose is not an option for the modify_singlept_site_neon.py script
ekluzek Jan 5, 2022
94777dc
Add setup only option for run neon case, so that it will only setup a…
ekluzek Jan 5, 2022
3e5fedd
verbose debug as subparser options.
negin513 Jan 5, 2022
416f45f
bug fix one line.
negin513 Jan 5, 2022
72261e3
Merge branch 'python_dev_meeting' of github.com:negin513/CTSM into py…
ekluzek Jan 5, 2022
96717fb
updates to argparse for clarity
Jan 5, 2022
5f417ec
more updates to argparse
Jan 5, 2022
717ce37
Merge pull request #3 from adrifoster/subsetdata_update
negin513 Jan 6, 2022
d3a0c14
putting args_utils.
negin513 Jan 8, 2022
fc082ef
committing args_utils
negin513 Jan 8, 2022
694d1b3
moving plat and plon types to args_utils.
negin513 Jan 8, 2022
85cc861
removing plat plon types from subset_data.py
negin513 Jan 8, 2022
4329726
updating get_git functions
negin513 Jan 8, 2022
86d6e79
modifying surface dataset update get_git.
negin513 Jan 8, 2022
00120d7
addressing minor comments.
negin513 Jan 8, 2022
7991267
removing the lsmlat lsmlon update lines that were duplicates.
negin513 Jan 8, 2022
3c0827f
updating config_utils.py
negin513 Jan 8, 2022
bccabbd
removing defaults.
negin513 Jan 8, 2022
f362eb0
abort!
negin513 Jan 8, 2022
d000f53
updating the use of get_config_value
negin513 Jan 8, 2022
07d677d
Remove the getregional_datasets scripts and the testing for it as wel…
ekluzek Jan 10, 2022
612e35a
Remove the getregional_datasets scripts and the testing for it as wel…
ekluzek Jan 10, 2022
632a500
update more comments.
negin513 Jan 10, 2022
2b20576
updates regarding config_vals.
negin513 Jan 10, 2022
d6a26f3
sorting glob.glob for #1437.
negin513 Jan 10, 2022
d2dc19c
improving the download_data
negin513 Jan 10, 2022
0407486
imroving the download_data for #1594
negin513 Jan 10, 2022
1754666
removing overwrite_single_pft
negin513 Jan 10, 2022
7770c9e
removing overwrite
negin513 Jan 18, 2022
b6ee377
fixing messege when no args provided.
negin513 Jan 18, 2022
ca2e297
ability to overwrite when dom_pft is higher than 16.
negin513 Jan 18, 2022
7290331
subset_data accepts pct_pft.
negin513 Jan 25, 2022
76b4678
first draft of multiple pfts
negin513 Jan 26, 2022
09b1d70
moving the error location.
negin513 Jan 26, 2022
7368664
some modifications.
negin513 Jan 26, 2022
e3e511c
adding tests for args_utils.py
negin513 Jan 26, 2022
7332c33
adding tests + pylint cleanups.
negin513 Jan 26, 2022
ba95375
cleaning up pylint.
negin513 Jan 26, 2022
fc88c13
single_point_case.py tests.
negin513 Jan 27, 2022
dad831b
unit tests for single_point_case.py
negin513 Jan 27, 2022
5a2aba1
few fixes regarding pfts
negin513 Jan 27, 2022
fc9c6db
remove duplicated function.
negin513 Jan 27, 2022
6540948
cleaning up run_neon download_file
negin513 Jan 27, 2022
4d22aed
cleaned up formatting of run_neon.
negin513 Jan 27, 2022
4c22765
addressing more comments.
negin513 Jan 27, 2022
13fec89
addressing more comments.
negin513 Jan 27, 2022
8d35b10
update readme.
negin513 Jan 27, 2022
b4660c3
pylint issues
negin513 Jan 27, 2022
0c35f06
correct initialization and fix pylint.
negin513 Jan 27, 2022
935d72c
update neon script to reflect the recent changes.
negin513 Jan 27, 2022
4a5469a
update utils.py try block to move response outside
negin513 Jan 27, 2022
500e973
update dom_pft-1 handling which was done by mistake.
negin513 Jan 27, 2022
138686b
update my unit tests.
negin513 Jan 27, 2022
f6fe40e
move download_file to download_utils so that lilac test pass.
negin513 Jan 27, 2022
2b30cb1
adding download_utils.
negin513 Jan 28, 2022
1dd54c9
one more comment.
negin513 Jan 28, 2022
9aafcbb
fixing landuse name
negin513 Jan 28, 2022
88c074b
fixed typos.
negin513 Jan 28, 2022
3432581
update README.
negin513 Jan 28, 2022
237ae68
removing --help line.
negin513 Jan 28, 2022
16db43b
Introduce local variable in plon_type
billsacks Jan 30, 2022
9ef3901
Restore on/off options
billsacks Jan 30, 2022
d09113d
Fix some documentation
billsacks Jan 30, 2022
5804aa6
adding constants for months.
negin513 Feb 9, 2022
1e7c8ac
adding test cased for the edge cases
negin513 Feb 9, 2022
6346ec5
updating single_point docs
negin513 Feb 9, 2022
124ee5a
adding more unit tests to this code.
negin513 Feb 9, 2022
324e9e7
check to see if file exists
negin513 Feb 9, 2022
dbc48b7
updating capability to check if files exist before overwriting.
negin513 Feb 10, 2022
50e20bb
updating to not use file
negin513 Feb 10, 2022
e501e4b
Merge pull request #4 from billsacks/python_dev_meeting
negin513 Feb 10, 2022
d947ba6
Merge remote-tracking branch 'origin/python_dev_meeting' into python_…
negin513 Feb 10, 2022
740347a
updating the unit tests
negin513 Feb 10, 2022
2bed6f7
seperate modify_surfdata_atpoint following Erik's suggestion.
negin513 Feb 10, 2022
ba98245
adding some unit-testing for netcdf data...
negin513 Feb 10, 2022
5e2a83a
pylint clean ups
negin513 Feb 10, 2022
c1d4758
more clean ups
negin513 Feb 10, 2022
c498a66
more reformatting.
negin513 Feb 10, 2022
d941097
reformatting unit test singlept
negin513 Feb 10, 2022
b4d4914
clean pylint
negin513 Feb 10, 2022
e77de84
clean up base_case.py through black
negin513 Feb 10, 2022
4134faa
clean up regional case through black
negin513 Feb 10, 2022
d4554b3
changing names of pandas dataset.
negin513 Feb 11, 2022
4903f4f
fixing pct_urban dims but it was working previously.
negin513 Feb 11, 2022
2d88dc2
updating unit tests.
negin513 Feb 11, 2022
b4154f1
fewer lines for minor pylint complaints
negin513 Feb 11, 2022
6dbd29e
blacking reformatter
negin513 Feb 11, 2022
e2218dc
running this through black
negin513 Feb 11, 2022
e61fbcc
Add KONA, US-UMB, and regional tests for subset_data
ekluzek Feb 15, 2022
e859158
Merge branch 'python_dev_meeting' of github.com:negin513/CTSM into py…
ekluzek Feb 15, 2022
4ae0dd9
Add test for a point from the global f09 grid for subset_data
ekluzek Feb 17, 2022
5902cd3
Changes to get modify_data_YELL and subset_data_KONA tests to pass, h…
ekluzek Feb 17, 2022
985b00e
adding test for region1
negin513 Feb 17, 2022
2850dfb
modifying both versions of files from subset_data.
negin513 Feb 17, 2022
ef8a9d9
updating the surf_wrapper to point to local directory instead of scr…
negin513 Feb 17, 2022
9ad7de0
Merge branch 'master' into python_dev_meeting
negin513 Feb 17, 2022
ade3853
fixing the test lists
negin513 Feb 17, 2022
55cbf35
new tests added
negin513 Feb 17, 2022
ba08c13
updating README for neon site
negin513 Feb 17, 2022
e532b2b
updating README file list names
negin513 Feb 18, 2022
6911b5e
merge updates needed
negin513 Feb 18, 2022
4e64c84
updating docs for dev076
negin513 Feb 18, 2022
a4e712f
adding subset_data tests in here
negin513 Feb 18, 2022
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
Empty file.
145 changes: 145 additions & 0 deletions python/ctsm/site_and_regional/base_case.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import os
import numpy as np
import xarray as xr
import subprocess

from datetime import date
from getpass import getuser

myname = getuser()


class BaseCase:
"""
Parent class to SinglePointCase and RegionalCase

...

Attributes
----------
create_domain : bool
flag for creating domain file
create_surfdata : bool
flag for creating surface dataset
create_landuse : bool
flag for creating landuse file
create_datm : bool
flag for creating DATM files

Methods
-------
create_1d_coord(filename, lon_varname , lat_varname,x_dim , y_dim )
create 1d coordinate variables to enable sel() method

add_tag_to_filename(filename, tag)
add a tag and timetag to a filename ending with
[._]cYYMMDD.nc or [._]YYMMDD.nc
"""

def __init__(self, create_domain, create_surfdata, create_landuse, create_datm):
self.create_domain = create_domain
self.create_surfdata = create_surfdata
self.create_landuse = create_landuse
self.create_datm = create_datm

def __str__(self):
return (
str(self.__class__)
+ "\n"
+ "\n".join(
(
str(item) + " = " + str(self.__dict__[item])
for item in sorted(self.__dict__)
)
)
)

@staticmethod
def create_1d_coord(filename, lon_varname, lat_varname, x_dim, y_dim):
"""
lon_varname : variable name that has 2d lon
lat_varname : variable name that has 2d lat
x_dim: dimension name in X -- lon
y_dim: dimension name in Y -- lat
"""
print("Open file: " + filename)
f1 = xr.open_dataset(filename)

# create 1d coordinate variables to enable sel() method
lon0 = np.asarray(f1[lon_varname][0, :])
lat0 = np.asarray(f1[lat_varname][:, 0])
lon = xr.DataArray(lon0, name="lon", dims=x_dim, coords={x_dim: lon0})
lat = xr.DataArray(lat0, name="lat", dims=y_dim, coords={y_dim: lat0})

f2 = f1.assign({"lon": lon, "lat": lat})

f2.reset_coords([lon_varname, lat_varname])
f1.close()
return f2

@staticmethod
def add_tag_to_filename(filename, tag):
"""
Add a tag and replace timetag of a filename
# Expects file to end with [._]cYYMMDD.nc or [._]YYMMDD.nc
# Add the tag to just before that ending part
# and change the ending part to the current time tag
"""
basename = os.path.basename(filename)
cend = -10
if basename[cend] == "c":
cend = cend - 1
if (basename[cend] != ".") and (basename[cend] != "_"):
print("Trouble figuring out where to add tag to filename:" + filename)
os.abort()
today = date.today()
today_string = today.strftime("%y%m%d")
return basename[:cend] + "_" + tag + "_c" + today_string + ".nc"

def update_metadata(self, nc):
# update attributes
today = date.today()
today_string = today.strftime("%Y-%m-%d")

# get git hash
sha = self.get_git_sha()

nc.attrs["Created_on"] = today_string
nc.attrs["Created_by"] = myname
nc.attrs["Created_with"] = os.path.abspath(__file__) + " -- " + sha

# delete unrelated attributes if they exist
del_attrs = [
"source_code",
"SVN_url",
"hostname",
"history" "History_Log",
"Logname",
"Host",
"Version",
"Compiler_Optimized",
]
attr_list = nc.attrs

for attr in del_attrs:
if attr in attr_list:
# print ("This attr should be deleted:", attr)
del nc.attrs[attr]

# for attr, value in attr_list.items():
# print (attr + " = "+str(value))

@staticmethod
def get_git_sha():
"""
Returns Git short SHA for the currect directory.
"""
try:
sha = (
subprocess.check_output(["git", "rev-parse", "--short", "HEAD"])
.strip()
.decode()
)
except subprocess.CalledProcessError:
sha = "NOT-A-GIT-REPOSITORY"
return sha
113 changes: 113 additions & 0 deletions python/ctsm/site_and_regional/regional_case.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
from ctsm.site_and_regional.base_case import BaseCase

import numpy as np
import xarray as xr


class RegionalCase(BaseCase):
"""
A case to encapsulate regional cases.
"""

def __init__(
self,
lat1,
lat2,
lon1,
lon2,
reg_name,
create_domain,
create_surfdata,
create_landuse,
create_datm,
):
super().__init__(create_domain, create_surfdata, create_landuse, create_datm)
self.lat1 = lat1
self.lat2 = lat2
self.lon1 = lon1
self.lon2 = lon2
self.reg_name = reg_name

def create_tag(self):
if self.reg_name:
self.tag = self.reg_name
else:
self.tag = (
str(self.lon1)
+ "-"
+ str(self.lon2)
+ "_"
+ str(self.lat1)
+ "-"
+ str(self.lat2)
)

def create_domain_at_reg(self):
# print ("Creating domain file at region", self.lon1+"-"+self.lat2,self.lat1+"-"+self.lat2)
print("Creating domain file at region:", self.tag)
# create 1d coordinate variables to enable sel() method
f2 = self.create_1d_coord(self.fdomain_in, "xc", "yc", "ni", "nj")
lat = f2["lat"]
lon = f2["lon"]
# subset longitude and latitude arrays
xind = np.where((lon >= self.lon1) & (lon <= self.lon2))[0]
yind = np.where((lat >= self.lat1) & (lat <= self.lat2))[0]
f3 = f2.isel(nj=yind, ni=xind)

# update attributes
self.update_metadata(f3)
f3.attrs["Created_from"] = self.fdomain_in

wfile = self.fdomain_out
# mode 'w' overwrites file
f3.to_netcdf(path=wfile, mode="w")
print("Successfully created file (fdomain_out)" + self.fdomain_out)
f2.close()
f3.close()

def create_surfdata_at_reg(self):
# print ("Creating surface dataset file at region", self.lon1+"-"+self.lat2,self.lat1+"-"+self.lat2)
print("Creating surface dataset file at region:", self.tag)
# create 1d coordinate variables to enable sel() method
filename = self.fsurf_in
f2 = self.create_1d_coord(filename, "LONGXY", "LATIXY", "lsmlon", "lsmlat")
lat = f2["lat"]
lon = f2["lon"]
# subset longitude and latitude arrays
xind = np.where((lon >= self.lon1) & (lon <= self.lon2))[0]
yind = np.where((lat >= self.lat1) & (lat <= self.lat2))[0]
f3 = f2.isel(lsmlat=yind, lsmlon=xind)

# update attributes
self.update_metadata(f3)
f3.attrs["Created_from"] = self.fsurf_in

# mode 'w' overwrites file
f3.to_netcdf(path=self.fsurf_out, mode="w")
print("created file (fsurf_out)" + self.fsurf_out)
# f1.close();
f2.close()
f3.close()

def create_landuse_at_reg(self):
# print ("Creating surface dataset file at region", self.lon1+"-"+self.lat2,self.lat1+"-"+self.lat2)
print("Creating surface dataset file at region:", self.tag)
# create 1d coordinate variables to enable sel() method
f2 = self.create_1d_coord(self.fluse_in, "LONGXY", "LATIXY", "lsmlon", "lsmlat")
lat = f2["lat"]
lon = f2["lon"]
# subset longitude and latitude arrays
xind = np.where((lon >= self.lon1) & (lon <= self.lon2))[0]
yind = np.where((lat >= self.lat1) & (lat <= self.lat2))[0]
f3 = f2.isel(lsmlat=yind, lsmlon=xind)

# update attributes
self.update_metadata(f3)
f3.attrs["Created_from"] = self.fluse_in

wfile = self.fluse_out
# mode 'w' overwrites file
f3.to_netcdf(path=wfile, mode="w")
print("Successfully created file (fdomain_out)" + self.fdomain_out)
f2.close()
f3.close()
Loading