def getsvg(path,pagenr,usecache=True): assert type(pagenr)==int inputfile=fetchdata.getdatafilename(path,country="se",maxcacheage=7200) svged=fetchdata.getcachename(path,'svg') if os.path.exists(svged) and usecache: cacheddate=fetchdata.get_filedate(svged) print "Cached svg version exists, date:",svged,cacheddate if fetchdata.is_devcomp() or datetime.now()-cacheddate<timedelta(0,86400/2): print "Using svg cache" try: return open(svged).read() except Exception,cause: print "Couldn't read cached svg version",cause
def getsvg(path, pagenr, usecache=True): assert type(pagenr) == int inputfile = fetchdata.getdatafilename(path, country="se", maxcacheage=7200) svged = fetchdata.getcachename(path, 'svg') if os.path.exists(svged) and usecache: cacheddate = fetchdata.get_filedate(svged) print "Cached svg version exists, date:", svged, cacheddate if fetchdata.is_devcomp() or datetime.now() - cacheddate < timedelta( 0, 86400 / 2): print "Using svg cache" try: return open(svged).read() except Exception, cause: print "Couldn't read cached svg version", cause
def get_gfs(dt,future): gfspath=None sdate=dt.strftime("%Y%m%d%H") shour=dt.strftime("%H") print "Trying download for",sdate url='http://nomads.ncep.noaa.gov/pub/data/nccf/com/gfs/prod/gfs.%s/gfs.t%sz.pgrbf%02d.grib2'%(sdate,shour,future) try: gfspath=fetchdata.getdatafilename( url, country='raw',maxcacheage=86400*24) except: print traceback.format_exc() fetchdata.deletecache(url) return None if os.path.getsize(gfspath)<=100: fetchdata.deletecache(url) return None return get_gfs_impl(gfspath,future)
def parse_landing_chart(path,arppos,icao,country='se',variant=''): icao=icao.upper() if variant and not variant.startswith("."): variant="."+variant print "Running parse_landing_chart" print "country:",country #p=parse.Parser(path,country=country) arppos=mapper.from_str(arppos) res=[] #assert p.get_num_pages()<=2 url=fetchdata.getrawurl(path,country=country) ret=dict() ret['url']=url data,nowdate=fetchdata.getdata(path,country=country,maxcacheage=7200) cksum=md5.md5(data).hexdigest() ret['checksum']=cksum #page=p.parse_page_to_items(0, donormalize=False) #ret['width']=page.width #ret['height']=page.height #width=page.width #height=page.height #scale=2048.0/min(width,height) #width*=scale #height*=scale #width=int(width+0.5) #height=int(height+0.5) blobname=icao+variant tmppath=os.path.join(os.getenv("SWFP_DATADIR"),"adcharts",icao) if not os.path.exists(tmppath): os.makedirs(tmppath) assert len(icao)==4 outpath=os.path.join(tmppath,blobname+"."+cksum+".png") def render(inputfile,outputfile): ext=inputfile.split(".")[-1].lower() if ext=='jpg' or ext=='png': assert 0==os.system("convert -adaptive-resize 2500x2500 %s %s"%(inputfile,outputfile)) else: ext='pdf' r="pdftoppm -f 0 -l 0 -scale-to 2500 -png -freetype yes -aa yes -aaVector yes %s >%s"%( inputfile,outputfile) print "rendering",r assert 0==os.system(r) ret['image']=blobname+"."+cksum+".png" fetchdata.getcreate_derived_data_raw( path,outpath,render,"png",country=country) fspath=fetchdata.getdatafilename(path,country=country) sizepts=None for line in os.popen("pdfinfo "+fspath): m=re.match(r"\s*.age\s+size:\s*(\d+\.?\d*)\s*x\s*(\d+\.?\d*)\s*pts.*",line) if m: sizepts=(float(m.groups()[0]),float(m.groups()[1])) if sizepts: sizemm=(0.3527*sizepts[0],0.3527*sizepts[1]) ret['mapsize']=sizemm print "Mapsize:",sizemm else: raise Exception("No size of this PDF!") outpath2=os.path.join(tmppath,blobname+"."+cksum+".2.png") def greyscale(input,output): assert 0==os.system("convert -define png:color-type=3 -depth 8 -type Palette -define \"png:compression-level=9\" %s %s"%(input,output)) fetchdata.getcreate_local_data_raw( outpath,outpath2,greyscale) i=Image.open(outpath2) width,height=i.size #ret['width']=page.width #ret['height']=page.height ret['render_width']=width ret['render_height']=height if country!='raw': icao_prefix=get_icao_prefix(country) assert icao.startswith(icao_prefix) for level in xrange(5): hashpath=os.path.join(tmppath,"%s.%s-%d.bin"%(blobname,cksum,level)) fetchdata.getcreate_local_data_raw( outpath2,hashpath,lambda input,output:chop_up(input,output,level)) ret['blobname']=blobname ret['variant']=variant return ret