Search code examples
ubuntunetcdfqgisgeocdo-climate

Is there a way to calculate and save a new variable that is a function of multiple netCDF or tif files?


I have multiple .nc which are also in .tif files that I would like to use to calculate the probability based on a statistical model. I have 5 files (V1.nc, V2.nc, V3.nc, V4.nc, V5.nc), and a statistical model being: y = 1 / (1 + exp(-(Intercept + V1 * 1.87 + V2 * 1.53 + V3 * (-4.18) + V4 * 3.58 + V5 * 9.13))).

I normally use Qgis, RStudio and Ubuntu, and I do not care which of these programs to use.

Is there a way for me to use those files to calculate the overall y?


Solution

  • You can try to cat the files into one using

    cdo cat V?.nc combined.nc 
    

    and then make a new variable using the expression command

    intercept=2.3123 # or whatever the value is 
    cdo expr,'y=1 / (1 + exp(-(${intercept} + V1 * 1.87 + V2 * 1.53 - 4.18*V3 + V4 * 3.58 + V5 * 9.13)))' combined.nc newvar.nc
    

    I'm assuming the variable in V1.nc is called "V1" etc, if not substitute the name accordingly.

    EDIT: According to the comment each file uses the same variable name. So either the variable should be renamed in each file using NCO prior to the above, or one could combine the original files using separate cdo functions as follows:

    cdo -pow,-1 -addc,1 -exp -mulc,-1 -addc,$intercept -add -mulc,1.87 V1.nc -add -mulc,1.53 V2.nc -add -mulc,-4.18 V4.nc -add -mulc,3.58 V4.nc -mulc,9.13 V5.nc mynewvariable.nc 
    

    I've used piping to get it on one line and avoid intermediate files, check my equation carefully but I hope it doesn't contain mistakes. With piping it helps to read from right to left.

    The variable in the file mynewvariable.nc will also be called Band1; you might want to change the metadata appropriately using nco, always a good habit to have even if the files are only for your own use...