Adding_new_input_files - PIK-LPJmL/LPJmL GitHub Wiki

Once you have a data set that you want to use as input, you use any programming language, to write the header to a file.
In C, you can use the function “fwriteheader(..)” in src/tools/header.c to write headers (or freadheader(…) to read it)
Data needs to be stored in the same order as the input grid.

Structure of the header for input files

  • header name: 1 byte char array

  • header version: 4 byte integer, set to 1 for CLM and 2 for CLM2

  • order: 4 byte integer, set to 1 (cellyear)

  • firstyear: 4 byte integer, first year of data record

  • nyear: 4 byte integer, total number of years

  • firstcell: 4 byte integer

  • ncell: 4 byte integer, number of grid cells represented

  • nbands: 4 byte integer

  • cellsize: 4 byte float

  • scalar: 4 byte float

    • 0.001 for landuse input file because area shares are be multiplied with 1000

    • 0.01 for grid input file because coordinates are multiplied with 100

    • 0.1 for temperature input file because temperatures are multiplied with 10

Some header names for CLM

  • LPJCLIMATE (10 bytes) for temperature,precipitation,wet days and cloudiness

  • LPJGRID (7 bytes) for coordinate data

  • LPJLANDUSE (10 bytes) for landuse data

  • LPJ_COUNTRY (11 bytes) for country codes

Some header names for CLM2

  • LPJCLIM (7 bytes) for temperature, precipitation, wet days, cloudiness

  • LPJLUSE (7 bytes) for landuse input file

  • LPJSOWD (7 bytes) for sowing date input file

  • LPJGRID (7 bytes) for coordinates input file

  • for more header names see header.h

For writing the header with R you can use this function:

fwriteheader <- function(file.out,headername,version,bands,firstyear,nyears,ncells,scalar){
  writeChar(headername,file.out,eos=NULL)
  writeBin(as.integer(version),file.out,size=4,endian=.Platform$endian)   # CLIMATE VERSION
  writeBin(as.integer(1),file.out,size=4,endian=.Platform$endian)     # ORDER
  writeBin(as.integer(firstyear),file.out,size=4,endian=.Platform$endian) # FIRSTYEAR
  writeBin(as.integer(nyears),file.out,size=4,endian=.Platform$endian)    # NYEAR
  writeBin(as.integer(0),file.out,size=4,endian=.Platform$endian)     # FIRSTCELL
  writeBin(as.integer(ncells),file.out,size=4,endian=.Platform$endian)    # NCELL
  writeBin(as.integer(bands),file.out,size=4,endian=.Platform$endian)     # NBAND
  writeBin(0.5,file.out,size=4,endian=.Platform$endian)               # CELLSIZE
  writeBin(scalar,file.out,size=4,endian=.Platform$endian)            # SCALAR
}