Flag Regulations of the Navy are, as one would imagine for any armed service anywhere in the world, extensive and convoluted. Below are only a handful for the regulations covering what, why and when to do things with various type of flag on naval vessals or shore establishments.
Royal Decree 1024 of May 23,1984, for the approval of the Royal Ordinances of the Navy
Article 621. In ports where several warships are located, the ensign shall be raised and lowered, after the signal of the Senior Officer.
Article 622. During twilight, when another warship is under way, every naval ship moored or anchored will raise the flag, without honors, lowering it when the other ship moors.
Article 623. When a ship puts to sea, the ensign will be lowered from the stern staff and raised at the gaff the moment the anchor is weighed. The operation will be carried out in reverse when dropping anchor. It will always be raised at the gaff when under way.
Article 630. In full-dressing ship, the national ensign will be raised and the jack at the jackstaff. Between the jackstaff and the foremast will be located red and white flags of the Code, alternating one pennant for every three or four rectangular flags; between the foremast and the aftermast will be located white and blue flags, the rest of the flags between the aftermast and the stern.
Article 639. Insignia hoisted on small craft, passing close to ships, will be saluted only with the honours of forming the guard and sounding the bugle.
Article 643. When national or foreign merchant ships salute warships upon meeting at sea or in port by lowering their ensigns, the warship will reciprocate by lowering its own ensign once to half mast.
Official Spanish Navy Site