Getting started

Running the raw weathergen

After successfully installing the FORTRAN files, you can run the weather generator by typing:

./weathergen <input-file.csv> <output-file.csv>

<input-file.csv> thereby is a csv-file (with header) containing the columns (the order is important!)

station id (character string of 11 characters)
a unique identifier of the weather station
lon (float)
the longitude of the weather station (only necessary if the use_geohash namelist parameter in the main namelist of weathergen.nml is True (the default))
lat (float)
the latitude of the weather station (see lon)
year (int)
the year of the month
month (int)
the month
min. temperature (float)
the minimum temperature degrees Celsius
max. temperature (float)
the maximum temperature in degrees Celsius
cloud fraction (float)
the mean cloud fraction during the month between 0 and 1
wind speed (float)
the mean wind speed during the month in m/s
precipitation (float)
the total precipitation in the month in mm/day
wet (int)
the number of wet days in the month

The output file will contain the same columns except lon and lat but with an additional day column

Using the python package

The GWGEN package uses the model-organization framework and thus can be used from the command line. The corresponding subclass of the model_organization.ModelOrganizer is the gwgen.main.GWGENOrganizer class.

After having installed the full python package you can setup a new project with the gwgen setup command via

In [1]: !gwgen setup . -p my_first_project
INFO:gwgen.my_first_project:Initializing project my_first_project

This will copy the fortran source files into my_first_project/src where you can then modify them according to your needs. To compile the model, use the gwgen compile command:

In [2]: !gwgen compile -p my_first_project
INFO:gwgen.my_first_project:Compiling my_first_project
make: Entering directory `/home/travis/build/ARVE-Research/gwgen/docs/my_first_project/bin'
gfortran  -c -o parametersmod.o  parametersmod.f90
gfortran  -c -o geohashmod.o  geohashmod.f90
gfortran  -c -o randomdistmod.o  randomdistmod.f90
gfortran  -c -o weathergenmod.o  weathergenmod.f90
gfortran  -c -o csv_file.o  csv_file.f90
gfortran  -c -o main.o  main.f90
gfortran -o weathergen parametersmod.o geohashmod.o randomdistmod.o weathergenmod.o csv_file.o main.o  
make: Leaving directory `/home/travis/build/ARVE-Research/gwgen/docs/my_first_project/bin'

Note that you can also omit the -p option. If you do so, it uses the last created project.

To create a new experiment inside the project, use the gwgen init command:

In [3]: !gwgen -id my_first_experiment init -p my_first_project
INFO:gwgen.my_first_experiment:Initializing experiment my_first_experiment of project my_first_project

To run the weather generator, use the gwgen run command

In [4]: !gwgen -id my_first_experiment run -i <input-file.csv>

(see Running the raw weathergen for the format of <input-file.csv>). Note that you can as well omit the -id option. By doing so, it uses the last created experiment.

Parameterizing an experiment

The default parameterization of the weather generator uses about 8500 stations world wide from the [GHCN] database and 8500 stations from the [EECRA] database. I you however want to have your own parameterization, you can use the gwgen param command.

The parameterization is split up into tasks, where each task processed a given set of GHCN or EECRA stations. Each task that is used for the parameterization, requires some intermediate tasks. For example, the prcp task that determines the relationship between mean precipitation, number of wet days and the precipitation distribution parameters, requires the reading and downloading of the daily GHCN data, the calculation of the monthly averages and the extraction of the complete months. However, these dependencies are specified in the corresponding Parameterizer subclass (e.g. gwgen.parameterization.PrcpDistParams) and the only question you have to take care about is: What stations do you want to use for the parameterization? You can use the climap to select the stations you need for your region but note that you should have as many weather stations as you can.

For our demonstration, we only use two weather stations from Hamburg:

GM000010147
Hamburg-Fuhlsbüttel
GM000003865
Hamburg-Bergedorf

and save the corresponding IDs in a file

In [5]: with open('hamburg_stations.dat', 'w') as f:
   ...:     f.write('GM000010147\n')
   ...:     f.write('GM000003865')
   ...: 

then, we use the day task to download the necessary data files and run our prcp parameterization:

In [6]: !gwgen param -s hamburg_stations.dat day --download single prcp
INFO:gwgen.my_first_experiment:Parameterizing experiment my_first_experiment of project my_first_project
INFO:gwgen.utils:Downloading ftp://ftp.ncdc.noaa.gov/pub/data/ghcn/daily/all/GM000010147.dly to /home/travis/build/ARVE-Research/gwgen/docs/my_first_project/data/ghcn/ghcnd_all/GM000010147.dly
Traceback (most recent call last):
  File "/home/travis/miniconda/envs/test/bin/gwgen", line 10, in <module>
    sys.exit(main())
  File "/home/travis/miniconda/envs/test/lib/python2.7/site-packages/gwgen/main.py", line 1704, in main
    GWGENOrganizer.main(args)
  File "/home/travis/miniconda/envs/test/lib/python2.7/site-packages/model_organization/__init__.py", line 153, in main
    organizer.parse_args(args)
  File "/home/travis/miniconda/envs/test/lib/python2.7/site-packages/model_organization/__init__.py", line 1580, in parse_args
    ret = self.start(**vars(self.parser.parse_args(args)))
  File "/home/travis/miniconda/envs/test/lib/python2.7/site-packages/model_organization/__init__.py", line 184, in start
    ret[cmd] = func(**kws)
  File "/home/travis/miniconda/envs/test/lib/python2.7/site-packages/gwgen/main.py", line 647, in param
    parameterizer_kws)
  File "/home/travis/miniconda/envs/test/lib/python2.7/site-packages/gwgen/main.py", line 1571, in _setup_manager
    manager.initialize_tasks(stations, task_kws=base_kws)
  File "/home/travis/miniconda/envs/test/lib/python2.7/site-packages/gwgen/utils.py", line 1526, in initialize_tasks
    instance.init_task()
  File "/home/travis/miniconda/envs/test/lib/python2.7/site-packages/gwgen/utils.py", line 1012, in init_task
    return self._setup_or_init('init')
  File "/home/travis/miniconda/envs/test/lib/python2.7/site-packages/gwgen/utils.py", line 1004, in _setup_or_init
    ret = getattr(self, method + '_from_' + setup_from)()
  File "/home/travis/miniconda/envs/test/lib/python2.7/site-packages/gwgen/parameterization.py", line 210, in init_from_scratch
    self.http_single.format(osp.basename(f)), f)
  File "/home/travis/miniconda/envs/test/lib/python2.7/site-packages/gwgen/utils.py", line 57, in download_file
    ret = urllib.urlretrieve(url, target)[0]
  File "/home/travis/miniconda/envs/test/lib/python2.7/urllib.py", line 98, in urlretrieve
    return opener.retrieve(url, filename, reporthook, data)
  File "/home/travis/miniconda/envs/test/lib/python2.7/urllib.py", line 245, in retrieve
    fp = self.open(url, data)
  File "/home/travis/miniconda/envs/test/lib/python2.7/urllib.py", line 213, in open
    return getattr(self, name)(url)
  File "/home/travis/miniconda/envs/test/lib/python2.7/urllib.py", line 558, in open_ftp
    (fp, retrlen) = self.ftpcache[key].retrfile(file, type)
  File "/home/travis/miniconda/envs/test/lib/python2.7/urllib.py", line 906, in retrfile
    conn, retrlen = self.ftp.ntransfercmd(cmd)
  File "/home/travis/miniconda/envs/test/lib/python2.7/ftplib.py", line 337, in ntransfercmd
    conn = socket.create_connection((host, port), self.timeout)
  File "/home/travis/miniconda/envs/test/lib/python2.7/socket.py", line 575, in create_connection
    raise err
IOError: [Errno ftp error] [Errno 110] Connection timed out

This then also creates a plot that shows the relation ship between the mean precipitation on wet days (as it is calculated in the weather generator) and the gamma shape parameter

Relation ship of mean precipitation and gamma shape parameter

This procedure now modified the namelist of our experiment and added two namelist parameters

In [7]: !gwgen get-value namelist
Traceback (most recent call last):
  File "/home/travis/miniconda/envs/test/bin/gwgen", line 10, in <module>
    sys.exit(main())
  File "/home/travis/miniconda/envs/test/lib/python2.7/site-packages/gwgen/main.py", line 1704, in main
    GWGENOrganizer.main(args)
  File "/home/travis/miniconda/envs/test/lib/python2.7/site-packages/model_organization/__init__.py", line 153, in main
    organizer.parse_args(args)
  File "/home/travis/miniconda/envs/test/lib/python2.7/site-packages/model_organization/__init__.py", line 1580, in parse_args
    ret = self.start(**vars(self.parser.parse_args(args)))
  File "/home/travis/miniconda/envs/test/lib/python2.7/site-packages/model_organization/__init__.py", line 184, in start
    ret[cmd] = func(**kws)
  File "/home/travis/miniconda/envs/test/lib/python2.7/site-packages/model_organization/__init__.py", line 1198, in get_value
    ret[i] = sub_config[key]
KeyError: u'namelist'

Otherwise you can of course always modify the namelist of your experiment using the set-value and del-value commands or by modifying the configuration file (gwgen info -ep) by hand

Note

Since we use the psyplot package, you can also easily change the above created plot after the parameterization. For example, to change the number of bins in the density plot of the above plot, just load the created psyplot project and update the plot:

In [8]: import psyplot.project as psy

In [9]: p = psy.Project.load_project(
   ...:     'my_first_project/experiments/my_first_experiment/'
   ...:     'parameterization/prcp.pkl')
   ...: 

In [10]: p.update(bins=20)