Exemple #1
0
def BIL2netCDF(BILfile, 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)
    bd.read()
    yy, mm, dd = bd.get_date()
    tstring = yy+'-'+mm+'-'+dd+' 06:00:00'
    secs = date2epoch(tstring)

    ncdata = flipud(bd.data).T # array needs to be flipped ud-down and transposed to fit the coordinate system
    ncfile = NCdata(os.path.join(outdir, ncfilename))
#    ncfile.zip = True
    ncfile.new(secs)
    ncfile.add_variable(theme_name, bd.data.dtype, theme_unit, long_name, ncdata, lsd=0)
    ncfile.close()
Exemple #2
0
def BIL2netCDF(BILfile,
               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)
    bd.read()
    yy, mm, dd = bd.get_date()
    tstring = yy + '-' + mm + '-' + dd + ' 06:00:00'
    secs = date2epoch(tstring)

    ncdata = flipud(
        bd.data
    ).T  # array needs to be flipped ud-down and transposed to fit the coordinate system
    ncfile = NCdata(os.path.join(outdir, ncfilename))
    #    ncfile.zip = True
    ncfile.new(secs)
    ncfile.add_variable(theme_name,
                        bd.data.dtype,
                        theme_unit,
                        long_name,
                        ncdata,
                        lsd=0)
    ncfile.close()
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"
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"