889 def write_paramCard(self, output_location=None):
890 """Write out paramCardDict to disk.
891 The function will copy the layout and format from the default card.
892 """
893 if self.paramCard_default_loc is None or not os.path.isfile(self.paramCard_default_loc):
894 self.paramCard_default_loc = self.paramCard_loc +'.old_to_be_deleted'
895 os.rename(self.paramCard_loc, self.paramCard_default_loc)
896
897 with open(self.paramCard_default_loc,'r') as f:
898 oldCard = f.read()
899
900
901 newCard = open(self.paramCard_loc if output_location is None else output_location,'w')
902 dict_blocks = [v.lower() for v in self.paramCardDict]
903
904 oldCard_blocks = oldCard.split('\n\n')
905
906 for block in oldCard_blocks:
907 name = None
908 nParams = []
909
910 for line in block.split('\n'):
911 l = line.strip()
912 if l.startswith('#'):
913 newCard.write(f"{line} \n")
914 elif l == '':
915 newCard.write("\n")
916 elif l.lower().startswith('block'):
917 if name is not None and len(nParams) == len(self.paramCardDict[name]):
918 name = None
919 nParams = []
920
921 elif name is not None and len(nParams) != len(self.paramCardDict[name]):
922
923 for key in self.paramCardDict[name]:
924
925 if key in nParams:
926 continue
927 elif key not in nParams:
928 newCard.write(f" {key} {self.paramCardDict[name][key]}\n")
929 nParams.append(key)
930
931 name = l.split(
' ',1)[1].
strip()
932 nParams = []
933 if name.lower() not in dict_blocks:
934 raise RuntimeError("Cannot find %s in paramCardDict"%str(name))
935 elif name not in self.paramCardDict:
936 for b in self.paramCardDict:
937 if b.lower() == name.lower():
938 name = b
939 else:
940 continue
941
942 newCard.write(f"Block {name}\n")
943 elif l.lower().startswith('decay'):
944
945 if name is not None and name.lower() != 'decay':
946 if len(nParams) == len(self.paramCardDict[name]):
947 continue
948 elif len(nParams) != len(self.paramCardDict[name]):
949
950 for key in self.paramCardDict[name]:
951
952 if key in nParams:
953 continue
954 elif key not in nParams:
955 newCard.write(f" {key} {self.paramCardDict[name][key]}\n")
956 nParams.append(key)
957
958 nParams = []
959
961
962 command = l[5:].
strip()
963 ID = command.split(' ',1)[0]
964 nParams.append(ID)
965
966 newCard.write(f"{self.paramCardDict[name][ID]} \n")
967
968 elif l == '\n':
969 newCard.write(l)
970
971 else:
972 if name.lower() == 'decay':
973 continue
974 else:
975 ID =
' '.join(l.partition(
'#')[0].
split()[:-1])
976 newCard.write(f" {ID} {self.paramCardDict[name][ID]}\n")
977 nParams.append(ID)
978
979
980
981 if name is not None and len(nParams) == len(self.paramCardDict[name]):
982 name = None
983 nParams = []
984
985 elif name is not None and len(nParams) != len(self.paramCardDict[name]):
986
987 for key in self.paramCardDict[name]:
988
989 if key in nParams:
990 continue
991 elif key not in nParams and key is not None and key.strip() != '':
992 newCard.write(f" {key} {self.paramCardDict[name][key]}\n")
993 nParams.append(key)
994 elif key is None or key.strip() == '':
995 continue
996
997 mglog.info("Finished writing paramCardDict to " + ("param_card.dat" if output_location is None else output_location))