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

Developing a solution for u-cp647 #95

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
10 changes: 7 additions & 3 deletions bgcval2/download_from_mass.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,15 +409,18 @@ def download_from_mass(
outputFold = folder([paths.ModelFolder_pref, jobID,] )
# make this folder group writeable.
st = os.stat(outputFold)
os.chmod(outputFold, st.st_mode | stat.S_IWGRP)

try: os.chmod(outputFold, st.st_mode | stat.S_IWGRP)
ledm marked this conversation as resolved.
Show resolved Hide resolved
except: pass
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be something, not except pass.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fully agreed, maybe an OSError: could not change permissions on such and such?

ledm marked this conversation as resolved.
Show resolved Hide resolved

deleteBadLinksAndZeroSize(outputFold, jobID)

fixFilePaths(outputFold, jobID)
deleteBadLinksAndZeroSize(outputFold, jobID)

# Set up a file to save command to a new file.
download_script_path = ''.join([folder('mass_scripts/'), jobID,'.sh'])
username = os.getlogin()
download_script_path = ''.join([folder('mass_scripts/'), jobID,'_',username, '.sh'])
ledm marked this conversation as resolved.
Show resolved Hide resolved
header_lines = ['# Run this script on mass-cli1.jasmin.ac.uk\n',]
header_lines.append('# from login1.jasmin.ac.uk, ssh to the mass machine:\n# ssh -X mass-cli\n')
header_lines.append(''.join(['# run script with:\n# source ', os.path.abspath(download_script_path),'\n']))
Expand Down Expand Up @@ -483,7 +486,8 @@ def download_from_mass(
if auto_download:
shared_file_path = os.path.join(paths.shared_mass_scripts, os.path.basename(download_script_path))
print('writing file in shared path', shared_file_path)
shutil.copy(download_script_path, shared_file_path)
try: shutil.copy(download_script_path, shared_file_path)
except: pass
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be something, no except pass

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, what'd you expect here to go wrong, maybe an IOError I think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EnvironmentError?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's the BaseClass of IOError and such, I've not used the base class ever - you need to put whatever that should raise - am not sure what, you'd know 😃

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure either - I don't make errors...

...

...

...

😆

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hahaha 🤣 Seriously now - whatever happens in there when there is an exception - make the thing fail and see what exception it throws


fixFilePaths(outputFold, jobID, debug=False,)
deleteBadLinksAndZeroSize(outputFold, jobID, debug=False,)
Expand Down
3 changes: 2 additions & 1 deletion bgcval2/functions/AirSeaFluxCO2.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import numpy as np
from bgcval2.bgcvaltools.dataset import dataset
from bgcval2.functions.get_kwarg_file import get_kwarg_file
from bgcval2.functions.tools import load_area

global loaded_area_and_mask
global tmask
Expand All @@ -46,7 +47,7 @@ def load_area_and_mask(areafile):
if isinstance(areafile, list) and len(areafile)==1:
areafile = areafile[0]
nc = dataset(areafile, 'r')
area = nc.variables['e2t'][:] * nc.variables['e1t'][:]
area = load_area(nc)
nc.close()
loaded_area_and_mask = True
return area
Expand Down
3 changes: 2 additions & 1 deletion bgcval2/functions/BenCa.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import numpy as np
from bgcval2.bgcvaltools.dataset import dataset
from bgcval2.functions.get_kwarg_file import get_kwarg_file
from bgcval2.functions.tools import load_area

global loaded_area_and_mask
global tmask
Expand All @@ -48,7 +49,7 @@ def load_area_and_mask(areafile):
else:
raise ValueError(f"{areafile} must be a length=1 list containing a single file descriptor.")
nc = dataset(areafile, 'r')
area = nc.variables['e2t'][:] * nc.variables['e1t'][:]
area = load_area(nc)
nc.close()
loaded_area_and_mask = True
return area
Expand Down
3 changes: 2 additions & 1 deletion bgcval2/functions/InvtAlk.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import numpy as np
from bgcval2.bgcvaltools.dataset import dataset
from bgcval2.functions.get_kwarg_file import get_kwarg_file
from bgcval2.functions.tools import load_area

global loaded_area_and_mask
global tmask
Expand All @@ -48,7 +49,7 @@ def load_area_and_mask(areafile):
else:
raise ValueError(f"{areafile} must be a length=1 list containing a single file descriptor.")
nc = dataset(areafile, 'r')
area = nc.variables['e2t'][:] * nc.variables['e1t'][:]
area = load_area(nc)
nc.close()
loaded_area_and_mask = True
return area
Expand Down
6 changes: 2 additions & 4 deletions bgcval2/functions/TotalIntPP.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from bgcval2.bgcvaltools.dataset import dataset
from bgcval2.functions.get_kwarg_file import get_kwarg_file
from bgcval2.functions.standard_functions import find_best_var
from bgcval2.functions.tools import load_area

global loadedArea
global model_area
Expand All @@ -41,10 +42,7 @@

def loadDataMask(gridfn):
nc = dataset(gridfn,'r')
try:
model_area = nc.variables['area'][:]
except:
model_area = nc.variables['e1t'][:]*nc.variables['e2t'][:]
model_area = load_area(nc)
nc.close()
loadedArea = True
return model_area
Expand Down
3 changes: 2 additions & 1 deletion bgcval2/functions/dust.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def loadDataMask(gridfn):
global loadedArea
global masked_area
nc = dataset(gridfn,'r')
masked_area = nc.variables['e2t'][:] * nc.variables['e1t'][:]*nc.variables['tmask'][0]
area = load_area(nc)
masked_area = area *nc.variables['tmask'][0]
nc.close()
loadedArea = True

Expand Down
9 changes: 6 additions & 3 deletions bgcval2/functions/globalVolMean.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@

import numpy as np
from bgcval2.functions.standard_functions import choose_best_var

from bgcval2.functions.tools import load_area

def calc_vol(nc):
"""
Calculate volume from the (grid-T) netcdf file.
"""
area = nc.variables['area'][:]
area = load_area(area)
thkcello = nc.variables['thkcello'][:]
area = area[None, None,:, :]
if area.ndim == 4:
area = area[None, None,:, :]
if area.ndim == 3:
area = area[None, :, :]
return thkcello * area


Expand Down
5 changes: 3 additions & 2 deletions bgcval2/functions/ice.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import numpy as np
from bgcval2.bgcvaltools.dataset import dataset
from bgcval2.functions.get_kwarg_file import get_kwarg_file
from bgcval2.functions.tools import load_area

global loaded_area_and_mask
global tmask
Expand All @@ -50,7 +51,7 @@ def load_area_and_mask(gridfn, maskname,):
gridfn = gridfn[0]
nc = dataset(gridfn, 'r')
tmask = nc.variables[maskname][0]
area = nc.variables['e2t'][:] * nc.variables['e1t'][:]
area = load_area(nc)
lat = nc.variables['nav_lat'][:]
nc.close()
loaded_area_and_mask = True
Expand All @@ -74,7 +75,7 @@ def calculate_ice_extent(nc, keys, **kwargs):
if 'area' not in nc.variables and not loaded_area_and_mask:
area, tmask, lat = load_area_and_mask(areafile, maskname)
else:
area = nc.variables['area'][:]
area = load_area(nc)
lat = nc.variables['nav_lat'][:]
tmask = nc.variables[keys[0]][:].squeeze().mask

Expand Down
49 changes: 49 additions & 0 deletions bgcval2/functions/tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2023, Plymouth Marine Laboratory
#
# This file is part of the bgc-val library.
#
# bgc-val is free software: you can redistribute it and/or modify it
# under the terms of the Revised Berkeley Software Distribution (BSD) 3-clause license.

# bgc-val is distributed in the hope that it will be useful, but
# without any warranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the revised BSD license for more details.
# You should have received a copy of the revised BSD license along with bgc-val.
# If not, see <http://opensource.org/licenses/BSD-3-Clause>.
#
# Address:
# Plymouth Marine Laboratory
# Prospect Place, The Hoe
# Plymouth, PL1 3DH, UK
#
# Email:
# ledm@pml.ac.uk
#
"""
.. module:: tools
:platform: Unix
:synopsis: This uses some tools for the functions library.

.. moduleauthor:: Lee de Mora <ledm@pml.ac.uk>

"""

ledm marked this conversation as resolved.
Show resolved Hide resolved


import numpy as np
from bgcval2.functions.standard_functions import choose_best_var


def load_area(nc):
"""
Generic tool for loading area:
"""
area_keys = ['area', 'area_grid_T', 'area_grid_W', 'area_grid_V', 'area_grid_U']
if set(area_keys).intersection(set(nc.variables.keys())):
area = choose_best_var(nc, area_keys)
elif set(['e1t', 'e2t']).intersection(set(nc.variables.keys())):
area = nc.variables['e1t'][:]*nc.variables['e2t'][:]
else:
raise ValueError('Unable to load or calculate area from this file.')
ledm marked this conversation as resolved.
Show resolved Hide resolved
return area