Menu

ncdpq reverse values in vars along dimension

Help
Malcolm
2008-11-19
2013-10-17
  • Malcolm

    Malcolm - 2008-11-19

    Hello,
    I am trying to reverse the values of all variables on a particular dimension. I have managed to do that using

    ncpdq -a "-PERIM" input_file.nc rev_file.nc

    where PERIM is a dimension from 1 to 1756 for eg.

    O3(PERIM, LEVEL, TSTEP)
    NO2(PERIM, LEVEL, TSTEP)...........And so on.

    In the original file PERIM is 1 to 1756...and in reversed file, it becomes 1756 to 1.

    I would like to reverse the values in the original file in a similar way (for all variables again on PERIM), but for PERIM (800-1400) and another for PERIM (1401-1756)

    In other words, 1-799 remains unchanged, only the two blocks reverse inside...so eventually the index should be this:

    PERIM (1-799,1400-800,1756-1401)....in all variables

    I have managed to write a fortran program to do this and it works...however, I tried to use '-d' option... but got errors for both (below):

    ncpdq -d "-PERIM",800,1400,1 input_file.nc rev_file.nc

    ncpdq -a -d "-PERIM,800,1400,1" input_file.nc rev_file.nc

    Can anyone please help?

    Thanks!
    Malcolm
    (Barcelona Supercomputing Center)

     
    • henry Butowsky

      henry Butowsky - 2008-11-19

      Hi Malcom,
      Your ncpdq command line should be something like
      ncpdq -F -a -PERIM -d PERIM,800,1400 in.nc out.nc

      So if you do this three times you can then glue the files the files together using ncrcat
      ncks -F  -d PERIM,1,799 in.nc out.nc
      ncpdq -F -a -PERIM -d PERIM,800,1400 in.nc out1.nc
      ncpdq -F -a -PERIM -d PERIM,1401,1756 in.nc out2.nc

      ncrcat out.nc out1.nc out2.nc final.nc;

      Another way to get what you want is to use the ncap2 operator  with array subscripting
      e.g
      ncap2 -v -O -F -S rev.nco in.nc out.nc

      rev.nco
      /***********************************************/
      NO2_REV=NO2.reverse($PERIM);

      NO2(800:1400,:,:)=NO2_REV(356:756,:,:)
      NO2(1401:1756,:,:)=NO2_REV(1:355,:,:);
      /***********************************************/

      Regards Henry

       
    • Malcolm

      Malcolm - 2008-11-20

      Thanks Henry!
      However, I still get errors.

      > Your ncpdq command line should be something like
      > ncpdq -F -a -PERIM -d PERIM,800,1400 in.nc out.nc

      Although PERIM has 1756 (i.e 0 to 1755), I get :
      ---------------------------------------------------
      ERROR: program exiting through nco_err_exit() which will now call exit(EXIT_FAILURE)
      nco_err_exit(): ERROR nco_put_vara
      Index exceeds dimension bound
      ----------------------------------------------------

      This is very strange considering the following works:

      ncks -d PERIM,800,1400 in.nc out.nc

      >So if you do this three times you can then glue the files the files together
      > using ncrcat

      > ncks -F  -d PERIM,1,799 in.nc out.nc
      > ncpdq -F -a -PERIM -d PERIM,800,1400 in.nc out1.nc
      > ncpdq -F -a -PERIM -d PERIM,1401,1756 in.nc out2.nc

      > ncrcat out.nc out1.nc out2.nc final.nc;

      Well, even assuming "ncdpq  -F -a -PERIM -d PERIM,800,1400 in.nc out1.nc" did not give me the above error, I can't use ncrcat to glue together as PERIM is not a record dimension/variable. I tried:

      > ncrcat out.nc out1.nc out2.nc final.nc

      and here's the error: ncrcat:

      "ERROR input file in.nc lacks a record dimension"

      I suppose ncrcat can actually Concatenate (glue) PERIM along "0-799" + "800-1400" + "1400-1755"  to give me PERIM "0-1755" as a dimension.

      I couldn't use "ncap2" as I have the earlier version of NCO. Tried installing the latest version and I got some errors (which I should probably post in another thread).

      I could upload my .nc file on a ftp and give you the user name/password (if can try it out at your end). Even better, if you have an anonymous ftp where i could upload it. Thanks once again.

       
    • henry Butowsky

      henry Butowsky - 2008-11-20

      Hi Malcom,
      A couple of things - Because the way you presented the hyperslab values I thought you were using fortran indices (they start at one) Hence the -F argument in the commands .

      1) To make PERIM a record dimension do the following:
        ncecat in.nc in1.nc
        ncpdq -a PREIM,record in1.nc in2.nc
        ncwa -a record in2.nc in3.nc          
        (in3.nc now has PERIM as a record dimension)
       
      2) 
        ncks  -d PERIM,0,799 in3.nc out.nc 
        ncpdq  -a -PERIM -d PERIM,800,1400 in3.nc out1.nc
        ncpdq  -a -PERIM -d PERIM,1401,1755 in3.nc out2.nc

      3) ncrcat out.nc out1.nc out2.nc final.nc;

      If any of the above fail please email me the file if its not to huge i.e less that 20M -henryb@ntlworld.com

       
      • Malcolm

        Malcolm - 2008-11-21

        Hi Henry

        > A couple of things -Because the way you presented the hyperslab values I thought > you were using fortran indices (they start at 1) Hence the -F argument in the          > commands .

        Yes, I probably did..but its okay..thanks for making me realize that.

        > 1) To make PERIM a record dimension do the following: 
        >  ncecat in.nc in1.nc
        >  ncpdq -a PREIM,record in1.nc in2.nc
        >  ncwa -a record in2.nc in3.nc 
        >  (in3.nc now has PERIM as a record dimension)

        when I do "ncpdq -a PREIM,record in1.nc in2.nc" , I get (I think its NOT an error..but just a info msg)

        "ncpdq: INFO nco_var_dmn_rdr_mtd() for variable NO reports old input record dimension record is now ordinal dimension 3, new record dimension must be PERIM

        ncpdq: INFO Requested re-order will change record dimension from record to PERIM. netCDF allows only one record dimension. Hence ncpdq will make PERIM record (least rapidly varying) dimension in all variables that contain it."

        I checked the ncdump of in1.nc and in2.nc and I guess the message means the record dimension has changed to "PERIM=unlimited (1756 currently)" (..so far so good).

        "ncwa -a record in2.nc in3.nc" does a weighted avg on record and gets rid of that dimension...so at the end of all this, I have a perfectly defined record dimension "PERIM".

        SO STEP 1 works well

        > 2)  ncks  -d PERIM,0,799 in3.nc out.nc 
        >       ncpdq  -a -PERIM -d PERIM,800,1400 in3.nc out1.nc
        >       ncpdq  -a -PERIM -d PERIM,1401,1755 in3.nc out2.nc

        "ncpdq  -a -PERIM -d PERIM,800,1400 in3.nc out1.nc"

        Surprisingly, this DOES NOT create PERIM (1400-800=600), but creates PERIM (1401)

        Similarly,

        " ncpdq  -a -PERIM -d PERIM,1401,1755 in3.nc out2.nc"

        Creates PERIM (1756). So not surprisingly....the final step

        > 3) ncrcat out.nc out1.nc out2.nc final.nc

        creates  'final.nc' with  "PERIM = UNLIMITED ; // (3957 currently)...which is (800 + 1401 + 1756)"

        My requirement actually is "PERIM = UNLIMITED ; // (1756 currently)"

        Any comments here? I will email the original .nc file...Thanks once again
        Malcolm

         

Log in to post a comment.