Commit 120d4bfd255c1a08c4657a09071591f2c2eda0fd

Authored by Alain Klotz
1 parent cb1da290
Exists in master

Ameliorations method join of FileNames.

Showing 1 changed file with 85 additions and 16 deletions   Show diff stats
src/guitastro/filenames.py
... ... @@ -1966,9 +1966,11 @@ class FileNames(FileNamesException, GuitastroTools):
1966 1966 """
1967 1967 subdir = os.path.dirname(filename)
1968 1968 fname = filename
1969   - if self.fcontext_value('naming') != self.NAMING_NONE:
1970   - nkw = self.naming_get(filename)
1971   - if outfname:
  1969 + nkw = self.naming_get(filename)
  1970 + if outfname:
  1971 + if self.fcontext_value('naming') == self.NAMING_NONE:
  1972 + fname = nkw['fname']
  1973 + else:
1972 1974 fname = self.naming_set(nkw)
1973 1975 if self.fcontext_value('pathnaming') == self.NAMING_PYROS_IMG1:
1974 1976 d = nkw['date']
... ... @@ -1988,7 +1990,6 @@ class FileNames(FileNamesException, GuitastroTools):
1988 1990 fullname = self.pathnaming_set(pparam, nkw)
1989 1991 #print(f"fullname={fullname}")
1990 1992 subdir = os.path.dirname(fullname)
1991   - #print(f"path={path}")
1992 1993 if outfname:
1993 1994 return subdir, fname
1994 1995 return subdir
... ... @@ -2265,9 +2266,63 @@ class FileNames(FileNamesException, GuitastroTools):
2265 2266 dico['absolute'] = str(absolute)
2266 2267 return dico
2267 2268  
2268   - def join(self):
  2269 + def join(self, filename:str = "") -> str:
  2270 + """Join the four parts of a context to get a full file name.
  2271 +
  2272 + Args:
  2273 +
  2274 + filename: A file name from which the fname will be extracted and subdir calculated. Initial path and extensions are lost. If filename is "" the current contents of the context is used to generate the output full file name.
  2275 +
  2276 + Returns:
  2277 +
  2278 + The full file name in a string (rootdir, subdir, fname, extension)
  2279 +
  2280 + First consider the following file context:
  2281 +
  2282 + ::
  2283 +
  2284 + fn = FileNames()
  2285 + fn.longitude(165.85944)
  2286 + fn.fcontext_create("my_context", "Test")
  2287 + fn.fcontext = "my_context"
  2288 + fn.rootdir = "/tmp"
  2289 + fn.extension = ".fit"
  2290 +
  2291 + Use the method update_subdir_fname_from_filename to update 'subdir' and 'fname' in the context from the filename. Then use join whith no parameter:
  2292 +
  2293 + ::
  2294 +
  2295 + in_fullname = "/home/myuser/messier63.fits.gz"
  2296 + fn.update_subdir_fname_from_filename(in_fullname)
  2297 + out_fullname = fn.join()
  2298 +
  2299 + out_fullname should be '/tmp/messier63.fit'.
  2300 +
  2301 + The following example do the same thing but with no use of the method update_subdir_fname_from_filename:
  2302 +
  2303 + ::
  2304 +
  2305 + in_fullname = "/home/myuser/messier63.fits.gz"
  2306 + out_fullname = fn.join(in_fullname)
  2307 +
  2308 + """
  2309 + if filename != "":
  2310 + self.update_subdir_fname_from_filename(filename)
2269 2311 return os.path.join(self.rootdir, self.subdir, self.fname + self.extension)
2270 2312  
  2313 + def joinabs(self, filename:str = ""):
  2314 + """Join the four parts of a context to get a full file name with absolute path.
  2315 +
  2316 + Args:
  2317 +
  2318 + filename: A file name from which the fname will be extracted and subdir calculated. Initial path and extensions are lost. If filename is "" the current contents of the context is used to generate the output full file name.
  2319 +
  2320 + Returns:
  2321 +
  2322 + The full file name in a string with absolute path (rootdir, subdir, fname, extension)
  2323 + """
  2324 + return os.path.abspath(self.join(filename))
  2325 +
2271 2326 def glob(self, wildcard:str, *args) -> list:
2272 2327 """Glob from a wildcard. Add -R to be recursive.
2273 2328 """
... ... @@ -2941,8 +2996,8 @@ class FileNames(FileNamesException, GuitastroTools):
2941 2996  
2942 2997 if __name__ == "__main__":
2943 2998  
2944   - default = 14
2945   - example = input(f"Select the example (0 to 14) ({default}) ")
  2999 + default = 15
  3000 + example = input(f"Select the example (0 to 15) ({default}) ")
2946 3001 try:
2947 3002 example = int(example)
2948 3003 except:
... ... @@ -3201,9 +3256,8 @@ if __name__ == "__main__":
3201 3256 # --- Place an image at the good place
3202 3257 fn.fcontext = "pyros_img_L0"
3203 3258 in_fullname = r"/srv/work/L0A_20211026_194522999983_1_TNC_CH1_0123456789_001_001.fits.gz"
3204   - param = fn.naming_get(in_fullname)
3205   - fn.fname = fn.naming_set(param)
3206   - out_fullname = fn.join()
  3259 + #fn.update_subdir_fname_from_filename(in_fullname)
  3260 + out_fullname = fn.join(in_fullname)
3207 3261 print(f"==== Copy an image in the good path (context {fn.fcontext_value('description')}) ====")
3208 3262 print(f"{in_fullname=:}")
3209 3263 print(f"{out_fullname=:}")
... ... @@ -3211,8 +3265,8 @@ if __name__ == "__main__":
3211 3265 # --- Place a sequence at the good place
3212 3266 fn.fcontext = "pyros_seq"
3213 3267 in_fullname = r"/srv/work/P001_20211026_1_TNC_0123456789.p"
3214   - fn.update_subdir_fname_from_filename(in_fullname)
3215   - out_fullname = fn.join()
  3268 + #fn.update_subdir_fname_from_filename(in_fullname)
  3269 + out_fullname = fn.join(in_fullname)
3216 3270 print(f"\n==== Copy sequence in the good path (context {fn.fcontext_value('description')}) ====")
3217 3271 print(f"{in_fullname=:}")
3218 3272 print(f"{out_fullname=:}")
... ... @@ -3229,8 +3283,8 @@ if __name__ == "__main__":
3229 3283 param['plane'] = 1
3230 3284 param['frame'] = 1
3231 3285 fname = fn.naming_set(param)
3232   - fn.update_subdir_fname_from_filename(fname)
3233   - out_fullname = fn.join()
  3286 + #fn.update_subdir_fname_from_filename(fname)
  3287 + out_fullname = fn.join(fname)
3234 3288 print(f"\n==== Create an image in the good path (context {fn.fcontext_value('description')}) ====")
3235 3289 print(f"{out_fullname=:}")
3236 3290  
... ... @@ -3242,7 +3296,22 @@ if __name__ == "__main__":
3242 3296 fn.fcontext = "pyros_img_L1"
3243 3297 param['ftype'] = "L1A"
3244 3298 fname = fn.naming_set(param)
3245   - fn.update_subdir_fname_from_filename(fname)
3246   - out_fullname = fn.join()
  3299 + #fn.update_subdir_fname_from_filename(fname)
  3300 + out_fullname = fn.joinabs(fname)
3247 3301 print(f"\n==== Process an image L0 and save in the good path (context {fn.fcontext_value('description')}) ====")
3248 3302 print(f"{out_fullname=:}")
  3303 +
  3304 + if example == 15:
  3305 + """
  3306 + Manage default context
  3307 + """
  3308 + fn = FileNames()
  3309 +
  3310 + fn.fcontext_create("my_context", "Test")
  3311 + fn.fcontext = "my_context"
  3312 + fn.rootdir = "/tmp"
  3313 + fn.extension = ".fit"
  3314 +
  3315 + in_fullname = "/home/myuser/messier63.fits.gz"
  3316 + out_fullname = fn.join(in_fullname)
  3317 + print(out_fullname)
... ...