99import os
1010import pandas as pd
1111import datetime
12+ import boto3
13+
14+
15+ def checkUrl (s3 , url ):
16+ siteroot = os .getenv ('WEBSITEBUCKET' , default = 's3://ukmda-website' )
17+ retval = url
18+ tmpurl = url
19+ if url [0 ]== ',' :
20+ tmpurl = url [1 :]
21+ try :
22+ _ = s3 .head_object (Bucket = siteroot [5 :], Key = tmpurl [1 :])
23+ except Exception :
24+ #print(e)
25+ retval = '/img/missing-white.png'
26+ print (url , retval )
27+ return retval
1228
1329
1430def convertSingletoSrchable (datadir , year , newonly = True ):
1531 print (datetime .datetime .now (), 'single-detection searchable index start' )
32+ s3 = boto3 .client ('s3' )
1633
1734 # load the single-station combined data
1835 if newonly is False :
@@ -34,6 +51,10 @@ def convertSingletoSrchable(datadir, year, newonly=True):
3451 uadata ['fn' ]= [f'/img/single/{ y } /{ y } { m :02d} /' + f .replace ('.fits' ,'.jpg' )
3552 for f ,y ,m in zip (uadata .Filename , uadata .Y , uadata .M )]
3653
54+ print (datetime .datetime .now (), 'checking target urls exist' )
55+ uadata ['targfn' ] = [checkUrl (s3 , x ) for x in uadata .fn ]
56+ print (datetime .datetime .now (), 'done' )
57+
3758 # create array for source
3859 print (datetime .datetime .now (), 'add source column' )
3960 srcs = ['2Single' ]* len (uadata .Filename )
@@ -44,41 +65,13 @@ def convertSingletoSrchable(datadir, year, newonly=True):
4465 print (datetime .datetime .now (), 'create interim dataframe' )
4566 hdr = ['eventtime' ,'source' ,'shower' ,'Mag' ,'loccam' ,'url' ,'imgs' , 'loctime' , 'Y' ,'M' ]
4667 resdf = pd .DataFrame (zip (uadata .Dtstamp , srcs , uadata .Shwr ,
47- uadata .Mag , uadata .ID , uadata .fn , uadata .fn , uadata .LocalTime ,
68+ uadata .Mag , uadata .ID , uadata .targfn , uadata .targfn , uadata .LocalTime ,
4869 uadata .Y , uadata .M ), columns = hdr )
4970
5071 # fix up some mangled historical data
5172 resdf .loc [resdf .loccam == 'Ringwood_N_UK000S' , 'loccam' ] = 'UK000S'
5273 resdf .loc [resdf .loccam == 'Tackley_SW_UK0006' , 'loccam' ] = 'UK0006'
5374
54- # select the RMS data out, its good now
55- # FIXME - needs to select for "not FF_UK9" so we can include non-UK cameras
56- rmsdata = resdf [resdf .url .str .contains ('FF_UK0' )]
57- rmsdata = rmsdata .drop (columns = ['Y' ,'M' ,'loctime' ])
58-
59- # now select out the UFO dta and fix it up
60- ufodata = resdf [resdf .url .str .contains ('FF_UK9' )]
61- #fix up Clanfield cameras
62- ufodata .loc [ufodata .loccam == 'UK9990' , 'loccam' ] = 'Clanfield_NE'
63- ufodata .loc [ufodata .loccam == 'UK9989' , 'loccam' ] = 'Clanfield_NW'
64- ufodata .loc [ufodata .loccam == 'UK9988' , 'loccam' ] = 'Clanfield_SE'
65- ufodata = ufodata .drop (columns = ['url' ,'imgs' ])
66-
67- # create the URL and imgs fields
68- ufodata ['url' ]= [f'/img/single/{ y } /{ y } { m :02d} /M{ lt } _{ f } P.jpg'
69- for f ,y ,m ,lt in zip (ufodata .loccam , ufodata .Y , ufodata .M , ufodata .loctime )]
70- ufodata ['imgs' ] = ufodata .url
71- ufodata = ufodata .drop (columns = ['loctime' ,'Y' ,'M' ])
72-
73- # annoying special case for UK0001, H and S which do not upload JPGs
74- rmsdata .loc [rmsdata .loccam == 'UK0001' ,'url' ]= '/img/missing-white.png'
75- rmsdata .loc [rmsdata .loccam == 'UK0001' ,'imgs' ]= '/img/missing-white.png'
76- rmsdata .loc [rmsdata .loccam == 'UK000H' ,'url' ]= '/img/missing-white.png'
77- rmsdata .loc [rmsdata .loccam == 'UK000H' ,'imgs' ]= '/img/missing-white.png'
78- rmsdata .loc [rmsdata .loccam == 'UK000S' ,'url' ]= '/img/missing-white.png'
79- rmsdata .loc [rmsdata .loccam == 'UK000S' ,'imgs' ]= '/img/missing-white.png'
80-
81- resdf = pd .concat ([rmsdata ,ufodata ])
8275 if newonly is True :
8376 return resdf , rmsuafile
8477 else :
@@ -114,19 +107,22 @@ def convertMatchToSrchable(datadir, year, newonly=True):
114107
115108if __name__ == '__main__' :
116109 if len (sys .argv ) < 3 :
117- print ('usage: python createSearchableFormat.py year dest mode' )
110+ print ('usage: python createSearchableFormat.py year mode outdir ' )
118111 exit (1 )
119112 else :
120113 datadir = os .getenv ('DATADIR' , default = '/home/ec2-user/prod/data' )
121114
122115 year = sys .argv [1 ]
123116 mode = sys .argv [2 ]
117+ outdir = os .path .join (datadir , 'searchidx' )
118+ if len (sys .argv ) > 3 :
119+ outdir = sys .argv [3 ]
124120
125121 # create a set of single-station data and merge with last match set
126122 if mode == 'singles' :
127123 print (datetime .datetime .now (), 'converting single-station data' )
128124 newsingles , fname = convertSingletoSrchable (datadir , year , True )
129- outfile = os .path .join (datadir , 'searchidx' , '{:s}-singles-new.csv' .format (year ))
125+ outfile = os .path .join (outdir , '{:s}-singles-new.csv' .format (year ))
130126 if newsingles is not None :
131127 newsingles .to_csv (outfile , index = False , header = False )
132128 if fname is not None :
@@ -136,11 +132,11 @@ def convertMatchToSrchable(datadir, year, newonly=True):
136132 elif mode == 'matches' :
137133 print (datetime .datetime .now (), 'converting match data' )
138134 newmatches , fname = convertMatchToSrchable (datadir , year , True )
139- outfile = os .path .join (datadir , 'searchidx' , '{:s}-matches-new.csv' .format (year ))
135+ outfile = os .path .join (outdir , '{:s}-matches-new.csv' .format (year ))
140136 if newmatches is not None :
141137 newmatches .to_csv (outfile , index = False , header = False )
142138 if fname is not None :
143139 os .remove (fname )
144140
145141 else :
146- print ('usage: createSearchableFormat yyyy matches_or_singles ' )
142+ print ('usage: createSearchableFormat year mode outdir ' )
0 commit comments