Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
110 changes: 110 additions & 0 deletions src/common_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Copyright 2024 ACCESS-NRI (https://www.access-nri.org.au/)
# See the top-level COPYRIGHT.txt file for details.
#
# SPDX-License-Identifier: Apache-2.0
#
# Created by: Chermelle Engel <Chermelle.Engel@anu.edu.au>

import mule
import iris
import xarray as xr
import numpy as np

class ReplaceOperator(mule.DataOperator):
""" Mule operator for replacing the data"""
def __init__(self):
pass
def new_field(self, sources):
print('new_field')
return sources[0]
def transform(self, sources, result):
print('transform')
return sources[1]

def replace_in_ff_problematic(f, mf_out, replace, stashcode, canopy_pixels, landsea_pixels):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It would be good to add a docstring to better explain the purpose of the function.

I would also rename to something a bit clearer like replace_problematic_pixels_in_ff

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
def replace_in_ff_problematic(f, mf_out, replace, stashcode, canopy_pixels, landsea_pixels):
def replace_in_ff_problematic(f, mf_out, replace, missing_canopy, misclassified_land):
stashcode = f.lbuser4

I would avoid passing an argument that can be computed within the function.
stashcode is always going to be f.lbuser4, but since f is an argument of the function, we can compute it inside the function itself.
Note the function call should be modified in the other files after this modification.

For changes in canopy_pixels and landsea_pixels, refer to the related comments below.


current_data = f.get_data()
data=current_data.copy()

if stashcode == 218:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It would be better to capture this number in a constant variable at the beinning of the script (outside the function) that makes it clearer what it stands for.
Something like:

STASH_CANOPY_HEIGHT=218

then change

Suggested change
if stashcode == 218:
if stashcode == STASH_CANOPY_HEIGHT:

for j in range(len(canopy_pixels)):
iy=canopy_pixels[j][0]
ix=canopy_pixels[j][1]
if np.isnan(data[iy,ix]):
data[iy,ix]=1.
elif stashcode == 33:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same as above with the hardcoded number for the canopy height stash.

for j in range(len(landsea_pixels)):
data[landsea_pixels[j][0],landsea_pixels[j][1]]=0.
Comment on lines +26 to +37
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
current_data = f.get_data()
data=current_data.copy()
if stashcode == 218:
for j in range(len(canopy_pixels)):
iy=canopy_pixels[j][0]
ix=canopy_pixels[j][1]
if np.isnan(data[iy,ix]):
data[iy,ix]=1.
elif stashcode == 33:
for j in range(len(landsea_pixels)):
data[landsea_pixels[j][0],landsea_pixels[j][1]]=0.
if stashcode == 218:
data = np.where(missing_canopy, 1., f.get_data())
elif stashcode == 33:
data = np.where(misclassified_land, 0., f.get_data())

If we use missing_canopy and misclassified_land (the masks) as input arguments we can simplify the fixing logic without needing loops.


mf_out.fields.append(replace([f, data]))

def problematic_pixels(infile):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I would rename the function to be a bit more specific of its purpose. For example:

  • get_problematic_pixels
  • find_problematic_pixels

"""
Function to get locations of pixels that have incomplete canopy height information or
problematic land/sea definitions.

Parameters
----------
infile : string
The name of the start dump file to read

Returns
-------
2d numpy array
A 2-D numpy array containg the field data for the date/time and spatial extent
"""

# defining the variable names of surface altitude, soil porosity and canopy height in the start dump files
var_altitude="surface_altitude"
var_soil="soil_porosity"
var_canopy="canopy_height"

# Read in the relevant data
# One single read of the relevant variables is faster than individual reads
d = iris.load(infile,[var_altitude,var_soil,var_canopy])

# Creating an orography mask and storing the latitude and longitude data for reporting purposes
orog_data=xr.DataArray.from_iris(d[0])
lats=orog_data['latitude'].data
lons=orog_data['longitude'].data
Comment on lines +68 to +69
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
lats=orog_data['latitude'].data
lons=orog_data['longitude'].data

orog_mask=np.where(orog_data.data[:,:]>0.,1,0)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since the output is basically a boolean mask (0 or 1), the logic can be simplified to:

Suggested change
orog_mask=np.where(orog_data.data[:,:]>0.,1,0)
orog_mask=orog_data>0


# Creating a soil parameter based mask
soil_data=xr.DataArray.from_iris(d[1])
soil_mask=np.where(np.isnan(soil_data.data[:,:]),1,0)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
soil_mask=np.where(np.isnan(soil_data.data[:,:]),1,0)
soil_mask=soil_data.isnull()


# Creating a canopy mask - screening across all five different plant types
canopy_data=xr.DataArray.from_iris(d[2])
canopy_mask=np.where(np.isnan(canopy_data.data[:,:,:]),1,0)
canopy_mask=np.sum(canopy_mask,axis=0)
canopy_mask=np.where(canopy_mask>0,1,0)
Comment on lines +78 to +80
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
canopy_mask=np.where(np.isnan(canopy_data.data[:,:,:]),1,0)
canopy_mask=np.sum(canopy_mask,axis=0)
canopy_mask=np.where(canopy_mask>0,1,0)
canopy_mask = canopy_data.isnull().any(dim='pseudo_level')


# Creating a compound mask that will indicate 1 for just canopy nan or 2 for canopy and soil nan
compound_data=canopy_mask+soil_mask

# Removing any sea points (points with altitude 0 or less
compound_data=compound_data*orog_mask

# Finding locations of problematic pixels in two sets
# One for missing canopy alone and one for misclassified land
try:
canopy_pixels=np.argwhere(compound_data==1).compute()
landsea_pixels=np.argwhere(compound_data==2).compute()
except:
canopy_pixels=np.argwhere(compound_data==1)
landsea_pixels=np.argwhere(compound_data==1)
Comment on lines +83 to +95
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
compound_data=canopy_mask+soil_mask
# Removing any sea points (points with altitude 0 or less
compound_data=compound_data*orog_mask
# Finding locations of problematic pixels in two sets
# One for missing canopy alone and one for misclassified land
try:
canopy_pixels=np.argwhere(compound_data==1).compute()
landsea_pixels=np.argwhere(compound_data==2).compute()
except:
canopy_pixels=np.argwhere(compound_data==1)
landsea_pixels=np.argwhere(compound_data==1)
# Finding locations of problematic pixels in two sets:
# - Missing canopy --> land points (orog mask is True) where only one between canopy mask and soil mask is True (XOR)
# - Misclassified land --> land points (orog mask is True) where both canopy mask and soil mask are True (AND)
missing_canopy = (canopy_mask ^ soil_mask) & orog_mask
misclassified_land = canopy_mask & soil_mask & orog_mask
canopy_pixels=np.column_stack(np.where(missing_canopy))
landsea_pixels=np.column_stack(np.where(misclassified_land))


# Printing information to the standard output for reporting purposes (so scientists can be aware)
npoints,nxy = (canopy_pixels.shape)
if npoints>0:
for i in range(npoints):
print("%.1f, %.1f, Nan canopy"%(lons[canopy_pixels[i,1]],lats[canopy_pixels[i,0]]))

npoints,nxy = (landsea_pixels.shape)
if npoints>0:
for i in range(npoints):
print("%.1f, %.1f, Misplaced Orography"%(lons[landsea_pixels[i,1]],lats[landsea_pixels[i,0]]))
Comment on lines +97 to +106
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# Printing information to the standard output for reporting purposes (so scientists can be aware)
npoints,nxy = (canopy_pixels.shape)
if npoints>0:
for i in range(npoints):
print("%.1f, %.1f, Nan canopy"%(lons[canopy_pixels[i,1]],lats[canopy_pixels[i,0]]))
npoints,nxy = (landsea_pixels.shape)
if npoints>0:
for i in range(npoints):
print("%.1f, %.1f, Misplaced Orography"%(lons[landsea_pixels[i,1]],lats[landsea_pixels[i,0]]))
# Printing information to the standard output for reporting purposes (so scientists can be aware)
tmp = missing_canopy.where(missing_canopy.compute(), drop=True)
for lon, lat in zip(tmp.longitude, tmp.latitude):
print(f"{lat:.1f}, {lon:.1f}, NaN Canopy")
tmp = misclassified_land.where(misclassified_land.compute(), drop=True)
for lon, lat in zip(tmp.longitude, tmp.latitude):
print(f"{lat:.1f}, {lon:.1f}, Misplaced Orography")

Also a minor thing, I swapped the printing of 'latitude' and 'longitude' values (placing 'latitude' first), to be more consistent with the usual coordinate specification.


# returning pixel locations so they can be processed appropriately
return canopy_pixels,landsea_pixels
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
return canopy_pixels,landsea_pixels
return missing_canopy,misclassified_land

This would simplify the logic in the replace_in_ff_problematic function (see related comments above for details).


2 changes: 1 addition & 1 deletion src/hres_eccb.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python3
# Copyright 2024 ACCESS-NRI (https://www.access-nri.org.au/)
# See the top-level COPYRIGHT.txt file for details.
#
# SPDX-License-Identifier: Apache-2.0
#
# Created by: Chermelle Engel <Chermelle.Engel@anu.edu.au>

#!/usr/bin/env python3

"""
Replace the land/surface fields in the ec_cb000 file with higher-resolution
Expand Down
8 changes: 4 additions & 4 deletions src/hres_ic.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python3
# Copyright 2024 ACCESS-NRI (https://www.access-nri.org.au/)
# See the top-level COPYRIGHT.txt file for details.
#
# SPDX-License-Identifier: Apache-2.0
#
# Created by: Chermelle Engel <Chermelle.Engel@anu.edu.au>

#!/usr/bin/env python3

"""
Replace the land/surface fields in the astart file with higher-resolution
Expand Down Expand Up @@ -59,13 +59,13 @@ def main():

# If necessary replace ERA5 land/surface fields with higher-resolution options
if "era5land" in args.type:
replace_landsurface_with_ERA5land_IC.swap_land_era5land(args.mask, args.file, t)
replace_landsurface_with_ERA5land_IC.swap_land_era5land(args.mask, args.file, t, fix_problematic_pixels="yes")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
replace_landsurface_with_ERA5land_IC.swap_land_era5land(args.mask, args.file, t, fix_problematic_pixels="yes")
replace_landsurface_with_ERA5land_IC.swap_land_era5land(args.mask, args.file, t, fix_problematic_pixels=True)

shutil.move(args.file.as_posix(), args.file.as_posix().replace('.tmp', ''))
elif "barra" in args.type:
replace_landsurface_with_BARRA2R_IC.swap_land_barra(args.mask, args.file, t)
replace_landsurface_with_BARRA2R_IC.swap_land_barra(args.mask, args.file, t, fix_problematic_pixels="yes")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
replace_landsurface_with_BARRA2R_IC.swap_land_barra(args.mask, args.file, t, fix_problematic_pixels="yes")
replace_landsurface_with_BARRA2R_IC.swap_land_barra(args.mask, args.file, t, fix_problematic_pixels=True)

shutil.move(args.file.as_posix(), args.file.as_posix().replace('.tmp', ''))
elif "astart" in args.type:
replace_landsurface_with_FF_IC.swap_land_ff(args.mask, args.file, args.hres_ic,t)
replace_landsurface_with_FF_IC.swap_land_ff(args.mask, args.file, args.hres_ic,t, fix_problematic_pixels="yes")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
replace_landsurface_with_FF_IC.swap_land_ff(args.mask, args.file, args.hres_ic,t, fix_problematic_pixels="yes")
replace_landsurface_with_FF_IC.swap_land_ff(args.mask, args.file, args.hres_ic,t, fix_problematic_pixels=True)

shutil.move(args.file.as_posix(), args.file.as_posix().replace('.tmp', ''))

else:
Expand Down
23 changes: 8 additions & 15 deletions src/replace_landsurface_with_BARRA2R_IC.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@
BARRA_DIR = os.path.join(ROSE_DATA, 'etc', 'barra_r2')


class ReplaceOperator(mule.DataOperator):
""" Mule operator for replacing the data"""
def __init__(self):
pass
def new_field(self, sources):
print('new_field')
return sources[0]
def transform(self, sources, result):
print('transform')
return sources[1]


class bounding_box():
""" Container class to hold spatial extent information."""
def __init__(self, ncfname, maskfname, var):
Expand Down Expand Up @@ -156,7 +144,7 @@ def get_BARRA_nc_data(ncfname, FIELDN, wanted_dt, NLAYERS, bounds):
return data.data


def swap_land_barra(mask_fullpath, ec_cb_file_fullpath, ic_date):
def swap_land_barra(mask_fullpath, ec_cb_file_fullpath, ic_date, fix_problematic_pixels="no"):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
def swap_land_barra(mask_fullpath, ec_cb_file_fullpath, ic_date, fix_problematic_pixels="no"):
def swap_land_barra(mask_fullpath, ec_cb_file_fullpath, ic_date, fix_problematic_pixels=False):

"""
Function to get the BARRA2-R data for all land/surface variables.

Expand Down Expand Up @@ -187,6 +175,9 @@ def swap_land_barra(mask_fullpath, ec_cb_file_fullpath, ic_date):
# Path to input file
ff_in = ec_cb_file_fullpath.as_posix().replace('.tmp', '')

if fix_problematic_pixels == "yes":
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if fix_problematic_pixels == "yes":
if fix_problematic_pixels:

canopy_pixels,landsea_pixels=problematic_pixels(ff_in)

# Path to output file
ff_out = ec_cb_file_fullpath.as_posix()
print(ff_in, ff_out)
Expand All @@ -195,7 +186,7 @@ def swap_land_barra(mask_fullpath, ec_cb_file_fullpath, ic_date):
mf_in = mule.load_umfile(ff_in)

# Create Mule Replacement Operator
replace = ReplaceOperator()
replace = common_utilities.ReplaceOperator()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

common_utilities might have not been imported in this file, as I don't see any changes related to an import common_utilities line.


# Read in the surface temperature data from the archive
BARRA_FIELDN = 'ts'
Expand Down Expand Up @@ -232,7 +223,6 @@ def swap_land_barra(mask_fullpath, ec_cb_file_fullpath, ic_date):
# For each field in the input write to the output file (but modify as required)
for f in mf_in.fields:

print(f.lbuser4, f.lblev, f.lblrec, f.lbhr, f.lbcode)
if f.lbuser4 == 9:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Refer to the comment about the STASH_CANOPY_HEIGHT constant.

I would add the constant in the common_utilities.py, import it in this script and use it in place of the number.

# replace coarse soil moisture with high-res information
current_data = f.get_data()
Expand All @@ -251,6 +241,9 @@ def swap_land_barra(mask_fullpath, ec_cb_file_fullpath, ic_date):
data = surface_temp
data = np.where(np.isnan(data), current_data, data)
mf_out.fields.append(replace([f, data]))
elif ((f.lbuser4 == 33) or (f.lbuser4 == 218)) and fix_problematic_pixels == "yes":
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
elif ((f.lbuser4 == 33) or (f.lbuser4 == 218)) and fix_problematic_pixels == "yes":
elif ((f.lbuser4 == 33) or (f.lbuser4 == 218)) and fix_problematic_pixels:

Also refer to the comment about the STASH_CANOPY_HEIGHT constant and the comment right above for the hardcoded numbers.

# replace surface altitude and canopy_height
common_utilities.replace_in_ff_problematic(f, mf_out, replace,f.lbuser4,canopy_pixels,landsea_pixels)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
common_utilities.replace_in_ff_problematic(f, mf_out, replace,f.lbuser4,canopy_pixels,landsea_pixels)
common_utilities.replace_in_ff_problematic(f, mf_out, replace, f.lbuser4, canopy_pixels, landsea_pixels)

else:
mf_out.fields.append(f)

Expand Down
28 changes: 13 additions & 15 deletions src/replace_landsurface_with_ERA5land_IC.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from pathlib import Path
import xarray as xr, sys, argparse
from datetime import datetime,timedelta
import common_utilities


ROSE_DATA = os.environ.get('ROSE_DATA')
# Base directory of the ERA5-land archive on NCI
Expand All @@ -23,17 +25,6 @@
##########multipliers=[7.*10., 21.*10., 72.*10., 189.*10.]
multipliers = [10.*10., 25.*10., 65.*10., 200.*10.]

class ReplaceOperator(mule.DataOperator):
""" Mule operator for replacing the data"""
def __init__(self):
pass
def new_field(self, sources):
print('new_field')
return sources[0]
def transform(self, sources, result):
print('transform')
return sources[1]

class bounding_box():
""" Container class to hold spatial extent information."""
def __init__(self, ncfname, maskfname, var):
Expand Down Expand Up @@ -196,7 +187,7 @@ def replace_in_ff(f, generic_era5_fname, ERA_FIELDN, multiplier, ic_z_date, mf_o
data = np.where(np.isnan(data), current_data, data)
mf_out.fields.append(replace([f, data]))

def swap_land_era5land(mask_fullpath, ic_file_fullpath, ic_date):
def swap_land_era5land(mask_fullpath, ic_file_fullpath, ic_date, fix_problematic_pixels="no"):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
def swap_land_era5land(mask_fullpath, ic_file_fullpath, ic_date, fix_problematic_pixels="no"):
def swap_land_era5land(mask_fullpath, ic_file_fullpath, ic_date, fix_problematic_pixels=False):

"""
Function to get the ERA5-land data for all land/surface variables.

Expand Down Expand Up @@ -236,6 +227,9 @@ def swap_land_era5land(mask_fullpath, ic_file_fullpath, ic_date):
# Path to input file
ff_in = ic_file_fullpath.as_posix().replace('.tmp', '')

if fix_problematic_pixels == "yes":
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if fix_problematic_pixels == "yes":
if fix_problematic_pixels:

canopy_pixels,landsea_pixels=common_utilities.problematic_pixels(ff_in)

# Path to output file
ff_out = ic_file_fullpath.as_posix()
print(ff_in, ff_out)
Expand All @@ -244,7 +238,7 @@ def swap_land_era5land(mask_fullpath, ic_file_fullpath, ic_date):
mf_in = mule.load_umfile(ff_in)

# Create Mule Replacement Operator
replace = ReplaceOperator()
replace = common_utilities.ReplaceOperator()

# Define spatial extent of grid required
bounds = bounding_box(era5_fname, mask_fullpath.as_posix(), "land_binary_mask")
Expand All @@ -255,8 +249,6 @@ def swap_land_era5land(mask_fullpath, ic_file_fullpath, ic_date):
# For each field in the input write to the output file (but modify as required)
for f in mf_in.fields:

print(f.lbuser4, f.lblev, f.lblrec, f.lbhr, f.lbcode)

if f.lbuser4 == 9:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Refer to the comments about hardcoded numbers.

# replace coarse soil moisture with high-res information
if f.lblev == 4:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Refer to the comments about hardcoded numbers.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The whole if loop (L262-269) can be refactored with:

Suggested change
if f.lblev == 4:
if f.lblev in [1,2,3,4]:
lblev=f.lblev
replace_in_ff(f, generic_era5_fname, f'swvl{lblev}', multipliers[lblev-1], ic_z_date, mf_out, replace, bounds)

A similar refactoring can be done in lines 273-280, and in other similar files

Expand All @@ -280,7 +272,13 @@ def swap_land_era5land(mask_fullpath, ic_file_fullpath, ic_date):
replace_in_ff(f, generic_era5_fname, 'stl1', -1, ic_z_date, mf_out, replace, bounds)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Similar refactoring (L273-280) as mentioned above.


elif f.lbuser4 == 24:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Refer to the comments about hardcoded numbers.

# surface temperature
replace_in_ff(f, generic_era5_fname, 'skt', -1, ic_z_date, mf_out, replace, bounds)

elif ((f.lbuser4 == 33) or (f.lbuser4 == 218)) and fix_problematic_pixels == "yes":
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
elif ((f.lbuser4 == 33) or (f.lbuser4 == 218)) and fix_problematic_pixels == "yes":
elif ((f.lbuser4 == 33) or (f.lbuser4 == 218)) and fix_problematic_pixels:

Also refer to the comments about hardcoded numbers.

# surface altitude and canopy_height
common_utilities.replace_in_ff_problematic(f, mf_out, replace,f.lbuser4,canopy_pixels,landsea_pixels)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
common_utilities.replace_in_ff_problematic(f, mf_out, replace,f.lbuser4,canopy_pixels,landsea_pixels)
common_utilities.replace_in_ff_problematic(f, mf_out, replace, f.lbuser4, canopy_pixels, landsea_pixels)


else:
mf_out.fields.append(f)

Expand Down
21 changes: 8 additions & 13 deletions src/replace_landsurface_with_FF_IC.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,12 @@

import mule

class ReplaceOperator(mule.DataOperator):
""" Mule operator for replacing the data"""
def __init__(self):
pass
def new_field(self, sources):
print('new_field')
return sources[0]
def transform(self, sources, result):
print('transform')
return sources[1]

def replace_in_ff_from_ff(f, sf, mf_out, replace):

replacement_data = sf.get_data()
mf_out.fields.append(replace([f, replacement_data]))

def swap_land_ff(mask_fullpath, ic_file_fullpath, source_fullpath, ic_date):
def swap_land_ff(mask_fullpath, ic_file_fullpath, source_fullpath, ic_date, fix_problematic_pixels="no"):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
def swap_land_ff(mask_fullpath, ic_file_fullpath, source_fullpath, ic_date, fix_problematic_pixels="no"):
def swap_land_ff(mask_fullpath, ic_file_fullpath, source_fullpath, ic_date, fix_problematic_pixels=False):

"""
Function to get the land/surface data from another fields file into the start dump.

Expand All @@ -49,6 +38,9 @@ def swap_land_ff(mask_fullpath, ic_file_fullpath, source_fullpath, ic_date):
# Path to input file
ff_in = ic_file_fullpath.as_posix().replace('.tmp', '')

if fix_problematic_pixels == "yes":
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if fix_problematic_pixels == "yes":
if fix_problematic_pixels:

canopy_pixels,landsea_pixels=common_utilities.problematic_pixels(ff_in)

# Path to input file
sf_in = source_fullpath.as_posix()

Expand All @@ -61,7 +53,7 @@ def swap_land_ff(mask_fullpath, ic_file_fullpath, source_fullpath, ic_date):
msf_in = mule.load_umfile(sf_in)

# Create Mule Replacement Operator
replace = ReplaceOperator()
replace = common_utilities.ReplaceOperator()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same as above, common_utilities might have not been imported in this file, as I don't see any changes related to an import common_utilities line.


# Set up the output file
mf_out = mf_in.copy()
Expand All @@ -71,6 +63,9 @@ def swap_land_ff(mask_fullpath, ic_file_fullpath, source_fullpath, ic_date):

if f.lbuser4 in [9, 20, 24]:
replace_in_ff_from_ff(f, sf, mf_out, replace)
elif ((f.lbuser4 == 33) or (f.lbuser4 == 218)) and fix_problematic_pixels == "yes":
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
elif ((f.lbuser4 == 33) or (f.lbuser4 == 218)) and fix_problematic_pixels == "yes":
elif ((f.lbuser4 == 33) or (f.lbuser4 == 218)) and fix_problematic_pixels:

Refer to the comments about hardcoded numbers.

# surface altitude and canopy_height
common_utilities.replace_in_ff_problematic(f, mf_out, replace,f.lbuser4,canopy_pixels,landsea_pixels)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
common_utilities.replace_in_ff_problematic(f, mf_out, replace,f.lbuser4,canopy_pixels,landsea_pixels)
common_utilities.replace_in_ff_problematic(f, mf_out, replace, f.lbuser4, canopy_pixels, landsea_pixels)

else:
mf_out.fields.append(f)

Expand Down