Skip to content

Instantly share code, notes, and snippets.

@uturuncoglu
Created November 10, 2023 22:47
Show Gist options
  • Save uturuncoglu/856e3d006b1d4c5bc07599736504be8f to your computer and use it in GitHub Desktop.
Save uturuncoglu/856e3d006b1d4c5bc07599736504be8f to your computer and use it in GitHub Desktop.
Convert regional FV3ATM output on mediator history file from 1d to 2d
begin
;--- arguments ---
nx = 720
ny = 600
;--- get list of files ---
files = systemfunc("ls -al ufs.hafs.cpl.hi.* | awk '{print $9}' | grep -v '_reshp.nc'")
nfiles = dimsizes(files)
;--- loop over files and reshape atm ones ---
do i = 0, nfiles-1
print("processing "+files(i))
;--- open file ---
nci = addfile(files(i), "r")
;--- create new file for reshaped variables ---
ofile = str_sub_str(files(i), ".nc", "_reshp.nc")
system("rm -f "+ofile)
nco = addfile(ofile, "c")
setfileoption(nco, "DefineMode", True)
;--- define dimensions ---
dimNames = (/ "lat", "lon" /)
dimSizes = (/ ny, nx /)
dimUnlim = (/ False, False /)
filedimdef(nco, dimNames, dimSizes, dimUnlim)
;--- get variable names ---
vars = getfilevarnames(nci)
nvars = dimsizes(vars)
;--- loop over variables and process them ---
do j = 0, nvars-1
if (isStrSubset(vars(j), "atmImp_") .or. isStrSubset(vars(j), "atmExp_")) then
;--- read variable ---
var = nci->$vars(j)$
;--- define variable in output file ---
filevardef(nco, vars(j), typeof(var) , (/ "lat", "lon" /))
;--- define attributes ---
filevarattdef(nco, vars(j), var)
;--- delete temporary variable ---
delete(var)
end if
end do
;--- exit define mode ---
setfileoption(nco, "DefineMode", False)
;--- reshape variables and save in output file ---
do j = 0, nvars-1
if (isStrSubset(vars(j), "atmImp_") .or. isStrSubset(vars(j), "atmExp_")) then
;--- create reshaped variable ---
var = reshape(rm_single_dims(nci->$vars(j)$), (/ ny, nx /))
;--- write to file ---
nco->$vars(j)$ = (/ var(::-1,::-1) /)
;--- delete temporary variable ---
delete(var)
end if
end do
;--- delete temporary variables ---
delete(vars)
end do
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment