Project

General

Profile

Changing level units and values from cm to m

Added by Seth Bushinsky over 2 years ago

I am processing CMIP6 files and interpolating ocean variables to 10m depth. One of my files has centimeters as the z unit, all others have meters. Is there a way to convert from cm to m? I can use chlevel to adjust the level values, for instance changine 500 cm to 5 m:

cdo chlevel,500,5 talk_Omon_CESM2_ssp585_r4i1p1f1_gn_201501-206412.nc temp2.nc

However, I need to know the original depth values to do this. I'm guessing I'm missing something but I haven't been able to find any examples in my searches. Thanks for any help.


Replies (8)

RE: Changing level units and values from cm to m - Added by Ralf Mueller over 2 years ago

the setpartab operator (https://code.mpimet.mpg.de/projects/cdo/embedded/index.html#x1-2440002.6.2) support automatic unit conversion if you build CDO with udunits2 support.

you can check you CDO with cdo -V

RE: Changing level units and values from cm to m - Added by Seth Bushinsky over 2 years ago

Thanks - I was able to update CDO and run setpartab. I wasn't clear in that the main variable for my file (dissic) has coordinates of (time, lev, nlat, nlon). The "lev" vertical coordinate is currently in units of centimeters and I would like to change it to meters so that my later interpolation commands work. setpartab seems to want to change the main parameter, not a coordinate parameter. That said, there may be a different problem because I am getting the warning "Warning (cdfInqContents): Coordinates variable lev can't be assigned!". I'll keep trying to figure out what is going on.

RE: Changing level units and values from cm to m - Added by Ralf Mueller over 2 years ago

can you upload the file maybe? or at least a single timestep of it?

RE: Changing level units and values from cm to m - Added by Seth Bushinsky over 2 years ago

Here is one time step (201501) of the file. Some of the problematic file construction may have been lost, as the first selyear command generated a warning that the next selmonth command did not:

smb-uh@smb-uh ssp585_download % cdo selyear,2015 dissic_Omon_CESM2_ssp585_r4i1p1f1_gn_201501-206412.nc dissic_Omon_CESM2_ssp585_r4i1p1f1_gn_201501-201512.nc
Warning (cdfInqContents): Coordinates variable lev can't be assigned!
cdo selyear: Processed 88473600 values from 1 variable over 600 timesteps [1.91s 76MB].
smb-uh@smb-uh ssp585_download % cdo selmonth,1 dissic_Omon_CESM2_ssp585_r4i1p1f1_gn_201501-201512.nc dissic_Omon_CESM2_ssp585_r4i1p1f1_gn_201501-201501.nc
cdo selmonth: Processed 7372800 values from 1 variable over 12 timesteps [0.49s 60MB].

Thanks for any help. I might be able to find ways around this by doing more of my processing in cdo and manually ingesting this single file, but so far have been having trouble creating a good workaround.

dissic_Omon_CESM2_ssp585_r4i1p1f1_gn_201501-201501.nc (39.1 MB) dissic_Omon_CESM2_ssp585_r4i1p1f1_gn_201501-201501.nc partial dataset w/ the vertical coordinates in units of "centimeters"

RE: Changing level units and values from cm to m - Added by Ralf Mueller over 2 years ago

yes, you are right: setpartab only works on the data variables not the coordinates. Unfortunately CDO does not have an operators to set a complete list of new levels. There is only the chlevel operators, which replaces old with new values, but that won't work for you.

you can set the attribute with

cdo -setattribute,lev@units=m
but for the values you need to work with another tool I think

RE: Changing level units and values from cm to m - Added by Karin Meier-Fleischer over 2 years ago

Hi,

if I'm not completely barking up the wrong tree, the following should be what you're looking for.

You can use the setzaxis operator to reset the z-axis by a descriptor file. If you do not know the levels you can write a shell script which retrieves the levels from the input file, creates the z-axis descriptor file and generates the new file with z-axis units 'm' and correct values.

#!/usr/bin/env bash

infile="dissic_Omon_CESM2_ssp585_r4i1p1f1_gn_201501-201501.nc" 

levels=$(cdo -s showlevel $infile)
echo "levels = $levels" 

i=0
for value in $levels; do
    v=$(echo "scale=6; $value/100" | bc -l)
    echo "$value cm = $v m " 
    if [[ $i -gt 0 ]]; then
       levelsm=$(echo "$levelsm, $v")
    else
       levelsm=$v
    fi
    i=`expr $i + 1`
done

cat > zaxis_meter.txt << EOF
zaxistype = depth_below_land
size      = $i
name      = olevel
units     = "m" 
levels    = $levelsm
EOF

cdo -f nc setzaxis,zaxis_meter.txt $infile outfile.nc
cdo showlevel outfile.nc
 5 15 25 35 45 55 65 75 85 95 105 115 125 135 145 155 165.098398 175.479043 186.29127 197.660273 209.711387 222.578281 236.408828 251.370156 267.654199 285.483652 305.119219 326.867988 351.093477 378.227617 408.784648 443.377695 482.736719 527.728008 579.372891 638.862617 707.563281 787.0025 878.825234 984.705859 1106.20422 1244.56688 1400.49719 1573.94641 1764.00328 1968.94422 2186.45656 2413.97156 2649.00125 2889.38469 3133.40469 3379.79344 3627.67031 3876.45188 4125.76812 4375.3925 4625.19031 4875.08344 5125.02812 5375
cdo sinfon outfile.nc
   File format : NetCDF2
    -1 : Institut Source   T Steptype Levels Num    Points Num Dtype : Parameter name
     1 : NCAR     CESM2    v instant      60   1    122880   1  F32  : dissic        
   Grid coordinates :
     1 : curvilinear              : points=122880 (320x384)
                              lon : 0.0147311 to 359.996 degrees_east  circular
                              lat : -79.22052 to 89.70641 degrees_north
                        available : cellbounds
                          mapping : Projection
                             nlon : 1 to 320 by 1 1
                             nlat : 1 to 384 by 1 1
   Vertical coordinates :
     1 : depth_below_land         : levels=60
                           olevel : 5 to 5375 m
   Time coordinate :  1 step
     RefTime =  0001-01-01 00:00:00  Units = days  Calendar = 365_day  Bounds = true
  YYYY-MM-DD hh:mm:ss  YYYY-MM-DD hh:mm:ss  YYYY-MM-DD hh:mm:ss  YYYY-MM-DD hh:mm:ss
  2015-01-15 13:00:00

-Karin ;)

RE: Changing level units and values from cm to m - Added by Seth Bushinsky over 2 years ago

Thank you Ralf for your continued help and Karin for your detailed reply and solution.

Karin, your solution does seem like it will work and I really appreciate you writing out the example as my shell scripting is slow and heavily reliant on google. I'll work on implementing it and report back.

Best,
Seth

RE: Changing level units and values from cm to m - Added by Seth Bushinsky over 2 years ago

Hi Karin,

I adapted your solution to my processing code and all seems to be working great. Thanks a lot for the help.

Best,
Seth

    (1-8/8)