Skip to content

Commit

Permalink
Merge pull request #2801 from GEOS-ESM/feature/mathomp4/update-mapl-2…
Browse files Browse the repository at this point in the history
….8.0-to-python3

Backport Python3 changes to MAPL 2.8.0
  • Loading branch information
mathomp4 authored May 14, 2024
2 parents 2c2d928 + d274b2a commit 2c0401c
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 112 deletions.
8 changes: 3 additions & 5 deletions Apps/MAPL_GridCompSpecs_ACG.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
#!/usr/bin/env python3
import argparse
import sys
import os
import csv
import pandas as pd
Expand Down Expand Up @@ -202,9 +201,8 @@ def csv_record_reader(csv_reader):
with open(specs_filename, 'r') as specs_file:
specs_reader = csv.reader(specs_file, skipinitialspace=True,delimiter='|')
gen = csv_record_reader(specs_reader)
schema_version = next(gen)[0].split(' ')[1]
component = next(gen)[0].split(' ')[1]
# print("Generating specification code for component: ",component)
#component = next(gen)[0].split(' ')[1]
#print("Generating specification code for component: ",component)
while True:
try:
gen = csv_record_reader(specs_reader)
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
### Fixed

## [2.8.0.10] - 2024-05-14

### Changed

- Backport Python3 changes for `Python/` code to 2.8
- Change shebang in `Apps/MAPL_GridCompSpecs_ACG.py` to use `python3`

## [2.8.0.9] - 2022-08-04

### Added
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cmake_policy (SET CMP0054 NEW)

project (
MAPL
VERSION 2.8.0.9
VERSION 2.8.0.10
LANGUAGES Fortran CXX C) # Note - CXX is required for ESMF

# mepo can now clone subrepos in three styles
Expand Down
28 changes: 14 additions & 14 deletions Python/MAPL/Date.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def copy(self):
def __iter__(self):
return self

def next(self):
def __next__(self):
#Last day of the month.
if self.day == NumberDaysMonth(self.month, self.year):
self.day = 1
Expand Down Expand Up @@ -230,7 +230,7 @@ def __add__(self, n):
#Convert back to date format.
return DateFromJDNumber(temp)
else:
raise TypeError, "%s is not an integer." % str(n)
raise TypeError("%s is not an integer." % str(n))

def __sub__(self, date):
"""Returns the (signed) difference of days between the dates."""
Expand All @@ -253,7 +253,7 @@ def __sub__(self, date):
ret += NumberDaysYear(year)
return ret
else:
raise TypeError, "%s is neither an integer nor a Date." % str(date)
raise TypeError("%s is neither an integer nor a Date." % str(date))

#Adding an integer is "commutative".
def __radd__(self, n):
Expand Down Expand Up @@ -283,7 +283,7 @@ def ToCOMTime(self):
def DateFromJDNumber(n):
"""Returns a date corresponding to the given Julian day number."""
if not isinstance(n, int):
raise TypeError, "%s is not an integer." % str(n)
raise TypeError("%s is not an integer." % str(n))

a = n + 32044
b = (4*a + 3)//146097
Expand Down Expand Up @@ -320,21 +320,21 @@ def strpdate(s):
temp = Date()
curr_month = temp.month
while temp.month == curr_month:
print temp
temp.next()
print(temp)
next(temp)

print "\n"
print("\n")

#How many days until the end of the year?
temp = Date()
temp.day, temp.month = 1, 1
curr_year = temp.year
while temp.year == curr_year:
print "%s is %d days away from the end of the year." % (str(temp),
temp.DaysToEndYear())
print("%s is %d days away from the end of the year." % (str(temp),
temp.DaysToEndYear()))
temp += NumberDaysMonth(temp.month)

print "\n"
print("\n")

#Playing with __sub__.
temp = Date()
Expand All @@ -344,11 +344,11 @@ def strpdate(s):
temp_list.append(temp)
temp += NumberDaysMonth(temp.month)
for elem in temp_list:
print "%s differs %d days from current date: %s" % (str(elem),
print("%s differs %d days from current date: %s" % (str(elem),
elem - Date(),
str(Date()))
str(Date())))

print "\n"
print("\n")

#Swapping arguments works?
print 23 + Date()
print(23 + Date())
21 changes: 10 additions & 11 deletions Python/MAPL/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
carried out by means of several *jobs* which are submitted through a
queueing system such as PBS.
job
job
This package defines the base class *Job* which inherits from
*Exp*. A *job* carries out a portion of the *experiment*,
itself consisting of several *run* segments.
Expand All @@ -38,19 +38,18 @@
|----------------------- Experiment ------------------------|
|------ Job 1 ------|------ Job 2 ------|------ Job 3 ------|
|- Run 1 -|- Run 2 -|- Run 3 -|- Run 4 -|- Run 5 -|- Run 6 -|
If each run segment is 2 weeks long, each job performs a 4 week
integration, and the the whole experiment is about 3 month long.
"""

__version__ = "0.1.2"

from exp import *
from job import *
from run import *
from config import *
from history import *
from Date import *
from filelock import *
__version__ = "1.0.0"

from .exp import *
from .job import *
from .run import *
from .config import *
from .history import *
from .Date import *
from .filelock import *
96 changes: 48 additions & 48 deletions Python/MAPL/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import sys
from types import *
from datetime import datetime

class Config(object):

def __init__(self,RcFiles,delim=':',Environ=True):
Expand All @@ -23,7 +23,7 @@ def __init__(self,RcFiles,delim=':',Environ=True):
the current value of environment variables.
"""

if type(RcFiles) is StringType:
if isinstance(RcFiles, str):
Files = ( RcFiles, ) # in case a single file is given
else:
Files = RcFiles # more often, a List/Tuple of RC files
Expand All @@ -39,22 +39,22 @@ def __init__(self,RcFiles,delim=':',Environ=True):
if value is not None:
value = string.Template(value).safe_substitute(os.environ)
if name:
self.Rc[name] = { 'value': value,
'comment': comment,
self.Rc[name] = { 'value': value,
'comment': comment,
'flag': 0}

def __call__(self,name,value=None):
"""Either get or set a resource depending on whether *value* is given"""
if value == None:
if self.Rc.__contains__(name):
return self.Rc[name]['value']
else:
if self.Rc.__contains__(name):
self.Rc[name]['value'] = value
self.Rc[name]['value'] = value
self.Rc[name]['flag'] = 1
return self.Rc[name]['value']
return None

get = __call__

def set(self,name,value):
Expand All @@ -65,7 +65,7 @@ def save(self,rcfile=None):
if rcfile is None:
f = sys.stdout
else:
f = open(rcfile,'w')
f = open(rcfile,'w')
for line in self.Lines:
line = line.rstrip()
name, value, comment = _parseLine(line,self.delim)
Expand All @@ -76,16 +76,16 @@ def save(self,rcfile=None):
else:
comment = ''
value = self.Rc[name]['value']
print >>f, name + self.delim+' ' + str(value) + comment # this line has been edited
print(name + self.delim+' ' + str(value) + comment, file=f) # this line has been edited
else:
print >>f, line
print(line, file=f)
else:
print >>f, line
print(line, file=f)
f.close()

def upd(self,dict):
pass

def interp(self,str,outFile=None,**kws):
"""
Use the resource values for $-substitution (a.k.a.
Expand All @@ -108,7 +108,7 @@ def interpFile(self,template,outFile,**kws):
for tmpl in Tmpl:
Text.append(self.interpStr(tmpl,**kws))
open(outFile,"w").writelines(Text)

def interpStr(self,template,strict=False):
"""
Replace occurences of resource variables in the
Expand Down Expand Up @@ -149,21 +149,21 @@ def setenv(self,Only=None):
Use resources to set environment variables. Option,
one can provide a list of strings (*Only*) with those
resources to be turned into environment variables.
"""
"""
for name in self.Rc:
if Only is None:
os.environ[name] = self.Rc[name]['value']
elif name in Only:
os.environ[name] = self.Rc[name]['value']

def keys(self):
"""
"""
Return list of resource names.
"""
return self.Rc.keys()
return list(self.Rc.keys())

def values(self):
"""
"""
Return list of resource names.
"""
vals = []
Expand Down Expand Up @@ -216,7 +216,7 @@ def strTemplate(templ,expid=None,nymd=None,nhms=None,
dtime --- python datetime
Unlike GrADS, notice that seconds are expanded using the %S2 token.
Unlike GrADS, notice that seconds are expanded using the %S2 token.
Input date/time can be either strings or integers.
Examples:
Expand All @@ -229,9 +229,9 @@ def strTemplate(templ,expid=None,nymd=None,nhms=None,
"""

MMM = ( 'jan', 'feb', 'mar', 'apr', 'may', 'jun',
'jul', 'aug', 'sep', 'oct', 'nov', 'dec' )
MMM = ( 'jan', 'feb', 'mar', 'apr', 'may', 'jun',
'jul', 'aug', 'sep', 'oct', 'nov', 'dec' )

str_ = templ[:]

if dtime is not None:
Expand All @@ -244,34 +244,34 @@ def strTemplate(templ,expid=None,nymd=None,nhms=None,

if nymd is not None:
nymd = int(nymd)
yy = nymd/10000
mm = (nymd - yy*10000)/100
yy = nymd//10000
mm = (nymd - yy*10000)//100
dd = nymd - (10000*yy + 100*mm )

if nhms is not None:
nhms = int(nhms)
h = nhms/10000
m = (nhms - h * 10000)/100
h = nhms//10000
m = (nhms - h * 10000)//100
s = nhms - (10000*h + 100*m)

if expid is not None:
if expid is not None:
str_ = str_.replace('%s',expid)
if yy is not None:
if yy is not None:
y2 = yy%100
str_ = str_.replace('%y4',str(yy))
str_ = str_.replace('%y2',"%02d"%y2)
if mm is not None:
if mm is not None:
mm = int(mm)
mmm = MMM[mm-1]
str_ = str_.replace('%m2',"%02d"%mm)
str_ = str_.replace('%m3',mmm)
if dd is not None:
if dd is not None:
str_ = str_.replace('%d2',"%02d"%int(dd))
if h is not None:
if h is not None:
str_ = str_.replace('%h2',"%02d"%int(h))
if m is not None:
if m is not None:
str_ = str_.replace('%n2',"%02d"%int(m))
if s is not None:
if s is not None:
str_ = str_.replace('%S2',"%02d"%int(s))

return str_
Expand All @@ -284,14 +284,14 @@ def strTemplate(templ,expid=None,nymd=None,nhms=None,
def _ut_strTemplate():



templ = "%s.aer_f.eta.%m3%y2.%y4%m2%d2_%h2:%n2:%S2z.nc"

expid = "e0054A"
yy = "2008"
mm = "10"
dd = "30"

h = "1"
m = "30"
s = "47"
Expand All @@ -301,20 +301,20 @@ def _ut_strTemplate():
nymd = int(yy) * 10000 + int(mm)*100 + int(dd)
nhms = int(h) * 10000 + int(m) * 100 + int(s)

print "Template: "+templ
print strTemplate(templ)
print strTemplate(templ,expid=expid)
print strTemplate(templ,expid=expid,yy=2008)
print strTemplate(templ,expid=expid,yy=2008,mm=mm)
print strTemplate(templ,expid=expid,yy=2008,mm=mm,dd=dd)
print strTemplate(templ,expid=expid,yy=2008,mm=mm,dd=dd,h=h)
print strTemplate(templ,expid=expid,yy=2008,mm=mm,dd=dd,h=h,m=m,s=s)
print strTemplate(templ,expid=expid,nymd=nymd)
print strTemplate(templ,expid=expid,nymd=nymd,nhms=nhms)
print strTemplate(templ,expid=expid,dtime=dtime)
print("Template: "+templ)
print(strTemplate(templ))
print(strTemplate(templ,expid=expid))
print(strTemplate(templ,expid=expid,yy=2008))
print(strTemplate(templ,expid=expid,yy=2008,mm=mm))
print(strTemplate(templ,expid=expid,yy=2008,mm=mm,dd=dd))
print(strTemplate(templ,expid=expid,yy=2008,mm=mm,dd=dd,h=h))
print(strTemplate(templ,expid=expid,yy=2008,mm=mm,dd=dd,h=h,m=m,s=s))
print(strTemplate(templ,expid=expid,nymd=nymd))
print(strTemplate(templ,expid=expid,nymd=nymd,nhms=nhms))
print(strTemplate(templ,expid=expid,dtime=dtime))

if __name__ == "__main__":
cf = Config('test.rc', delim=' = ')

# _ut_strTemplate()

Loading

0 comments on commit 2c0401c

Please sign in to comment.