Question

I have a large series of netcdf files representing daily snapshots of data. I am hoping to hook these up to a software which is asking me to add to the namelist the maximum and minimum values for a variable in the files. How can I enquire about the maximum and minimum values stored in a variable?

My variable is depth (here is an excerpt from an ncdump for an idea of the size of that variable) ...

dimensions:
    z = 40 ;
    lat = 224 ;
    lon = 198 ;
    time = 1 ;
variables:
    float depth(z, lat, lon) ;
        depth:long_name = "cell centre depth" ;
        depth:units = "m" ;
...

I'm still a beginner at handling these files, and have been using NCO operators and/or matlab for netcdf handling to date - is there an easy way to perform this max min enquiry using either of these tools?

Before now I have had netcdfs where the value range was helpfully displayed in the attributes or it has been a sufficiently small amount of data to be displayed easily with a simple ncdump -v look at the values or storing the variable in matlab which auto displays the max min, but now I have too many values to use these quick and dirty methods.

Any help is greatfully received. All the best, Bex

Was it helpful?

Solution 2

If you have a newer version of MATLAB, try using the ncread function.

% Update with your filename and variable name below.
% This reads in the full variable into MATLAB
variableData = ncread(filename,varname);
% Query max and min values
minValue     = min(variableData(:))
maxValue     = max(variableData(:))
% you could also write this information back to the file for future reference.
% see https://www.unidata.ucar.edu/software/netcdf/docs/netcdf/Attribute-Conventions.html
ncwriteatt(filename, varname, 'valid_range', [minValue, maxValue]);
% check result
ncdisp(filename, varname);

You could add two additional loops outside, one for looping through all your files and another for looping through all the variables in a file (look at ncinfo) to automate the whole thing.

OTHER TIPS

One NCO method would be to use the ncrng command, which is simply a "filter" for a longer ncap2 command: zender@roulee:~/nco/data$ ncrng three_dmn_rec_var in.nc 1.000000 to 80.000000 So, it's a three word command. Documentation on filters is here.

The CDO method would be

cdo vertmax -fldmax in.nc max.nc
cdo vertmin -fldmin in.nc min.nc

The advantage is that you can calculate min/max just over x-y space (fldmax/fldmin), vertically (vertmax/min) or over time (timmax/min), or a combination of the three.

To dump the values from the netcdf to ascii you can use ncks

ncks -s '%13.9f\n' -C -H  -v depth max.nc

To construct a namelist therefore you could for example write

echo min=`ncks -s '%13.9f\n' -C -H  -v depth min.nc` >> namelist.txt
echo max=`ncks -s '%13.9f\n' -C -H  -v depth max.nc` >> namelist.txt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top