"-15 to -45":  np.abs(theta["outer"].deg + 30.0) <= 15.0, 
        "bg": np.abs(z - 1.5) <= 0.3,
    }

    try:
        regions = pyregion.open(cmd_args.source + "-mask.reg")
    except IOError:
        print("No bad pixel mask found")
        mask["good"] = np.ones_like(x, dtype=bool)
    else:
        print("Using " + cmd_args.source + "-mask.reg as bad pixel mask")
        mask["good"] = ~regions.get_mask(hdu=hdu)
    #
    # Calculate robust statistics 
    m = mask["<45"] & mask["bg"] & mask["good"]
    avbg, wbg = trimean_and_iqr(hdu.data[m])
    nbg = m.sum()

    m = mask["<45"] & mask["shell"] & mask["good"]
    avsh, wsh = trimean_and_iqr(hdu.data[m])
    nsh = m.sum()

    m = mask["<45"] & mask["shell center"] & mask["good"]
    avshc, wshc = trimean_and_iqr(hdu.data[m])
    nshc = m.sum()
    ymin = avbg - 2*wbg 
    ymax = avsh + 2*wsh


    print("BG trimean = {:.2f}, iqr = {:.2f}".format(avbg, wbg))
    print("Shell trimean = {:.2f}, iqr = {:.2f}".format(avsh, wsh))
    source = sourcefile.replace("-arcdata.json","")
    #print source so far working
    source_db = json.load(open(sourcefile))
    pcoords = coords(source_db["star"])
    print source,pcoords

    if source in table:
        rcoords = coords(table[source])
        dRa = pcoords.ra - rcoords.ra
        dDec = pcoords.dec - rcoords.dec
        print "Offset:", dRa.arcsec, dDec.arcsec
        dx.append(dRa.arcsec)
        dy.append(dDec.arcsec)
        sources.append(source)
#print dx,dy
avdx, sigdx = image_statistics.trimean_and_iqr(dx)
avdy, sigdy = image_statistics.trimean_and_iqr(dy)

#Apparently dx and dy cannot be converted into arrays, so i'm having troubles

offsets = {
   "dx":dx,
   "dy":dy,
   "sources":sources,
   "avdx": [avdx,sigdx],
   "avdy": [avdy,sigdy]
   }
with open("mosaic_offsets.json", "w") as f:
   json.dump(offsets,f,indent=2)