1
votes

I want to use nco to extract variables and dimensions from a very large netcdf file. I know that I have to use ncks, but the structure of my original file is so complicated and huge that I will need some advices. I want to start with a file with multiple variables/dimensions to create a file with only 7 variables 4 dimensions and 2 global attributes. Below, the 2 descriptions of my files (i'm on linux). I don't know how to use ncks to extract more than 1 variable or if it's possible to do it in one step. Thank you for your help :-)

1/ My starting file

File champs_meno_BE201704.nc (NC_FORMAT_NETCDF4_CLASSIC):

     21 variables (excluding dimension variables):
        double latitude[ni,nj]   
            long_name: latitude
            standard_name: latitude
            units: degrees_north
            _FillValue: 1.7e+38
            valid_min: -90
            valid_max: 90
        double longitude[ni,nj]   
            long_name: longitude
            standard_name: longitude
            units: degrees_east
            _FillValue: 1.7e+38
            valid_min: -180
            valid_max: 180
        short longitude_u[ni_u,nj_u]   
            long_name: longitude at u location
            standard_name: longitude_at_u_location
            units: degrees_east
            scale_factor: 0.000247199011200289
            add_offset: 7.99996999999985
            valid_min: -32767
            valid_max: 32767
            _FillValue: -32768
        short longitude_v[ni_v,nj_v]   
            long_name: longitude at v location
            standard_name: longitude_at_v_location
            units: degrees_east
            scale_factor: 0.000247199011200289
            add_offset: 7.99996999999985
            valid_min: -32767
            valid_max: 32767
            _FillValue: -32768
        short latitude_u[ni_u,nj_u]   
            long_name: latitude at u location
            standard_name: latitude_at_u_location
            units: degrees_north
            scale_factor: 7.93480483413422e-05
            add_offset: 41.9999975000008
            valid_min: -32767
            valid_max: 32767
            _FillValue: -32768
        short latitude_v[ni_v,nj_v]   
            long_name: latitude at v location
            standard_name: latitude_at_v_location
            units: degrees_north
            scale_factor: 7.93480483413422e-05
            add_offset: 41.9999975000008
            valid_min: -32767
            valid_max: 32767
            _FillValue: -32768
        float SIG[level]   
            long_name: sigma variable
            standard_name: ocean_s_variable
            units: 
            valid_min: -1
            valid_max: 0
            _FillValue: 999
        float Csu_sig[level]   
            long_name: C(s) law at mid layer
            standard_name: ocean_s_coordinate_function_at_midlayer
            units: 
            valid_min: -2
            valid_max: 2
            _FillValue: 999
            formula_definition: C(k) = (1-b)*sinh(a*s(k))/sinh(a) + b*[tanh(a*(s(k)+0.5))-tanh(0.5*a)]/(2*tanh(0.5*a))
        short hc[ni,nj]   
            long_name: depth over which the resolution is increased
            standard_name: ocean_s_coordinate_thick_of_surface_resolution
            units: m
            coordinates: latitude longitude
            scale_factor: 0.0079348124563694
            add_offset: 240
            valid_min: -32767
            valid_max: 32767
            _FillValue: -32768
        float b[]   
            long_name: bottom control parameter
            standard_name: ocean_s_coordinate_bottom_control
            units: 
            valid_min: 0
            valid_max: 1
            _FillValue: 999
        float theta[]   
            long_name: surface control parameter
            standard_name: ocean_s_coordinate_surface_control
            units: 
            valid_min: 0
            valid_max: 20
            _FillValue: 999
        short H0[ni,nj]   
            long_name: bathymetry relative to the mean level
            standard_name: model_sea_floor_depth_below_geoid
            units: m
            coordinates: latitude longitude
            scale_factor: 0.15335550904274
            add_offset: 4975
            valid_min: -32767
            valid_max: 32767
            _FillValue: -32768
        short HX[ni_u,nj_u]   
            long_name: bathymetry at u-location relative to the mean level
            standard_name: model_sea_floor_depth_below_geoid_at_u_location
            units: m
            coordinates: latitude_u longitude_u
            scale_factor: 0.15335550904274
            add_offset: 4975
            valid_min: -32767
            valid_max: 32767
            _FillValue: -32768
        short HY[ni_v,nj_v]   
            long_name: bathymetry at v-location relative to the mean level
            standard_name: model_sea_floor_depth_below_geoid_at_v_location
            units: m
            coordinates: latitude_v longitude_v
            scale_factor: 0.15335550904274
            add_offset: 4975
            valid_min: -32767
            valid_max: 32767
            _FillValue: -32768
        short XE[ni,nj,time]   
            long_name: sea surface height
            standard_name: sea_surface_height_above_geoid
            units: m
            coordinates: latitude longitude
            scale_factor: 0.00152592547237873
            add_offset: 0
            valid_min: -32767
            valid_max: 32767
            _FillValue: -32768
        short UZ[ni_u,nj_u,level,time]   
            long_name: 3d zonal velocity
            standard_name: sea_water_x_velocity_at_u_location
            units: m s-1
            coordinates: latitude_u longitude_u
            scale_factor: 0.000610370188951492
            add_offset: 0
            valid_min: -32767
            valid_max: 32767
            _FillValue: -32768
        short VZ[ni_v,nj_v,level,time]   
            long_name: 3d meridional velocity
            standard_name: sea_water_y_velocity_at_v_location
            units: m s-1
            coordinates: latitude_v longitude_v
            scale_factor: 0.000610370188951492
            add_offset: 0
            valid_min: -32767
            valid_max: 32767
            _FillValue: -32768
        short SAL[ni,nj,level,time]   
            long_name: salinity
            standard_name: sea_water_salinity
            units: 1e-3
            coordinates: latitude longitude
            scale_factor: 0.000839259009808302
            add_offset: 22.5
            valid_min: -32767
            valid_max: 32767
            _FillValue: -32768
        short TEMP[ni,nj,level,time]   
            long_name: temperature
            standard_name: sea_water_potential_temperature
            units: degrees_Celsius
            coordinates: latitude longitude
            scale_factor: 0.000839259009808302
            add_offset: 22.5
            valid_min: -32767
            valid_max: 32767
            _FillValue: -32768
        short TAUX[ni_u,nj_u,time]   
            long_name: U-component of Surface Wind Stress
            standard_name: surface_downward_x_stress_at_u_location
            units: N m-2
            coordinates: latitude_u longitude_u
            scale_factor: 0.00152592547237873
            add_offset: 0
            valid_min: -32767
            valid_max: 32767
            _FillValue: -32768
        short TAUY[ni_v,nj_v,time]   
            long_name: V-component of Surface Wind Stress
            standard_name: surface_downward_y_stress_at_v_location
            units: N m-2
            coordinates: latitude_v longitude_v
            scale_factor: 0.00152592547237873
            add_offset: 0
            valid_min: -32767
            valid_max: 32767
            _FillValue: -32768

     10 dimensions:
        ni  Size:1101
            long_name: x-dimension of the grid
            standard_name: x_grid_index
            c_grid_axis_shift: 0
            axis: X
            c_grid_dynamic_range: 4:1088
        nj  Size:463
            long_name: y-dimension of the grid
            standard_name: y_grid_index
            axis: Y
            c_grid_axis_shift: 0
            c_grid_dynamic_range: 3:456
        ni_u  Size:1101
            long_name: x-dimension of the grid at u location
            standard_name: x_grid_index_at_u_location
            c_grid_axis_shift: 0.5
            axis: X
            c_grid_dynamic_range: 4:1087
        nj_u  Size:463
            long_name: y-dimension of the grid at u location
            standard_name: y_grid_index_at_u_location
            axis: Y
            c_grid_axis_shift: 0
            c_grid_dynamic_range: 3:456
        ni_v  Size:1101
            long_name: x-dimension of the grid at v location
            standard_name: x_grid_index_at_v_location
            c_grid_axis_shift: 0
            axis: X
            c_grid_dynamic_range: 4:1088
        nj_v  Size:463
            long_name: y-dimension of the grid at v location
            standard_name: y_grid_index_at_v_location
            axis: Y
            c_grid_axis_shift: 0.5
            c_grid_dynamic_range: 2:455
        ni_f  Size:1101
            long_name: x-dimension of the grid at f location
            standard_name: x_grid_index_at_f_location
            c_grid_axis_shift: 0.5
            axis: X
            c_grid_dynamic_range: 4:1087
        nj_f  Size:463
            long_name: y-dimension of the grid at f location
            standard_name: y_grid_index_at_f_location
            axis: Y
            c_grid_axis_shift: 0.5
            c_grid_dynamic_range: 2:455
        time  Size:240   *** is unlimited ***
            long_name: time in seconds (UT)
            standard_name: time
            units: seconds since 1900-01-01T00:00:00Z
            axis: T
            time_origin: 01-JAN-1900 00:00:00
            conventions: relative number of seconds with no decimal part
        level  Size:60
            long_name: sigma level
            axis: Z
            c_grid_axis_shift: 0
            positive: up
            valid_min: -1
            valid_max: 0
            standard_name: ocean_s_coordinate
            formula_terms: s: level eta: XE depth: H0 a: theta b: b depth_c: hc
            formula_definition: z(n,k,j,i) = eta(n,j,i)*(1+s(k)) + depth_c*s(k) + (depth(j,i)-depth_c)*C(k)

    57 global attributes:
        limi: 0
        lima: 1100
        pasi: 1
        ljmi: 0
        ljma: 462
        pasj: 1
        lkmi: 1
        lkma: 60
        pask: 1
        global_imin: 0
        global_imax: 1100
        global_jmin: 0
        global_jmax: 462
        data_type: OCO oriented grid
        format_version: 1.3.1
        Conventions: CF-1.6 OCO-1.3.1 COMODO-1.0
        netcdf_version: 4.1.2
        product_version: 1.0
        references: http://www.previmer.org/
        easting: longitude
        northing: latitude
        grid_projection: n/a
        distribution_statement: Data restrictions: for registered users only
        institution: IFREMER
        institution_references: http://www.ifremer.fr/
        data_centre: IFREMER OCO DATA CENTER
        data_centre_references: http://www.previmer.org/
        title: PREVIMER MENOR 1200 forecast
        creation_date: 2017-04-04T21:09:49Z
        run_time: 2017-04-04T21:09:49Z
        history: ...(too long)
2017-04-04T21:09:49Z: creation
        model_name: MARS
        arakawa_grid_type: C1
        source: MARS3D V10.10
        area: North Western Mediterranean Sea
        southernmost_latitude: 39.5000
        northernmost_latitude: 44.5000
        latitude_resolution: 1.082250000000000E-002
        westernmost_longitude: 0.0000
        easternmost_longitude: 15.9999
        longitude_resolution: 1.454540000000000E-002
        minimum_depth: 5.000000
        maximum_depth: 3500.000
        depth_resolution: n/a
        forecast_range: 4-days forecast
        forecast_type: forecast
        operational_status: experimental
        NCO: "4.6.4"
        start_date: 2017-04-01T00:00:00Z
        stop_date: 2017-04-01T00:00:00Z
        software_version: PREVIMER forecasting system v2
        product_name: PREVIMER_F2-MARS3D-MENOR1200_20170401T0000Z.nc
        field_type: 3-hourly
        comment: Use of Meteo-France ARPEGEHR meteorological data
        contact: [email protected]
        quality_index: 0
        nco_openmp_thread_number: 1

2/ The structure (variables, dimensions and global attributes) I want for my output file below (of course this is not the same file, but another MARS3D file with the structure I need).

File mars-iroise.nc (NC_FORMAT_CLASSIC):

     7 variables (excluding dimension variables):
        float h0[longitude,latitude]   
        float temp[longitude,latitude,level,time]   
        float u[longitude,latitude,time]   
        float uz[longitude,latitude,level,time]   
        float v[longitude,latitude,time]   
        float vz[longitude,latitude,level,time]   
        float xe[longitude,latitude,time]   

     4 dimensions:
        latitude  Size:50
        longitude  Size:40
        level  Size:15
        time  Size:13   *** is unlimited ***

    2 global attributes:
        history: Mon Jan 22 17:01:24 2018: ncks --mk_rec_dmn time mars-iroise.nc mars-iroise.nc
        NCO: "4.5.4"
2

2 Answers

2
votes

NCO's ncks documentation has examples, e.g.,

ncks -v h0,temp,u,v,uz,v,vz,xe in.nc out.nc
1
votes

Extracting the variables is easily done with cdo:

cdo selname,h0,temp,u,v,uz,v,vz,xe infile outfile

I am not sure, without looking at the file itself, precisely how to reduce the dimensions. But it looks like reducing it to 4 would require regridding it. Some of the other dimensions appear to be essential info for the velocities.