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"
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(): # 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(): # 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"