Exemplo n.º 1
0
 def __init__(self):
     self.res = Resources()
     self.main = BrowserWidget()
     self.main.set_html(self.res.getStartScreen())
     self.main.set_form_handler(self._create_snap_form_handler())
     self.main.show()
     self.snapRunning = "inactive"
     self.lastOutputDir = ""
     self.lastQDict = {}
Exemplo n.º 2
0
    def calc(self, proc=None):
        '''run the calculation of ec-data if required.
        
        Args:
           proc -- A QProcess, which will be used to run a longer process in the background.
                  STDERR/STDOUT and signal-handler should be set. If proc is None, the
                  subprocess will be run in the current-process. If proc is set, the caller
                  needs to wait for the proc to finish before calling other methods of this object
'''
        if (not self.must_calc()):
            return

        precommand = '''#! /bin/bash
. /usr/share/modules/init/bash
module load fimex/1.4.2
export OMP_NUM_THREADS=1

cd {outputdir} || exit 1
umask 000
touch running
cp {resdir}/icon_fimex.cfg .
cp {resdir}/icon_sigma_hybrid.ncml .
tmpfile=out$$.nc4
echo "Preprocessing 7days icon meteorology, please wait ca. 3min"
fimex -c icon_fimex.cfg \
      --input.file={globalfile} \
      --interpolate.xAxisValues={xAxisValues} \
      --interpolate.yAxisValues={yAxisValues} \
      --output.file=$tmpfile \
      --output.type=nc4 \
      && mv $tmpfile {outputfile}
rm {outputdir}/running
'''
        command = precommand.format(resdir=Resources().directory,
                                    xAxisValues="{},{},...,{}".format(
                                        self.lon0,
                                        self.lon0 + self.res.domainDeltaX,
                                        self.lon0 + self.res.domainWidth),
                                    yAxisValues="{},{},...,{}".format(
                                        self.lat0,
                                        self.lat0 + self.res.domainDeltaY,
                                        self.lat0 + self.res.domainHeight),
                                    globalfile=self.globalfile,
                                    outputfile=self.files[0],
                                    outputdir=self.outputdir)
        scriptFile = os.path.join(self.outputdir, "command.sh")
        with open(scriptFile, 'w') as script:
            script.write(command)

        if proc is None:
            subprocess.call(['/bin/bash', scriptFile])
        else:
            self.proc = proc  # make sure proc lives long enough
            proc.start('/bin/bash', [scriptFile])

        return
Exemplo n.º 3
0
def convert_to_grib(snapNc, basedir, ident, isotopes):
    config = Resources().getGribWriterConfig(isotopes)
    xmlFile = "cdmGribWriterConfig.xml"
    basexmlFile = os.path.join(basedir, xmlFile)
    with open(basexmlFile, 'w') as xh:
        xh.write(config['xml'])
    errlog = open(os.path.join(basedir, "fimex.errlog"), "w")
    outlog = open(os.path.join(basedir, "fimex.outlog"), "w")
    tempfile = 'tmp.grib'
    basetempfile = os.path.join(basedir, tempfile)
    # fimex works in basedir, so it does not need the basefiles
    for appendix, params in config['extracts'].items():
        outFile = os.path.join(
            basedir, "{ident}_{type}".format(ident=ident, type=appendix))
        with open(outFile, 'wb') as gh:
            for param in params:
                if (os.path.exists(basetempfile)):
                    os.remove(basetempfile)
                procOptions = [
                    'fimex',
                    '--input.file={}'.format(snapNc),
                    '--input.config={}'.format(config['ncml']),
                    # avoid problem with lat/lon variables
                    # in fimex grib-writer< 0.64
                    # '--extract.removeVariable=longitude',
                    # '--extract.removeVariable=latitude',
                    '--output.file={}'.format(tempfile),
                    '--output.type=grib',
                    '--output.config={}'.format(xmlFile)
                ]
                procOptions.append(
                    '--extract.selectVariables={}'.format(param))
                print(" ".join(procOptions))
                proc = subprocess.Popen(procOptions,
                                        cwd=basedir,
                                        stderr=errlog,
                                        stdout=outlog)
                if (proc.wait() != 0):
                    errlog.write("'{fimex}' in {dir} failed".format(
                        fimex=' '.join(procOptions), dir=basedir))
                else:
                    # append tmp-file to final grib-file
                    with (open(basetempfile, 'rb')) as th:
                        while True:
                            data = th.read(16 * 1024 *
                                           1024)  # read max 16M blocks
                            if data:
                                gh.write(data)
                            else:
                                break
                if (os.path.exists(basetempfile)):
                    os.remove(basetempfile)

    errlog.close()
    outlog.close()
Exemplo n.º 4
0
 def __init__(self):
     self.res = Resources()
     self.lastOutputDir = os.path.join(
         self.res.getSnapOutputDir(),
         "{0}_{1}".format("backtrack", strftime("%Y-%m-%dT%H%M%S",
                                                gmtime())))
     self.main = BrowserWidget()
     self.main.set_html(self.res.getStartScreenInverse().replace(
         "OUTPUTDIR", self.lastOutputDir))
     self.main.set_form_handler(self._create_snap_form_handler())
     self.main.show()
     self.snapRunning = "inactive"
     self.lastQDict = {}
Exemplo n.º 5
0
    def add_expected_files(self, date):
        self.files = []
        self.optFiles = []

        # only one file expected
        self.files.append(
            os.path.join(
                self.outputdir,
                self.res.output_filename_pattern.format(
                    year=date.year,
                    month=date.month,
                    day=date.day,
                    UTC=date.hour,
                    resdir=Resources().directory)))
        return
Exemplo n.º 6
0
 def getGlobalMeteoResources():
     '''retrieve the GlobalMeteoResources from internal resources'''
     ecres = Resources()
     res = Snappy.MeteorologyCalculator.GlobalMeteoResource()
     res.indirs = ecres.MET_GLOBAL_INPUTDIRS[MetModel.NrpaEC0p1Global]
     res.pathglob = "ec_atmo_0_1deg_????????T??????Z_3h.nc"
     res.pathptime = "ec_atmo_0_1deg_%Y%m%dT%H%M%SZ_3h.nc"
     res.outputdir = ecres.getSnapOutputDir()
     res.output_filename_pattern = ecres.EC_FILENAME_PATTERN
     res.domainHeight = ecres.ecDomainHeight
     res.domainWidth = ecres.ecDomainWidth
     res.domainDeltaX = ecres.ecDomainRes
     res.domainDeltaY = ecres.ecDomainRes
     res.timeoffset = 3  # required offset between reference-time and first useful startup-time
     return res
Exemplo n.º 7
0
 def getGlobalMeteoResources():
     '''retrieve the GlobalMeteoResources from internal resources'''
     gres = Resources()
     res = Snappy.MeteorologyCalculator.GlobalMeteoResource()
     res.indirs = gres.MET_GLOBAL_INPUTDIRS[MetModel.Icon0p25Global]
     # icon_20200427T00Z.nc
     res.pathglob = "icon_????????T??Z.nc"
     res.pathptime = "icon_%Y%m%dT%HZ.nc"
     res.outputdir = gres.getSnapOutputDir()
     res.output_filename_pattern = gres.MET_FILENAME_PATTERN[
         MetModel.Icon0p25Global]  # keeping filename for extracted data
     res.domainHeight = gres.ecDomainHeight  # reuse domainHeight/Width
     res.domainWidth = gres.ecDomainWidth
     res.domainDeltaX = 0.25
     res.domainDeltaY = 0.25
     res.timeoffset = 0  # required offset between reference-time and first useful startup-time
     return res