bashnetcdfncocdo-climate

Converting longitude in NetCDF from 0:360 to -180:180 using nco


I have two NetCDF files, each for a different period of years, that I want to concatenate. They appear to have identical structures, with time as the unlimited dimension, except that for one, longitude is on the scale of 0:360 and for the other on the scale of -180:180.

This question addresses how to deal with this issue using the raster package in R, but I want to use nco.

I have found the instructions (provided below) for converting -180:180-->0:360, but am not familiar enough with the syntax to allow me to reverse the instructions in the other direction.

Details about my NetCDF file:

netcdf soda3.3.1_1980_2015_sst {
dimensions:
    depth = 1 ;
    latitude = 330 ;
    longitude = 720 ;
    time = UNLIMITED ; // (432 currently)
variables:
    float depth(depth) ;
        depth:long_name = "depth" ;
        depth:units = "m" ;
        depth:axis = "Z" ;
    float latitude(latitude) ;
        latitude:long_name = "latitude" ;
        latitude:units = "degrees_north" ;
        latitude:axis = "Y" ;
    float longitude(longitude) ;
        longitude:long_name = "longitude" ;
        longitude:units = "degrees_east" ;
        longitude:modulo = 360.f ;
        longitude:axis = "X" ;
    float temp(time, depth, latitude, longitude) ;
        temp:long_name = "Potential temperature" ;
        temp:units = "degrees C" ;
        temp:valid_range = -10.f, 500.f ;
        temp:missing_value = -1.e+20f ;
        temp:cell_methods = "time: mean" ;
        temp:standard_name = "sea_water_potential_temperature" ;
    float time(time) ;
        time:long_nime = "time" ;
        time:units = "month" ;

  data:
    longitude = 0.25, 0.75, 1.25, 1.75, 2.25, 2.75, 3.25, 3.75, 4.25, 4.75, 5.25, 5.75, 6.25, 6.75, 7.25, 7.75, 8.25, 8.75, 9.25, 9.75, 10.25, 10.75, 11.25, 11.75, 12.25, 12.75, 13.25, 13.75, 14.25, 14.75, 15.25, 15.75, 16.25, 16.75, 17.25, 17.75, 18.25, 18.75, 19.25, 19.75, 20.25, 20.75, 21.25, 21.75, 22.25, 22.75, 23.25, 23.75, 24.25, 24.75, 25.25, 25.75, 26.25, 26.75, 27.25, 27.75, 28.25, 28.75, 29.25, 29.75, 30.25, 30.75, 31.25, 31.75, 32.25, 32.75, 33.25, 33.75, 34.25, 34.75, 35.25, 35.75, 36.25, 36.75, 37.25, 37.75, 38.25, 38.75, 39.25, 39.75, 40.25, 40.75, 41.25, 41.75, 42.25, 42.75, 43.25, 43.75, 44.25, 44.75, 45.25, 45.75, 46.25, 46.75, 47.25, 47.75, 48.25, 48.75, 49.25, 49.75, 50.25, 50.75, 51.25, 51.75, 52.25, 52.75, 53.25, 53.75, 54.25, 54.75, 55.25, 55.75, 56.25, 56.75, 57.25, 57.75, 58.25, 58.75, 59.25, 59.75, 60.25, 60.75, 61.25, 61.75, 62.25, 62.75, 63.25, 63.75, 64.25, 64.75, 65.25, 65.75, 66.25, 66.75, 67.25, 67.75, 68.25, 68.75, 69.25, 69.75, 70.25, 70.75, 71.25, 71.75, 72.25, 72.75, 73.25, 73.75, 74.25, 74.75, 75.25, 75.75, 76.25, 76.75, 77.25, 77.75, 78.25, 78.75, 79.25, 79.75, 80.25, 80.75, 81.25, 81.75, 82.25, 82.75, 83.25, 83.75, 84.25, 84.75, 85.25, 85.75, 86.25, 86.75, 87.25, 87.75, 88.25, 88.75, 89.25, 89.75, 90.25, 90.75, 91.25, 91.75, 92.25, 92.75, 93.25, 93.75, 94.25, 94.75, 95.25, 95.75, 96.25, 96.75, 97.25, 97.75, 98.25, 98.75, 99.25, 99.75, 100.25, 100.75, 101.25, 101.75, 102.25, 102.75, 103.25, 103.75, 104.25, 104.75, 105.25, 105.75, 106.25, 106.75, 107.25, 107.75, 108.25, 108.75, 109.25, 109.75, 110.25, 110.75, 111.25, 111.75, 112.25, 112.75, 113.25, 113.75, 114.25, 114.75, 115.25, 115.75, 116.25, 116.75, 117.25, 117.75, 118.25, 118.75, 119.25, 119.75, 120.25, 120.75, 121.25, 121.75, 122.25, 122.75, 123.25, 123.75, 124.25, 124.75, 125.25, 125.75, 126.25, 126.75, 127.25, 127.75, 128.25, 128.75, 129.25, 129.75, 130.25, 130.75, 131.25, 131.75, 132.25, 132.75, 133.25, 133.75, 134.25, 134.75, 135.25, 135.75, 136.25, 136.75, 137.25, 137.75, 138.25, 138.75, 139.25, 139.75, 140.25, 140.75, 141.25, 141.75, 142.25, 142.75, 143.25, 143.75, 144.25, 144.75, 145.25, 145.75, 146.25, 146.75, 147.25, 147.75, 148.25, 148.75, 149.25, 149.75, 150.25, 150.75, 151.25, 151.75, 152.25, 152.75, 153.25, 153.75, 154.25, 154.75, 155.25, 155.75, 156.25, 156.75, 157.25, 157.75, 158.25, 158.75, 159.25, 159.75, 160.25, 160.75, 161.25, 161.75, 162.25, 162.75, 163.25, 163.75, 164.25, 164.75, 165.25, 165.75, 166.25, 166.75, 167.25, 167.75, 168.25, 168.75, 169.25, 169.75, 170.25, 170.75, 171.25, 171.75, 172.25, 172.75, 173.25, 173.75, 174.25, 174.75, 175.25, 175.75, 176.25, 176.75, 177.25, 177.75, 178.25, 178.75, 179.25, 179.75, 180.25, 180.75, 181.25, 181.75, 182.25, 182.75, 183.25, 183.75, 184.25, 184.75, 185.25, 185.75, 186.25, 186.75, 187.25, 187.75, 188.25, 188.75, 189.25, 189.75, 190.25, 190.75, 191.25, 191.75, 192.25, 192.75, 193.25, 193.75, 194.25, 194.75, 195.25, 195.75, 196.25, 196.75, 197.25, 197.75, 198.25, 198.75, 199.25, 199.75, 200.25, 200.75, 201.25, 201.75, 202.25, 202.75, 203.25, 203.75, 204.25, 204.75, 205.25, 205.75, 206.25, 206.75, 207.25, 207.75, 208.25, 208.75, 209.25, 209.75, 210.25, 210.75, 211.25, 211.75, 212.25, 212.75, 213.25, 213.75, 214.25, 214.75, 215.25, 215.75, 216.25, 216.75, 217.25, 217.75, 218.25, 218.75, 219.25, 219.75, 220.25, 220.75, 221.25, 221.75, 222.25, 222.75, 223.25, 223.75, 224.25, 224.75, 225.25, 225.75, 226.25, 226.75, 227.25, 227.75, 228.25, 228.75, 229.25, 229.75, 230.25, 230.75, 231.25, 231.75, 232.25, 232.75, 233.25, 233.75, 234.25, 234.75, 235.25, 235.75, 236.25, 236.75, 237.25, 237.75, 238.25, 238.75, 239.25, 239.75, 240.25, 240.75, 241.25, 241.75, 242.25, 242.75, 243.25, 243.75, 244.25, 244.75, 245.25, 245.75, 246.25, 246.75, 247.25, 247.75, 248.25, 248.75, 249.25, 249.75, 250.25, 250.75, 251.25, 251.75, 252.25, 252.75, 253.25, 253.75, 254.25, 254.75, 255.25, 255.75, 256.25, 256.75, 257.25, 257.75, 258.25, 258.75, 259.25, 259.75, 260.25, 260.75, 261.25, 261.75, 262.25, 262.75, 263.25, 263.75, 264.25, 264.75, 265.25, 265.75, 266.25, 266.75, 267.25, 267.75, 268.25, 268.75, 269.25, 269.75, 270.25, 270.75, 271.25, 271.75, 272.25, 272.75, 273.25, 273.75, 274.25, 274.75, 275.25, 275.75, 276.25, 276.75, 277.25, 277.75, 278.25, 278.75, 279.25, 279.75, 280.25, 280.75, 281.25, 281.75, 282.25, 282.75, 283.25, 283.75, 284.25, 284.75, 285.25, 285.75, 286.25, 286.75, 287.25, 287.75, 288.25, 288.75, 289.25, 289.75, 290.25, 290.75, 291.25, 291.75, 292.25, 292.75, 293.25, 293.75, 294.25, 294.75, 295.25, 295.75, 296.25, 296.75, 297.25, 297.75, 298.25, 298.75, 299.25, 299.75, 300.25, 300.75, 301.25, 301.75, 302.25, 302.75, 303.25, 303.75, 304.25, 304.75, 305.25, 305.75, 306.25, 306.75, 307.25, 307.75, 308.25, 308.75, 309.25, 309.75, 310.25, 310.75, 311.25, 311.75, 312.25, 312.75, 313.25, 313.75, 314.25, 314.75, 315.25, 315.75, 316.25, 316.75, 317.25, 317.75, 318.25, 318.75, 319.25, 319.75, 320.25, 320.75, 321.25, 321.75, 322.25, 322.75, 323.25, 323.75, 324.25, 324.75, 325.25, 325.75, 326.25, 326.75, 327.25, 327.75, 328.25, 328.75, 329.25, 329.75, 330.25, 330.75, 331.25, 331.75, 332.25, 332.75, 333.25, 333.75, 334.25, 334.75, 335.25, 335.75, 336.25, 336.75, 337.25, 337.75, 338.25, 338.75, 339.25, 339.75, 340.25, 340.75, 341.25, 341.75, 342.25, 342.75, 343.25, 343.75, 344.25, 344.75, 345.25, 345.75, 346.25, 346.75, 347.25, 347.75, 348.25, 348.75, 349.25, 349.75, 350.25, 350.75, 351.25, 351.75, 352.25, 352.75, 353.25, 353.75, 354.25, 354.75, 355.25, 355.75, 356.25, 356.75, 357.25, 357.75, 358.25, 358.75, 359.25, 359.75 ;

This is the code I found in the manual to convert -180:180 to 0:360.

With this background, one sees that the following commands suffice to rotate the input file by 180 degrees longitude:

% ncks -O -v LatLon --msa -d Lon,0.,180. -d Lon,-180.,-1.0 in.nc out.nc
% ncap2 -O -s 'where(Lon < 0) Lon=Lon+360' out.nc out.nc
% ncks --trd -C -H -v LatLon ~/nco/data/in.nc
Lat[0]=-45 Lon[0]=-180 LatLon[0]=0 
Lat[0]=-45 Lon[1]=-90 LatLon[1]=1 
Lat[0]=-45 Lon[2]=0 LatLon[2]=2 
Lat[0]=-45 Lon[3]=90 LatLon[3]=3 
Lat[1]=45 Lon[0]=-180 LatLon[4]=4 
Lat[1]=45 Lon[1]=-90 LatLon[5]=5 
Lat[1]=45 Lon[2]=0 LatLon[6]=6 
Lat[1]=45 Lon[3]=90 LatLon[7]=7 
% ncks --trd -C -H -v LatLon ~/out.nc
Lat[0]=-45 Lon[0]=0 LatLon[0]=2 
Lat[0]=-45 Lon[1]=90 LatLon[1]=3 
Lat[0]=-45 Lon[2]=180 LatLon[2]=0 
Lat[0]=-45 Lon[3]=270 LatLon[3]=1 
Lat[1]=45 Lon[0]=0 LatLon[4]=6 
Lat[1]=45 Lon[1]=90 LatLon[5]=7 
Lat[1]=45 Lon[2]=180 LatLon[6]=4 
Lat[1]=45 Lon[3]=270 LatLon[7]=5

Thanks in advance!


Solution

  • Good question. Try

    ncks -O --msa -d lon,181.,360. -d lon,0.,180.0 in.nc out.nc
    ncap2 -O -s 'where(lon > 180) lon=lon-360' out.nc out.nc
    

    The first command shifts the data, the second command recalibrates the coordinate to the newly shifted data. One comment on applying this algorithm: Be careful to specify hemispheres that do not overlap, e.g., by inadvertently specifying coordinate ranges in the first command that both include the date line. Some users will find using index-based rather than coordinate-based hyperslabs makes this clearer. Examine a plot of the field to make sure your rotation was correct.