示例#1
0
def BIL2netCDF(BILfile, BILdtype='uint16', outdir=os.getcwd(), theme_name='undefined',
               theme_unit='undefined', long_name='undefined'):
    '''
    Convenience function converting the given BIL file to netCDF.
    
    @param BILfile: Input BIl file.
    @param outdir: Output directory.
    @param theme_name: Short name for the theme.   
    @param theme_unit: Metric unit of the theme data.
    @param long_name: Descriptive name for the theme. 
    
    @return: netCDF file in the output directory.
    '''
    ncfilename = os.path.splitext(os.path.basename(BILfile))[0] + '.nc'
    bd = BILdata(BILfile, BILdtype)
    bd.read()
    yy, mm, dd = "13", "09", "11"
    tstring = yy+'-'+mm+'-'+dd+' 06:00:00'
    secs = date2num(iso2datetime(tstring), timeunit)
    mask = flipud(senorge_mask())
    ncdata = flipud(int16(bd.data)) # array needs to be flipped ud-down and transposed to fit the coordinate system
    ncdata[mask] = get_FillValue(ncdata.dtype)
    ncfile = NCdata(os.path.join(outdir, ncfilename))
#    ncfile.zip = True
    ncfile.new(secs)
    print ncdata.dtype.str
    ncfile.add_variable(theme_name, ncdata.dtype.str, theme_unit,
                        long_name, ncdata, lsd=1)
    ncfile.close()
示例#2
0
def BIL2netCDF(BILfile,
               BILdtype='uint16',
               outdir=os.getcwd(),
               theme_name='undefined',
               theme_unit='undefined',
               long_name='undefined'):
    '''
    Convenience function converting the given BIL file to netCDF.
    
    @param BILfile: Input BIl file.
    @param outdir: Output directory.
    @param theme_name: Short name for the theme.   
    @param theme_unit: Metric unit of the theme data.
    @param long_name: Descriptive name for the theme. 
    
    @return: netCDF file in the output directory.
    '''
    ncfilename = os.path.splitext(os.path.basename(BILfile))[0] + '.nc'
    bd = BILdata(BILfile, BILdtype)
    bd.read()
    yy, mm, dd = "13", "09", "11"
    tstring = yy + '-' + mm + '-' + dd + ' 06:00:00'
    secs = date2num(iso2datetime(tstring), timeunit)
    mask = flipud(senorge_mask())
    ncdata = flipud(
        int16(bd.data)
    )  # array needs to be flipped ud-down and transposed to fit the coordinate system
    ncdata[mask] = get_FillValue(ncdata.dtype)
    ncfile = NCdata(os.path.join(outdir, ncfilename))
    #    ncfile.zip = True
    ncfile.new(secs)
    print ncdata.dtype.str
    ncfile.add_variable(theme_name,
                        ncdata.dtype.str,
                        theme_unit,
                        long_name,
                        ncdata,
                        lsd=1)
    ncfile.close()
示例#3
0
def main():
    """
    Loads and verifies input data, calls the model, and controls the output stream. 
    
    Command line usage::
    
        python //~HOME/pysenorge/themes/wind_10m_daily.py YYYY-MM-DD [options]
    """
    # Theme variables
    themedir1 = 'wind_speed_avg_10m'
    themedir2 = 'wind_speed_max_10m'
    
    # Setup input parser
    usage = "usage: python //~HOME/pysenorge/theme_layers/average_windspeed_daily.py YYYY-MM-DD [options]"
    
    parser = OptionParser(usage=usage)
    parser.add_option("-t", "--timerange", 
                      action="store", dest="timerange", type="string",
                      default="[7,31]",
                      help='''Time-range as "[6,30]"''')
    parser.add_option("--no-bil",
                  action="store_false", dest="bil", default=True,
                  help="Set to suppress output in BIL format")
    parser.add_option("--nc",
                  action="store_true", dest="nc", default=False,
                  help="Set to store output in netCDF format")
    parser.add_option("--png",
                  action="store_true", dest="png", default=False,
                  help="Set to store output as PNG image")
    
    # Comment to suppress help
#    parser.print_help()

    (options, args) = parser.parse_args()
    
    # Verify input parameters
    if len(args) != 1:
        parser.error("Please provide the date in ISO format YYYY-MM-DD!")
        parser.print_help() 
    
    # get current datetime
    cdt = iso2datetime(args[0]+" 06:00:00")
    ncfilename = "UM4_sf00_%s.nc" % datetime2BILdate(cdt-timedelta(days=1))
    if len(args) != 1:
        parser.error("Please provide an input file!")
    else:
        # Add full path to the filename
        ncfile = os.path.join(netCDFin, str(cdt.year), ncfilename)
    
    timerange = eval(options.timerange)
    print 'Time-range', timerange
    if not os.path.exists(ncfile):
        parser.error("%s does not exist!" % ncfile)
    else:
        if timerange == None:
            # Load wind data from prognosis (netCDF file) for entire time-range
            ds = Dataset(ncfile, 'r')
            wind_time = ds.variables['time'][:]
            x_wind = ds.variables['x_wind'][:,:,:]
            y_wind = ds.variables['y_wind'][:,:,:]
            rlon = ds.variables['rlon'][:]
            rlat = ds.variables['rlat'][:]
            ds.close()
        else:
            # Load wind data from prognosis (netCDF file) for selected time-range
            ds = Dataset(ncfile, 'r')
            wind_time = ds.variables['time'][timerange[0]:timerange[1]]
            x_wind = ds.variables['x_wind'][timerange[0]:timerange[1],:,:]
            y_wind = ds.variables['y_wind'][timerange[0]:timerange[1],:,:]
            rlon = ds.variables['rlon'][:]
            rlat = ds.variables['rlat'][:]
            ds.close()
    
#    from netCDF4 import num2date
#    for t in wind_time:
#        print num2date(t, "seconds since 1970-01-01 00:00:00 +00:00")
    
    print "Using input data from file %s" % ncfilename
    
    # Setup outputs
    _tstart = time.gmtime(wind_time[0])
    tstruct = time.gmtime(wind_time[-1]) # or -1 if it should be the average until that date
    print "For the period %s-%s-%s:%s - %s-%s-%s:%s" % (str(_tstart.tm_year).zfill(4),
                               str(_tstart.tm_mon).zfill(2),
                               str(_tstart.tm_mday).zfill(2),
                               str(_tstart.tm_hour).zfill(2),
                               str(tstruct.tm_year).zfill(4),
                               str(tstruct.tm_mon).zfill(2),
                               str(tstruct.tm_mday).zfill(2),
                               str(tstruct.tm_hour).zfill(2))
    outfile1 = '%s_%s_%s_%s' % (themedir1, str(tstruct.tm_year).zfill(4),
                               str(tstruct.tm_mon).zfill(2),
                               str(tstruct.tm_mday).zfill(2))
    outfile2 = '%s_%s_%s_%s' % (themedir2, str(tstruct.tm_year).zfill(4),
                               str(tstruct.tm_mon).zfill(2),
                               str(tstruct.tm_mday).zfill(2))
    
    outdir1 = os.path.join(BILout, themedir1, str(get_hydroyear(cdt)))
    if not os.path.exists(outdir1):
        if not os.path.exists(os.path.join(BILout, themedir1)):
            os.chdir(BILout)
            os.system('mkdir %s' % themedir1)
        os.chdir(os.path.join(BILout, themedir1))
        os.system('mkdir %s' % str(get_hydroyear(cdt)))

    outdir2 = os.path.join(BILout, themedir2, str(get_hydroyear(cdt)))
    if not os.path.exists(outdir2):
        if not os.path.exists(os.path.join(BILout, themedir2)):
            os.chdir(BILout)
            os.system('mkdir %s' % themedir2)
        os.chdir(os.path.join(BILout, themedir2))
        os.system('mkdir %s' % str(get_hydroyear(cdt)))

    # Calculate the wind speed vector - using model()
    total_wind_avg, max_wind, wind_dir = model(x_wind, y_wind)
    
    # interpolate total average wind speed to seNorge grid
    total_wind_avg_intp = interpolate(rlon, rlat, total_wind_avg)
    max_wind_intp = interpolate(rlon, rlat, max_wind)
    wind_dir_intp = interpolate(rlon, rlat, wind_dir)
    
    
    # Replace NaN values with the appropriate FillValue
    total_wind_avg_intp = nan2fill(total_wind_avg_intp)
    max_wind_intp = nan2fill(max_wind_intp)
    wind_dir_intp = nan2fill(wind_dir_intp)
    
    if options.bil:
        from pysenorge.grid import senorge_mask
        
        mask = senorge_mask()
        
        # Write to BIL file
        
        # avg wind
        bil_avg_wind = flipud(uint16(total_wind_avg_intp*10.0))
        bil_avg_wind[mask] = UintFillValue
        
        bilfile = BILdata(os.path.join(outdir1,
                          outfile1+'.bil'),
                          datatype='uint16')
        biltext = bilfile.write(bil_avg_wind.flatten()) # collapsed into one dimension
        print biltext
        
        #  max wind
        bil_max_wind = flipud(uint16(max_wind_intp*10.0))
        bil_max_wind[mask] = UintFillValue
        bilfile = BILdata(os.path.join(outdir2,
                          outfile2+'.bil'),
                          datatype='uint16')
        biltext = bilfile.write(bil_max_wind.flatten())
        print biltext
        
#        # wind direction
#        bil_dir_wind = flipud(uint16(wind_dir_intp))
#        bil_dir_wind[mask] = UintFillValue
#        bilfile = BILdata(os.path.join(outdir,
##                          'wind_direction_10m'+'_'+dtstr+'.bil'),
#                          outfile+'.bil'),
#                          datatype='uint16')
#        biltext = bilfile.write(bil_dir_wind.flatten())
#        print biltext
    
    if options.nc:
        # Write to NC file
        ncfile = NCdata(os.path.join(outdir1, outfile1+'.nc'))
#        ncfile.rootgrp.info = themename
        ncfile.new(wind_time[-1])
        
        ncfile.add_variable('avg_wind_speed', total_wind_avg.dtype.str, "m s-1",
                            'Average wind speed last 24h', total_wind_avg_intp)
        ncfile.add_variable('max_wind_speed', max_wind.dtype.str, "m s-1",
                            'Maximum wind gust last 24h', max_wind_intp)
        ncfile.add_variable('wind_direction', wind_dir.dtype.str,
                            "cardinal direction",
                            'Prevailing wind direction last 24h', wind_dir_intp)
        ncfile.close()
        
    if options.png:
        # Write to PNG file
        writePNG(total_wind_avg_intp[0,:,:],
                 os.path.join(outdir1, outfile1),
                 cltfile=r"Z:\tmp\wind_10m_daily\avg_wind_speed_10_no.clt"
                 )
        writePNG(max_wind_intp[0,:,:],
                 os.path.join(outdir2, outfile2),
                 cltfile=r"Z:\tmp\wind_10m_daily\max_wind_speed_10_no.clt"
                 )
#        writePNG(wind_dir_intp[0,:,:],
#                 os.path.join(outdir, 'wind_direction'+'_'+dt),
#                 cltfile=r"Z:\tmp\wind_10m_daily\wind_direction_10_no.clt"
#                 )
    
    # At last - cross fingers it all worked out!
    print "\n*** Finished successfully ***\n"
示例#4
0
def main():
    '''
    Loads and verifies input data, calls the model, and controls the output stream. 
    
    Command line usage::
    
        python //~HOME/pysenorge/themes/additional_snow_depth_wind.py YYYY-MM-DD [options]
    '''
    # Theme variables
    themedir = 'additional_snow_depth'
    themename = ''
    
    # Setup input parser
    usage = "usage: python //~HOME/pysenorge/themes/additional_snow_depth.py YYYY-MM-DD [options]"
    
    parser = OptionParser(usage=usage)
    parser.add_option("-o", "--outdir", 
                      action="store", dest="outdir", type="string",
                      metavar="DIR", default=os.path.join(BILout, themedir),
                      help="Output directory - default: $BILout/%s/$HYDROYEAR" % \
                      themedir)
    parser.add_option("--no-bil",
                  action="store_false", dest="bil", default=True,
                  help="Set to suppress output in BIL format")
    parser.add_option("--nc",
                  action="store_true", dest="nc", default=False,
                  help="Set to store output in netCDF format")
    parser.add_option("--png",
                  action="store_true", dest="png", default=False,
                  help="Set to store output as PNG image")
    # Comment to suppress help
#    parser.print_help()

    (options, args) = parser.parse_args()
    
    if len(args) != 1:
        parser.error("Please provide the date in ISO format YYYY-MM-DD!")
        parser.print_help() 
    
    # get current datetime
    cdt = iso2datetime(args[0]+" 06:00:00")
    windfilename = "wind_speed_avg_10m_%s.bil" % datetime2BILdate(cdt)
    
    # Add full path to the filename
    windfile = os.path.join(BILout, "wind_speed_avg_10m", str(get_hydroyear(cdt)),
                            windfilename)
    if not os.path.exists(windfile):
        parser.error("BIL file %s containing wind data does not exist!" %\
                     windfile)
    else:
        # Load todays data
        wind = BILdata(windfile, 'uint16')
        wind.read()
        # convert to Celsius
        wind.data = float32(wind.data)*0.1
    
    sdfilename = "sdfsw_%s.bil" % datetime2BILdate(cdt)
    sdfile = os.path.join(BILout, "sdfsw", str(get_hydroyear(cdt)), sdfilename)
        
    if not os.path.exists(sdfile):
        parser.error("BIL file %s containing snow-depth data does not exist!" %\
                     sdfile)
    else:
        sd = BILdata(sdfile, 'uint16')
        sd.read()
        
    agefilename = "age_%s.bil" % datetime2BILdate(cdt)
    agefile = os.path.join(BILout, "age", str(get_hydroyear(cdt)), agefilename)
        
    if not os.path.exists(agefile):
        parser.error("BIL file %s containing snow-age data does not exist!" %\
                     agefile)
    else:
        age = BILdata(agefile, 'uint8')
        age.read()
    
    lwcfilename = "lwc_%s.bil" % datetime2BILdate(cdt)
    lwcfile = os.path.join(BILout, "lwc", str(get_hydroyear(cdt)), lwcfilename)
        
    if not os.path.exists(lwcfile):
        parser.error("BIL file %s containing snow-LWC data does not exist!" %\
                     lwcfile)
    else:
        lwc = BILdata(lwcfile, 'uint8')
        lwc.read()
#    from pysenorge.tools.show_histogram import Histogram
#    Histogram(sd.data, R=(0, 2000))
    
    # Setup outputs
    outfile = themedir+'_'+datetime2BILdate(cdt)
    outdir = os.path.join(options.outdir, str(get_hydroyear(cdt)))
    if not os.path.exists(outdir):
        if not os.path.exists(options.outdir):
            os.chdir(BILout)
            os.system('mkdir %s' % themedir)
        os.chdir(options.outdir)
        os.system('mkdir %s' % str(get_hydroyear(cdt)))
        
    
    # Calculate additional snow depth due to wind
    #Hwind = __model(wind.data, sd.data)
    Hwind = model(wind.data, sd.data, lwc.data, age.data)
    # Set no-data values to UintFillValue
    mask = senorge_mask()
    Hwind[mask] = UintFillValue
    
    
    if options.bil:
        # Write to BIL file
        bilfile = BILdata(os.path.join(outdir, outfile+'.bil'),
                          datatype='uint16')
        Hwind = uint16(Hwind*1000)
        Hwind[mask] = UintFillValue
        
#        import pylab
#        pylab.imshow(Hwind, vmin=0, vmax=300, cmap=pylab.cm.gist_stern)
#        pylab.colorbar()
#        pylab.show()
        
#        from pysenorge.tools.show_histogram import Histogram
#        Histogram(Hwind, R=(0, 500))
        
        biltext = bilfile.write(Hwind)
        print biltext
    
    if options.nc:
        from pysenorge.io.nc import NCdata
        # Prepare data
        # Change array order 
        ncHwind = flipud(Hwind)
        # Write to NC file
        ncfile = NCdata(os.path.join(outdir, outfile+'.nc'))
        ncfile.zip = True
#        secs = date2num(cdt, timeunit) # used in NCdata.new()
#        ncfile.new(secs)
        ncfile.add_variable(themedir, ncHwind.dtype.str, "m",
                            themename, ncHwind)
        ncfile.close()
    
    if options.png:
        from pysenorge.io.png import writePNG
        # Write to PNG file
        writePNG(flipud(Hwind), os.path.join(outdir, outfile),
                 cltfile=os.path.join(BILout, themedir, "Hwind.clt")
                 )
        
    # At last - cross fingers it all worked out!
    print "\n*** Finished successfully ***\n"
示例#5
0
def main():
    # Theme variables
    themedir = 'awsd'
    themename = 'Average wind speed 24h'

    # Setup input parser
    usage = "usage: python //~HOME/pysenorge/theme_layers/average_windspeed_daily.py YYYY-MM-DD [options]"

    parser = OptionParser(usage=usage)
    parser.add_option(
        "-o",
        "--outdir",
        action="store",
        dest="outdir",
        type="string",
        default=os.path.join(netCDFout, themedir),
        help="Output directory for netCDF file - default: $netCDFout/%s/$YEAR"
        % themedir)
    parser.add_option("-t",
                      "--timerange",
                      action="store",
                      dest="timerange",
                      type="string",
                      default="None",
                      help='''Time-range as "[6,30]"''')

    # Comment to suppress help
    parser.print_help()

    (options, args) = parser.parse_args()

    # Verify input parameters
    if len(args) != 1:
        parser.error("Please provide the date in ISO format YYYY-MM-DD!")
        parser.print_help()

    # get current datetime
    cdt = iso2datetime(args[0] + " 06:00:00")
    ncfilename = "UM4_sf_%s.nc" % datetime2BILdate(
        cdt)  # e.g. UM4_sf_2010_11_28.nc
    if len(args) != 1:
        parser.error("Please provide an input file!")
    else:
        # Add full path to the filename
        ncfile = os.path.join(netCDFin, str(cdt.year), ncfilename)

    timerange = eval(options.timerange)

    if not os.path.exists(ncfile):
        parser.error("%s does not exist!" % ncfile)
    else:
        if timerange == None:
            # Load wind data from prognosis (netCDF file) for full timerange
            ds = Dataset(ncfile, 'r')
            wind_time = ds.variables['time'][:]
            x_wind = ds.variables['x_wind'][:, :, :]
            y_wind = ds.variables['y_wind'][:, :, :]
            rlon = ds.variables['rlon'][:]
            rlat = ds.variables['rlat'][:]
            ds.close()
            print "xwind-shape", x_wind.shape
        else:
            # Load wind data from prognosis (netCDF file) for selected timerange
            ds = Dataset(ncfile, 'r')
            wind_time = ds.variables['time'][timerange[0]:timerange[1]]
            x_wind = ds.variables['x_wind'][timerange[0]:timerange[1], :, :]
            y_wind = ds.variables['y_wind'][timerange[0]:timerange[1], :, :]
            rlon = ds.variables['rlon'][:]
            rlat = ds.variables['rlat'][:]
            ds.close()
            print "xwind-shape", x_wind.shape

    # Setup outputs
    tstruct = time.gmtime(
        wind_time[-1])  # or -1 if it should be the average until that date
    outfile = '%s_%s_%s_%s' % (themedir, str(
        tstruct.tm_year).zfill(4), str(
            tstruct.tm_mon).zfill(2), str(tstruct.tm_mday).zfill(2))
    outdir = os.path.join(options.outdir, str(cdt.year))
    if not os.path.exists(outdir):
        if not os.path.exists(options.outdir):
            os.chdir(netCDFout)
            os.system('mkdir %s' % themedir)
        os.chdir(options.outdir)
        os.system('mkdir %s' % str(cdt.year))

    # Calculate the wind speed vector - using model()
    total_wind = model(x_wind, y_wind)
    # average over 24h
    total_wind_avg = mean(total_wind, axis=0)

    # interpolate total average wind speed to seNorge grid
    total_wind_avg_intp = interpolate(rlon, rlat, total_wind_avg)

    # Replace NaN values with the appropriate FillValue
    total_wind_avg_intp = nan2fill(total_wind_avg_intp)

    # Write to NC file
    ncfile = NCdata(os.path.join(outdir, outfile + '.nc'))
    ncfile.new(wind_time[0])

    ncfile.add_variable(themedir, total_wind_avg.dtype.str, "m s-1", themename,
                        total_wind_avg_intp)
    ncfile.close()

    # At last - cross fingers it all worked out!
    print "\n*** Finished successfully ***\n"
def main():
    '''
    Loads and verifies input data, calls the model, and controls the output stream. 
    
    Command line usage::
    
        python //~HOME/pysenorge/themes/temperature_destabilization.py YYYY-MM-DD [options]
    '''
    # Theme variables
    themedir = 'depth_hoar_index_2'
    themename = 'Depth hoar index 2'
    
    # Setup input parser
    usage = "usage: python //~HOME/pysenorge/themes/temperature_destabilization.py YYYY-MM-DD [options]"
    
    parser = OptionParser(usage=usage)
    parser.add_option("-o", "--outdir", 
                      action="store", dest="outdir", type="string",
                      metavar="DIR", default=os.path.join(BILout, themedir),
                      help="Output directory - default: $BILout/%s/$HYDROYEAR" % themedir)
    parser.add_option("--no-bil",
                  action="store_false", dest="bil", default=True,
                  help="Set to suppress output in BIL format")
    parser.add_option("--nc",
                  action="store_true", dest="nc", default=False,
                  help="Set to store output in netCDF format")
    parser.add_option("--png",
                  action="store_true", dest="png", default=False,
                  help="Set to store output as PNG image")
    # Comment to suppress help
    parser.print_help()

    (options, args) = parser.parse_args()
    
    if len(args) != 1:
        parser.error("Please provide the date in ISO format YYYY-MM-DD!")
        parser.print_help() 
    
    # get current datetime
    cdt = iso2datetime(args[0]+" 06:00:00")
    oneday = timedelta(days=1)
    tmfilename = "tm_%s.bil" % datetime2BILdate(cdt)
    sdfilename = "sd_%s.bil" % datetime2BILdate(cdt)
    # yesterdays tds data file
    tdsfilename = "%s_%s.bil" % (themedir, datetime2BILdate(cdt-oneday))
    
    # Add full path to the filename
    tmfile = os.path.join(METdir, "tm", str(cdt.year), tmfilename)
    sdfile = os.path.join(BILin, "sd", str(get_hydroyear(cdt)), sdfilename)
    tdsfile = os.path.join(BILout, themedir, str(get_hydroyear(cdt-oneday)),
                            tdsfilename)
    
    if not os.path.exists(tmfile):
        tmfile = os.path.join(PROGdir, str(cdt.year), tmfilename)
        print "Warning: Observation not found - using prognosis instead!"
    
    if not os.path.exists(tmfile):
        parser.error("BIL file containing temperature data does not exist!" %\
                     tmfile)
    elif not os.path.exists(sdfile):
        parser.error("BIL file %s containing snow-depth data does not exist!" %\
                     sdfile)
    else:
        # Load todays data
        tm = BILdata(tmfile, 'uint16')
        tm.read()
        # convert to Celsius
        tm.data = (float32(tm.data) / 10.0) - 273.1
        # Load yesterdays data 
        sd = BILdata(sdfile, 'uint16')
        sd.read()
        # convert mm to m 
        sd.data = float32(sd.data) / 1000.0  
        
    if not os.path.exists(tdsfile):
        print "Yesterdays TDS2 data does not exist - using None!"
        tds = BILdata("tdsdummy", 'uint16')
    else:
        # Load todays data
        tds = BILdata(tdsfile, 'uint16')
        tds.read()
#        tds.data = float32(tds.data)
        
    # Setup outputs
    outfile = themedir+'_'+datetime2BILdate(cdt)
    if options.nc:
        secs = date2num(cdt, timeunit) # used in NCdata.new()
    outdir = os.path.join(options.outdir, str(get_hydroyear(cdt)))
    if not os.path.exists(outdir):
        if not os.path.exists(options.outdir):
            os.chdir(BILout)
            os.system('mkdir %s' % themedir)
        os.chdir(options.outdir)
        os.system('mkdir %s' % str(get_hydroyear(cdt)))
        
    # Calculate sum of snow temperature gradients    
#    tds = uint16(model(tm.data, sd.data, tds.data))
    tds = model(tm.data, sd.data, tds.data)
        
    # Set no-data values to UintFillValue
    mask = senorge_mask()
    tds[mask] = UintFillValue    
    
    if options.bil:
        # Write to BIL file
        bilfile = BILdata(os.path.join(outdir, outfile+'.bil'),
                          datatype='uint16')
        biltext = bilfile.write(tds)
        print biltext
    
    if options.nc:
        from pysenorge.io.nc import NCdata
        # Prepare data
#        nctds = int2float(tds)
#        imask = mask == False
#        # Convert to Celcius/Kelvin
#        nctds[imask] = nctds[imask]/10.0
        # Change array order 
        nctds = flipud(tds)
        # Write to NC file
        ncfile = NCdata(os.path.join(outdir, outfile+'.nc'))
        ncfile.zip = True
        ncfile.new(secs)
        ncfile.add_variable(themedir, nctds.dtype.str, "days",
                            themename, nctds)
        ncfile.close()
    
    if options.png:
        from pysenorge.io.png import writePNG
        # Write to PNG file
        writePNG(flipud(tds), os.path.join(outdir, outfile),
                 cltfile=r"Z:\snowsim\%s\tgss2_v2.clt" % themedir
                 )
        
    # At last - cross fingers it all worked out!
    print "\n*** Finished successfully ***\n"
示例#7
0
def main():
    """
    Loads and verifies input data, calls the model, and controls the output stream.
    """

    # Theme variables
    themedir1 = 'wind_speed_avg_1500m'
    themedir2 = 'wind_speed_max_1500m'

    today = datetime.date.today().strftime("%Y_%m_%d")
    tomorrow = (datetime.date.today() +
                datetime.timedelta(days=1)).strftime("%Y_%m_%d")

    # Setup input parser
    usage = "usage: python //~HOME/pysenorge/theme_layers/ \
            average_windspeed_daily.py YYYY-MM-DD NC-File-Describtion [options]"

    #---------------------------------------------------------
    #add options with parser module
    parser = OptionParser(usage=usage)
    #create no bil
    parser.add_option("--no-bil",
                      action="store_false",
                      dest="bil",
                      default=True,
                      help="Set to suppress output in BIL format")
    #create netCDF
    parser.add_option("--nc",
                      action="store_true",
                      dest="nc",
                      default=False,
                      help="Set to store output in netCDF format")
    #create png
    parser.add_option("--png",
                      action="store_true",
                      dest="png",
                      default=False,
                      help="Set to store output as PNG image")

    #   Comment to suppress help
    #   parser.print_help()
    (options, args) = parser.parse_args()

    # Verify input parameters
    if len(args) != 2:
        parser.error(
            "Please provide the date in ISO format YYYY-MM-DD and the timenc!")
        parser.print_help()

    #Select which date
    yr = str(args[0].split("-")[0])
    mon = str(args[0].split("-")[1])
    day = str(args[0].split("-")[2])

    #Select which nc-file
    timearg = args[1]

    load_date = "%s_%s_%s" % (yr, mon, day)

    ncfilename = "AROME_WIND_850_NVE_%s_%s.nc" % (timearg, load_date)
    ncfile = os.path.join(netCDFin, "2013", ncfilename)

    #Test if path of the netCDF file exists
    if not os.path.exists(ncfile):
        parser.error("%s does not exist!" % ncfile)
    else:
        # Load wind data from prognosis (netCDF file) for entire time-range
        ds = Dataset(ncfile, 'r')
        wind_time = ds.variables['time'][:]
        x_wind = ds.variables['x_wind_850hpa'][:, :, :]
        y_wind = ds.variables['y_wind_850hpa'][:, :, :]
        ds.close()

    print "Using input data from file %s" % ncfile

    #Get timenc and control if the time is right
    ob_time = time.strftime("%H", time.gmtime(wind_time[0]))
    data_timenc = _time_fun(ob_time)
    if timearg == data_timenc:
        pass
    else:
        print "The given input parameter and time value of the netCDF are not the same"
    timenc = data_timenc

    #---------------------------------------------------------
    #Output paths, output filenames and timerange
    begin_time = time.strftime("%d-%m-%Y-%H:%M:%S", time.gmtime(wind_time[0]))
    end_time = time.strftime("%d-%m-%Y-%H:%M:%S", time.gmtime(wind_time[24]))

    print "For the period from the", begin_time, "to", end_time

    outfile1 = '%s_%s' % (themedir1, today)
    outfile2 = '%s_%s' % (themedir2, today)

    cdt = iso2datetime(args[0] + " 06:00:00")

    #---------------------------------------------------------
    #Output path 1
    outdir1 = os.path.join(BILout, themedir1, str(get_hydroyear(cdt)))
    if not os.path.exists(outdir1):
        if not os.path.exists(os.path.join(BILout, themedir1)):
            os.chdir(BILout)
            os.system('mkdir %s' % themedir1)
        os.chdir(os.path.join(BILout, themedir1))
        os.system('mkdir %s' % str(get_hydroyear(cdt)))

    #Output path 2
    outdir2 = os.path.join(BILout, themedir2, str(get_hydroyear(cdt)))
    if not os.path.exists(outdir2):
        if not os.path.exists(os.path.join(BILout, themedir2)):
            os.chdir(BILout)
            os.system('mkdir %s' % themedir2)
        os.chdir(os.path.join(BILout, themedir2))
        os.system('mkdir %s' % str(get_hydroyear(cdt)))

    #Output path for wind_00 til wind _18
    if timenc == "00":
        outdir_hour = os.path.join(BILout, str(get_hydroyear(cdt)), load_date,
                                   "00")
    elif timenc == "06":
        outdir_hour = os.path.join(BILout, str(get_hydroyear(cdt)), load_date,
                                   "06")
    elif timenc == "12":
        outdir_hour = os.path.join(BILout, str(get_hydroyear(cdt)), load_date,
                                   "12")
    elif timenc == "18":
        outdir_hour = os.path.join(BILout, str(get_hydroyear(cdt)), load_date,
                                   "18")

    if not os.path.exists(outdir_hour):
        if not os.path.exists(os.path.join(BILout, str(get_hydroyear(cdt)))):
            os.chdir(BILout)
            os.system('mkdir %s' % str(get_hydroyear(cdt)))
        os.chdir(os.path.join(BILout, str(get_hydroyear(cdt))))
        os.makedirs('%s/%s/' % (load_date, timenc))

#---------------------------------------------------------
# Calculate the wind speed vector - using model()
    total_wind_avg, max_wind, total_wind, wind_dir_cat, \
    hour_wind = model(x_wind, y_wind)

    # interpolate total average wind speed to seNorge grid
    total_wind_avg_intp = interpolate_new(total_wind_avg)
    max_wind_intp = interpolate_new(max_wind)
    wind_dir_intp = interpolate_new(wind_dir_cat)
    #dom_wind_tab_intp = interpolate_new(dom_wind_tab)

    # Replace NaN values with the appropriate FillValue
    total_wind_avg_intp = nan2fill(total_wind_avg_intp)
    max_wind_intp = nan2fill(max_wind_intp)
    wind_dir_intp = nan2fill(wind_dir_intp)
    #dom_wind_tab_intp = nan2fill(dom_wind_tab_intp)

    #--------------------------------------------------------
    #Hourly wind forecast based on the recent AROME input file
    if timenc == "18":
        #at 01:00
        wind_18 = interpolate_new(total_wind[0, :, :])
        hour_wind_intp_18 = interpolate_new(hour_wind[0, :, :])
        #at 07:00
        wind_00 = interpolate_new(total_wind[6, :, :])
        hour_wind_intp_00 = interpolate_new(hour_wind[6, :, :])
        #at 13:00
        wind_06 = interpolate_new(total_wind[12, :, :])
        hour_wind_intp_06 = interpolate_new(hour_wind[12, :, :])
        #at 19:00
        wind_12 = interpolate_new(total_wind[18, :, :])
        hour_wind_intp_12 = interpolate_new(hour_wind[18, :, :])

        outfile3 = '%s_%s' % ("wind_speed_18_1500m", today)
        outfile4 = '%s_%s' % ("wind_speed_00_1500m", today)
        outfile5 = '%s_%s' % ("wind_speed_06_1500m", today)
        outfile6 = '%s_%s' % ("wind_speed_12_1500m", today)

    elif timenc == "00":
        #at 07:00
        wind_00 = interpolate_new(total_wind[0, :, :])
        hour_wind_intp_00 = interpolate_new(hour_wind[0, :, :])
        #at 13:00
        wind_06 = interpolate_new(total_wind[6, :, :])
        hour_wind_intp_06 = interpolate_new(hour_wind[6, :, :])
        #at 19:00
        wind_12 = interpolate_new(total_wind[12, :, :])
        hour_wind_intp_12 = interpolate_new(hour_wind[12, :, :])
        #next day at 01:00
        wind_18 = interpolate_new(total_wind[18, :, :])
        hour_wind_intp_18 = interpolate_new(hour_wind[18, :, :])

        outfile3 = '%s_%s' % ("wind_speed_00_1500m", today)
        outfile4 = '%s_%s' % ("wind_speed_06_1500m", today)
        outfile5 = '%s_%s' % ("wind_speed_12_1500m", today)
        outfile6 = '%s_%s' % ("wind_speed_18_1500m", tomorrow)

    elif timenc == "06":
        #at 13:00
        wind_06 = interpolate_new(total_wind[0, :, :])
        hour_wind_intp_06 = interpolate_new(hour_wind[0, :, :])
        #at 19:00
        wind_12 = interpolate_new(total_wind[6, :, :])
        hour_wind_intp_12 = interpolate_new(hour_wind[6, :, :])
        #next day 01:00
        wind_18 = interpolate_new(total_wind[12, :, :])
        hour_wind_intp_18 = interpolate_new(hour_wind[12, :, :])
        #next day 07:00
        wind_00 = interpolate_new(total_wind[18, :, :])
        hour_wind_intp_00 = interpolate_new(hour_wind[18, :, :])

        outfile3 = '%s_%s' % ("wind_speed_06_1500m", today)
        outfile4 = '%s_%s' % ("wind_speed_12_1500m", today)
        outfile5 = '%s_%s' % ("wind_speed_18_1500m", tomorrow)
        outfile6 = '%s_%s' % ("wind_speed_00_1500m", tomorrow)

    elif timenc == "12":
        #at 19:00
        wind_12 = interpolate_new(total_wind[0, :, :])
        hour_wind_intp_12 = interpolate_new(hour_wind[0, :, :])
        #next day 01:00
        wind_18 = interpolate_new(total_wind[6, :, :])
        hour_wind_intp_18 = interpolate_new(hour_wind[6, :, :])
        #next day 07:00
        wind_00 = interpolate_new(total_wind[12, :, :])
        hour_wind_intp_00 = interpolate_new(hour_wind[12, :, :])
        #next day 13:00
        wind_06 = interpolate_new(total_wind[18, :, :])
        hour_wind_intp_06 = interpolate_new(hour_wind[18, :, :])

        outfile3 = '%s_%s' % ("wind_speed_12_1500m", today)
        outfile4 = '%s_%s' % ("wind_speed_18_1500m", tomorrow)
        outfile5 = '%s_%s' % ("wind_speed_00_1500m", tomorrow)
        outfile6 = '%s_%s' % ("wind_speed_06_1500m", tomorrow)

#---------------------------------------------------------
#Option --bil: Multiplied by 10 to store data as integer
    if options.bil:
        from pysenorge.grid import senorge_mask
        mask = senorge_mask()

        #Avg wind
        bil_avg_wind = flipud(uint16(total_wind_avg_intp * 10.0))
        bil_avg_wind[mask] = UintFillValue

        bilfile = BILdata(os.path.join(outdir1, outfile1 + '.bil'),
                          datatype='uint16')
        #Colapse into one dimension
        biltext = bilfile.write(bil_avg_wind.flatten())
        print biltext

        # Max wind
        bil_max_wind = flipud(uint16(max_wind_intp * 10.0))
        bil_max_wind[mask] = UintFillValue
        bilfile = BILdata(os.path.join(outdir2, outfile2 + '.bil'),
                          datatype='uint16')
        biltext = bilfile.write(bil_max_wind.flatten())
        print biltext

        # bil wind at 00
        bil_wind_00 = flipud(uint16(wind_00 * 10.0))
        bil_wind_00[mask] = UintFillValue
        bilfile = BILdata(os.path.join(outdir_hour, outfile3 + '.bil'),
                          datatype='uint16')
        biltext = bilfile.write(bil_wind_00.flatten())
        print biltext

        # bil wind at 06
        bil_wind_06 = flipud(uint16(wind_06 * 10.0))
        bil_wind_06[mask] = UintFillValue
        bilfile = BILdata(os.path.join(outdir_hour, outfile4 + '.bil'),
                          datatype='uint16')
        biltext = bilfile.write(bil_wind_06.flatten())
        print biltext

        #bil wind at 12
        bil_wind_12 = flipud(uint16(wind_12 * 10.0))
        bil_wind_12[mask] = UintFillValue
        bilfile = BILdata(os.path.join(outdir_hour, outfile5 + '.bil'),
                          datatype='uint16')
        biltext = bilfile.write(bil_wind_12.flatten())
        print biltext

        #bil wind at 18
        bil_wind_18 = flipud(uint16(wind_18 * 10.0))
        bil_wind_18[mask] = UintFillValue
        bilfile = BILdata(os.path.join(outdir_hour, outfile6 + '.bil'),
                          datatype='uint16')
        biltext = bilfile.write(bil_wind_18.flatten())
        print biltext

#---------------------------------------------------------
#Option --nc: write a nc file
    if options.nc:
        ncfile = NCdata(os.path.join(outdir1, outfile1 + '.nc'))

        ncfile.new(wind_time[-1])

        ncfile.add_variable('avg_wind_speed', total_wind_avg.dtype.str,
                            "m s-1", 'Average wind speed last 24h',
                            total_wind_avg_intp)
        ncfile.add_variable('max_wind_speed', max_wind.dtype.str, "m s-1",
                            'Maximum wind gust last 24h', max_wind_intp)
        ncfile.add_variable('wind_direction', wind_dir_cat.dtype.str,
                            "cardinal direction",
                            'Prevailing wind direction last 24h',
                            wind_dir_intp)
        ncfile.add_variable('wind_00', wind_00.dtype.str, "m s-1",
                            'Wind forecast 01:00', wind_00)
        ncfile.add_variable('wind_06', wind_06.dtype.str, "m s-1",
                            'Wind forecast 07:00', wind_06)
        ncfile.add_variable('wind_12', wind_12.dtype.str, "m s-1",
                            'Wind forecast 13:00', wind_12)
        ncfile.add_variable('wind_18', wind_18.dtype.str, "m s-1",
                            'Wind forecast 19:00', wind_18)

        ncfile.close()


#---------------------------------------------------------
#Option --png: Write a PNG files
    if options.png:
        avg_clt_file_path = "/home/ralf/Dokumente/summerjob/data/avg_wind_speed_10_no.clt"
        max_clt_file_path = "/home/ralf/Dokumente/summerjob/data/max_wind_speed_10_no.clt"
        direction_clt_file_path = "/home/ralf/Dokumente/summerjob/data/wind_direction_10_no.clt"

        #Creates png file wind average
        writePNG(total_wind_avg_intp[:, :],
                 os.path.join(outdir1, outfile1),
                 cltfile=avg_clt_file_path)

        #Creates png file wind max
        writePNG(max_wind_intp[:, :],
                 os.path.join(outdir2, outfile2),
                 cltfile=max_clt_file_path)

        #Hourly wind speed maps
        writePNG(wind_00,
                 os.path.join(outdir_hour, outfile3),
                 cltfile=avg_clt_file_path)

        writePNG(wind_06,
                 os.path.join(outdir_hour, outfile4),
                 cltfile=avg_clt_file_path)

        writePNG(wind_12,
                 os.path.join(outdir_hour, outfile5),
                 cltfile=avg_clt_file_path)

        writePNG(wind_18,
                 os.path.join(outdir_hour, outfile6),
                 cltfile=avg_clt_file_path)

        #Wind direction
        writePNG(wind_dir_intp[:, :],
                 os.path.join(outdir1, 'wind_direction'),
                 cltfile=direction_clt_file_path)

        #Hourly wind directions
        writePNG(hour_wind_intp_00[:, :],
                 os.path.join(outdir1, 'wind_direction_hour_00'),
                 cltfile=direction_clt_file_path)

        writePNG(hour_wind_intp_06[:, :],
                 os.path.join(outdir1, 'wind_direction_hour_06'),
                 cltfile=direction_clt_file_path)

        writePNG(hour_wind_intp_12[:, :],
                 os.path.join(outdir1, 'wind_direction_hour_12'),
                 cltfile=direction_clt_file_path)

        writePNG(hour_wind_intp_18[:, :],
                 os.path.join(outdir1, 'wind_direction_hour_18'),
                 cltfile=direction_clt_file_path)

        #Maximal dominate wind speed
        #writePNG(dom_wind_tab_intp[:, :],
        #          os.path.join(outdir1, today, 'wind_direction_dom_wind_tab'),
        #          cltfile=direction_clt_file_path
        #          )
    # At last - cross fingers* it all worked out! *and toes !!!
    print "\n***Finished successfully***\n"
def main():
    # Theme variables
    themedir = 'awsd'
    themename = 'Average wind speed 24h'
    
    # Setup input parser
    usage = "usage: python //~HOME/pysenorge/theme_layers/average_windspeed_daily.py YYYY-MM-DD [options]"
    
    parser = OptionParser(usage=usage)
    parser.add_option("-o", "--outdir", 
                      action="store", dest="outdir", type="string",
                      default=os.path.join(netCDFout, themedir),
                      help="Output directory for netCDF file - default: $netCDFout/%s/$YEAR" % themedir)
    parser.add_option("-t", "--timerange", 
                      action="store", dest="timerange", type="string",
                      default="None",
                      help='''Time-range as "[6,30]"''')
    
    # Comment to suppress help
    parser.print_help()

    (options, args) = parser.parse_args()
    
    # Verify input parameters
    if len(args) != 1:
        parser.error("Please provide the date in ISO format YYYY-MM-DD!")
        parser.print_help() 
    
    # get current datetime
    cdt = iso2datetime(args[0]+" 06:00:00")
    ncfilename = "UM4_sf_%s.nc" % datetime2BILdate(cdt) # e.g. UM4_sf_2010_11_28.nc
    if len(args) != 1:
        parser.error("Please provide an input file!")
    else:
        # Add full path to the filename
        ncfile = os.path.join(netCDFin, str(cdt.year), ncfilename)
    
    timerange = eval(options.timerange)
    
    if not os.path.exists(ncfile):
        parser.error("%s does not exist!" % ncfile)
    else:
        if timerange == None:
            # Load wind data from prognosis (netCDF file) for full timerange
            ds = Dataset(ncfile, 'r')
            wind_time = ds.variables['time'][:]
            x_wind = ds.variables['x_wind'][:,:,:]
            y_wind = ds.variables['y_wind'][:,:,:]
            rlon = ds.variables['rlon'][:]
            rlat = ds.variables['rlat'][:]
            ds.close()    
            print "xwind-shape", x_wind.shape
        else:
            # Load wind data from prognosis (netCDF file) for selected timerange
            ds = Dataset(ncfile, 'r')
            wind_time = ds.variables['time'][timerange[0]:timerange[1]]
            x_wind = ds.variables['x_wind'][timerange[0]:timerange[1],:,:]
            y_wind = ds.variables['y_wind'][timerange[0]:timerange[1],:,:]
            rlon = ds.variables['rlon'][:]
            rlat = ds.variables['rlat'][:]
            ds.close()    
            print "xwind-shape", x_wind.shape

    
    # Setup outputs
    tstruct = time.gmtime(wind_time[-1]) # or -1 if it should be the average until that date
    outfile = '%s_%s_%s_%s' % (themedir, str(tstruct.tm_year).zfill(4),
                               str(tstruct.tm_mon).zfill(2),
                               str(tstruct.tm_mday).zfill(2))
    outdir = os.path.join(options.outdir, str(cdt.year))
    if not os.path.exists(outdir):
        if not os.path.exists(options.outdir):
            os.chdir(netCDFout)
            os.system('mkdir %s' % themedir)
        os.chdir(options.outdir)
        os.system('mkdir %s' % str(cdt.year))

    # Calculate the wind speed vector - using model()
    total_wind = model(x_wind, y_wind)
    # average over 24h
    total_wind_avg = mean(total_wind, axis=0)
    
    # interpolate total average wind speed to seNorge grid
    total_wind_avg_intp = interpolate(rlon, rlat, total_wind_avg)
    
    # Replace NaN values with the appropriate FillValue
    total_wind_avg_intp = nan2fill(total_wind_avg_intp)
    
    # Write to NC file
    ncfile = NCdata(os.path.join(outdir, outfile+'.nc'))
    ncfile.new(wind_time[0])
    
    ncfile.add_variable(themedir, total_wind_avg.dtype.str, "m s-1", themename, total_wind_avg_intp)
    ncfile.close()
    
    # At last - cross fingers it all worked out!
    print "\n*** Finished successfully ***\n"
def main():
    '''
    Loads and verifies input data, calls the models, and controls the output stream. 
    
    Command line usage::
    
        python //~HOME/pysenorge/themes/additional_snow_depth_wind.py YYYY-MM-DD [options]
    '''
    # Theme variables
    themedir = 'additional_snow_depth'
    themename = ''
    
    # Setup input parser
    usage = "usage: python //~HOME/pysenorge/themes/additional_snow_depth.py YYYY-MM-DD [options]"
    
    parser = OptionParser(usage=usage)
    parser.add_option("-o", "--outdir", 
                      action="store", dest="outdir", type="string",
                      metavar="DIR", default=os.path.join(BILout, themedir),
                      help="Output directory - default: $BILout/%s/$HYDROYEAR" % \
                      themedir)
    parser.add_option("--no-bil",
                  action="store_false", dest="bil", default=True,
                  help="Set to suppress output in BIL format")
    parser.add_option("--nc",
                  action="store_true", dest="nc", default=False,
                  help="Set to store output in netCDF format")
    parser.add_option("--png",
                  action="store_true", dest="png", default=False,
                  help="Set to store output as PNG image")
    
    # Comment to suppress help
    # parser.print_help()
    (options, args) = parser.parse_args()
    
    if len(args) != 1:
        parser.error("Please provide the date in ISO format YYYY-MM-DD!")
        parser.print_help() 
    
    # Get current datetime
    cdt = iso2datetime(args[0]+" 06:00:00")
    #Ändern Fehler...
    windfilename = "wind_speed_avg_10m_%s.bil" % datetime2BILdate(cdt)
    
    #----------------------------------------------------------------------------
    # Import wind-speed 10m map 
    # windfile = os.path.join(BILout, "wind_speed_avg_10m", str(get_hydroyear(cdt)),windfilename)
    windfile = os.path.join(BILout, windfilename)
    
    if not os.path.exists(windfile):
        parser.error("BIL file %s containing wind data does not exist!" %\
                     windfile)
    else:
        # Load todays data
        wind = BILdata(windfile, 'uint16')
        wind.read()
        # convert to Celsius
        wind.data = float32(wind.data)*0.1 #multiplyed by 0.1 because of in integer values in wind_model
    
    #----------------------------------------------------------------------------
    #import snow-depth map 
    sdfilename = "sdfsw_%s.bil" % datetime2BILdate(cdt)
    #sdfile = os.path.join(BILout, "sdfsw", str(get_hydroyear(cdt)), sdfilename)
    sdfile = os.path.join(BILout, sdfilename)
         
    if not os.path.exists(sdfile):
        parser.error("BIL file %s containing snow-depth data does not exist!" %\
                     sdfile)
    else:
        sd = BILdata(sdfile, 'uint16')
        sd.read()
    
    #----------------------------------------------------------------------------
    #import snow-age map 
    agefilename = "age_%s.bil" % datetime2BILdate(cdt)
    #agefile = os.path.join(BILout, "age", str(get_hydroyear(cdt)), agefilename)
    agefile = os.path.join(BILout, agefilename)
    
    if not os.path.exists(agefile):
        parser.error("BIL file %s containing snow-age data does not exist!" %\
                     agefile)
    else:
        age = BILdata(agefile, 'uint8')
        age.read()
    
    #----------------------------------------------------------------------------
    #import liquid-water-content map 
    lwcfilename = "lwc_%s.bil" % datetime2BILdate(cdt)
    #lwcfile = os.path.join(BILout, "lwc", str(get_hydroyear(cdt)), lwcfilename)
    lwcfile = os.path.join(BILout, lwcfilename)

    if not os.path.exists(lwcfile):
        parser.error("BIL file %s containing snow-LWC data does not exist!" %\
                     lwcfile)
    else:
        lwc = BILdata(lwcfile, 'uint8')
        lwc.read()

    #----------------------------------------------------------------------------
    #Calculate and import frozen-snow map 
    try:
        frz = frozen_snow.frozen()
        frz.load_data(os.path.join(BILout, "ssttm_2013_01_13.bil"))
        frz.check_value(frz.tsi)    
    except (IOError,NameError):
        print "Couldn't find or load frozen-snow map"

    # Setup outputs
    outfile = themedir+'_'+datetime2BILdate(cdt)
    outdir = os.path.join(options.outdir, str(get_hydroyear(cdt)))
    if not os.path.exists(outdir):
        if not os.path.exists(options.outdir):
            os.chdir(BILout)
            os.system('mkdir %s' % themedir)
        os.chdir(options.outdir)
        os.system('mkdir %s' % str(get_hydroyear(cdt)))

    #Run additional-snow map model
    Hwind = model(wind.data, sd.data, lwc.data, age.data, frz.idex)

    # Set no-data values to UintFillValue
    mask = senorge_mask()
    Hwind[mask] = UintFillValue

    #Option create bil-file
    if options.bil:
        # Write to BIL file
        bilfile = BILdata(os.path.join(outdir, outfile+'.bil'),
                          datatype='uint16')
        Hwind = uint16(Hwind*1000)
        Hwind[mask] = UintFillValue

        biltext = bilfile.write(Hwind)
        print biltext

    #Option create nc-file
    if options.nc:
        from pysenorge.io.nc import NCdata
        # Prepare data
        # Change array order 
        ncHwind = flipud(Hwind)
        # Write to NC file
        ncfile = NCdata(os.path.join(outdir, outfile+'.nc'))
        ncfile.zip = True
        ncfile.add_variable(themedir, ncHwind.dtype.str, "m",
                            themename, ncHwind)
        ncfile.close()

    #Option create png imagefile
    if options.png:
        from pysenorge.io.png import writePNG
        # Write to PNG file
        writePNG(flipud(Hwind), os.path.join(outdir, outfile),
                 cltfile=os.path.join("/home/ralf/Dokumente/summerjob/data/additional_snow_depth_wind.clt")
                 )

    # At last - cross fingers it all worked out!
    print "\n*** Finished successfully ***\n"
示例#10
0
def main():
    '''
    Loads and verifies input data, calls the model, and controls the output stream. 
    
    Command line usage::
    
        python //~HOME/pysenorge/themes/temperature_destabilization.py YYYY-MM-DD [options]
    '''
    # Theme variables
    themedir = 'depth_hoar_index_2'
    themename = 'Depth hoar index 2'

    # Setup input parser
    usage = "usage: python //~HOME/pysenorge/themes/temperature_destabilization.py YYYY-MM-DD [options]"

    parser = OptionParser(usage=usage)
    parser.add_option(
        "-o",
        "--outdir",
        action="store",
        dest="outdir",
        type="string",
        metavar="DIR",
        default=os.path.join(BILout, themedir),
        help="Output directory - default: $BILout/%s/$HYDROYEAR" % themedir)
    parser.add_option("--no-bil",
                      action="store_false",
                      dest="bil",
                      default=True,
                      help="Set to suppress output in BIL format")
    parser.add_option("--nc",
                      action="store_true",
                      dest="nc",
                      default=False,
                      help="Set to store output in netCDF format")
    parser.add_option("--png",
                      action="store_true",
                      dest="png",
                      default=False,
                      help="Set to store output as PNG image")
    # Comment to suppress help
    parser.print_help()

    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.error("Please provide the date in ISO format YYYY-MM-DD!")
        parser.print_help()

    # get current datetime
    cdt = iso2datetime(args[0] + " 06:00:00")
    oneday = timedelta(days=1)
    tmfilename = "tm_%s.bil" % datetime2BILdate(cdt)
    sdfilename = "sd_%s.bil" % datetime2BILdate(cdt)
    # yesterdays tds data file
    tdsfilename = "%s_%s.bil" % (themedir, datetime2BILdate(cdt - oneday))

    # Add full path to the filename
    tmfile = os.path.join(METdir, "tm", str(cdt.year), tmfilename)
    sdfile = os.path.join(BILin, "sd", str(get_hydroyear(cdt)), sdfilename)
    tdsfile = os.path.join(BILout, themedir, str(get_hydroyear(cdt - oneday)),
                           tdsfilename)

    if not os.path.exists(tmfile):
        tmfile = os.path.join(PROGdir, str(cdt.year), tmfilename)
        print "Warning: Observation not found - using prognosis instead!"

    if not os.path.exists(tmfile):
        parser.error("BIL file containing temperature data does not exist!" %\
                     tmfile)
    elif not os.path.exists(sdfile):
        parser.error("BIL file %s containing snow-depth data does not exist!" %\
                     sdfile)
    else:
        # Load todays data
        tm = BILdata(tmfile, 'uint16')
        tm.read()
        # convert to Celsius
        tm.data = (float32(tm.data) / 10.0) - 273.1
        # Load yesterdays data
        sd = BILdata(sdfile, 'uint16')
        sd.read()
        # convert mm to m
        sd.data = float32(sd.data) / 1000.0

    if not os.path.exists(tdsfile):
        print "Yesterdays TDS2 data does not exist - using None!"
        tds = BILdata("tdsdummy", 'uint16')
    else:
        # Load todays data
        tds = BILdata(tdsfile, 'uint16')
        tds.read()
#        tds.data = float32(tds.data)

# Setup outputs
    outfile = themedir + '_' + datetime2BILdate(cdt)
    if options.nc:
        secs = date2num(cdt, timeunit)  # used in NCdata.new()
    outdir = os.path.join(options.outdir, str(get_hydroyear(cdt)))
    if not os.path.exists(outdir):
        if not os.path.exists(options.outdir):
            os.chdir(BILout)
            os.system('mkdir %s' % themedir)
        os.chdir(options.outdir)
        os.system('mkdir %s' % str(get_hydroyear(cdt)))

    # Calculate sum of snow temperature gradients


#    tds = uint16(model(tm.data, sd.data, tds.data))
    tds = model(tm.data, sd.data, tds.data)

    # Set no-data values to UintFillValue
    mask = senorge_mask()
    tds[mask] = UintFillValue

    if options.bil:
        # Write to BIL file
        bilfile = BILdata(os.path.join(outdir, outfile + '.bil'),
                          datatype='uint16')
        biltext = bilfile.write(tds)
        print biltext

    if options.nc:
        from pysenorge.io.nc import NCdata
        # Prepare data
        #        nctds = int2float(tds)
        #        imask = mask == False
        #        # Convert to Celcius/Kelvin
        #        nctds[imask] = nctds[imask]/10.0
        # Change array order
        nctds = flipud(tds)
        # Write to NC file
        ncfile = NCdata(os.path.join(outdir, outfile + '.nc'))
        ncfile.zip = True
        ncfile.new(secs)
        ncfile.add_variable(themedir, nctds.dtype.str, "days", themename,
                            nctds)
        ncfile.close()

    if options.png:
        from pysenorge.io.png import writePNG
        # Write to PNG file
        writePNG(flipud(tds),
                 os.path.join(outdir, outfile),
                 cltfile=r"Z:\snowsim\%s\tgss2_v2.clt" % themedir)

    # At last - cross fingers it all worked out!
    print "\n*** Finished successfully ***\n"
示例#11
0
def main():
    
    # Theme variables
    themedir = 'net_radiative_flux'
    themename = 'Daily avg. net radiation flux'
    
    # Setup input parser
    usage = "usage: python //~HOME/pysenorge/theme_layers/net_radiative_flux.py YYYY-MM-DD [options]"
    
    parser = OptionParser(usage=usage)
    parser.add_option("-t", "--timerange", 
                      action="store", dest="timerange", type="string",
                      default="[7,31]",
                      help='''Time-range as "[7,31]"''')
    parser.add_option("--no-bil",
                  action="store_false", dest="bil", default=True,
                  help="Set to suppress output in BIL format")
    parser.add_option("--nc",
                  action="store_true", dest="nc", default=False,
                  help="Set to store output in netCDF format")
    parser.add_option("--png",
                  action="store_true", dest="png", default=False,
                  help="Set to store output as PNG image")
    
    # Comment to suppress help
#    parser.print_help()

    (options, args) = parser.parse_args()
    
    # Verify input parameters
    if len(args) != 1:
        parser.error("Please provide the date in ISO format YYYY-MM-DD!")
        parser.print_help() 
    
    # get current datetime
    cdt = iso2datetime(args[0]+" 06:00:00")
    ncfilename = "UM4_sf00_%s.nc" % datetime2BILdate(cdt)
    if len(args) != 1:
        parser.error("Please provide an input file!")
    else:
        # Add full path to the filename
        ncfile = os.path.join(netCDFin, str(cdt.year), ncfilename)
    
    timerange = eval(options.timerange)
    
    if not os.path.exists(ncfile):
        parser.error("%s does not exist!" % ncfile)
    else:
        if timerange == None:
            # Load wind data from prognosis (netCDF file) for entire time-range
            ds = Dataset(ncfile, 'r')
            _time = ds.variables['time'][:]
            SWnet = ds.variables['net_sw_surface'][:,:,:]
            LWnet = ds.variables['net_lw_surface'][:,:,:]
            Hs = ds.variables['sensible_heat_surface'][:,:,:]
            Hl = ds.variables['latent_heat_surface'][:,:,:]
            rlon = ds.variables['rlon'][:]
            rlat = ds.variables['rlat'][:]
            ds.close()
        else:
            # Load wind data from prognosis (netCDF file) for selected time-range
            ds = Dataset(ncfile, 'r')
            _time = ds.variables['time'][timerange[0]:timerange[1]]
            SWnet = ds.variables['net_sw_surface'][timerange[0]:timerange[1],:,:]
            LWnet = ds.variables['net_lw_surface'][timerange[0]:timerange[1],:,:]
            Hs = ds.variables['sensible_heat_surface'][timerange[0]:timerange[1],:,:]
            Hl = ds.variables['latent_heat_surface'][timerange[0]:timerange[1],:,:]
            rlon = ds.variables['rlon'][:]
            rlat = ds.variables['rlat'][:]
            ds.close()
    
    from netCDF4 import num2date
    for t in _time:
        print num2date(t, "seconds since 1970-01-01 00:00:00 +00:00")
            
    # Setup outputs
    tstruct = time.gmtime(_time[-1]) # or -1 if it should be the average until that date
    outfile = '%s_%s_%s_%s' % (themedir, str(tstruct.tm_year).zfill(4),
                               str(tstruct.tm_mon).zfill(2),
                               str(tstruct.tm_mday).zfill(2))
    
    print outfile
    outdir = os.path.join(BILout, themedir, str(cdt.year))
    if not os.path.exists(outdir):
        if not os.path.exists(os.path.join(BILout, themedir)):
            os.chdir(BILout)
            os.system('mkdir %s' % themedir)
        os.chdir(os.path.join(BILout, themedir))
        os.system('mkdir %s' % str(cdt.year))

    # Calculate the wind speed vector - using model()
    Rnet = model(SWnet, LWnet, Hs, Hl)
    
    # interpolate total average wind speed to seNorge grid
    Rnet_intp = interpolate(rlon, rlat, Rnet)
    
    # Replace NaN values with the appropriate FillValue
    Rnet_intp = nan2fill(Rnet_intp)
    
#    # Set no-data values to IntFillValue
#    mask = senorge_mask()
#    tgss[mask] = IntFillValue
#    
#    if options.bil:
#        # Write to BIL file
#        bilfile = BILdata(os.path.join(outdir, outfile+'.bil'),
#                          datatype='int16')
#        biltext = bilfile.write(tgss)
#        print biltext
    
    if options.nc:
        # Prepare data
        # Change array order 
        ncRnet = (Rnet_intp)
        # Write to NC file
        ncfile = NCdata(os.path.join(outdir, outfile+'.nc'))
        ncfile.new(_time[-1])
        ncfile.add_variable(themedir, ncRnet.dtype.str, "W m-2",
                            themename, ncRnet)
        ncfile.close()
    
#    if options.png:
#        from pysenorge.io.png import writePNG
#        # Write to PNG file
#        writePNG(flipud(Rnet_intp), os.path.join(outdir, outfile),
#                 cltfile=os.path.join(BILout, themedir, "tgss.clt")
#                 )
        
    # At last - cross fingers it all worked out!
    print "\n*** Finished successfully ***\n"
示例#12
0
def main():
    '''
    Loads and verifies input data, calls the models, and controls the output stream. 
    
    Command line usage::
    
        python //~HOME/pysenorge/themes/additional_snow_depth_wind.py YYYY-MM-DD [options]
    '''
    # Theme variables
    themedir = 'additional_snow_depth'
    themename = ''

    # Setup input parser
    usage = "usage: python //~HOME/pysenorge/themes/additional_snow_depth.py YYYY-MM-DD [options]"

    parser = OptionParser(usage=usage)
    parser.add_option("-o", "--outdir",
                      action="store", dest="outdir", type="string",
                      metavar="DIR", default=os.path.join(BILout, themedir),
                      help="Output directory - default: $BILout/%s/$HYDROYEAR" % \
                      themedir)
    parser.add_option("--no-bil",
                      action="store_false",
                      dest="bil",
                      default=True,
                      help="Set to suppress output in BIL format")
    parser.add_option("--nc",
                      action="store_true",
                      dest="nc",
                      default=False,
                      help="Set to store output in netCDF format")
    parser.add_option("--png",
                      action="store_true",
                      dest="png",
                      default=False,
                      help="Set to store output as PNG image")

    # Comment to suppress help
    # parser.print_help()
    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.error("Please provide the date in ISO format YYYY-MM-DD!")
        parser.print_help()

    # Get current datetime
    cdt = iso2datetime(args[0] + " 06:00:00")
    #Ändern Fehler...
    windfilename = "wind_speed_avg_10m_%s.bil" % datetime2BILdate(cdt)

    #----------------------------------------------------------------------------
    # Import wind-speed 10m map
    # windfile = os.path.join(BILout, "wind_speed_avg_10m", str(get_hydroyear(cdt)),windfilename)
    windfile = os.path.join(BILout, windfilename)

    if not os.path.exists(windfile):
        parser.error("BIL file %s containing wind data does not exist!" %\
                     windfile)
    else:
        # Load todays data
        wind = BILdata(windfile, 'uint16')
        wind.read()
        # convert to Celsius
        wind.data = float32(
            wind.data
        ) * 0.1  #multiplyed by 0.1 because of in integer values in wind_model

    #----------------------------------------------------------------------------
    #import snow-depth map
    sdfilename = "sdfsw_%s.bil" % datetime2BILdate(cdt)
    #sdfile = os.path.join(BILout, "sdfsw", str(get_hydroyear(cdt)), sdfilename)
    sdfile = os.path.join(BILout, sdfilename)

    if not os.path.exists(sdfile):
        parser.error("BIL file %s containing snow-depth data does not exist!" %\
                     sdfile)
    else:
        sd = BILdata(sdfile, 'uint16')
        sd.read()

    #----------------------------------------------------------------------------
    #import snow-age map
    agefilename = "age_%s.bil" % datetime2BILdate(cdt)
    #agefile = os.path.join(BILout, "age", str(get_hydroyear(cdt)), agefilename)
    agefile = os.path.join(BILout, agefilename)

    if not os.path.exists(agefile):
        parser.error("BIL file %s containing snow-age data does not exist!" %\
                     agefile)
    else:
        age = BILdata(agefile, 'uint8')
        age.read()

    #----------------------------------------------------------------------------
    #import liquid-water-content map
    lwcfilename = "lwc_%s.bil" % datetime2BILdate(cdt)
    #lwcfile = os.path.join(BILout, "lwc", str(get_hydroyear(cdt)), lwcfilename)
    lwcfile = os.path.join(BILout, lwcfilename)

    if not os.path.exists(lwcfile):
        parser.error("BIL file %s containing snow-LWC data does not exist!" %\
                     lwcfile)
    else:
        lwc = BILdata(lwcfile, 'uint8')
        lwc.read()

    #----------------------------------------------------------------------------
    #Calculate and import frozen-snow map
    try:
        frz = frozen_snow.frozen()
        frz.load_data(os.path.join(BILout, "ssttm_2013_01_13.bil"))
        frz.check_value(frz.tsi)
    except (IOError, NameError):
        print "Couldn't find or load frozen-snow map"

    # Setup outputs
    outfile = themedir + '_' + datetime2BILdate(cdt)
    outdir = os.path.join(options.outdir, str(get_hydroyear(cdt)))
    if not os.path.exists(outdir):
        if not os.path.exists(options.outdir):
            os.chdir(BILout)
            os.system('mkdir %s' % themedir)
        os.chdir(options.outdir)
        os.system('mkdir %s' % str(get_hydroyear(cdt)))

    #Run additional-snow map model
    Hwind = model(wind.data, sd.data, lwc.data, age.data, frz.idex)

    # Set no-data values to UintFillValue
    mask = senorge_mask()
    Hwind[mask] = UintFillValue

    #Option create bil-file
    if options.bil:
        # Write to BIL file
        bilfile = BILdata(os.path.join(outdir, outfile + '.bil'),
                          datatype='uint16')
        Hwind = uint16(Hwind * 1000)
        Hwind[mask] = UintFillValue

        biltext = bilfile.write(Hwind)
        print biltext

    #Option create nc-file
    if options.nc:
        from pysenorge.io.nc import NCdata
        # Prepare data
        # Change array order
        ncHwind = flipud(Hwind)
        # Write to NC file
        ncfile = NCdata(os.path.join(outdir, outfile + '.nc'))
        ncfile.zip = True
        ncfile.add_variable(themedir, ncHwind.dtype.str, "m", themename,
                            ncHwind)
        ncfile.close()

    #Option create png imagefile
    if options.png:
        from pysenorge.io.png import writePNG
        # Write to PNG file
        writePNG(
            flipud(Hwind),
            os.path.join(outdir, outfile),
            cltfile=os.path.join(
                "/home/ralf/Dokumente/summerjob/data/additional_snow_depth_wind.clt"
            ))

    # At last - cross fingers it all worked out!
    print "\n*** Finished successfully ***\n"
def main():
    '''
    Loads and verifies input data, calls the model, and controls the output stream. 
    '''
    # Theme variables
    themedir = 'tmgr'
    themename = 'Temperature gradient last 24h'
    
    # Setup input parser
    usage = "usage: python //~HOME/pysenorge/themes/temperature_gradient_daily.py tm_TODAY.bil tm_YESTERDAY.bil [options]"
    
    parser = OptionParser(usage=usage)
    parser.add_option("-o", "--outdir", 
                      action="store", dest="outdir", type="string",
                      metavar="DIR", default=os.path.join(BILout, themedir),
                      help="Output directory - default: $BILout/%s/$YEAR" % themedir)
    parser.add_option("--no-bil",
                  action="store_false", dest="bil", default=True,
                  help="Set to suppress output in BIL format")
    parser.add_option("--nc",
                  action="store_true", dest="nc", default=False,
                  help="Set to store output in netCDF format")
    parser.add_option("--png",
                  action="store_true", dest="png", default=False,
                  help="Set to store output as PNG image")
    # Comment to suppress help
    parser.print_help()

    (options, args) = parser.parse_args()
    
    yy, mm, dd = get_date_filename(args[0])
    yy_range = arange(1950, 2050)
    
    # Verify input parameters
    if int(yy) not in yy_range:
        parser.error("Could not determine year from file name.")
    
    if len(args) != 2:
        parser.error("Please provide two input files!")
        parser.print_help() 
    else:
        # Add full path to the filename
        todaysfile = os.path.join(METdir, "tm", yy, args[0])
        yesterdaysfile = os.path.join(METdir, "tm", yy, args[1])
        
    
    if not os.path.exists(todaysfile):# and os.path.isfile(options.todaysfile):
        parser.error("BIL file containing todays temperature data does not exist!")
    elif not os.path.exists(yesterdaysfile):# and os.path.isfile(options.todaysfile):
        parser.error("BIL file containing yesterdays temperature data does not exist!")
    else:
        # Load todays data
        today = BILdata(todaysfile, 'uint16')
        today.read()
        # Load yesterdays data 
        yesterday = BILdata(yesterdaysfile, 'uint16')
        yesterday.read()
    
    
    # Setup outputs
    outfile = themedir+'_'+yy+'_'+mm+'_'+dd
    tstring = yy+'-'+mm+'-'+dd+' 06:00:00'
    if options.nc:
        secs = date2epoch(tstring) # used in NCdata.new()
    outdir = os.path.join(options.outdir, yy)
    if not os.path.exists(outdir):
        if not os.path.exists(options.outdir):
            os.chdir(BILout)
            os.system('mkdir %s' % themedir)
        os.chdir(options.outdir)
        os.system('mkdir %s' % yy)
    
    
#    # Calculate temperature gradient    
    tmgr = int16(model(float32(today.data), float32(yesterday.data)))
    
    # Set no-data values to IntFillValue
    mask = senorge_mask()
    imask = mask==False
    tmgr[mask] = IntFillValue
    
    if options.bil:
        # Write to BIL file
        bilfile = BILdata(os.path.join(outdir, outfile+'.bil'), datatype='int16')
        biltext = bilfile.write(tmgr)
        print biltext
    
    if options.nc:
        from pysenorge.io.nc import NCdata #@UnresolvedImport
        # Prepare data
        nctmgr = int2float(tmgr)
        imask = mask == False
        # Convert to Celcius/Kelvin
        nctmgr[imask] = nctmgr[imask]/10.0
        # Change array order 
        nctmgr = flipud(nctmgr)
        # Write to NC file
        ncfile = NCdata(os.path.join(outdir, outfile+'.nc'))
        ncfile.zip = True
        ncfile.new(secs)
        ncfile.add_variable(themedir, nctmgr.dtype.str, "K s-1", themename, nctmgr)
        ncfile.close()
    
    if options.png:
        from pysenorge.io.png import writePNG #@UnresolvedImport
        # Write to PNG file
        writePNG(tmgr, os.path.join(outdir, outfile))
    # At last - cross fingers it all worked out!
    print "\n*** Finished successfully ***\n"
示例#14
0
def main():
    """
    Loads and verifies input data, calls the model, and controls the output stream.
    """

    # Theme variables
    themedir1 = 'wind_speed_avg_1500m'
    themedir2 = 'wind_speed_max_1500m'

    today = datetime.date.today().strftime("%Y_%m_%d")
    tomorrow = (datetime.date.today() + datetime.timedelta(days=1)).strftime("%Y_%m_%d")

    # Setup input parser
    usage = "usage: python //~HOME/pysenorge/theme_layers/ \
            average_windspeed_daily.py YYYY-MM-DD NC-File-Describtion [options]"

#---------------------------------------------------------
    #add options with parser module
    parser = OptionParser(usage=usage)
    #create no bil
    parser.add_option("--no-bil",
                  action="store_false", dest="bil", default=True,
                  help="Set to suppress output in BIL format")
    #create netCDF
    parser.add_option("--nc",
                  action="store_true", dest="nc", default=False,
                  help="Set to store output in netCDF format")
    #create png
    parser.add_option("--png",
                  action="store_true", dest="png", default=False,
                  help="Set to store output as PNG image")

#   Comment to suppress help
#   parser.print_help()
    (options, args) = parser.parse_args()

    # Verify input parameters
    if len(args) != 2:
        parser.error("Please provide the date in ISO format YYYY-MM-DD and the timenc!")
        parser.print_help()

    #Select which date
    yr = str(args[0].split("-")[0])
    mon = str(args[0].split("-")[1])
    day = str(args[0].split("-")[2])

    #Select which nc-file
    timearg = args[1]

    load_date = "%s_%s_%s" % (yr, mon, day)

    ncfilename = "AROME_WIND_850_NVE_%s_%s.nc" % (timearg, load_date)
    ncfile = os.path.join(netCDFin, "2013", ncfilename)

    #Test if path of the netCDF file exists
    if not os.path.exists(ncfile):
        parser.error("%s does not exist!" % ncfile)
    else:
    # Load wind data from prognosis (netCDF file) for entire time-range
        ds = Dataset(ncfile, 'r')
        wind_time = ds.variables['time'][:]
        x_wind = ds.variables['x_wind_850hpa'][:, :, :]
        y_wind = ds.variables['y_wind_850hpa'][:, :, :]
        ds.close()

    print "Using input data from file %s" % ncfile

    #Get timenc and control if the time is right
    ob_time = time.strftime("%H", time.gmtime(wind_time[0]))
    data_timenc = _time_fun(ob_time)
    if timearg == data_timenc:
        pass
    else:
        print "The given input parameter and time value of the netCDF are not the same"
    timenc = data_timenc

#---------------------------------------------------------
    #Output paths, output filenames and timerange
    begin_time = time.strftime("%d-%m-%Y-%H:%M:%S", time.gmtime(wind_time[0]))
    end_time = time.strftime("%d-%m-%Y-%H:%M:%S", time.gmtime(wind_time[24]))

    print "For the period from the", begin_time, "to", end_time

    outfile1 = '%s_%s' % (themedir1, today)
    outfile2 = '%s_%s' % (themedir2, today)

    cdt = iso2datetime(args[0] + " 06:00:00")

#---------------------------------------------------------
    #Output path 1
    outdir1 = os.path.join(BILout, themedir1, str(get_hydroyear(cdt)))
    if not os.path.exists(outdir1):
        if not os.path.exists(os.path.join(BILout, themedir1)):
            os.chdir(BILout)
            os.system('mkdir %s' % themedir1)
        os.chdir(os.path.join(BILout, themedir1))
        os.system('mkdir %s' % str(get_hydroyear(cdt)))

    #Output path 2
    outdir2 = os.path.join(BILout, themedir2, str(get_hydroyear(cdt)))
    if not os.path.exists(outdir2):
        if not os.path.exists(os.path.join(BILout, themedir2)):
            os.chdir(BILout)
            os.system('mkdir %s' % themedir2)
        os.chdir(os.path.join(BILout, themedir2))
        os.system('mkdir %s' % str(get_hydroyear(cdt)))

    #Output path for wind_00 til wind _18
    if timenc == "00":
        outdir_hour = os.path.join(BILout, str(get_hydroyear(cdt)), load_date, "00")
    elif timenc == "06":
        outdir_hour = os.path.join(BILout, str(get_hydroyear(cdt)), load_date, "06")
    elif timenc == "12":
        outdir_hour = os.path.join(BILout, str(get_hydroyear(cdt)), load_date, "12")
    elif timenc == "18":
        outdir_hour = os.path.join(BILout, str(get_hydroyear(cdt)), load_date, "18")

    if not os.path.exists(outdir_hour):
        if not os.path.exists(os.path.join(BILout, str(get_hydroyear(cdt)))):
            os.chdir(BILout)
            os.system('mkdir %s' % str(get_hydroyear(cdt)))
        os.chdir(os.path.join(BILout, str(get_hydroyear(cdt))))
        os.makedirs('%s/%s/' % (load_date, timenc))

#---------------------------------------------------------
    # Calculate the wind speed vector - using model()
    total_wind_avg, max_wind, total_wind, wind_dir_cat, \
    hour_wind = model(x_wind, y_wind)

    # interpolate total average wind speed to seNorge grid
    total_wind_avg_intp = interpolate_new(total_wind_avg)
    max_wind_intp = interpolate_new(max_wind)
    wind_dir_intp = interpolate_new(wind_dir_cat)
    #dom_wind_tab_intp = interpolate_new(dom_wind_tab)

    # Replace NaN values with the appropriate FillValue
    total_wind_avg_intp = nan2fill(total_wind_avg_intp)
    max_wind_intp = nan2fill(max_wind_intp)
    wind_dir_intp = nan2fill(wind_dir_intp)
    #dom_wind_tab_intp = nan2fill(dom_wind_tab_intp)

#--------------------------------------------------------
    #Hourly wind forecast based on the recent AROME input file
    if timenc == "18":
    #at 01:00
        wind_18 = interpolate_new(total_wind[0, :, :])
        hour_wind_intp_18 = interpolate_new(hour_wind[0, :, :])
    #at 07:00
        wind_00 = interpolate_new(total_wind[6, :, :])
        hour_wind_intp_00 = interpolate_new(hour_wind[6, :, :])
    #at 13:00
        wind_06 = interpolate_new(total_wind[12, :, :])
        hour_wind_intp_06 = interpolate_new(hour_wind[12, :, :])
    #at 19:00
        wind_12 = interpolate_new(total_wind[18, :, :])
        hour_wind_intp_12 = interpolate_new(hour_wind[18, :, :])

        outfile3 = '%s_%s' % ("wind_speed_18_1500m", today)
        outfile4 = '%s_%s' % ("wind_speed_00_1500m", today)
        outfile5 = '%s_%s' % ("wind_speed_06_1500m", today)
        outfile6 = '%s_%s' % ("wind_speed_12_1500m", today)

    elif timenc == "00":
    #at 07:00
        wind_00 = interpolate_new(total_wind[0, :, :])
        hour_wind_intp_00 = interpolate_new(hour_wind[0, :, :])
    #at 13:00
        wind_06 = interpolate_new(total_wind[6, :, :])
        hour_wind_intp_06 = interpolate_new(hour_wind[6, :, :])
    #at 19:00
        wind_12 = interpolate_new(total_wind[12, :, :])
        hour_wind_intp_12 = interpolate_new(hour_wind[12, :, :])
    #next day at 01:00
        wind_18 = interpolate_new(total_wind[18, :, :])
        hour_wind_intp_18 = interpolate_new(hour_wind[18, :, :])

        outfile3 = '%s_%s' % ("wind_speed_00_1500m", today)
        outfile4 = '%s_%s' % ("wind_speed_06_1500m", today)
        outfile5 = '%s_%s' % ("wind_speed_12_1500m", today)
        outfile6 = '%s_%s' % ("wind_speed_18_1500m", tomorrow)

    elif timenc == "06":
        #at 13:00
        wind_06 = interpolate_new(total_wind[0, :, :])
        hour_wind_intp_06 = interpolate_new(hour_wind[0, :, :])
        #at 19:00
        wind_12 = interpolate_new(total_wind[6, :, :])
        hour_wind_intp_12 = interpolate_new(hour_wind[6, :, :])
        #next day 01:00
        wind_18 = interpolate_new(total_wind[12, :, :])
        hour_wind_intp_18 = interpolate_new(hour_wind[12, :, :])
        #next day 07:00
        wind_00 = interpolate_new(total_wind[18, :, :])
        hour_wind_intp_00 = interpolate_new(hour_wind[18, :, :])

        outfile3 = '%s_%s' % ("wind_speed_06_1500m", today)
        outfile4 = '%s_%s' % ("wind_speed_12_1500m", today)
        outfile5 = '%s_%s' % ("wind_speed_18_1500m", tomorrow)
        outfile6 = '%s_%s' % ("wind_speed_00_1500m", tomorrow)

    elif timenc == "12":
        #at 19:00
        wind_12 = interpolate_new(total_wind[0, :, :])
        hour_wind_intp_12 = interpolate_new(hour_wind[0, :, :])
        #next day 01:00
        wind_18 = interpolate_new(total_wind[6, :, :])
        hour_wind_intp_18 = interpolate_new(hour_wind[6, :, :])
        #next day 07:00
        wind_00 = interpolate_new(total_wind[12, :, :])
        hour_wind_intp_00 = interpolate_new(hour_wind[12, :, :])
        #next day 13:00
        wind_06 = interpolate_new(total_wind[18, :, :])
        hour_wind_intp_06 = interpolate_new(hour_wind[18, :, :])

        outfile3 = '%s_%s' % ("wind_speed_12_1500m", today)
        outfile4 = '%s_%s' % ("wind_speed_18_1500m", tomorrow)
        outfile5 = '%s_%s' % ("wind_speed_00_1500m", tomorrow)
        outfile6 = '%s_%s' % ("wind_speed_06_1500m", tomorrow)

#---------------------------------------------------------
    #Option --bil: Multiplied by 10 to store data as integer
    if options.bil:
        from pysenorge.grid import senorge_mask
        mask = senorge_mask()

        #Avg wind
        bil_avg_wind = flipud(uint16(total_wind_avg_intp * 10.0))
        bil_avg_wind[mask] = UintFillValue

        bilfile = BILdata(os.path.join(outdir1,
                          outfile1 + '.bil'),
                          datatype='uint16')
        #Colapse into one dimension
        biltext = bilfile.write(bil_avg_wind.flatten())
        print biltext

        # Max wind
        bil_max_wind = flipud(uint16(max_wind_intp * 10.0))
        bil_max_wind[mask] = UintFillValue
        bilfile = BILdata(os.path.join(outdir2,
                          outfile2 + '.bil'),
                          datatype='uint16')
        biltext = bilfile.write(bil_max_wind.flatten())
        print biltext

        # bil wind at 00
        bil_wind_00 = flipud(uint16(wind_00 * 10.0))
        bil_wind_00[mask] = UintFillValue
        bilfile = BILdata(os.path.join(outdir_hour,
                          outfile3 + '.bil'),
                          datatype='uint16')
        biltext = bilfile.write(bil_wind_00.flatten())
        print biltext

        # bil wind at 06
        bil_wind_06 = flipud(uint16(wind_06 * 10.0))
        bil_wind_06[mask] = UintFillValue
        bilfile = BILdata(os.path.join(outdir_hour,
                          outfile4 + '.bil'),
                          datatype='uint16')
        biltext = bilfile.write(bil_wind_06.flatten())
        print biltext

        #bil wind at 12
        bil_wind_12 = flipud(uint16(wind_12 * 10.0))
        bil_wind_12[mask] = UintFillValue
        bilfile = BILdata(os.path.join(outdir_hour,
                          outfile5 + '.bil'),
                          datatype='uint16')
        biltext = bilfile.write(bil_wind_12.flatten())
        print biltext

        #bil wind at 18
        bil_wind_18 = flipud(uint16(wind_18 * 10.0))
        bil_wind_18[mask] = UintFillValue
        bilfile = BILdata(os.path.join(outdir_hour,
                          outfile6 + '.bil'),
                          datatype='uint16')
        biltext = bilfile.write(bil_wind_18.flatten())
        print biltext

#---------------------------------------------------------
    #Option --nc: write a nc file
    if options.nc:
        ncfile = NCdata(os.path.join(outdir1, outfile1 + '.nc'))

        ncfile.new(wind_time[-1])

        ncfile.add_variable('avg_wind_speed', total_wind_avg.dtype.str, "m s-1",
                             'Average wind speed last 24h', total_wind_avg_intp)
        ncfile.add_variable('max_wind_speed', max_wind.dtype.str, "m s-1",
                             'Maximum wind gust last 24h', max_wind_intp)
        ncfile.add_variable('wind_direction', wind_dir_cat.dtype.str,
                             "cardinal direction",
                             'Prevailing wind direction last 24h', wind_dir_intp)
        ncfile.add_variable('wind_00', wind_00.dtype.str, "m s-1",
                             'Wind forecast 01:00', wind_00)
        ncfile.add_variable('wind_06', wind_06.dtype.str, "m s-1",
                             'Wind forecast 07:00', wind_06)
        ncfile.add_variable('wind_12', wind_12.dtype.str, "m s-1",
                             'Wind forecast 13:00', wind_12)
        ncfile.add_variable('wind_18', wind_18.dtype.str, "m s-1",
                             'Wind forecast 19:00', wind_18)

        ncfile.close()

#---------------------------------------------------------
    #Option --png: Write a PNG files
    if options.png:
        avg_clt_file_path = "/home/ralf/Dokumente/summerjob/data/avg_wind_speed_10_no.clt"
        max_clt_file_path = "/home/ralf/Dokumente/summerjob/data/max_wind_speed_10_no.clt"
        direction_clt_file_path = "/home/ralf/Dokumente/summerjob/data/wind_direction_10_no.clt"

        #Creates png file wind average
        writePNG(total_wind_avg_intp[:, :],
                os.path.join(outdir1, outfile1),
                 cltfile=avg_clt_file_path
                 )

        #Creates png file wind max
        writePNG(max_wind_intp[:, :],
                 os.path.join(outdir2, outfile2),
                 cltfile=max_clt_file_path
                 )

        #Hourly wind speed maps
        writePNG(wind_00,
                os.path.join(outdir_hour, outfile3),
                cltfile=avg_clt_file_path
                )

        writePNG(wind_06,
                os.path.join(outdir_hour, outfile4),
                cltfile=avg_clt_file_path
                )

        writePNG(wind_12,
                os.path.join(outdir_hour, outfile5),
                cltfile=avg_clt_file_path
                )

        writePNG(wind_18,
                os.path.join(outdir_hour, outfile6),
                cltfile=avg_clt_file_path
                )

        #Wind direction
        writePNG(wind_dir_intp[:, :],
                  os.path.join(outdir1, 'wind_direction'),
                  cltfile=direction_clt_file_path
                  )

        #Hourly wind directions
        writePNG(hour_wind_intp_00[:, :],
                os.path.join(outdir1, 'wind_direction_hour_00'),
                cltfile=direction_clt_file_path
                )

        writePNG(hour_wind_intp_06[:, :],
                os.path.join(outdir1, 'wind_direction_hour_06'),
                cltfile=direction_clt_file_path
                )

        writePNG(hour_wind_intp_12[:, :],
                os.path.join(outdir1, 'wind_direction_hour_12'),
                cltfile=direction_clt_file_path
                )

        writePNG(hour_wind_intp_18[:, :],
                os.path.join(outdir1, 'wind_direction_hour_18'),
                cltfile=direction_clt_file_path
                )

        #Maximal dominate wind speed
        #writePNG(dom_wind_tab_intp[:, :],
        #          os.path.join(outdir1, today, 'wind_direction_dom_wind_tab'),
        #          cltfile=direction_clt_file_path
        #          )
    # At last - cross fingers* it all worked out! *and toes !!!
    print "\n***Finished successfully***\n"
示例#15
0
def main():
    '''
    Loads and verifies input data, calls the model, and controls the output stream. 
    '''
    # Theme variables
    themedir = 'tmgr'
    themename = 'Temperature gradient last 24h'

    # Setup input parser
    usage = "usage: python //~HOME/pysenorge/themes/temperature_gradient_daily.py tm_TODAY.bil tm_YESTERDAY.bil [options]"

    parser = OptionParser(usage=usage)
    parser.add_option("-o",
                      "--outdir",
                      action="store",
                      dest="outdir",
                      type="string",
                      metavar="DIR",
                      default=os.path.join(BILout, themedir),
                      help="Output directory - default: $BILout/%s/$YEAR" %
                      themedir)
    parser.add_option("--no-bil",
                      action="store_false",
                      dest="bil",
                      default=True,
                      help="Set to suppress output in BIL format")
    parser.add_option("--nc",
                      action="store_true",
                      dest="nc",
                      default=False,
                      help="Set to store output in netCDF format")
    parser.add_option("--png",
                      action="store_true",
                      dest="png",
                      default=False,
                      help="Set to store output as PNG image")
    # Comment to suppress help
    parser.print_help()

    (options, args) = parser.parse_args()

    yy, mm, dd = get_date_filename(args[0])
    yy_range = arange(1950, 2050)

    # Verify input parameters
    if int(yy) not in yy_range:
        parser.error("Could not determine year from file name.")

    if len(args) != 2:
        parser.error("Please provide two input files!")
        parser.print_help()
    else:
        # Add full path to the filename
        todaysfile = os.path.join(METdir, "tm", yy, args[0])
        yesterdaysfile = os.path.join(METdir, "tm", yy, args[1])

    if not os.path.exists(
            todaysfile):  # and os.path.isfile(options.todaysfile):
        parser.error(
            "BIL file containing todays temperature data does not exist!")
    elif not os.path.exists(
            yesterdaysfile):  # and os.path.isfile(options.todaysfile):
        parser.error(
            "BIL file containing yesterdays temperature data does not exist!")
    else:
        # Load todays data
        today = BILdata(todaysfile, 'uint16')
        today.read()
        # Load yesterdays data
        yesterday = BILdata(yesterdaysfile, 'uint16')
        yesterday.read()

    # Setup outputs
    outfile = themedir + '_' + yy + '_' + mm + '_' + dd
    tstring = yy + '-' + mm + '-' + dd + ' 06:00:00'
    if options.nc:
        secs = date2epoch(tstring)  # used in NCdata.new()
    outdir = os.path.join(options.outdir, yy)
    if not os.path.exists(outdir):
        if not os.path.exists(options.outdir):
            os.chdir(BILout)
            os.system('mkdir %s' % themedir)
        os.chdir(options.outdir)
        os.system('mkdir %s' % yy)


#    # Calculate temperature gradient
    tmgr = int16(model(float32(today.data), float32(yesterday.data)))

    # Set no-data values to IntFillValue
    mask = senorge_mask()
    imask = mask == False
    tmgr[mask] = IntFillValue

    if options.bil:
        # Write to BIL file
        bilfile = BILdata(os.path.join(outdir, outfile + '.bil'),
                          datatype='int16')
        biltext = bilfile.write(tmgr)
        print biltext

    if options.nc:
        from pysenorge.io.nc import NCdata  #@UnresolvedImport
        # Prepare data
        nctmgr = int2float(tmgr)
        imask = mask == False
        # Convert to Celcius/Kelvin
        nctmgr[imask] = nctmgr[imask] / 10.0
        # Change array order
        nctmgr = flipud(nctmgr)
        # Write to NC file
        ncfile = NCdata(os.path.join(outdir, outfile + '.nc'))
        ncfile.zip = True
        ncfile.new(secs)
        ncfile.add_variable(themedir, nctmgr.dtype.str, "K s-1", themename,
                            nctmgr)
        ncfile.close()

    if options.png:
        from pysenorge.io.png import writePNG  #@UnresolvedImport
        # Write to PNG file
        writePNG(tmgr, os.path.join(outdir, outfile))
    # At last - cross fingers it all worked out!
    print "\n*** Finished successfully ***\n"