Replacing fields in wrfinput

From mn/geo/geoit
Jump to: navigation, search


If your simulation involves changing initial conditions, you need to replace one or more fields in your wrfinput_d0x file. You can do this using CDO and R.

Preparations

First you have to run all parts of your model up to real.exe before this step. Real.exe gives you wrfbdy_d01 and d02; wrfinput_d01 and d02; and wrflowinp_d01 and 02 (if you have varying SST). You also need something to replace into wrfinp_d0x.

I want to use the ground fields from a 9-month long spinup simulation for d01 as initial conditions for d02. Kjetil had the idea that you might regrid the d01 spinup and replace it into wrfinput_d02, saving months of simulation time, and many clock hours on Abel.

You should also set up an sshfs link between Astra (on Abel) and Sverdrup (or Wessel) so you can access your Abel files from Sverdup from the folder /var/sshfs/username. See https://wiki.uio.no/mn/geo/geoit/index.php/A_WRF_example/Get NORSTORE access.

Replacement

cdo: regridding

On Abel, create symbolic links to the d01 file you want to use as spinup and the wrfinput_d02 that does not have those variables spun up. You'll save space if you create a symbolic link instead of copying it.

ln -s /work/users/irenebn/spinup-simulation-d01/wrfrst_d01_2014-06-11_00\:00\:00 
ln -s /work/users/irenebn/real_simulation_two_domains/wrfinput_d02

Extract the fields you want to regrid.

cdo selname,ISNOW,SNOW,SNOWC,SNOALB,SNOWH,TSK,SMOIS,SOILCBOT,SOILCTOP,T_INIT wrfrst_d01_2014-06-11_00\:00\:00 spun-upvars_d01_2014-06-11

Create a text file describing the d02 grid (griddes.txt)

cdo griddes wrfinput_d02 >> griddes.txt

Remap using one of the remap functions. Remap-bil means bilinear interpolation.

cdo remapbil,griddes.txt spun-upvars_d01_2014-06-11 remapped_spinup_d02_2014-06-11 # bilinear interpolation

Unfortunately, the cdo command to replace the regridded fields into the orignal wrfinput_d02 field gave an error message (see "potential error messages" below). Thus,

## cdo delname,SMOIS,SNOW,SNOWH,SNOWC,TSK,ISNOW,SOILCBOT,SOILCTOP,T_INIT,SNOALB wrfinput_d02-with_spunup_variables2 out.nc # before merging, remove the fields you have changed.
## cdo merge out.nc remapped_spinup_d02_2014-06-11 wrfinput_2014-06-11  # merge two files. This does not give a wrfinput_d02 file that wrf can recognise...

R: replace fields

Moving to sverdrup (or wessel), copy files from Astra to the folder where you run R.

cp /var/sshfs/irenebn/regrid/remapped_spinup_d02_2014-06-11 .
cp /var/sshfs/irenebn/regrid/wrfinput_d02 ./wrfinput_d02_2014-06-11


################################################################################
# Replace initial values in d02 with remapped and spun-up variables (from cdo).
# Written by IBN, 28.12.2016.
################################################################################
replaceFields <- function(remapfile="remapped_spinup_d02_2014-06-11", wrfinpfile="wrfinput_d02_2014-06-11"){
 # spinudir <- file.path(wrfPath,"regrid", paste("remapped_spinup_d02_1995-05-12", sep=""))
 # wrfindir <- file.path(wrfPath,"regrid", paste("wrfinput_d02-1995spinup", sep=""))
 spinudir <- file.path(wrfPath,"regrid", remapfile)
 wrfindir <- file.path(wrfPath,"regrid", wrfinpfile)
 wrfin <- nc_open(wrfindir, write=TRUE)
 spinu <- nc_open(spinudir)
 ncvar_put(wrfin, 'SNOW', ncvar_get(spinu,'SNOW')[,1:185])
 ncvar_put(wrfin, 'SNOWH', ncvar_get(spinu,'SNOWH')[,1:185])
 ncvar_put(wrfin, 'SNOWC', ncvar_get(spinu,'SNOWC')[,1:185])
 ncvar_put(wrfin, 'ISNOW', ncvar_get(spinu,'ISNOW')[,1:185])
 ncvar_put(wrfin, 'SNOALB', ncvar_get(spinu,'SNOALB')[,1:185])
 ncvar_put(wrfin, 'TSK', ncvar_get(spinu,'TSK')[,1:185])
 ncvar_put(wrfin, 'T_INIT', ncvar_get(spinu,'T_INIT')[,1:185,])
 ncvar_put(wrfin, 'SMOIS', ncvar_get(spinu,'SMOIS')[,1:185,])
 ncvar_put(wrfin, 'SOILCBOT', ncvar_get(spinu,'SOILCBOT')[,1:185,])
 ncvar_put(wrfin, 'SOILCTOP', ncvar_get(spinu,'SOILCTOP')[,1:185,])
 nc_close(wrfin)
 nc_close(spinu)
} # end function replaceFields


Move the wrfinput file back to Abel:

mv wrfinput_d02_2014-06-11 /var/sshfs/irenebn/regrid/


Running wrf

On Abel, you now have a nice wrfinput_d02_2014-06-11 file on Astra (/var/sshfs/irenebn/regrid/). Simply create a symbolic link from $WORKDIR (in the file where you want to run your simulation):

cd /work/users/irenebn/real_simulation_two_domains/
ln -s /var/sshfs/irenebn/regrid/wrfinput_d02_2014-06-11
mv wrfinput_d02_2014-06-11 wrfinput_d02                  # rename it wrfinput_d02 so WRF can find it. You'll see the date origin in the symlink filename, which is useful if you have many wrfinput_d02 files.
cd ~/real_simulation_two_domains/                        # move to the folder where you have namelist.input etc. 
sbatch job_wrf.sh                                        # submit the job AFTER having checked dates, parameterisations etc in namelist.input and runtime, cpus, accounts etc in job_wrf.sh.


Potential errors

In WRF:

*************************************
d01 2014-05-12_00:00:00 *** Initializing nest domain # 2 from an input file. ***
-------------- FATAL CALLED ---------------
FATAL CALLED FROM FILE:  <stdin>  LINE:      70
program wrf: error opening wrfinput_d02 for reading ierr=          -8
-------------------------------------------

WRF cannot recognise the file type of wrfinput_d02 because you have changed it with cdo (and likely "cdo merge"). Do step "R: replace fields" above to solve the problem.


In R:

>   wrfin <- nc_open(wrfindir, write=TRUE)
Error in R_nc4_open: No such file or directory
Error in nc_open(wrfindir, write = TRUE) : 
 Error in nc_open trying to open file /mn/vann/metos-d2/irenebn/Trends//wrf_output//regrid/wrfinput_d02_2014-06-11

R cannot find the file. Remember to copy it from Astra/Abel to Sverdrup.

>   ncvar_put(wrfin, 'SNOW', ncvar_get(spinu,'SNOW')[,1:185])
Error in ncvar_put(wrfin, "SNOW", ncvar_get(spinu, "SNOW")[, 1:185]) : 
 ncvar_put: error: you asked to write 28830 values, but the passed data array only has 28675 entries!

The sizes do not match. Find the size of wrfinput_d02 and the remapped file and try again.


Final remarks

Good luck! It takes some effort, but hopefully you'll get this soon:

d01 2014-05-12_00:00:00 *** Initializing nest domain # 2 from an input file. ***


-Irene Brox Nilsen