[d69b677] | 1 | #!/usr/bin/env python |
---|
[991df6a] | 2 | # -*- coding: utf-8 -*- |
---|
| 3 | #************************************************************************ |
---|
[ff99eae] | 4 | # ToDo AP |
---|
[991df6a] | 5 | # - documentation der Funktionen |
---|
| 6 | # - docu der progam functionality |
---|
| 7 | # - apply pep8 |
---|
| 8 | #************************************************************************ |
---|
| 9 | #******************************************************************************* |
---|
| 10 | # @Author: Leopold Haimberger (University of Vienna) |
---|
[64cf353] | 11 | # |
---|
[991df6a] | 12 | # @Date: November 2015 |
---|
[64cf353] | 13 | # |
---|
[991df6a] | 14 | # @Change History: |
---|
| 15 | # |
---|
| 16 | # February 2018 - Anne Philipp (University of Vienna): |
---|
| 17 | # - applied PEP8 style guide |
---|
| 18 | # - added documentation |
---|
| 19 | # - created function main and moved the two function calls for |
---|
| 20 | # arguments and plotting into it |
---|
[ff99eae] | 21 | # - added function get_basics to extract the boundary conditions |
---|
[ccab809] | 22 | # of the data fields from the first grib file it gets. |
---|
[991df6a] | 23 | # |
---|
| 24 | # @License: |
---|
| 25 | # (C) Copyright 2015-2018. |
---|
| 26 | # |
---|
| 27 | # This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
| 28 | # which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
| 29 | # |
---|
| 30 | # @Program Functionality: |
---|
| 31 | # Simple tool for creating maps and time series of retrieved fields. |
---|
| 32 | # |
---|
| 33 | # @Program Content: |
---|
[ccab809] | 34 | # - main |
---|
[ff99eae] | 35 | # - get_basics |
---|
| 36 | # - plot_retrieved |
---|
| 37 | # - plot_timeseries |
---|
| 38 | # - plot_map |
---|
| 39 | # - get_plot_args |
---|
[991df6a] | 40 | # |
---|
| 41 | #******************************************************************************* |
---|
[d69b677] | 42 | |
---|
[991df6a] | 43 | # ------------------------------------------------------------------------------ |
---|
| 44 | # MODULES |
---|
| 45 | # ------------------------------------------------------------------------------ |
---|
[d69b677] | 46 | import time |
---|
[ccab809] | 47 | import datetime |
---|
[991df6a] | 48 | import os |
---|
| 49 | import inspect |
---|
| 50 | import sys |
---|
| 51 | from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter |
---|
[ff99eae] | 52 | import matplotlib |
---|
| 53 | import matplotlib.pyplot as plt |
---|
| 54 | from mpl_toolkits.basemap import Basemap |
---|
| 55 | from eccodes import codes_grib_new_from_file, codes_get, codes_release, \ |
---|
| 56 | codes_get_values |
---|
[ccab809] | 57 | import numpy as np |
---|
[d69b677] | 58 | |
---|
[991df6a] | 59 | # software specific classes and modules from flex_extract |
---|
| 60 | from ControlFile import ControlFile |
---|
[ff99eae] | 61 | from UioFiles import UioFiles |
---|
| 62 | |
---|
| 63 | # add path to pythonpath so that python finds its buddies |
---|
| 64 | LOCAL_PYTHON_PATH = os.path.dirname(os.path.abspath( |
---|
| 65 | inspect.getfile(inspect.currentframe()))) |
---|
| 66 | if LOCAL_PYTHON_PATH not in sys.path: |
---|
| 67 | sys.path.append(LOCAL_PYTHON_PATH) |
---|
[64cf353] | 68 | |
---|
[ff99eae] | 69 | font = {'family': 'monospace', 'size': 12} |
---|
| 70 | matplotlib.rcParams['xtick.major.pad'] = '20' |
---|
| 71 | matplotlib.rc('font', **font) |
---|
[991df6a] | 72 | # ------------------------------------------------------------------------------ |
---|
| 73 | # FUNCTION |
---|
| 74 | # ------------------------------------------------------------------------------ |
---|
| 75 | def main(): |
---|
| 76 | ''' |
---|
| 77 | @Description: |
---|
[ff99eae] | 78 | If plot_retrieved is called from command line, this function controls |
---|
[991df6a] | 79 | the program flow and calls the argumentparser function and |
---|
[ff99eae] | 80 | the plot_retrieved function for plotting the retrieved GRIB data. |
---|
[991df6a] | 81 | |
---|
| 82 | @Input: |
---|
| 83 | <nothing> |
---|
| 84 | |
---|
| 85 | @Return: |
---|
| 86 | <nothing> |
---|
| 87 | ''' |
---|
[ff99eae] | 88 | args, c = get_plot_args() |
---|
| 89 | plot_retrieved(c) |
---|
[991df6a] | 90 | |
---|
| 91 | return |
---|
| 92 | |
---|
[ff99eae] | 93 | def get_basics(ifile, verb=False): |
---|
[ccab809] | 94 | """ |
---|
| 95 | @Description: |
---|
| 96 | An example grib file will be opened and basic information will |
---|
| 97 | be extracted. These information are important for later use and the |
---|
| 98 | initialization of numpy arrays for data storing. |
---|
| 99 | |
---|
| 100 | @Input: |
---|
| 101 | ifile: string |
---|
| 102 | Contains the full absolute path to the ECMWF grib file. |
---|
[ff99eae] | 103 | |
---|
[ccab809] | 104 | verb (opt): bool |
---|
| 105 | Is True if there should be extra output in verbose mode. |
---|
| 106 | Default value is False. |
---|
| 107 | |
---|
| 108 | @Return: |
---|
| 109 | data: dict |
---|
| 110 | Contains basic informations of the ECMWF grib files, e.g. |
---|
| 111 | 'Ni', 'Nj', 'latitudeOfFirstGridPointInDegrees', |
---|
| 112 | 'longitudeOfFirstGridPointInDegrees', |
---|
| 113 | 'latitudeOfLastGridPointInDegrees', |
---|
| 114 | 'longitudeOfLastGridPointInDegrees', |
---|
| 115 | 'jDirectionIncrementInDegrees', |
---|
| 116 | 'iDirectionIncrementInDegrees' |
---|
| 117 | """ |
---|
| 118 | |
---|
| 119 | data = {} |
---|
| 120 | |
---|
| 121 | # --- open file --- |
---|
| 122 | print("Opening file for getting information data --- %s" % |
---|
| 123 | os.path.basename(ifile)) |
---|
| 124 | |
---|
| 125 | with open(ifile) as f: |
---|
| 126 | |
---|
| 127 | # load first message from file |
---|
| 128 | gid = codes_grib_new_from_file(f) |
---|
| 129 | |
---|
| 130 | # information needed from grib message |
---|
[ff99eae] | 131 | keys = ['Ni', |
---|
[ccab809] | 132 | 'Nj', |
---|
| 133 | 'latitudeOfFirstGridPointInDegrees', |
---|
| 134 | 'longitudeOfFirstGridPointInDegrees', |
---|
| 135 | 'latitudeOfLastGridPointInDegrees', |
---|
| 136 | 'longitudeOfLastGridPointInDegrees', |
---|
| 137 | 'jDirectionIncrementInDegrees', |
---|
[ff99eae] | 138 | 'iDirectionIncrementInDegrees'] |
---|
[ccab809] | 139 | |
---|
[ff99eae] | 140 | if verb: |
---|
| 141 | print '\nInformations are: ' |
---|
[ccab809] | 142 | for key in keys: |
---|
| 143 | # Get the value of the key in a grib message. |
---|
[ff99eae] | 144 | data[key] = codes_get(gid, key) |
---|
| 145 | if verb: |
---|
| 146 | print "%s = %s" % (key, data[key]) |
---|
| 147 | if verb: |
---|
| 148 | print '\n' |
---|
[ccab809] | 149 | |
---|
| 150 | # Free the memory for the message referred as gribid. |
---|
| 151 | codes_release(gid) |
---|
| 152 | |
---|
| 153 | return data |
---|
| 154 | |
---|
[ff99eae] | 155 | def get_files_per_date(files, datelist): |
---|
[ccab809] | 156 | ''' |
---|
| 157 | @Description: |
---|
| 158 | The filenames contain dates which are used to select a list |
---|
| 159 | of files for a specific time period specified in datelist. |
---|
| 160 | |
---|
| 161 | @Input: |
---|
[ff99eae] | 162 | files: instance of UioFiles |
---|
[ccab809] | 163 | For description see class documentation. |
---|
| 164 | It contains the attribute "files" which is a list of pathes |
---|
| 165 | to filenames. |
---|
| 166 | |
---|
| 167 | datelist: list of datetimes |
---|
| 168 | Contains the list of dates which should be processed for plotting. |
---|
| 169 | |
---|
| 170 | @Return: |
---|
| 171 | filelist: list of strings |
---|
| 172 | Contains the selected files for the time period. |
---|
| 173 | ''' |
---|
| 174 | |
---|
| 175 | filelist = [] |
---|
[ff99eae] | 176 | for filename in files: |
---|
| 177 | filedate = filename[-8:] |
---|
| 178 | ddate = datetime.datetime.strptime(filedate, '%y%m%d%H') |
---|
[ccab809] | 179 | if ddate in datelist: |
---|
[ff99eae] | 180 | filelist.append(filename) |
---|
[ccab809] | 181 | |
---|
| 182 | return filelist |
---|
| 183 | |
---|
[ff99eae] | 184 | def plot_retrieved(c): |
---|
[991df6a] | 185 | ''' |
---|
| 186 | @Description: |
---|
| 187 | Reads GRIB data from a specified time period, a list of levels |
---|
| 188 | and a specified list of parameter. |
---|
| 189 | |
---|
| 190 | @Input: |
---|
| 191 | c: instance of class ControlFile |
---|
| 192 | Contains all necessary information of a CONTROL file. The parameters |
---|
| 193 | are: DAY1, DAY2, DTIME, MAXSTEP, TYPE, TIME, STEP, CLASS, STREAM, |
---|
| 194 | NUMBER, EXPVER, GRID, LEFT, LOWER, UPPER, RIGHT, LEVEL, LEVELIST, |
---|
| 195 | RESOL, GAUSS, ACCURACY, OMEGA, OMEGADIFF, ETA, ETADIFF, DPDETA, |
---|
| 196 | SMOOTH, FORMAT, ADDPAR, WRF, CWC, PREFIX, ECSTORAGE, ECTRANS, |
---|
| 197 | ECFSDIR, MAILOPS, MAILFAIL, GRIB2FLEXPART, DEBUG, INPUTDIR, |
---|
| 198 | OUTPUTDIR, FLEXPART_ROOT_SCRIPTS |
---|
| 199 | For more information about format and content of the parameter see |
---|
| 200 | documentation. |
---|
| 201 | |
---|
| 202 | @Return: |
---|
| 203 | <nothing> |
---|
| 204 | ''' |
---|
| 205 | start = datetime.datetime.strptime(c.start_date, '%Y%m%d%H') |
---|
| 206 | end = datetime.datetime.strptime(c.end_date, '%Y%m%d%H') |
---|
| 207 | |
---|
[ccab809] | 208 | # create datelist between start and end date |
---|
| 209 | datelist = [start] # initialise datelist with first date |
---|
| 210 | run_date = start |
---|
| 211 | while run_date < end: |
---|
| 212 | run_date += datetime.timedelta(hours=int(c.dtime)) |
---|
| 213 | datelist.append(run_date) |
---|
| 214 | |
---|
| 215 | print 'datelist: ', datelist |
---|
| 216 | |
---|
[ff99eae] | 217 | c.paramIds = np.asarray(c.paramIds, dtype='int') |
---|
| 218 | c.levels = np.asarray(c.levels, dtype='int') |
---|
| 219 | c.area = np.asarray(c.area) |
---|
[ccab809] | 220 | |
---|
[ff99eae] | 221 | files = UioFiles(c.prefix+'*') |
---|
| 222 | files.list_files(c.inputdir) |
---|
| 223 | ifiles = get_files_per_date(files.files, datelist) |
---|
[ccab809] | 224 | ifiles.sort() |
---|
| 225 | |
---|
[ff99eae] | 226 | gdict = get_basics(ifiles[0], verb=False) |
---|
[64cf353] | 227 | |
---|
[991df6a] | 228 | fdict = dict() |
---|
| 229 | fmeta = dict() |
---|
| 230 | fstamp = dict() |
---|
[d69b677] | 231 | for p in c.paramIds: |
---|
| 232 | for l in c.levels: |
---|
[991df6a] | 233 | key = '{:0>3}_{:0>3}'.format(p, l) |
---|
| 234 | fdict[key] = [] |
---|
| 235 | fmeta[key] = [] |
---|
| 236 | fstamp[key] = [] |
---|
[d69b677] | 237 | |
---|
[ff99eae] | 238 | for filename in ifiles: |
---|
| 239 | f = open(filename) |
---|
| 240 | print "Opening file for reading data --- %s" % filename |
---|
| 241 | fdate = datetime.datetime.strptime(filename[-8:], "%y%m%d%H") |
---|
[d69b677] | 242 | |
---|
[ccab809] | 243 | # Load in memory a grib message from a file. |
---|
| 244 | gid = codes_grib_new_from_file(f) |
---|
[ff99eae] | 245 | while gid is not None: |
---|
[ccab809] | 246 | gtype = codes_get(gid, 'type') |
---|
| 247 | paramId = codes_get(gid, 'paramId') |
---|
| 248 | parameterName = codes_get(gid, 'parameterName') |
---|
| 249 | level = codes_get(gid, 'level') |
---|
| 250 | |
---|
| 251 | if paramId in c.paramIds and level in c.levels: |
---|
[991df6a] | 252 | key = '{:0>3}_{:0>3}'.format(paramId, level) |
---|
[ccab809] | 253 | print 'key: ', key |
---|
[ff99eae] | 254 | if fstamp[key]: |
---|
[d69b677] | 255 | for i in range(len(fstamp[key])): |
---|
[ccab809] | 256 | if fdate < fstamp[key][i]: |
---|
| 257 | fstamp[key].insert(i, fdate) |
---|
| 258 | fmeta[key].insert(i, [paramId, parameterName, gtype, |
---|
[ff99eae] | 259 | fdate, level]) |
---|
| 260 | fdict[key].insert(i, np.flipud(np.reshape( |
---|
| 261 | codes_get_values(gid), |
---|
| 262 | [gdict['Nj'], gdict['Ni']]))) |
---|
[d69b677] | 263 | break |
---|
[ccab809] | 264 | elif fdate > fstamp[key][i] and i == len(fstamp[key])-1: |
---|
| 265 | fstamp[key].append(fdate) |
---|
| 266 | fmeta[key].append([paramId, parameterName, gtype, |
---|
[ff99eae] | 267 | fdate, level]) |
---|
| 268 | fdict[key].append(np.flipud(np.reshape( |
---|
| 269 | codes_get_values(gid), |
---|
| 270 | [gdict['Nj'], gdict['Ni']]))) |
---|
[ccab809] | 271 | break |
---|
| 272 | elif fdate > fstamp[key][i] and i != len(fstamp[key])-1 \ |
---|
| 273 | and fdate < fstamp[key][i+1]: |
---|
| 274 | fstamp[key].insert(i, fdate) |
---|
| 275 | fmeta[key].insert(i, [paramId, parameterName, gtype, |
---|
[ff99eae] | 276 | fdate, level]) |
---|
| 277 | fdict[key].insert(i, np.flipud(np.reshape( |
---|
| 278 | codes_get_values(gid), |
---|
| 279 | [gdict['Nj'], gdict['Ni']]))) |
---|
[ccab809] | 280 | break |
---|
| 281 | else: |
---|
| 282 | pass |
---|
| 283 | else: |
---|
| 284 | fstamp[key].append(fdate) |
---|
| 285 | fmeta[key].append((paramId, parameterName, gtype, |
---|
| 286 | fdate, level)) |
---|
[ff99eae] | 287 | fdict[key].append(np.flipud(np.reshape( |
---|
[ccab809] | 288 | codes_get_values(gid), [gdict['Nj'], gdict['Ni']]))) |
---|
| 289 | |
---|
| 290 | codes_release(gid) |
---|
[d69b677] | 291 | |
---|
[ccab809] | 292 | # Load in memory a grib message from a file. |
---|
| 293 | gid = codes_grib_new_from_file(f) |
---|
| 294 | |
---|
| 295 | f.close() |
---|
[64cf353] | 296 | |
---|
[ff99eae] | 297 | for k in fdict.iterkeys(): |
---|
| 298 | print 'fmeta: ', len(fmeta), fmeta |
---|
[991df6a] | 299 | fml = fmeta[k] |
---|
| 300 | fdl = fdict[k] |
---|
[ccab809] | 301 | print 'fm1: ', len(fml), fml |
---|
[991df6a] | 302 | for fd, fm in zip(fdl, fml): |
---|
[ccab809] | 303 | print fm |
---|
[991df6a] | 304 | ftitle = fm[1] + ' {} '.format(fm[-1]) + \ |
---|
[ff99eae] | 305 | datetime.datetime.strftime(fm[3], '%Y%m%d%H') |
---|
[991df6a] | 306 | pname = '_'.join(fm[1].split()) + '_{}_'.format(fm[-1]) + \ |
---|
[ccab809] | 307 | datetime.datetime.strftime(fm[3], '%Y%m%d%H') |
---|
[ff99eae] | 308 | plot_map(c, fd, fm, gdict, ftitle, pname, 'png') |
---|
[64cf353] | 309 | |
---|
[ff99eae] | 310 | for k in fdict.iterkeys(): |
---|
[991df6a] | 311 | fml = fmeta[k] |
---|
| 312 | fdl = fdict[k] |
---|
| 313 | fsl = fstamp[k] |
---|
[d69b677] | 314 | if fdl: |
---|
[991df6a] | 315 | fm = fml[0] |
---|
| 316 | fd = fdl[0] |
---|
| 317 | ftitle = fm[1] + ' {} '.format(fm[-1]) + \ |
---|
[ff99eae] | 318 | datetime.datetime.strftime(fm[3], '%Y%m%d%H') |
---|
[991df6a] | 319 | pname = '_'.join(fm[1].split()) + '_{}_'.format(fm[-1]) + \ |
---|
[ccab809] | 320 | datetime.datetime.strftime(fm[3], '%Y%m%d%H') |
---|
[ff99eae] | 321 | lat = -20. |
---|
| 322 | lon = 20. |
---|
| 323 | plot_timeseries(c, fdl, fml, fsl, lat, lon, gdict, |
---|
| 324 | ftitle, pname, 'png') |
---|
[991df6a] | 325 | |
---|
| 326 | return |
---|
| 327 | |
---|
[ff99eae] | 328 | def plot_timeseries(c, flist, fmetalist, ftimestamps, lat, lon, |
---|
| 329 | gdict, ftitle, filename, fending, show=False): |
---|
[991df6a] | 330 | ''' |
---|
| 331 | @Description: |
---|
[ff99eae] | 332 | Creates a timeseries plot for a given lat/lon position. |
---|
[991df6a] | 333 | |
---|
| 334 | @Input: |
---|
[ff99eae] | 335 | c: instance of class ControlFile |
---|
| 336 | Contains all necessary information of a CONTROL file. The parameters |
---|
| 337 | are: DAY1, DAY2, DTIME, MAXSTEP, TYPE, TIME, STEP, CLASS, STREAM, |
---|
| 338 | NUMBER, EXPVER, GRID, LEFT, LOWER, UPPER, RIGHT, LEVEL, LEVELIST, |
---|
| 339 | RESOL, GAUSS, ACCURACY, OMEGA, OMEGADIFF, ETA, ETADIFF, DPDETA, |
---|
| 340 | SMOOTH, FORMAT, ADDPAR, WRF, CWC, PREFIX, ECSTORAGE, ECTRANS, |
---|
| 341 | ECFSDIR, MAILOPS, MAILFAIL, GRIB2FLEXPART, DEBUG, INPUTDIR, |
---|
| 342 | OUTPUTDIR, FLEXPART_ROOT_SCRIPTS |
---|
| 343 | For more information about format and content of the parameter see |
---|
| 344 | documentation. |
---|
[ccab809] | 345 | |
---|
[ff99eae] | 346 | flist: numpy array, 2d |
---|
[991df6a] | 347 | The actual data values to be plotted from the grib messages. |
---|
| 348 | |
---|
| 349 | fmetalist: list of strings |
---|
| 350 | Contains some meta date for the data field to be plotted: |
---|
[ccab809] | 351 | parameter id, parameter Name, grid type, datetime, level |
---|
[991df6a] | 352 | |
---|
| 353 | ftimestamps: list of datetime |
---|
[ccab809] | 354 | Contains the time stamps. |
---|
[991df6a] | 355 | |
---|
[ff99eae] | 356 | lat: float |
---|
| 357 | The latitude for which the timeseries should be plotted. |
---|
[991df6a] | 358 | |
---|
[ff99eae] | 359 | lon: float |
---|
| 360 | The longitude for which the timeseries should be plotted. |
---|
[991df6a] | 361 | |
---|
[ff99eae] | 362 | gdict: dict |
---|
| 363 | Contains basic informations of the ECMWF grib files, e.g. |
---|
| 364 | 'Ni', 'Nj', 'latitudeOfFirstGridPointInDegrees', |
---|
| 365 | 'longitudeOfFirstGridPointInDegrees', |
---|
| 366 | 'latitudeOfLastGridPointInDegrees', |
---|
| 367 | 'longitudeOfLastGridPointInDegrees', |
---|
| 368 | 'jDirectionIncrementInDegrees', |
---|
| 369 | 'iDirectionIncrementInDegrees' |
---|
[991df6a] | 370 | |
---|
| 371 | ftitle: string |
---|
| 372 | The title of the timeseries. |
---|
| 373 | |
---|
| 374 | filename: string |
---|
| 375 | The time series is stored in a file with this name. |
---|
| 376 | |
---|
[ccab809] | 377 | fending: string |
---|
| 378 | Contains the type of plot, e.g. pdf or png |
---|
| 379 | |
---|
| 380 | show: boolean |
---|
| 381 | Decides if the plot is shown after plotting or hidden. |
---|
| 382 | |
---|
[991df6a] | 383 | @Return: |
---|
| 384 | <nothing> |
---|
| 385 | ''' |
---|
[ccab809] | 386 | print 'plotting timeseries' |
---|
| 387 | |
---|
[991df6a] | 388 | t1 = time.time() |
---|
[ccab809] | 389 | |
---|
[ff99eae] | 390 | #llx = gdict['longitudeOfFirstGridPointInDegrees'] |
---|
| 391 | #if llx > 180. : |
---|
| 392 | # llx -= 360. |
---|
| 393 | #lly = gdict['latitudeOfLastGridPointInDegrees'] |
---|
| 394 | #dxout = gdict['iDirectionIncrementInDegrees'] |
---|
| 395 | #dyout = gdict['jDirectionIncrementInDegrees'] |
---|
| 396 | #urx = gdict['longitudeOfLastGridPointInDegrees'] |
---|
| 397 | #ury = gdict['latitudeOfFirstGridPointInDegrees'] |
---|
| 398 | #numxgrid = gdict['Ni'] |
---|
| 399 | #numygrid = gdict['Nj'] |
---|
| 400 | |
---|
| 401 | farr = np.asarray(flist) |
---|
[ccab809] | 402 | #(time, lat, lon) |
---|
| 403 | |
---|
[ff99eae] | 404 | #lonindex = linspace(llx, urx, numxgrid) |
---|
| 405 | #latindex = linspace(lly, ury, numygrid) |
---|
[ccab809] | 406 | |
---|
[ff99eae] | 407 | ts = farr[:, 0, 0] |
---|
[ccab809] | 408 | |
---|
| 409 | fig = plt.figure(figsize=(12, 6.7)) |
---|
| 410 | |
---|
[991df6a] | 411 | plt.plot(ftimestamps, ts) |
---|
[d69b677] | 412 | plt.title(ftitle) |
---|
[ccab809] | 413 | |
---|
[ff99eae] | 414 | plt.savefig(c.outputdir + '/' + filename + '_TS.' + fending, |
---|
| 415 | facecolor=fig.get_facecolor(), |
---|
| 416 | edgecolor='none', |
---|
| 417 | format=fending) |
---|
[991df6a] | 418 | print 'created ', c.outputdir + '/' + filename |
---|
[ff99eae] | 419 | if show: |
---|
[ccab809] | 420 | plt.show() |
---|
| 421 | fig.clf() |
---|
| 422 | plt.close(fig) |
---|
| 423 | |
---|
[991df6a] | 424 | print time.time() - t1, 's' |
---|
| 425 | |
---|
| 426 | return |
---|
| 427 | |
---|
[ff99eae] | 428 | def plot_map(c, flist, fmetalist, gdict, ftitle, filename, fending, show=False): |
---|
[991df6a] | 429 | ''' |
---|
| 430 | @Description: |
---|
[ff99eae] | 431 | Creates a basemap plot with imshow for a given data field. |
---|
[991df6a] | 432 | |
---|
| 433 | @Input: |
---|
[ccab809] | 434 | c: instance of class ControlFile |
---|
| 435 | Contains all necessary information of a CONTROL file. The parameters |
---|
| 436 | are: DAY1, DAY2, DTIME, MAXSTEP, TYPE, TIME, STEP, CLASS, STREAM, |
---|
| 437 | NUMBER, EXPVER, GRID, LEFT, LOWER, UPPER, RIGHT, LEVEL, LEVELIST, |
---|
| 438 | RESOL, GAUSS, ACCURACY, OMEGA, OMEGADIFF, ETA, ETADIFF, DPDETA, |
---|
| 439 | SMOOTH, FORMAT, ADDPAR, WRF, CWC, PREFIX, ECSTORAGE, ECTRANS, |
---|
| 440 | ECFSDIR, MAILOPS, MAILFAIL, GRIB2FLEXPART, DEBUG, INPUTDIR, |
---|
| 441 | OUTPUTDIR, FLEXPART_ROOT_SCRIPTS |
---|
| 442 | For more information about format and content of the parameter see |
---|
| 443 | documentation. |
---|
| 444 | |
---|
[ff99eae] | 445 | flist: numpy array, 2d |
---|
| 446 | The actual data values to be plotted from the grib messages. |
---|
[ccab809] | 447 | |
---|
[ff99eae] | 448 | fmetalist: list of strings |
---|
| 449 | Contains some meta date for the data field to be plotted: |
---|
| 450 | parameter id, parameter Name, grid type, datetime, level |
---|
[ccab809] | 451 | |
---|
| 452 | gdict: dict |
---|
| 453 | Contains basic informations of the ECMWF grib files, e.g. |
---|
| 454 | 'Ni', 'Nj', 'latitudeOfFirstGridPointInDegrees', |
---|
| 455 | 'longitudeOfFirstGridPointInDegrees', |
---|
| 456 | 'latitudeOfLastGridPointInDegrees', |
---|
| 457 | 'longitudeOfLastGridPointInDegrees', |
---|
| 458 | 'jDirectionIncrementInDegrees', |
---|
| 459 | 'iDirectionIncrementInDegrees' |
---|
| 460 | |
---|
| 461 | ftitle: string |
---|
| 462 | The titel of the plot. |
---|
| 463 | |
---|
| 464 | filename: string |
---|
| 465 | The plot is stored in a file with this name. |
---|
| 466 | |
---|
| 467 | fending: string |
---|
| 468 | Contains the type of plot, e.g. pdf or png |
---|
| 469 | |
---|
| 470 | show: boolean |
---|
| 471 | Decides if the plot is shown after plotting or hidden. |
---|
[991df6a] | 472 | |
---|
| 473 | @Return: |
---|
| 474 | <nothing> |
---|
| 475 | ''' |
---|
[ccab809] | 476 | print 'plotting map' |
---|
| 477 | |
---|
[991df6a] | 478 | t1 = time.time() |
---|
[ccab809] | 479 | |
---|
| 480 | fig = plt.figure(figsize=(12, 6.7)) |
---|
| 481 | #mbaxes = fig.add_axes([0.05, 0.15, 0.8, 0.7]) |
---|
| 482 | |
---|
| 483 | llx = gdict['longitudeOfFirstGridPointInDegrees'] #- 360 |
---|
[ff99eae] | 484 | if llx > 180.: |
---|
[ccab809] | 485 | llx -= 360. |
---|
| 486 | lly = gdict['latitudeOfLastGridPointInDegrees'] |
---|
[ff99eae] | 487 | #dxout = gdict['iDirectionIncrementInDegrees'] |
---|
| 488 | #dyout = gdict['jDirectionIncrementInDegrees'] |
---|
[ccab809] | 489 | urx = gdict['longitudeOfLastGridPointInDegrees'] |
---|
| 490 | ury = gdict['latitudeOfFirstGridPointInDegrees'] |
---|
[ff99eae] | 491 | #numxgrid = gdict['Ni'] |
---|
| 492 | #numygrid = gdict['Nj'] |
---|
[ccab809] | 493 | |
---|
| 494 | m = Basemap(projection='cyl', llcrnrlon=llx, llcrnrlat=lly, |
---|
[ff99eae] | 495 | urcrnrlon=urx, urcrnrlat=ury, resolution='i') |
---|
[ccab809] | 496 | |
---|
[ff99eae] | 497 | #lw = 0.5 |
---|
[d69b677] | 498 | m.drawmapboundary() |
---|
[ff99eae] | 499 | #x = linspace(llx, urx, numxgrid) |
---|
| 500 | #y = linspace(lly, ury, numygrid) |
---|
[991df6a] | 501 | |
---|
[ff99eae] | 502 | #xx, yy = m(*meshgrid(x, y)) |
---|
[64cf353] | 503 | |
---|
[ccab809] | 504 | #s = m.contourf(xx, yy, flist) |
---|
| 505 | |
---|
| 506 | s = plt.imshow(flist.T, |
---|
[ff99eae] | 507 | extent=(llx, urx, lly, ury), |
---|
| 508 | alpha=1.0, |
---|
| 509 | interpolation='nearest' |
---|
| 510 | #vmin=vn, |
---|
| 511 | #vmax=vx, |
---|
| 512 | #cmap=my_cmap, |
---|
| 513 | #levels=levels, |
---|
| 514 | #cmap=my_cmap, |
---|
| 515 | #norm=LogNorm(vn,vx) |
---|
| 516 | ) |
---|
| 517 | |
---|
| 518 | plt.title(ftitle, y=1.08) |
---|
[ccab809] | 519 | cb = m.colorbar(s, location="right", pad="10%") |
---|
[ff99eae] | 520 | cb.set_label('label', size=14) |
---|
| 521 | |
---|
| 522 | thickline = np.arange(lly, ury+1, 10.) |
---|
| 523 | thinline = np.arange(lly, ury+1, 5.) |
---|
| 524 | m.drawparallels(thickline, |
---|
| 525 | color='gray', |
---|
| 526 | dashes=[1, 1], |
---|
| 527 | linewidth=0.5, |
---|
| 528 | labels=[1, 1, 1, 1], |
---|
| 529 | xoffset=1.) |
---|
| 530 | m.drawparallels(np.setdiff1d(thinline, thickline), |
---|
| 531 | color='lightgray', |
---|
| 532 | dashes=[1, 1], |
---|
| 533 | linewidth=0.5, |
---|
| 534 | labels=[0, 0, 0, 0]) |
---|
| 535 | |
---|
| 536 | thickline = np.arange(llx, urx+1, 10.) |
---|
| 537 | thinline = np.arange(llx, urx+1, 5.) |
---|
| 538 | m.drawmeridians(thickline, |
---|
| 539 | color='gray', |
---|
| 540 | dashes=[1, 1], |
---|
| 541 | linewidth=0.5, |
---|
| 542 | labels=[1, 1, 1, 1], |
---|
| 543 | yoffset=1.) |
---|
| 544 | m.drawmeridians(np.setdiff1d(thinline, thickline), |
---|
| 545 | color='lightgray', |
---|
| 546 | dashes=[1, 1], |
---|
| 547 | linewidth=0.5, |
---|
| 548 | labels=[0, 0, 0, 0]) |
---|
[ccab809] | 549 | |
---|
| 550 | m.drawcoastlines() |
---|
| 551 | m.drawcountries() |
---|
| 552 | |
---|
[ff99eae] | 553 | plt.savefig(c.outputdir + '/' + filename + '_MAP.' + fending, |
---|
| 554 | facecolor=fig.get_facecolor(), |
---|
| 555 | edgecolor='none', |
---|
| 556 | format=fending) |
---|
[991df6a] | 557 | print 'created ', c.outputdir + '/' + filename |
---|
[ff99eae] | 558 | if show: |
---|
[ccab809] | 559 | plt.show() |
---|
| 560 | fig.clf() |
---|
| 561 | plt.close(fig) |
---|
| 562 | |
---|
[991df6a] | 563 | print time.time() - t1, 's' |
---|
| 564 | |
---|
| 565 | return |
---|
| 566 | |
---|
[ff99eae] | 567 | def get_plot_args(): |
---|
[991df6a] | 568 | ''' |
---|
| 569 | @Description: |
---|
| 570 | Assigns the command line arguments and reads CONTROL file |
---|
| 571 | content. Apply default values for non mentioned arguments. |
---|
| 572 | |
---|
| 573 | @Input: |
---|
| 574 | <nothing> |
---|
| 575 | |
---|
| 576 | @Return: |
---|
| 577 | args: instance of ArgumentParser |
---|
| 578 | Contains the commandline arguments from script/program call. |
---|
| 579 | |
---|
| 580 | c: instance of class ControlFile |
---|
| 581 | Contains all necessary information of a CONTROL file. The parameters |
---|
| 582 | are: DAY1, DAY2, DTIME, MAXSTEP, TYPE, TIME, STEP, CLASS, STREAM, |
---|
| 583 | NUMBER, EXPVER, GRID, LEFT, LOWER, UPPER, RIGHT, LEVEL, LEVELIST, |
---|
| 584 | RESOL, GAUSS, ACCURACY, OMEGA, OMEGADIFF, ETA, ETADIFF, DPDETA, |
---|
| 585 | SMOOTH, FORMAT, ADDPAR, WRF, CWC, PREFIX, ECSTORAGE, ECTRANS, |
---|
| 586 | ECFSDIR, MAILOPS, MAILFAIL, GRIB2FLEXPART, DEBUG, INPUTDIR, |
---|
| 587 | OUTPUTDIR, FLEXPART_ROOT_SCRIPTS |
---|
| 588 | For more information about format and content of the parameter see |
---|
| 589 | documentation. |
---|
| 590 | ''' |
---|
[ccab809] | 591 | parser = ArgumentParser(description='Plot retrieved GRIB data from ' + \ |
---|
[991df6a] | 592 | 'ECMWF MARS archive', |
---|
[d69b677] | 593 | formatter_class=ArgumentDefaultsHelpFormatter) |
---|
| 594 | |
---|
[64cf353] | 595 | # the most important arguments |
---|
[d69b677] | 596 | parser.add_argument("--start_date", dest="start_date", |
---|
| 597 | help="start date YYYYMMDD") |
---|
[ff99eae] | 598 | parser.add_argument("--end_date", dest="end_date", |
---|
| 599 | help="end_date YYYYMMDD") |
---|
[d69b677] | 600 | |
---|
| 601 | parser.add_argument("--start_step", dest="start_step", |
---|
| 602 | help="start step in hours") |
---|
[ff99eae] | 603 | parser.add_argument("--end_step", dest="end_step", |
---|
| 604 | help="end step in hours") |
---|
[d69b677] | 605 | |
---|
[991df6a] | 606 | # some arguments that override the default in the CONTROL file |
---|
| 607 | parser.add_argument("--levelist", dest="levelist", |
---|
[ccab809] | 608 | help="vertical levels to be retrieved, e.g. 30/to/60") |
---|
[991df6a] | 609 | parser.add_argument("--area", dest="area", |
---|
| 610 | help="area defined as north/west/south/east") |
---|
| 611 | parser.add_argument("--paramIds", dest="paramIds", |
---|
| 612 | help="parameter IDs") |
---|
| 613 | parser.add_argument("--prefix", dest="prefix", default='EN', |
---|
| 614 | help="output file name prefix") |
---|
[d69b677] | 615 | |
---|
| 616 | # set the working directories |
---|
[991df6a] | 617 | parser.add_argument("--inputdir", dest="inputdir", default=None, |
---|
[d69b677] | 618 | help="root directory for storing intermediate files") |
---|
[991df6a] | 619 | parser.add_argument("--outputdir", dest="outputdir", default=None, |
---|
[d69b677] | 620 | help="root directory for storing output files") |
---|
| 621 | parser.add_argument("--flexpart_root_scripts", dest="flexpart_root_scripts", |
---|
[991df6a] | 622 | help="FLEXPART root directory (to find \ |
---|
| 623 | 'grib2flexpart and COMMAND file)\n \ |
---|
| 624 | Normally ECMWFDATA resides in the scripts directory \ |
---|
| 625 | of the FLEXPART distribution") |
---|
[d69b677] | 626 | |
---|
[991df6a] | 627 | parser.add_argument("--controlfile", dest="controlfile", |
---|
[ccab809] | 628 | default='CONTROL.temp', |
---|
| 629 | help="file with CONTROL parameters") |
---|
[d69b677] | 630 | args = parser.parse_args() |
---|
| 631 | |
---|
| 632 | try: |
---|
[991df6a] | 633 | c = ControlFile(args.controlfile) |
---|
[d69b677] | 634 | except IOError: |
---|
| 635 | try: |
---|
[ff99eae] | 636 | c = ControlFile(LOCAL_PYTHON_PATH + args.controlfile) |
---|
| 637 | except IOError: |
---|
[991df6a] | 638 | print 'Could not read CONTROL file "' + args.controlfile + '"' |
---|
[d69b677] | 639 | print 'Either it does not exist or its syntax is wrong.' |
---|
[991df6a] | 640 | print 'Try "' + sys.argv[0].split('/')[-1] + \ |
---|
| 641 | ' -h" to print usage information' |
---|
[d69b677] | 642 | exit(1) |
---|
[64cf353] | 643 | |
---|
| 644 | if args.levelist: |
---|
[991df6a] | 645 | c.levels = args.levelist.split('/') |
---|
[d69b677] | 646 | else: |
---|
[991df6a] | 647 | c.levels = [0] |
---|
[ccab809] | 648 | |
---|
[d69b677] | 649 | if args.area: |
---|
[991df6a] | 650 | c.area = args.area.split('/') |
---|
[d69b677] | 651 | else: |
---|
[991df6a] | 652 | c.area = '[0,0]' |
---|
[64cf353] | 653 | |
---|
[991df6a] | 654 | c.paramIds = args.paramIds.split('/') |
---|
[ccab809] | 655 | |
---|
[d69b677] | 656 | if args.start_step: |
---|
[991df6a] | 657 | c.start_step = int(args.start_step) |
---|
[d69b677] | 658 | else: |
---|
[991df6a] | 659 | c.start_step = 0 |
---|
[ccab809] | 660 | |
---|
[d69b677] | 661 | if args.end_step: |
---|
[991df6a] | 662 | c.end_step = int(args.end_step) |
---|
[d69b677] | 663 | else: |
---|
[991df6a] | 664 | c.end_step = 0 |
---|
[d69b677] | 665 | |
---|
[991df6a] | 666 | c.start_date = args.start_date |
---|
| 667 | c.end_date = args.end_date |
---|
[ccab809] | 668 | |
---|
[991df6a] | 669 | c.prefix = args.prefix |
---|
[ccab809] | 670 | |
---|
[991df6a] | 671 | c.inputdir = args.inputdir |
---|
[ccab809] | 672 | |
---|
[d69b677] | 673 | if args.outputdir: |
---|
[991df6a] | 674 | c.outputdir = args.outputdir |
---|
[d69b677] | 675 | else: |
---|
[991df6a] | 676 | c.outputdir = c.inputdir |
---|
[64cf353] | 677 | |
---|
[991df6a] | 678 | return args, c |
---|
[d69b677] | 679 | |
---|
| 680 | if __name__ == "__main__": |
---|
[991df6a] | 681 | main() |
---|