how to select variables (surface and some vertical levels with index)?
Added by Jagdish Prajapati over 2 years ago
Hello,
I am new to CDO. I have a WRF model ncfile. I want to extract some surface variables and some variables on vertical levels (using index value:29,17,13,9 and 6). I used the following two commands
cdo -selvar,"U10,V10,PSFC,T2,Q2" infile.nc outfile1.nc
cdo -sellevidx,29,17,13,9,6 -selvar,"U,V,W,T,P,PB,PH,PHB,QVAPOR" infile.nc outfile2.nc
and then used
cdo merge outfile1.nc outfile2.nc outfile
If I am applying the same set of procedures to 5 files, It worked well for the first 3 files but not for the last 2.
What is wrong I am doing, is there a way to do the same in one line command? Any help will be greatly appreciated.
Replies (8)
RE: how to select variables (surface and some vertical levels with index)? - Added by Ralf Mueller over 2 years ago
hi!
Please upload some input. would be perfect to have 1 file, that causes the error and another one, that works fine
cheers
ralf
RE: how to select variables (surface and some vertical levels with index)? - Added by Jagdish Prajapati over 2 years ago
Hi Ralf,
Thanks for your quick response. The data file is too large to upload(around 5 gb). Actually, the commands are working well when I am running it manually for the file one by one. I think the problem is in automation. Please check the script if you can. Also, consider replying to how to judge the data at different indexes.
##
import glob
import subprocess
list_of_files = sorted(glob.glob("/home/imdhwrf/OLD_PYTHON_PACK_JAGDISH/PYTHON-PLOT/TRY/KE/WRF/itr1/202203*00/wrfout_d01_2022-03-*_*:00:00.nc"))
for fl in list_of_files:
#print (file)
dt = fl[72:74]
utc = fl[99:101]
if int(dt) >= 3 and int(dt) <= 3 and int(utc) >= 0 and int(utc) <= 4:
subprocess.call('cdo -selvar,U10,V10,PSFC,T2,Q2 %s out1.nc' % fl, shell=True)
subprocess.call('cdo -sellevidx,29,17,13,9,6 -selvar,U,V,W,T,P,PB,PH,PHB,QVAPOR %s out2.nc' % fl, shell=True)
subprocess.call('cdo merge out1.nc out2.nc new_'+fl[77:110], shell=True)
#subprocess.call(['rm' '-f' 'out1.nc'])
#subprocess.call(['rm' '-f' 'out2.nc'])
subprocess.call(['rm','-r','out1.nc'])
subprocess.call(['rm','-r','out2.nc'])
Thanks and Regards
Jagdish
RE: how to select variables (surface and some vertical levels with index)? - Added by Ralf Mueller over 2 years ago
hi!
In case of error, it would be helpful to provide the error messages. so far the calls look ok to me.
cheers
ralf
RE: how to select variables (surface and some vertical levels with index)? - Added by Jagdish Prajapati over 2 years ago
Please check it.
Warning (cdfInqContents): Coordinates variable XTIME can't be assigned!
cdf_get_vara_float: ncid = 65536 varid = 9
Error (cdf_get_vara_float): NetCDF: HDF error
Warning (cdfCheckVarname): Changed double entry of variable name 'XLONG' to 'XLONG_2'!
Warning (cdfCheckVarname): Changed double entry of variable name 'XLAT' to 'XLAT_2'!
cdo merge: Processed 11 variables over 2 timesteps [0.82s 224MB].
RE: how to select variables (surface and some vertical levels with index)? - Added by Ralf Mueller over 2 years ago
this is clearly related to the input, which makes it hard to comment on this. WRF output can be tricky to handle.
I would print the filename before executing cdo in the loop. This identifies the file(s) which causes the issues. you might upload the output of ncdump -h
on that file here. This represents the internal structure of the netcdf files.
RE: how to select variables (surface and some vertical levels with index)? - Added by Jagdish Prajapati over 2 years ago
Please see the attached file.
ncdump-h_result (49.7 KB) ncdump-h_result |
RE: how to select variables (surface and some vertical levels with index)? - Added by Ralf Mueller over 2 years ago
These wrf files .... here are my recommendations to make them work with CDO
- rename the variable 'XTIME' to 'Time'
- remove the string 'XTIME' from all 'coordinates' attributes (if the coordinates are NOT time constant, which is the case, I think)
- remove the coordinates attribute from the variables 'XLONG' and 'XLAT'
If your XLONG/XLAT fields really do change with Time, then CDO won't be able to do proper analysis on these files. CDO does not handle time-dependant coordinates
RE: how to select variables (surface and some vertical levels with index)? - Added by Jagdish Prajapati over 2 years ago
Thanks Ralf for clarifying the things.